Skip to content

Commit c6b29c6

Browse files
committed
StashBuilds: Add pipeline support
Implement in terms of Job and Run. Customized comments are not available in pipelines.
1 parent 1083086 commit c6b29c6

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuilds.java

+22-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package stashpullrequestbuilder.stashpullrequestbuilder;
22

33
import hudson.Util;
4-
import hudson.model.AbstractBuild;
4+
import hudson.model.AbstractProject;
5+
import hudson.model.Job;
56
import hudson.model.Result;
7+
import hudson.model.Run;
68
import hudson.model.TaskListener;
79
import java.io.IOException;
810
import java.lang.invoke.MethodHandles;
@@ -22,40 +24,45 @@ public StashBuilds(StashBuildTrigger trigger, StashRepository repository) {
2224
this.repository = repository;
2325
}
2426

25-
public void onStarted(AbstractBuild<?, ?> build) {
26-
StashCause cause = build.getCause(StashCause.class);
27+
public void onStarted(Run<?, ?> run) {
28+
StashCause cause = run.getCause(StashCause.class);
2729
if (cause == null) {
2830
return;
2931
}
3032
try {
31-
build.setDescription(cause.getShortDescription());
33+
run.setDescription(cause.getShortDescription());
3234
} catch (IOException e) {
3335
logger.log(Level.SEVERE, "Can't update build description", e);
3436
}
3537
}
3638

37-
public void onCompleted(AbstractBuild<?, ?> build, TaskListener listener) {
38-
StashCause cause = build.getCause(StashCause.class);
39+
public void onCompleted(Run<?, ?> run, TaskListener listener) {
40+
StashCause cause = run.getCause(StashCause.class);
3941
if (cause == null) {
4042
return;
4143
}
42-
Result result = build.getResult();
44+
Result result = run.getResult();
4345
// Note: current code should no longer use "new JenkinsLocationConfiguration()"
4446
// as only one instance per runtime is really supported by the current core.
4547
JenkinsLocationConfiguration globalConfig = JenkinsLocationConfiguration.get();
4648
String rootUrl = globalConfig == null ? null : globalConfig.getUrl();
4749
String buildUrl = "";
4850
if (rootUrl == null) {
49-
buildUrl = " PLEASE SET JENKINS ROOT URL FROM GLOBAL CONFIGURATION " + build.getUrl();
51+
buildUrl = " PLEASE SET JENKINS ROOT URL FROM GLOBAL CONFIGURATION " + run.getUrl();
5052
} else {
51-
buildUrl = rootUrl + build.getUrl();
53+
buildUrl = rootUrl + run.getUrl();
5254
}
5355
repository.deletePullRequestComment(cause.getPullRequestId(), cause.getBuildStartCommentId());
5456

5557
String additionalComment = "";
58+
StashPostBuildComment comments = null;
59+
Job<?, ?> job = run.getParent();
5660

57-
StashPostBuildComment comments =
58-
build.getProject().getPublishersList().get(StashPostBuildComment.class);
61+
// Post-build actions are not supported in pipelines. Need a different
62+
// approach to let pipelines publish build results.
63+
if (job instanceof AbstractProject<?, ?>) {
64+
comments = ((AbstractProject<?, ?>) job).getPublishersList().get(StashPostBuildComment.class);
65+
}
5966

6067
if (comments != null) {
6168
String buildComment =
@@ -66,28 +73,27 @@ public void onCompleted(AbstractBuild<?, ?> build, TaskListener listener) {
6673
if (buildComment != null && !buildComment.isEmpty()) {
6774
String expandedComment;
6875
try {
69-
expandedComment =
70-
Util.fixEmptyAndTrim(build.getEnvironment(listener).expand(buildComment));
76+
expandedComment = Util.fixEmptyAndTrim(run.getEnvironment(listener).expand(buildComment));
7177
} catch (IOException | InterruptedException e) {
7278
expandedComment = "Exception while expanding '" + buildComment + "': " + e;
7379
}
7480

7581
additionalComment = "\n\n" + expandedComment;
7682
}
7783
}
78-
String duration = build.getDurationString();
84+
String duration = run.getDurationString();
7985
repository.postFinishedComment(
8086
cause.getPullRequestId(),
8187
cause.getSourceCommitHash(),
8288
cause.getDestinationCommitHash(),
8389
result,
8490
buildUrl,
85-
build.getNumber(),
91+
run.getNumber(),
8692
additionalComment,
8793
duration);
8894

8995
// Merge PR
90-
if (trigger.getMergeOnSuccess() && build.getResult() == Result.SUCCESS) {
96+
if (trigger.getMergeOnSuccess() && run.getResult() == Result.SUCCESS) {
9197
boolean mergeStat =
9298
repository.mergePullRequest(cause.getPullRequestId(), cause.getPullRequestVersion());
9399
if (mergeStat == true) {

0 commit comments

Comments
 (0)