5858 */
5959public class BitbucketBuildStatusNotifications {
6060
61- private static final String SUCCESSFUL_STATE = "SUCCESSFUL" ;
62- private static final String FAILED_STATE = "FAILED" ;
63- private static final String STOPPED_STATE = "STOPPED" ;
64- private static final String INPROGRESS_STATE = "INPROGRESS" ;
65-
6661 private static String getRootURL (@ NonNull Run <?, ?> build ) {
6762 JenkinsLocationConfiguration cfg = JenkinsLocationConfiguration .get ();
6863
@@ -105,8 +100,8 @@ private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener
105100 @ NonNull BitbucketApi bitbucket , @ NonNull String key , @ NonNull String hash )
106101 throws IOException , InterruptedException {
107102
108- final SCMSource s = SCMSource .SourceByItem .findSource (build .getParent ());
109- if (!(s instanceof BitbucketSCMSource )) {
103+ final SCMSource source = SCMSource .SourceByItem .findSource (build .getParent ());
104+ if (!(source instanceof BitbucketSCMSource )) {
110105 return ;
111106 }
112107
@@ -123,44 +118,50 @@ private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener
123118 return ;
124119 }
125120
126- String name = build .getFullDisplayName (); // use the build number as the display name of the status
127- BitbucketBuildStatus status ;
128- Result result = build .getResult ();
121+ final Result result = build .getResult ();
122+ final String name = build .getFullDisplayName (); // use the build number as the display name of the status
129123 String buildDescription = build .getDescription ();
130124 String statusDescription ;
131- String state ;
125+ BitbucketBuildStatus . Status state ;
132126 if (Result .SUCCESS .equals (result )) {
133127 statusDescription = StringUtils .defaultIfBlank (buildDescription , "This commit looks good." );
134- state = SUCCESSFUL_STATE ;
128+ state = BitbucketBuildStatus . Status . SUCCESSFUL ;
135129 } else if (Result .UNSTABLE .equals (result )) {
136130 statusDescription = StringUtils .defaultIfBlank (buildDescription , "This commit has test failures." );
137-
138- BitbucketSCMSource source = (BitbucketSCMSource ) s ;
139- BitbucketSCMSourceContext sourceContext = new BitbucketSCMSourceContext (null , SCMHeadObserver .none ())
140- .withTraits (source .getTraits ());
141- if (sourceContext .sendSuccessNotificationForUnstableBuild ()) {
142- state = SUCCESSFUL_STATE ;
131+ BitbucketSCMSourceContext context = new BitbucketSCMSourceContext (null , SCMHeadObserver .none ()).withTraits (source .getTraits ());
132+ if (context .sendSuccessNotificationForUnstableBuild ()) {
133+ state = BitbucketBuildStatus .Status .SUCCESSFUL ;
143134 } else {
144- state = FAILED_STATE ;
135+ state = BitbucketBuildStatus . Status . FAILED ;
145136 }
146137 } else if (Result .FAILURE .equals (result )) {
147138 statusDescription = StringUtils .defaultIfBlank (buildDescription , "There was a failure building this commit." );
148- state = FAILED_STATE ;
139+ state = BitbucketBuildStatus . Status . FAILED ;
149140 } else if (Result .NOT_BUILT .equals (result )) {
150141 // Bitbucket Cloud and Server support different build states.
151- state = (bitbucket instanceof BitbucketCloudApiClient ) ? STOPPED_STATE : SUCCESSFUL_STATE ;
152142 statusDescription = StringUtils .defaultIfBlank (buildDescription , "This commit was not built (probably the build was skipped)" );
143+ BitbucketSCMSourceContext context = new BitbucketSCMSourceContext (null , SCMHeadObserver .none ()).withTraits (source .getTraits ());
144+ if (context .disableNotificationForNotBuildJobs ()) {
145+ state = (bitbucket instanceof BitbucketCloudApiClient ) ? BitbucketBuildStatus .Status .STOPPED : null ;
146+ } else {
147+ state = BitbucketBuildStatus .Status .SUCCESSFUL ;
148+ }
153149 } else if (result != null ) { // ABORTED etc.
154150 statusDescription = StringUtils .defaultIfBlank (buildDescription , "Something is wrong with the build of this commit." );
155- state = FAILED_STATE ;
151+ state = BitbucketBuildStatus . Status . FAILED ;
156152 } else {
157153 statusDescription = StringUtils .defaultIfBlank (buildDescription , "The build is in progress..." );
158- state = INPROGRESS_STATE ;
154+ state = BitbucketBuildStatus . Status . INPROGRESS ;
159155 }
160- status = new BitbucketBuildStatus (hash , statusDescription , state , url , key , name );
161- new BitbucketChangesetCommentNotifier (bitbucket ).buildStatus (status );
162- if (result != null ) {
163- listener .getLogger ().println ("[Bitbucket] Build result notified" );
156+
157+ if (state != null ) {
158+ BitbucketChangesetCommentNotifier notifier = new BitbucketChangesetCommentNotifier (bitbucket );
159+ notifier .buildStatus (new BitbucketBuildStatus (hash , statusDescription , state , url , key , name ));
160+ if (result != null ) {
161+ listener .getLogger ().println ("[Bitbucket] Build result notified" );
162+ }
163+ } else {
164+ listener .getLogger ().println ("[Bitbucket] Skip result notification" );
164165 }
165166 }
166167
0 commit comments