Skip to content
Permalink
Browse files

Merge pull request #71 from netceler/JENKINS-45887

JENKINS-45887 : Automatically archive JGiven reports
  • Loading branch information
Cyrille Le Clerc
Cyrille Le Clerc committed Oct 3, 2017
2 parents 7ae62bc + 749f7b6 commit 41ff6e83d7fed88999e8134113713a94f25dc4a4
@@ -59,6 +59,7 @@ Maven build executions inside the `withMaven(){...}` wil be detected and Jenkins
| Generated Findbugs reports | Publishing of the Findbugs reports generated by the Maven build | [FindBugs Plugin](https://wiki.jenkins-ci.org/display/JENKINS/FindBugs+Plugin) | `.skip-publish-findbugs-results` |
| Generated Invoker reports | Publishing of the Invoker reports generated by the Maven build | [Maven Invoker Plugin](https://wiki.jenkins.io/display/JENKINS/Maven+Invoker+Plugin) | `.skip-publish-invoker-results` |
| Generated Concordion reports | Publishing of the Concordion reports generated by the Maven build | [HTML Publisher Plugin](https://wiki.jenkins.io/display/JENKINS/HTML+Publisher+Plugin) | `.skip-publish-concordion-results` |
| Generated JGiven reports | Publishing of the JGiven reports generated by the Maven build | [JGiven Plugin](https://wiki.jenkins.io/display/JENKINS/JGiven+Plugin) | `.skip-publish-jgiven-results` |
(1) Jenkins Plugin to publish the reports on the Jenkins build page
@@ -168,6 +168,13 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>jgiven</artifactId>
<version>0.15.1</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
@@ -85,15 +85,15 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE

if (sureFireTestEvents.isEmpty()) {
if (LOGGER.isLoggable(Level.FINE)) {
listener.getLogger().println("[withMaven] No " + GROUP_ID + ":" + ARTIFACT_ID + ":" + GOAL + " execution found");
listener.getLogger().println("[withMaven] invokerPublisher - No " + GROUP_ID + ":" + ARTIFACT_ID + ":" + GOAL + " execution found");
}
return;
}

try {
Class.forName("org.jenkinsci.plugins.maveninvoker.MavenInvokerRecorder");
} catch (ClassNotFoundException e) {
listener.getLogger().print("[withMaven] Jenkins ");
listener.getLogger().print("[withMaven] invokerPublisher - Jenkins ");
listener.hyperlink("https://wiki.jenkins.io/display/JENKINS/Maven+Invoker+Plugin", "Maven Invoker Plugin");
listener.getLogger().println(" not found, don't display " + GROUP_ID + ":" + ARTIFACT_ID + ":" + GOAL + " results in pipeline screen.");
return;
@@ -128,14 +128,14 @@ private void executeReporter(StepContext context, TaskListener listener, List<El
if (reportsDirectory == null || projectsDirectory == null ) continue;

String testResults = reportsDirectory + fileSeparatorOnAgent + "*.xml";
listener.getLogger().println("[withMaven] Archive invoker results for Maven artifact " + mavenArtifact.toString() + " generated by " +
listener.getLogger().println("[withMaven] invokerPublisher - Archive invoker results for Maven artifact " + mavenArtifact.toString() + " generated by " +
pluginInvocation + ": " + testResults);
MavenInvokerRecorder archiver = new MavenInvokerRecorder("**/" + reportsDirectory + "/BUILD*.xml", "**/" + (cloneProjectsTo != null ? cloneProjectsTo : projectsDirectory));

try {
archiver.perform(run, workspace, launcher, listener);
} catch (Exception e) {
listener.error("[withMaven] Silently ignore exception archiving Invoker runs for Maven artifact " + mavenArtifact.toString() + " generated by " + pluginInvocation + ": " + e);
listener.error("[withMaven] invokerPublisher - Silently ignore exception archiving Invoker runs for Maven artifact " + mavenArtifact.toString() + " generated by " + pluginInvocation + ": " + e);
LOGGER.log(Level.WARNING, "Exception processing " + XmlUtils.toString(testEvent), e);
}

@@ -144,7 +144,7 @@ private void executeReporter(StepContext context, TaskListener listener, List<El

private String expandAndRelativize(Element element, String name, Element testEvent, Element projectElt, FilePath workspace, TaskListener listener) {
if (element == null) {
listener.getLogger().println("[withMaven] No <" + name + "> element found for <plugin> in " + XmlUtils.toString(testEvent));
listener.getLogger().println("[withMaven] invokerPublisher - No <" + name + "> element found for <plugin> in " + XmlUtils.toString(testEvent));
return null;
}

@@ -159,7 +159,7 @@ private String expandAndRelativize(Element element, String name, Element testEve
if (result.contains("${project.build.directory}")) {
String projectBuildDirectory = XmlUtils.getProjectBuildDirectory(projectElt);
if (projectBuildDirectory == null || projectBuildDirectory.isEmpty()) {
listener.getLogger().println("[withMaven] '${project.build.directory}' found for <project> in " + XmlUtils.toString(testEvent));
listener.getLogger().println("[withMaven] invokerPublisher - '${project.build.directory}' found for <project> in " + XmlUtils.toString(testEvent));
return null;
}

@@ -168,7 +168,7 @@ private String expandAndRelativize(Element element, String name, Element testEve
} else if (result.contains("${basedir}")) {
String baseDir = projectElt.getAttribute("baseDir");
if (baseDir.isEmpty()) {
listener.getLogger().println("[withMaven] '${basedir}' found for <project> in " + XmlUtils.toString(testEvent));
listener.getLogger().println("[withMaven] invokerPublisher - '${basedir}' found for <project> in " + XmlUtils.toString(testEvent));
return null;
}

@@ -0,0 +1,125 @@
/*
* The MIT License Copyright (c) 2016, CloudBees, Inc. 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: The above copyright notice and this
* permission notice shall be included in all copies or substantial portions of
* the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

package org.jenkinsci.plugins.pipeline.maven.publishers;

import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.jgiven.JgivenReportGenerator;
import org.jenkinsci.plugins.jgiven.JgivenReportGenerator.ReportConfig;
import org.jenkinsci.plugins.pipeline.maven.MavenPublisher;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.kohsuke.stapler.DataBoundConstructor;
import org.w3c.dom.Element;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Nonnull;

import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Run;
import hudson.model.StreamBuildListener;
import hudson.model.TaskListener;

/**
* @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a>
*/
public class JGivenTestsPublisher extends MavenPublisher {

public static final String REPORTS_DIR = "jgiven-reports";

private static final Logger LOGGER = Logger.getLogger(JGivenTestsPublisher.class.getName());

private static final long serialVersionUID = 1L;

@DataBoundConstructor
public JGivenTestsPublisher() {

}

@Override
public void process(@Nonnull final StepContext context, @Nonnull final Element mavenSpyLogsElt)
throws IOException, InterruptedException {

TaskListener listener = context.get(TaskListener.class);
if (listener == null) {
LOGGER.warning("TaskListener is NULL, default to stderr");
listener = new StreamBuildListener((OutputStream) System.err);
}

final FilePath workspace = context.get(FilePath.class);
final Run run = context.get(Run.class);
final Launcher launcher = context.get(Launcher.class);

final String pattern = "**/" + REPORTS_DIR + "/*";
final FilePath[] paths = workspace.list(pattern);
if (paths == null || paths.length == 0) {
if (LOGGER.isLoggable(Level.FINE)) {
listener.getLogger().println("[withMaven] jgivenPublisher - Pattern \"" + pattern
+ "\" does not match any file on workspace, aborting.");
}
return;
}

try {
Class.forName("org.jenkinsci.plugins.jgiven.JgivenReportGenerator");
} catch (final ClassNotFoundException e) {
listener.getLogger().print("[withMaven] jgivenPublisher - Jenkins ");
listener.hyperlink("https://wiki.jenkins.io/display/JENKINS/JGiven+Plugin", "JGiven Plugin");
listener.getLogger().println(" not found, do not archive jgiven reports.");
return;
}

final JgivenReportGenerator generator = new JgivenReportGenerator(new ArrayList<ReportConfig>());

try {
listener.getLogger().println("[withMaven] jgivenPublisher - Running JGiven report generator");
generator.perform(run, workspace, launcher, listener);
} catch (final Exception e) {
listener.error(
"[withMaven] jgivenPublisher - Silently ignore exception archiving JGiven reports: " + e);
LOGGER.log(Level.WARNING, "Exception processing JGiven reports archiving", e);
}
}

@Symbol("jgivenPublisher")
@Extension
public static class DescriptorImpl extends MavenPublisher.DescriptorImpl {
@Nonnull
@Override
public String getDisplayName() {
return "JGiven Publisher";
}

@Override
public int ordinal() {
return 20;
}

@Nonnull
@Override
public String getSkipFileName() {
return ".skip-publish-jgiven-results";
}
}
}
@@ -0,0 +1,31 @@
<!--
The MIT License
Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Seiji Sogabe, Tom Huybrechts
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
xmlns:t="/lib/hudson" xmlns:f="/lib/form">

<st:include page="maven-publisher" class="${descriptor.clazz}"/>

</j:jelly>
@@ -0,0 +1,3 @@
<div>
Skip the publishing of tests reports.
</div>
@@ -0,0 +1,5 @@
<div>
Publish <a href="http://jgiven.org/">JGiven</a> reports, if found.<br/>

Test results are by default published if the <a href="https://wiki.jenkins.io/display/JENKINS/JGiven+Plugin">Jenkins JGiven Plugin</a> is installed.
</div>

0 comments on commit 41ff6e8

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