Skip to content

Commit

Permalink
Get comment file from workspace, fixes jenkinsci#343 (jenkinsci#512)
Browse files Browse the repository at this point in the history
* Get comment file from workspace, fixes jenkinsci#343

Using the workspace will find the comment file even when the build was executed on a slave node.

* Fix checkstyle issues
  • Loading branch information
jrudolph authored and samrocketman committed Jan 28, 2018
1 parent 882783a commit 192cf21
Showing 1 changed file with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jenkinsci.plugins.ghprb.extensions.comments;

import hudson.Extension;
import hudson.FilePath;
import hudson.model.Build;
import hudson.model.Run;
import hudson.model.TaskListener;
import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -36,16 +38,39 @@ public boolean ignorePublishedUrl() {
public String postBuildComment(Run<?, ?> build, TaskListener listener) {
StringBuilder msg = new StringBuilder();
if (commentFilePath != null && !commentFilePath.isEmpty()) {
String scriptFilePathResolved = Ghprb.replaceMacros(build, listener, commentFilePath);

try {
String scriptFilePathResolved = Ghprb.replaceMacros(build, listener, commentFilePath);
String content = null;
if (build instanceof Build<?, ?>) {
final FilePath workspace = ((Build<?, ?>) build).getWorkspace();
final FilePath path = workspace.child(scriptFilePathResolved);

if (path.exists()) {
content = path.readToString();
} else {
listener.getLogger().println(
"Didn't find comment file in workspace at " + path.absolutize().getRemote()
+ ", falling back to file operations on master."
);
}
}

if (content == null) {
content = FileUtils.readFileToString(new File(scriptFilePathResolved));
}

String content = FileUtils.readFileToString(new File(scriptFilePathResolved));
msg.append("Build comment file: \n--------------\n");
msg.append(content);
msg.append("\n--------------\n");
} catch (IOException e) {
msg.append("\n!!! Couldn't read commit file !!!\n");
listener.getLogger().println("Couldn't read comment file");
listener.getLogger().println("Couldn't read comment file at " + scriptFilePathResolved);
e.printStackTrace(listener.getLogger());
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // set interrupt flag
msg.append("\n!!! Couldn't read commit file !!!\n");
listener.getLogger().println("Reading comment file at " + scriptFilePathResolved + " was interrupted");
e.printStackTrace(listener.getLogger());
}
}
Expand Down

0 comments on commit 192cf21

Please sign in to comment.