Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
update current year

pass compilation, but still have four test failures

update test cases to pass

add <repositories> and <pluginRepositories> to pass build on Buildhive.

add changelog

Merge branch 'upgrade-jenkins-version'

[maven-release-plugin] prepare release regression-report-plugin-1.3

[maven-release-plugin] prepare for next development iteration

add explanation about itself

upgrade Jenkins ver. to the latest LTS

[maven-release-plugin] prepare release regression-report-plugin-1.4

[maven-release-plugin] prepare for next development iteration

Added feature to attach build log to email

Attached text file without special characters

Added test for attaching build log
refactored the new attachment functionality

removed trailing whitespaces

Added feature to attach build log to email

Added feature to attach build log to email

Added feature to attach build log to email

Added feature to attach build log to emailMerge branch 'master' of github.com:asadimtiazbutt/regression-report-plugin
  • Loading branch information
aibutt2 committed Nov 25, 2015
1 parent 299b81a commit a630423
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 24 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This plugin sends a mail if your test cases find regressions.

![built and tested on DEV@cloud](http://static-www.cloudbees.com/images/badges/BuiltOnDEV.png)

A Jenkins plugin which solves one common problem to introduce CI to legacy project: a lot of failed tests hide regression (new failed bug).

Legacy project may have a lot of failed tests. In this case, finding regression is little difficult. It also keeps build result unstable, so benefit of CI will be limited and project members may not pay attention on it.
This plugin helps you to find regression even in this case. It should be helpful to introduce CI to your daily job.

Scenario
--------
Expand All @@ -24,10 +28,18 @@ Changelog
### 1.1
- send mail to culprit [#1](https://github.com/jenkinsci/regression-report-plugin/issues/1)

### 1.2
- fix #5 NoSuchMethodError

### 1.3
- upgrade Jenkins to the latest LTS (1.596)

### 1.4
- upgrade Jenkins to the latest LTS (1.609.3)

Copyright and license
---------------------
Copyright 2012 Kengo TODA (eller86)
Copyright 2012-2015 Kengo TODA (eller86)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
26 changes: 24 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.436</version>
<version>1.609.3</version>
</parent>
<artifactId>regression-report-plugin</artifactId>
<name>Regression Report Plugin</name>
<version>1.3-SNAPSHOT</version>
<version>1.5-SNAPSHOT</version>
<packaging>hpi</packaging>
<url>http://wiki.jenkins-ci.org/display/JENKINS/Regression+Report+Plugin</url>
<licenses>
Expand Down Expand Up @@ -73,6 +73,16 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>junit</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>mailer</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -98,4 +108,16 @@
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>
</project>
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package jp.skypencil.jenkins.regression;

import hudson.tasks.junit.CaseResult;
import hudson.tasks.test.TestResult;

import com.google.common.base.Predicate;

class RegressionPredicate implements Predicate<CaseResult> {
class RegressionPredicate implements Predicate<TestResult> {
@Override
public boolean apply(CaseResult input) {
return input.getStatus().isRegression();
public boolean apply(TestResult input) {
if (input instanceof CaseResult) {
CaseResult caseResult = (CaseResult) input;
return caseResult.getStatus().isRegression();
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import hudson.Extension;
import hudson.Launcher;
import hudson.Util;
import hudson.console.AnnotatedLargeText;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.AbstractBuild;
Expand All @@ -16,23 +17,37 @@
import hudson.tasks.Mailer;
import hudson.tasks.junit.CaseResult;
import hudson.tasks.test.AbstractTestResultAction;
import hudson.tasks.test.TestResult;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import jenkins.model.Jenkins;
import jenkins.model.JenkinsLocationConfiguration;

import org.kohsuke.stapler.DataBoundConstructor;

Expand All @@ -53,17 +68,25 @@ static interface MailSender {
private static final int MAX_RESULTS_PER_MAIL = 20;
private final String recipients;
private final boolean sendToCulprits;
private final boolean attachLog;
private MailSender mailSender = new RegressionReportNotifier.MailSender() {
@Override
public void send(MimeMessage message) throws MessagingException {
Transport.send(message);
}
};

@DataBoundConstructor
public RegressionReportNotifier(String recipients, boolean sendToCulprits) {
this.recipients = recipients;
this.sendToCulprits = sendToCulprits;
this.attachLog = false;
}

@DataBoundConstructor
public RegressionReportNotifier(String recipients, boolean sendToCulprits, boolean attachLog) {
this.recipients = recipients;
this.sendToCulprits = sendToCulprits;
this.attachLog = attachLog;
}

@VisibleForTesting
Expand All @@ -84,9 +107,13 @@ public boolean getSendToCulprits() {
return sendToCulprits;
}

public boolean getAttachLog() {
return attachLog;
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws InterruptedException {
BuildListener listener) throws InterruptedException, IOException {
PrintStream logger = listener.getLogger();

if (build.getResult() == Result.SUCCESS) {
Expand All @@ -103,8 +130,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
}

logger.println("regression reporter starts now...");
List<CaseResult> failedTest = testResultAction.getFailedTests();
List<CaseResult> regressionedTests = Lists.newArrayList(Iterables.filter(failedTest, new RegressionPredicate()));
List<CaseResult> regressionedTests = listRegressions(testResultAction);

writeToConsole(regressionedTests, listener);
try {
Expand All @@ -117,6 +143,15 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
return true;
}

private List<CaseResult> listRegressions(
AbstractTestResultAction<?> testResultAction) {
List<? extends TestResult> failedTest = testResultAction.getFailedTests();
Iterable<? extends TestResult> filtered = Iterables.filter(failedTest, new RegressionPredicate());
List<CaseResult> regressionedTests =
Lists.newArrayList(Iterables.transform(filtered, new TestResultToCaseResult()));
return regressionedTests;
}

private void writeToConsole(List<CaseResult> regressions,
BuildListener listener) {
if (regressions.isEmpty()) {
Expand All @@ -134,7 +169,7 @@ private void writeToConsole(List<CaseResult> regressions,

private void mailReport(List<CaseResult> regressions, String recipients,
BuildListener listener, AbstractBuild<?, ?> build)
throws MessagingException {
throws MessagingException, IOException {
if (regressions.isEmpty()) {
return;
}
Expand All @@ -147,8 +182,8 @@ private void mailReport(List<CaseResult> regressions, String recipients,
if (Jenkins.getInstance() != null) {
rootUrl = Jenkins.getInstance().getRootUrl();
session = Mailer.descriptor().createSession();
adminAddress = new InternetAddress(Mailer.descriptor()
.getAdminAddress());
adminAddress = new InternetAddress(
JenkinsLocationConfiguration.get().getAdminAddress());
}
builder.append(Util.encode(rootUrl));
builder.append(Util.encode(build.getUrl()));
Expand Down Expand Up @@ -185,6 +220,10 @@ private void mailReport(List<CaseResult> regressions, String recipients,
message.setText(builder.toString());
message.setSentDate(new Date());

if (attachLog) {
attachLogFile(build, message, builder.toString());
}

mailSender.send(message);
}

Expand Down Expand Up @@ -212,6 +251,30 @@ private List<Address> parse(String recipients, BuildListener listener) {
return list;
}

private void attachLogFile(AbstractBuild<?, ?> build, MimeMessage message, String content)
throws MessagingException, IOException {

Multipart multipart = new MimeMultipart();

BodyPart bodyText = new MimeBodyPart();
bodyText.setText(content);
multipart.addBodyPart(bodyText);

String filePath = build.getRootDir().getAbsolutePath()+"buildLog.txt";
File textFile = new File(filePath);
FileOutputStream out = new FileOutputStream(textFile);
build.getLogText().writeLogTo(0, out);

BodyPart emailAttachment = new MimeBodyPart();
String fileName = "buildLog.txt";
DataSource source = new FileDataSource(filePath);
emailAttachment.setDataHandler(new DataHandler(source));
emailAttachment.setFileName(fileName);
multipart.addBodyPart(emailAttachment);

message.setContent(multipart);
}

@Extension
public static final class DescriptorImpl extends
BuildStepDescriptor<Publisher> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package jp.skypencil.jenkins.regression;

import hudson.tasks.junit.CaseResult;
import hudson.tasks.test.TestResult;

import com.google.common.base.Function;

class TestResultToCaseResult implements Function<TestResult, CaseResult> {
@Override
public CaseResult apply(TestResult input) {
return (CaseResult) input;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
<f:entry field="sendToCulprits" title="${%SentToCulprits}">
<f:checkbox />
</f:entry>
<f:entry field="attachLog" title="${%AttachLog}">
<f:checkbox />
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Recipients=Recipients
SentToCulprits=Send mail to who might make regression
SentToCulprits=Send mail to who might make regression
AttachLog=Attach build log to email
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class ChangeLogSetMock extends ChangeLogSet<ChangeLogSet.Entry> {

protected ChangeLogSetMock(AbstractBuild<?, ?> build) {
super(build);
super(build, null);
}

private final Set<Entry> set = Sets.newHashSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@
import hudson.model.FreeStyleProject;

import java.io.IOException;
import java.util.List;

import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.JenkinsRule;
import org.xml.sax.SAXException;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class RegressionReportConfigTest extends HudsonTestCase {
public class RegressionReportConfigTest {
@Rule
public JenkinsRule jenkins = new JenkinsRule();

private static final String NAME_CHECKBOX = "jp-skypencil-jenkins-regression-RegressionReportNotifier";
private static final String JOB_NAME = "Test";
Expand All @@ -38,17 +44,16 @@ public void testEnabledIsFalse() throws FormException, IOException,

private void testEnabled(boolean isEnabled) throws FormException,
IOException, SAXException {
FreeStyleProject project = createFreeStyleProject(JOB_NAME);
FreeStyleProject project = jenkins.createFreeStyleProject(JOB_NAME);

try {
HtmlPage configPage = new WebClient().goTo("job/" + JOB_NAME
HtmlPage configPage = new WebClient().getPage("job/" + JOB_NAME
+ "/configure");
HtmlForm form = configPage.getFormByName("config");
form.getInputByName(NAME_CHECKBOX).setChecked(isEnabled);
form.submit((HtmlButton) last(form
.getHtmlElementsByTagName("button")));
form.submit(last(form.getHtmlElementsByTagName("button")));

configPage = new WebClient().goTo("job/" + JOB_NAME + "/configure");
configPage = new WebClient().getPage("job/" + JOB_NAME + "/configure");
form = configPage.getFormByName("config");
HtmlInput checkbox = form.getInputByName(NAME_CHECKBOX);
assertThat(checkbox.isChecked(), is(isEnabled));
Expand All @@ -60,4 +65,9 @@ private void testEnabled(boolean isEnabled) throws FormException,
}
}
}

private HtmlButton last(List<HtmlElement> htmlElementsByTagName) {
HtmlElement lastElement = htmlElementsByTagName.get(htmlElementsByTagName.size() - 1);
return (HtmlButton) lastElement;
}
}
Loading

0 comments on commit a630423

Please sign in to comment.