Skip to content
Permalink
Browse files

[JENKINS-4426] add support for the jenkins junit-attachments plugin

  • Loading branch information...
cyrille-leclerc committed May 19, 2017
1 parent ab71092 commit f3a423791cfd022a55912791332a4b10ca80b132
@@ -105,6 +105,13 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>junit-attachments</artifactId>
<version>1.4.2</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>findbugs</artifactId>
@@ -31,21 +31,27 @@
import hudson.model.StreamBuildListener;
import hudson.model.TaskListener;
import hudson.tasks.junit.JUnitResultArchiver;
import hudson.tasks.junit.TestDataPublisher;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.pipeline.maven.MavenSpyLogProcessor;
import org.jenkinsci.plugins.pipeline.maven.MavenPublisher;
import org.jenkinsci.plugins.pipeline.maven.util.XmlUtils;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.w3c.dom.Element;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a>
@@ -60,6 +66,9 @@

private static final long serialVersionUID = 1L;

@CheckForNull
private Boolean ignoreAttachments;

@DataBoundConstructor
public JunitTestsPublisher() {

@@ -232,6 +241,22 @@ private void executeReporter(StepContext context, TaskListener listener, List<El
// even if "org.apache.maven.plugins:maven-surefire-plugin@test" succeeds, it maybe with "-DskipTests" and thus not have any test results.
archiver.setAllowEmptyResults(true);


if (Boolean.TRUE.equals(this.ignoreAttachments)) {
String className = "hudson.plugins.junitattachments.AttachmentPublisher";
try {
TestDataPublisher attachmentPublisher = (TestDataPublisher) Class.forName(className).newInstance();
archiver.setTestDataPublishers(Collections.singletonList(attachmentPublisher));
} catch(ClassNotFoundException e){
listener.getLogger().print("[withMaven] Jenkins ");
listener.hyperlink("https://wiki.jenkins-ci.org/display/JENKINS/JUnit+Attachments+Plugin", "JUnit Attachments Plugin");
listener.getLogger().print(" not found, can't publish test attachments ");
} catch (IllegalAccessException|InstantiationException e) {
PrintWriter err = listener.error("[withMaven] Failure to publish test attachments, exception instantiating '" + className + "'");
e.printStackTrace(err);
}
}

try {
archiver.perform(run, workspace, launcher, listener);
} catch (Exception e) {
@@ -242,6 +267,16 @@ private void executeReporter(StepContext context, TaskListener listener, List<El
}
}

@CheckForNull
public Boolean getIgnoreAttachments() {
return ignoreAttachments;
}

@DataBoundSetter
public void setIgnoreAttachments(@Nullable Boolean ignoreAttachments) {
this.ignoreAttachments = ignoreAttachments;
}

/**
* Don't use the symbol "junit", it would collide with hudson.tasks.junit.JUnitResultArchiver
*/
@@ -32,4 +32,10 @@ THE SOFTWARE.
<option value="true">True</option>
</select>
</f:entry>
<f:entry title="${%Ignore Attachments}" field="ignoreAttachments">
<select name="ignoreAttachments">
<option value="false">False</option>
<option value="true">True</option>
</select>
</f:entry>
</j:jelly>
@@ -0,0 +1,7 @@
<div>
Skip the publishing of tests reports attachments.<br/>

Test result attachments are by default published if the
<a href="https://wiki.jenkins-ci.org/display/JENKINS/JUnit+Attachments+Plugin">Jenkins JUnit Attachments Plugin</a>
is installed.
</div>

0 comments on commit f3a4237

Please sign in to comment.
You can’t perform that action at this time.