Skip to content

Commit

Permalink
[JENKINS-26343] - Workflow Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-nenashev committed Aug 17, 2015
1 parent 0b55395 commit 75d92bb
Show file tree
Hide file tree
Showing 8 changed files with 711 additions and 30 deletions.
58 changes: 57 additions & 1 deletion pom.xml
Expand Up @@ -4,12 +4,13 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.424</version>
<version>1.580.1</version>
</parent>

<properties>
<findbugs-maven-plugin.version>3.0.1</findbugs-maven-plugin.version>
<findbugs.failOnError>true</findbugs.failOnError>
<workflow.version>1.4</workflow.version>
</properties>

<licenses>
Expand Down Expand Up @@ -67,6 +68,23 @@
</execution>
</executions>
</plugin>
<!--TODO: Remove after the update to 1.594+-->
<!--Prevents a conflict with groovy-all package, which happens for the default 1.5-jenkins-1-->
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.5-jenkins-3</version>
<configuration>
<providerSelection>1.7</providerSelection>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.gmaven.runtime</groupId>
<artifactId>gmaven-runtime-1.7</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

Expand All @@ -90,6 +108,44 @@
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>matrix-project</artifactId>
<version>1.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>${workflow.version}</version>
</dependency>
<dependency> <!-- Test framework -->
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>${workflow.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-aggregator</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.infradna.tool</groupId>
<artifactId>bridge-method-annotation</artifactId>
<version>1.14</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci</groupId>
<artifactId>annotation-indexer</artifactId>
<version>1.9</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>


51 changes: 41 additions & 10 deletions src/main/java/htmlpublisher/HtmlPublisher.java
Expand Up @@ -23,6 +23,7 @@
*/
package htmlpublisher;

import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
Expand All @@ -46,6 +47,8 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

/**
* Saves HTML reports for the project and publishes them.
Expand All @@ -57,6 +60,7 @@ public class HtmlPublisher extends Recorder {
private final ArrayList<HtmlPublisherTarget> reportTargets;

@DataBoundConstructor
@Restricted(NoExternalUse.class)
public HtmlPublisher(List<HtmlPublisherTarget> reportTargets) {
this.reportTargets = reportTargets != null ? new ArrayList<HtmlPublisherTarget>(reportTargets) : new ArrayList<HtmlPublisherTarget>();
}
Expand All @@ -80,10 +84,15 @@ private static void writeFile(ArrayList<String> lines, File path) throws IOExcep

public ArrayList<String> readFile(String filePath) throws java.io.FileNotFoundException,
java.io.IOException {
return readFile(filePath, this.getClass());
}

public static ArrayList<String> readFile(String filePath, Class<?> publisherClass)
throws java.io.FileNotFoundException, java.io.IOException {
ArrayList<String> aList = new ArrayList<String>();

try {
final InputStream is = this.getClass().getResourceAsStream(filePath);
final InputStream is = publisherClass.getResourceAsStream(filePath);
try {
// We expect that files have been generated with the default system's charset
final Reader r = new InputStreamReader(is, Charset.defaultCharset());
Expand Down Expand Up @@ -126,7 +135,7 @@ public ArrayList<String> readFile(String filePath) throws java.io.FileNotFoundEx
return aList;
}

protected static String resolveParametersInString(AbstractBuild<?, ?> build, BuildListener listener, String input) {
protected static String resolveParametersInString(Run<?, ?> build, TaskListener listener, String input) {
try {
return build.getEnvironment(listener).expand(input);
} catch (Exception e) {
Expand All @@ -135,18 +144,38 @@ protected static String resolveParametersInString(AbstractBuild<?, ?> build, Bui
}
return input;
}

protected static String resolveParametersInString(EnvVars envVars, TaskListener listener, String input) {
try {
return envVars.expand(input);
} catch (Exception e) {
listener.getLogger().println("Failed to resolve parameters in string \""+
input+"\" due to following error:\n"+e.getMessage());
}
return input;
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException {
return publishReports(build, build.getWorkspace(), launcher, listener, reportTargets, this.getClass());
}

/**
* Runs HTML the publishing operation for specified {@link HtmlPublisherTarget}s.
* @return False if the operation failed
* @since TODO
*/
public static boolean publishReports(Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener,
List<HtmlPublisherTarget> reportTargets, Class<?> publisherClass) throws InterruptedException {
listener.getLogger().println("[htmlpublisher] Archiving HTML reports...");

// Grab the contents of the header and footer as arrays
ArrayList<String> headerLines;
ArrayList<String> footerLines;
try {
headerLines = this.readFile("/htmlpublisher/HtmlPublisher/header.html");
footerLines = this.readFile("/htmlpublisher/HtmlPublisher/footer.html");
headerLines = readFile("/htmlpublisher/HtmlPublisher/header.html", publisherClass);
footerLines = readFile("/htmlpublisher/HtmlPublisher/footer.html", publisherClass);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
return false;
Expand All @@ -155,14 +184,14 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
return false;
}

for (int i=0; i < this.reportTargets.size(); i++) {
for (int i=0; i < reportTargets.size(); i++) {
// Create an array of lines we will eventually write out, initially the header.
ArrayList<String> reportLines = new ArrayList<String>(headerLines);
HtmlPublisherTarget reportTarget = this.reportTargets.get(i);
HtmlPublisherTarget reportTarget = reportTargets.get(i);
boolean keepAll = reportTarget.getKeepAll();
boolean allowMissing = reportTarget.getAllowMissing();

FilePath archiveDir = build.getWorkspace().child(resolveParametersInString(build, listener, reportTarget.getReportDir()));
FilePath archiveDir = workspace.child(resolveParametersInString(build, listener, reportTarget.getReportDir()));
FilePath targetDir = reportTarget.getArchiveTarget(build);

String levelString = keepAll ? "BUILD" : "PROJECT";
Expand Down Expand Up @@ -193,7 +222,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
}
// Add the JS to change the link as appropriate.
String hudsonUrl = Hudson.getInstance().getRootUrl();
AbstractProject job = build.getProject();
Job job = build.getParent();
reportLines.add("<script type=\"text/javascript\">document.getElementById(\"hudson_link\").innerHTML=\"Back to " + job.getName() + "\";</script>");
// If the URL isn't configured in Hudson, the best we can do is attempt to go Back.
if (hudsonUrl == null) {
Expand All @@ -217,7 +246,8 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

if (archiveDir.copyRecursiveTo("**/*", targetDir) == 0 && !allowMissing) {
listener.error("Directory '" + archiveDir + "' exists but failed copying to '" + targetDir + "'.");
if (build.getResult().isBetterOrEqualTo(Result.UNSTABLE)) {
final Result buildResult = build.getResult();
if (buildResult != null && buildResult.isBetterOrEqualTo(Result.UNSTABLE)) {
// If the build failed, don't complain that there was no coverage.
// The build probably didn't even get to the point where it produces coverage.
listener.error("This is especially strange since your build otherwise succeeded.");
Expand Down Expand Up @@ -295,6 +325,7 @@ public boolean isApplicable(Class<? extends AbstractProject> jobType) {
}
}

@Override
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}
Expand Down

0 comments on commit 75d92bb

Please sign in to comment.