4
4
import hudson .Util ;
5
5
import hudson .model .AbstractBuild ;
6
6
import hudson .model .Result ;
7
+ import hudson .model .Run ;
7
8
import hudson .model .TaskListener ;
8
9
import hudson .model .listeners .RunListener ;
9
10
import java .io .IOException ;
16
17
17
18
/** Created by Nathan McCarthy */
18
19
@ Extension
19
- public class StashBuildListener extends RunListener <AbstractBuild <?, ?>> {
20
+ public class StashBuildListener extends RunListener <Run <?, ?>> {
20
21
private static final Logger logger =
21
22
Logger .getLogger (MethodHandles .lookup ().lookupClass ().getName ());
22
23
23
24
@ Override
24
- public void onStarted (AbstractBuild <?, ?> abstractBuild , TaskListener listener ) {
25
+ public void onStarted (Run <?, ?> run , TaskListener listener ) {
25
26
logger .info ("BuildListener onStarted called." );
26
27
27
- StashCause cause = abstractBuild .getCause (StashCause .class );
28
+ StashCause cause = run .getCause (StashCause .class );
28
29
if (cause == null ) {
29
30
return ;
30
31
}
31
32
32
33
try {
33
- abstractBuild .setDescription (cause .getShortDescription ());
34
+ run .setDescription (cause .getShortDescription ());
34
35
} catch (IOException e ) {
35
36
logger .log (Level .SEVERE , "Can't update build description" , e );
36
37
}
37
38
}
38
39
39
40
@ Override
40
- public void onCompleted (AbstractBuild <?, ?> abstractBuild , @ Nonnull TaskListener listener ) {
41
- StashCause cause = abstractBuild .getCause (StashCause .class );
41
+ public void onCompleted (Run <?, ?> run , @ Nonnull TaskListener listener ) {
42
+ StashCause cause = run .getCause (StashCause .class );
42
43
if (cause == null ) {
43
44
return ;
44
45
}
45
46
46
47
StashBuildTrigger trigger =
47
- ParameterizedJobMixIn .getTrigger (abstractBuild .getParent (), StashBuildTrigger .class );
48
+ ParameterizedJobMixIn .getTrigger (run .getParent (), StashBuildTrigger .class );
48
49
if (trigger == null ) {
49
50
return ;
50
51
}
51
52
52
53
StashRepository repository = trigger .getBuilder ().getRepository ();
53
- Result result = abstractBuild .getResult ();
54
+ Result result = run .getResult ();
54
55
// Note: current code should no longer use "new JenkinsLocationConfiguration()"
55
56
// as only one instance per runtime is really supported by the current core.
56
57
JenkinsLocationConfiguration globalConfig = JenkinsLocationConfiguration .get ();
57
58
String rootUrl = globalConfig == null ? null : globalConfig .getUrl ();
58
59
String buildUrl = "" ;
59
60
if (rootUrl == null ) {
60
- buildUrl = " PLEASE SET JENKINS ROOT URL FROM GLOBAL CONFIGURATION " + abstractBuild .getUrl ();
61
+ buildUrl = " PLEASE SET JENKINS ROOT URL FROM GLOBAL CONFIGURATION " + run .getUrl ();
61
62
} else {
62
- buildUrl = rootUrl + abstractBuild .getUrl ();
63
+ buildUrl = rootUrl + run .getUrl ();
63
64
}
64
65
repository .deletePullRequestComment (cause .getPullRequestId (), cause .getBuildStartCommentId ());
65
66
66
67
String additionalComment = "" ;
68
+ StashPostBuildComment comments = null ;
67
69
68
- StashPostBuildComment comments =
69
- abstractBuild .getProject ().getPublishersList ().get (StashPostBuildComment .class );
70
+ if (run instanceof AbstractBuild ) {
71
+ AbstractBuild <?, ?> build = (AbstractBuild <?, ?>) run ;
72
+ comments = build .getProject ().getPublishersList ().get (StashPostBuildComment .class );
73
+ }
70
74
71
75
if (comments != null ) {
72
76
String buildComment =
@@ -77,28 +81,27 @@ public void onCompleted(AbstractBuild<?, ?> abstractBuild, @Nonnull TaskListener
77
81
if (buildComment != null && !buildComment .isEmpty ()) {
78
82
String expandedComment ;
79
83
try {
80
- expandedComment =
81
- Util .fixEmptyAndTrim (abstractBuild .getEnvironment (listener ).expand (buildComment ));
84
+ expandedComment = Util .fixEmptyAndTrim (run .getEnvironment (listener ).expand (buildComment ));
82
85
} catch (IOException | InterruptedException e ) {
83
86
expandedComment = "Exception while expanding '" + buildComment + "': " + e ;
84
87
}
85
88
86
89
additionalComment = "\n \n " + expandedComment ;
87
90
}
88
91
}
89
- String duration = abstractBuild .getDurationString ();
92
+ String duration = run .getDurationString ();
90
93
repository .postFinishedComment (
91
94
cause .getPullRequestId (),
92
95
cause .getSourceCommitHash (),
93
96
cause .getDestinationCommitHash (),
94
97
result ,
95
98
buildUrl ,
96
- abstractBuild .getNumber (),
99
+ run .getNumber (),
97
100
additionalComment ,
98
101
duration );
99
102
100
103
// Merge PR
101
- if (trigger .getMergeOnSuccess () && abstractBuild .getResult () == Result .SUCCESS ) {
104
+ if (trigger .getMergeOnSuccess () && run .getResult () == Result .SUCCESS ) {
102
105
boolean mergeStat =
103
106
repository .mergePullRequest (cause .getPullRequestId (), cause .getPullRequestVersion ());
104
107
if (mergeStat == true ) {
0 commit comments