1
1
package stashpullrequestbuilder .stashpullrequestbuilder ;
2
2
3
3
import hudson .Util ;
4
- import hudson .model .AbstractBuild ;
4
+ import hudson .model .AbstractProject ;
5
+ import hudson .model .Job ;
5
6
import hudson .model .Result ;
7
+ import hudson .model .Run ;
6
8
import hudson .model .TaskListener ;
7
9
import java .io .IOException ;
8
10
import java .lang .invoke .MethodHandles ;
@@ -22,40 +24,45 @@ public StashBuilds(StashBuildTrigger trigger, StashRepository repository) {
22
24
this .repository = repository ;
23
25
}
24
26
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 );
27
29
if (cause == null ) {
28
30
return ;
29
31
}
30
32
try {
31
- build .setDescription (cause .getShortDescription ());
33
+ run .setDescription (cause .getShortDescription ());
32
34
} catch (IOException e ) {
33
35
logger .log (Level .SEVERE , "Can't update build description" , e );
34
36
}
35
37
}
36
38
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 );
39
41
if (cause == null ) {
40
42
return ;
41
43
}
42
- Result result = build .getResult ();
44
+ Result result = run .getResult ();
43
45
// Note: current code should no longer use "new JenkinsLocationConfiguration()"
44
46
// as only one instance per runtime is really supported by the current core.
45
47
JenkinsLocationConfiguration globalConfig = JenkinsLocationConfiguration .get ();
46
48
String rootUrl = globalConfig == null ? null : globalConfig .getUrl ();
47
49
String buildUrl = "" ;
48
50
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 ();
50
52
} else {
51
- buildUrl = rootUrl + build .getUrl ();
53
+ buildUrl = rootUrl + run .getUrl ();
52
54
}
53
55
repository .deletePullRequestComment (cause .getPullRequestId (), cause .getBuildStartCommentId ());
54
56
55
57
String additionalComment = "" ;
58
+ StashPostBuildComment comments = null ;
59
+ Job <?, ?> job = run .getParent ();
56
60
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
+ }
59
66
60
67
if (comments != null ) {
61
68
String buildComment =
@@ -66,28 +73,27 @@ public void onCompleted(AbstractBuild<?, ?> build, TaskListener listener) {
66
73
if (buildComment != null && !buildComment .isEmpty ()) {
67
74
String expandedComment ;
68
75
try {
69
- expandedComment =
70
- Util .fixEmptyAndTrim (build .getEnvironment (listener ).expand (buildComment ));
76
+ expandedComment = Util .fixEmptyAndTrim (run .getEnvironment (listener ).expand (buildComment ));
71
77
} catch (IOException | InterruptedException e ) {
72
78
expandedComment = "Exception while expanding '" + buildComment + "': " + e ;
73
79
}
74
80
75
81
additionalComment = "\n \n " + expandedComment ;
76
82
}
77
83
}
78
- String duration = build .getDurationString ();
84
+ String duration = run .getDurationString ();
79
85
repository .postFinishedComment (
80
86
cause .getPullRequestId (),
81
87
cause .getSourceCommitHash (),
82
88
cause .getDestinationCommitHash (),
83
89
result ,
84
90
buildUrl ,
85
- build .getNumber (),
91
+ run .getNumber (),
86
92
additionalComment ,
87
93
duration );
88
94
89
95
// Merge PR
90
- if (trigger .getMergeOnSuccess () && build .getResult () == Result .SUCCESS ) {
96
+ if (trigger .getMergeOnSuccess () && run .getResult () == Result .SUCCESS ) {
91
97
boolean mergeStat =
92
98
repository .mergePullRequest (cause .getPullRequestId (), cause .getPullRequestVersion ());
93
99
if (mergeStat == true ) {
0 commit comments