Skip to content

Commit

Permalink
Merge pull request #43 from carlosgv87/master
Browse files Browse the repository at this point in the history
Enable use of Ant style wildcards for index files
  • Loading branch information
rbywater committed Sep 13, 2019
2 parents d93b25a + 29a2b30 commit dc2eeb2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/main/java/htmlpublisher/HtmlPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -67,8 +68,11 @@
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import hudson.util.FormValidation;

import org.apache.tools.ant.types.FileSet;
import jenkins.model.Jenkins;


/**
* Saves HTML reports for the project and publishes them.
*
Expand Down Expand Up @@ -215,8 +219,17 @@ public static boolean publishReports(Run<?, ?> build, FilePath workspace, TaskLi
String levelString = keepAll ? "BUILD" : "PROJECT";
logger.println("[htmlpublisher] Archiving at " + levelString + " level " + archiveDir + " to " + targetDir);

// The index name might be a comma separated list of names, so let's figure out all the pages we should index.
String[] csvReports = resolveParametersInString(build, listener, reportTarget.getReportFiles()).split(",");
// Index files might be a list of ant patterns, e.g. "**/*index.html,**/*otherFile.html"
// So split them and search for files within the archive directory that match that pattern
List<String> csvReports = new ArrayList<>();
File archiveDirFile = new File(archiveDir.getRemote());
if (archiveDirFile.exists()) {
String[] splitPatterns = resolveParametersInString(build, listener, reportTarget.getReportFiles()).split(",");
for (String pattern : splitPatterns) {
FileSet fs = Util.createFileSet(archiveDirFile, pattern);
csvReports.addAll(Arrays.asList(fs.getDirectoryScanner().getIncludedFiles()));
}
}

String[] titles = null;
if (reportTarget.getReportTitles() != null && reportTarget.getReportTitles().trim().length() > 0 ) {
Expand All @@ -227,10 +240,13 @@ public static boolean publishReports(Run<?, ?> build, FilePath workspace, TaskLi
}

List<String> reports = new ArrayList<>();
for (int j=0; j < csvReports.length; j++) {
String report = csvReports[j];
for (int j=0; j < csvReports.size(); j++) {
String report = csvReports.get(j);
report = report.trim();

// On windows file paths contains back slashes, but
// in the HTML file we do not want them, so replace them with forward slash
report = report.replace("\\", "/");

// Ignore blank report names caused by trailing or double commas.
if (report.isEmpty()) {
continue;
Expand Down Expand Up @@ -307,7 +323,6 @@ public static boolean publishReports(Run<?, ?> build, FilePath workspace, TaskLi
logger.println("Error: NoSuchAlgorithmException occured writing report to file "+outputFile.getAbsolutePath()+" to archiveDir:"+archiveDir.getName()+", error:"+e.getMessage());
}
}

return true;
}

Expand Down
27 changes: 27 additions & 0 deletions src/test/java/htmlpublisher/HtmlPublisherIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -111,6 +112,32 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
assertTrue(tab2Files.contains("afile.html"));
}

@Test
public void testWithWildcardPatterns() throws Exception {
FreeStyleProject p = j.createFreeStyleProject("variable_job");
final String reportDir = "autogen";
p.getBuildersList().add(new TestBuilder() {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
FilePath ws = build.getWorkspace().child(reportDir);
ws.child("nested1").child("aReportDir").child("nested").child("afile.html").write("hello", "UTF-8");
ws.child("notincluded").child("afile.html").write("hello", "UTF-8");
ws.child("otherDir").child("afile.html").write("hello", "UTF-8");
return true;
}
});
HtmlPublisherTarget target2 = new HtmlPublisherTarget("reportname", reportDir, "**/aReportDir/*/afile.html, **/otherDir/afile.html", true, true, false);
List<HtmlPublisherTarget> targets = new ArrayList<>();
targets.add(target2);
p.getPublishersList().add(new HtmlPublisher(targets));
AbstractBuild build = j.buildAndAssertSuccess(p);
File wrapperFile = new File(build.getRootDir(), "htmlreports/reportname/htmlpublisher-wrapper.html");
assertTrue(wrapperFile.exists());
String content = new String(Files.readAllBytes(wrapperFile.toPath()));
assertTrue(content.contains("nested1/aReportDir/nested/afile.html"));
assertTrue(content.contains("otherDir/afile.html"));
assertFalse(content.contains("notincluded/afile.html"));
}

private void addEnvironmentVariable(String key, String value) {
EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
EnvVars envVars = prop.getEnvVars();
Expand Down

0 comments on commit dc2eeb2

Please sign in to comment.