Skip to content

Commit

Permalink
Use jenkins own mechanisms for archiving / managing artifacts (#30)
Browse files Browse the repository at this point in the history
* Use jenkins own mechanisms for archiving / managing artifacts

added arguments
- disableCopyFilesToBuildDir (default: false)
- archiveDirName (default: robot-plugin)

* add new configuration parameters 'archiveDirName' and disableCopyFilesToBuildDir

* remove parameter disableCopyFilesToBuildDir

remove parameter disableCopyFilesToBuildDir and implicate its value.

* remove parameter disableCopyFilesToBuildDir

remove parameter disableCopyFilesToBuildDir and implicate its value.

* fix logging of copyFilesToBuildDir section

print Messages.robot_publisher_copying() and Messages.robot_publisher_done() only if copy is really done.

Co-authored-by: Aleksi Simell <aleksi.simell@eficode.com>
  • Loading branch information
pipapo-sl and asimell committed Jan 26, 2021
1 parent 99b1854 commit 38a3f71
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 73 deletions.
105 changes: 62 additions & 43 deletions src/main/java/hudson/plugins/robot/RobotPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ public class RobotPublisher extends Recorder implements Serializable,
private static final long serialVersionUID = 1L;

protected static final String DEFAULT_REPORT_FILE = "report.html";
protected static final String FILE_ARCHIVE_DIR = "robot-plugin";
protected static final String DEFAULT_ARCHIVE_DIR = "robot-plugin";
protected static final String DEFAULT_JENKINS_ARCHIVE_DIR = "archive";

private static final String DEFAULT_OUTPUT_FILE = "output.xml";
private static final String DEFAULT_LOG_FILE = "log.html";

final private String archiveDirName;
final private String outputPath;
final private String reportFileName;
final private String logFileName;
Expand All @@ -71,6 +73,8 @@ public class RobotPublisher extends Recorder implements Serializable,
/**
* Create new publisher for Robot Framework results
*
* @param archiveDirName
* Name of Archive dir
* @param outputPath
* Path to Robot Framework's output files
* @param outputFileName
Expand All @@ -93,10 +97,11 @@ public class RobotPublisher extends Recorder implements Serializable,
* True if caching is used
*/
@DataBoundConstructor
public RobotPublisher(String outputPath, String outputFileName,
boolean disableArchiveOutput, String reportFileName, String logFileName,
double passThreshold, double unstableThreshold,
boolean onlyCritical, String otherFiles, boolean enableCache, String overwriteXAxisLabel) {
public RobotPublisher(String archiveDirName, String outputPath, String outputFileName,
boolean disableArchiveOutput, String reportFileName, String logFileName,
double passThreshold, double unstableThreshold,
boolean onlyCritical, String otherFiles, boolean enableCache, String overwriteXAxisLabel) {
this.archiveDirName = archiveDirName;
this.outputPath = outputPath;
this.outputFileName = outputFileName;
this.disableArchiveOutput = disableArchiveOutput;
Expand All @@ -117,6 +122,17 @@ public RobotPublisher(String outputPath, String outputFileName,
}
}

/**
* Gets the name of archive dir. Reverts to default if empty or
* whitespace.
* @return the name of archive dir
*/
public String getArchiveDirName() {
if (StringUtils.isBlank(archiveDirName))
return DEFAULT_ARCHIVE_DIR;
return archiveDirName;
}

/**
* Gets the output path of Robot files
* @return the output path of Robot files
Expand All @@ -137,12 +153,12 @@ public String getOutputFileName() {
}

/**
* Get the value of disable Archive of output xml checkbox
* @return the value of disable Archive of output xml checkbox
*/
* Get the value of disable Archive of output xml checkbox
* @return the value of disable Archive of output xml checkbox
*/
public boolean getDisableArchiveOutput() {
return disableArchiveOutput;
}
}

/**
* Gets the name of report html file. Reverts to default if empty or
Expand Down Expand Up @@ -196,7 +212,6 @@ public boolean getOnlyCritical() {
*/
public boolean getEnableCache() { return enableCache; }


/**
* Gets the comma separated list of other filemasks to copy into build dir
* @return List of files as string
Expand Down Expand Up @@ -252,46 +267,50 @@ public void perform(Run<?, ?> build, FilePath workspace, Launcher launcher, Task
result = parse(expandedOutputFileName, expandedLogFileName, expandedReportFileName, expandedOutputPath, build, workspace, launcher, listener);

logger.println(Messages.robot_publisher_done());
logger.println(Messages.robot_publisher_copying());

//Save configured Robot files (including split output) to build dir
copyFilesToBuildDir(build, workspace, expandedOutputPath, StringUtils.join(modifyMasksforSplittedOutput(new String[]{expandedReportFileName, expandedLogFileName, logFileJavascripts}), ","));

if (!getDisableArchiveOutput()) {
copyFilesToBuildDir(build, workspace, expandedOutputPath, StringUtils.join(modifyMasksforSplittedOutput(new String[]{expandedOutputFileName}), ","));
}

//Save other configured files to build dir
if (StringUtils.isNotBlank(getOtherFiles())) {
String filemask = buildEnv.expand(getOtherFiles());
copyFilesToBuildDir(build, workspace, expandedOutputPath, filemask);
if (!DEFAULT_JENKINS_ARCHIVE_DIR.equalsIgnoreCase(getArchiveDirName())) {
logger.println(Messages.robot_publisher_copying());
//Save configured Robot files (including split output) to build dir
copyFilesToBuildDir(build, workspace, expandedOutputPath, StringUtils.join(modifyMasksforSplittedOutput(new String[]{expandedReportFileName, expandedLogFileName, logFileJavascripts}), ","));

if (!getDisableArchiveOutput()) {
copyFilesToBuildDir(build, workspace, expandedOutputPath, StringUtils.join(modifyMasksforSplittedOutput(new String[]{expandedOutputFileName}), ","));
}

//Save other configured files to build dir
if(StringUtils.isNotBlank(getOtherFiles())) {
String filemask = buildEnv.expand(getOtherFiles());
copyFilesToBuildDir(build, workspace, expandedOutputPath, filemask);
}
logger.println(Messages.robot_publisher_done());
}

logger.println(Messages.robot_publisher_done());
logger.println(Messages.robot_publisher_assigning());

RobotBuildAction action = new RobotBuildAction(build, result, FILE_ARCHIVE_DIR, listener, expandedReportFileName, expandedLogFileName, enableCache, overwriteXAxisLabel);
RobotBuildAction action = new RobotBuildAction(build, result, getArchiveDirName(), listener, expandedReportFileName, expandedLogFileName, enableCache, overwriteXAxisLabel);
build.addAction(action);

// set RobotProjectAction as project action for Blue Ocean
Job<?, ?> job = build.getParent();
RobotProjectAction projectAction = new RobotProjectAction(job);
try {
job.addOrReplaceAction(projectAction);
} catch (UnsupportedOperationException | NullPointerException e) {
// it is possible that the action collection is an unmodifiable collection
// NullPointerException is thrown if a freestyle job runs
// set RobotProjectAction as project action
Job<?,?> job = build.getParent();
if (job != null) {
RobotProjectAction projectAction = new RobotProjectAction(job);
try {
job.addOrReplaceAction(projectAction);
} catch (UnsupportedOperationException | NullPointerException e) {
// it is possible that the action collection is an unmodifiable collection
// NullPointerException is thrown if a freestyle job runs
}

logger.println(Messages.robot_publisher_done());
logger.println(Messages.robot_publisher_checking());

Result buildResult = getBuildResult(build, result);
build.setResult(buildResult);

logger.println(Messages.robot_publisher_done());
logger.println(Messages.robot_publisher_finished());
}

logger.println(Messages.robot_publisher_done());
logger.println(Messages.robot_publisher_checking());

Result buildResult = getBuildResult(build, result);
build.setResult(buildResult);

logger.println(Messages.robot_publisher_done());
logger.println(Messages.robot_publisher_finished());

} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
Expand All @@ -311,11 +330,11 @@ public void perform(Run<?, ?> build, FilePath workspace, Launcher launcher, Task
* @throws IOException thrown exception
* @throws InterruptedException thrown exception
*/
public static void copyFilesToBuildDir(Run<?, ?> build, FilePath workspace,
public void copyFilesToBuildDir(Run<?, ?> build, FilePath workspace,
String inputPath, String filemaskToCopy) throws IOException, InterruptedException {
FilePath srcDir = new FilePath(workspace, inputPath);
FilePath destDir = new FilePath(new FilePath(build.getRootDir()),
FILE_ARCHIVE_DIR);
getArchiveDirName());
srcDir.copyRecursiveTo(filemaskToCopy, destDir);
}

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/hudson/plugins/robot/RobotStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class RobotStep extends Step {

private static final Logger logger = Logger.getLogger(RobotStep.class.getName());

private @CheckForNull String archiveDirName;
private final @Nonnull String outputPath;
private @CheckForNull String reportFileName;
private @CheckForNull String logFileName;
Expand All @@ -51,6 +52,10 @@ public RobotStep(String outputPath) {
this.outputPath = outputPath;
}

public String getArchiveDirName() {
return this.archiveDirName;
}

public String getOutputPath() {
return this.outputPath;
}
Expand Down Expand Up @@ -95,6 +100,11 @@ public String getOverwriteXAxisLabel() {
return this.overwriteXAxisLabel;
}

@DataBoundSetter
public void setArchiveDirName(String archiveDirName) {
this.archiveDirName = Util.fixEmpty(archiveDirName);
}

@DataBoundSetter
public void setReportFileName(String reportFileName) {
this.reportFileName = Util.fixEmpty(reportFileName);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/robot/RobotStepExecution.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class RobotStepExecution extends SynchronousNonBlockingStepExecution<Void
@Override protected Void run() throws Exception {
FilePath workspace = getContext().get(FilePath.class);
workspace.mkdirs();
RobotPublisher rp = new RobotPublisher(step.getOutputPath(), step.getOutputFileName(), step.getDisableArchiveOutput(), step.getReportFileName(), step.getLogFileName(), step.getPassThreshold(), step.getUnstableThreshold(), step.getOnlyCritical(), step.getOtherFiles(), step.getEnableCache(), step.getOverwriteXAxisLabel());
RobotPublisher rp = new RobotPublisher(step.getArchiveDirName(), step.getOutputPath(), step.getOutputFileName(), step.getDisableArchiveOutput(), step.getReportFileName(), step.getLogFileName(), step.getPassThreshold(), step.getUnstableThreshold(), step.getOnlyCritical(), step.getOtherFiles(), step.getEnableCache(), step.getOverwriteXAxisLabel());
rp.perform(getContext().get(Run.class), workspace, getContext().get(Launcher.class), getContext().get(TaskListener.class));
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ limitations under the License.
<f:textbox />
</f:entry>
<f:advanced>
<f:entry title="${%advanced.archivedirname}" description="${%advanced.archivedirname.description}" field="archiveDirName">
<f:textbox default="robot-plugin"/>
</f:entry>
<f:entry title="${%advanced.outputxml}" description="${%advanced.outputxml.description}" field="outputFileName">
<f:textbox />
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ output.directory=Directory of Robot output
output.directory.description=Path to directory containing robot xml and html files (relative to build workspace)
logfile.link.name=Log/Report link
logfile.link.description=Name of log or report file to be linked on job's front page
advanced.archivedirname=Name of archive directory
advanced.archivedirname.description=Name of archive directory where to store builds. Set to 'archive' to use jenkins build archive directory.
advanced.outputxml=Output xml name
advanced.outputxml.description=Name of the xml file containing robot output
advanced.disablearchiveoutputxml=Disable archiving output xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ limitations under the License.
<f:textbox />
</f:entry>
<f:advanced>
<f:entry title="${%advanced.archivedirname}" description="${%advanced.archivedirname.description}" field="archiveDirName">
<f:textbox default="robot-plugin"/>
</f:entry>
<f:entry title="${%advanced.outputxml}" description="${%advanced.outputxml.description}" field="outputFileName">
<f:textbox />
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ output.directory=Directory of Robot output
output.directory.description=Path to directory containing robot xml and html files (relative to build workspace)
logfile.link.name=Log/Report link
logfile.link.description=Name of log or report file to be linked on job's front page
advanced.archivedirname=Name of archive directory
advanced.archivedirname.description=Name of archive directory where to store builds. Set to 'archive' to use jenkins build archive directory.
advanced.outputxml=Output xml name
advanced.outputxml.description=Name of the xml file containing robot output
advanced.disablearchiveoutputxml=Disable archiving output xml
Expand Down
56 changes: 28 additions & 28 deletions src/test/java/hudson/plugins/robot/RobotPublisherSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class RobotPublisherSystemTest {
@Test
public void testRoundTripConfig() throws Exception {
FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "testRoundTripConfig");
RobotPublisher before = new RobotPublisher("a", "b", false, "c", "d", 11, 27, true, "dir1/*.jpg, dir2/*.png",
RobotPublisher before = new RobotPublisher(null, "a", "b", false, "c", "d", 11, 27, true, "dir1/*.jpg, dir2/*.png",
false, "");
p.getPublishersList().add(before);
j.configRoundtrip(p);
Expand All @@ -69,7 +69,7 @@ public void testRoundTripConfig() throws Exception {
@Test
public void testConfigView() throws Exception {
FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "testConfigView");
RobotPublisher before = new RobotPublisher("a", "b", false, "c", "d", 11, 27, true, "dir1/*.jpg, dir2/*.png",
RobotPublisher before = new RobotPublisher(null, "a", "b", false, "c", "d", 11, 27, true, "dir1/*.jpg, dir2/*.png",
false, "");
p.getPublishersList().add(before);
HtmlPage page = j.createWebClient().getPage(p, "configure");
Expand Down Expand Up @@ -97,18 +97,18 @@ public void testConfigView() throws Exception {
public void testPublish() throws Exception {
Run lastBuild = this.executeJobWithSuccess("robot");

File storedOutput = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/output.xml");
File storedSplitOutput = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/output-001.xml");
File storedReport = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/report.html");
File storedSplitReport = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/report.html");
File storedLog = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/log.html");
File storedSplitLog = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/log-001.html");
File storedJs = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/log.js");
File storedSplitJs1 = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/log-001.js");
File storedImage1 = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/screenshot.png");
File storedOutput = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/output.xml");
File storedSplitOutput = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/output-001.xml");
File storedReport = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/report.html");
File storedSplitReport = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/report.html");
File storedLog = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/log.html");
File storedSplitLog = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/log-001.html");
File storedJs = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/log.js");
File storedSplitJs1 = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/log-001.js");
File storedImage1 = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/screenshot.png");
File storedImage2 = new File(lastBuild.getRootDir(),
RobotPublisher.FILE_ARCHIVE_DIR + "/subfolder/screenshot2.png");
File storedDummy = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "dummy.file");
RobotPublisher.DEFAULT_ARCHIVE_DIR + "/subfolder/screenshot2.png");
File storedDummy = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "dummy.file");

assertTrue("output.xml was not stored", storedOutput.exists());
assertTrue("output-001.xml was not stored", storedSplitOutput.exists());
Expand All @@ -128,18 +128,18 @@ public void testPublish() throws Exception {
public void testDontCopyOuputWhendisableArchiveOutput() throws Exception {
Run lastBuild = this.executeJobWithSuccess("disable-archive-output-xml");

File storedOutput = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/output.xml");
File storedSplitOutput = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/output-001.xml");
File storedReport = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/report.html");
File storedSplitReport = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/report.html");
File storedLog = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/log.html");
File storedSplitLog = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/log-001.html");
File storedJs = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/log.js");
File storedSplitJs1 = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/log-001.js");
File storedImage1 = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/screenshot.png");
File storedOutput = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/output.xml");
File storedSplitOutput = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/output-001.xml");
File storedReport = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/report.html");
File storedSplitReport = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/report.html");
File storedLog = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/log.html");
File storedSplitLog = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/log-001.html");
File storedJs = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/log.js");
File storedSplitJs1 = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/log-001.js");
File storedImage1 = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/screenshot.png");
File storedImage2 = new File(lastBuild.getRootDir(),
RobotPublisher.FILE_ARCHIVE_DIR + "/subfolder/screenshot2.png");
File storedDummy = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "dummy.file");
RobotPublisher.DEFAULT_ARCHIVE_DIR + "/subfolder/screenshot2.png");
File storedDummy = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "dummy.file");

assertFalse("output.xml was copied", storedOutput.exists());
assertFalse("output-001.xml was copied", storedSplitOutput.exists());
Expand All @@ -159,9 +159,9 @@ public void testDontCopyOuputWhendisableArchiveOutput() throws Exception {
public void testDontCopyExcessFilesWhenOtherFilesEmpty() throws Exception {
Run lastBuild = this.executeJobWithSuccess("dont-copy");

File storedOutput = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/output.xml");
File storedSplitOutput = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/output-001.xml");
File storedDummy = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/dummy.file");
File storedOutput = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/output.xml");
File storedSplitOutput = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/output-001.xml");
File storedDummy = new File(lastBuild.getRootDir(), RobotPublisher.DEFAULT_ARCHIVE_DIR + "/dummy.file");

assertTrue("output.xml was not stored", storedOutput.exists());
assertTrue("output-001.xml was not stored", storedSplitOutput.exists());
Expand Down Expand Up @@ -354,7 +354,7 @@ public void testMissingReportFileWithOld() throws Exception {
WebClient wc = j.createWebClient();

File buildRoot = testProject.getLastBuild().getRootDir();
File robotHtmlReport = new File(buildRoot, RobotPublisher.FILE_ARCHIVE_DIR + "/report.html");
File robotHtmlReport = new File(buildRoot, RobotPublisher.DEFAULT_ARCHIVE_DIR + "/report.html");
if (!robotHtmlReport.delete())
fail("Unable to delete report directory");

Expand Down

0 comments on commit 38a3f71

Please sign in to comment.