Skip to content

Commit 200b56c

Browse files
authored
Use CANCELLED build state in Bitbucket Data Center and Server 8.0 (#606) (#906)
Add an options to BitbucketBuildStatusNotificationsTrait to determine which notification to send to Bitbucket based on the Jenkins build result. Revert the default behaviour for NOT_BUILT notification from SUCCESS to FAILED (configurable to STOPPED) for Bitbucket Cloud, the SUCCESS status was erroneously introduced in 2022 as default instead of STOPPED with the disableNotificationForNotBuildJob (which can be misunderstood) configurable option. Add help description for all current configurable options for the build status notification trait
1 parent 35872b2 commit 200b56c

File tree

9 files changed

+271
-13
lines changed

9 files changed

+271
-13
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketBuildStatusNotifications.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class BitbucketBuildStatusNotifications {
6161
private static String getRootURL(@NonNull Run<?, ?> build) {
6262
JenkinsLocationConfiguration cfg = JenkinsLocationConfiguration.get();
6363

64-
if (cfg == null || cfg.getUrl() == null) {
64+
if (cfg.getUrl() == null) {
6565
throw new IllegalStateException("Could not determine Jenkins URL.");
6666
}
6767

@@ -118,6 +118,8 @@ private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener
118118
return;
119119
}
120120

121+
BitbucketSCMSourceContext context = new BitbucketSCMSourceContext(null, SCMHeadObserver.none())
122+
.withTraits(source.getTraits()); // NOSONAR
121123
final Result result = build.getResult();
122124
final String name = build.getFullDisplayName(); // use the build number as the display name of the status
123125
String buildDescription = build.getDescription();
@@ -128,7 +130,6 @@ private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener
128130
state = BitbucketBuildStatus.Status.SUCCESSFUL;
129131
} else if (Result.UNSTABLE.equals(result)) {
130132
statusDescription = StringUtils.defaultIfBlank(buildDescription, "This commit has test failures.");
131-
BitbucketSCMSourceContext context = new BitbucketSCMSourceContext(null, SCMHeadObserver.none()).withTraits(source.getTraits());
132133
if (context.sendSuccessNotificationForUnstableBuild()) {
133134
state = BitbucketBuildStatus.Status.SUCCESSFUL;
134135
} else {
@@ -138,17 +139,21 @@ private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener
138139
statusDescription = StringUtils.defaultIfBlank(buildDescription, "There was a failure building this commit.");
139140
state = BitbucketBuildStatus.Status.FAILED;
140141
} else if (Result.NOT_BUILT.equals(result)) {
141-
// Bitbucket Cloud and Server support different build states.
142142
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());
144143
if (context.disableNotificationForNotBuildJobs()) {
145-
state = (bitbucket instanceof BitbucketCloudApiClient) ? BitbucketBuildStatus.Status.STOPPED : null;
144+
// Bitbucket Cloud and Server support different build states.
145+
state = (bitbucket instanceof BitbucketCloudApiClient) ? BitbucketBuildStatus.Status.STOPPED : BitbucketBuildStatus.Status.CANCELLED;
146146
} else {
147-
state = BitbucketBuildStatus.Status.SUCCESSFUL;
147+
state = BitbucketBuildStatus.Status.FAILED;
148148
}
149149
} else if (result != null) { // ABORTED etc.
150150
statusDescription = StringUtils.defaultIfBlank(buildDescription, "Something is wrong with the build of this commit.");
151-
state = BitbucketBuildStatus.Status.FAILED;
151+
if (context.sendStopNotificationForAbortBuild()) {
152+
// Bitbucket Cloud and Server support different build states.
153+
state = (bitbucket instanceof BitbucketCloudApiClient) ? BitbucketBuildStatus.Status.STOPPED : BitbucketBuildStatus.Status.CANCELLED;
154+
} else {
155+
state = BitbucketBuildStatus.Status.FAILED;
156+
}
152157
} else {
153158
statusDescription = StringUtils.defaultIfBlank(buildDescription, "The build is in progress...");
154159
state = BitbucketBuildStatus.Status.INPROGRESS;
@@ -187,7 +192,7 @@ private static void sendNotifications(BitbucketSCMSource source, Run<?, ?> build
187192
}
188193
boolean shareBuildKeyBetweenBranchAndPR = sourceContext
189194
.filters().stream()
190-
.anyMatch(filter -> filter instanceof ExcludeOriginPRBranchesSCMHeadFilter);
195+
.anyMatch(ExcludeOriginPRBranchesSCMHeadFilter.class::isInstance);
191196

192197
String key;
193198
BitbucketApi bitbucket;
@@ -239,7 +244,7 @@ private static String getBuildKey(@NonNull Run<?, ?> build, String branch,
239244
* Sends notifications to Bitbucket on Checkout (for the "In Progress" Status).
240245
*/
241246
@Extension
242-
public static class JobCheckOutListener extends SCMListener {
247+
public static class JobCheckoutListener extends SCMListener {
243248

244249
@Override
245250
public void onCheckout(Run<?, ?> build, SCM scm, FilePath workspace, TaskListener listener, File changelogFile,

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketBuildStatusNotificationsTrait.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,17 @@
4141
public class BitbucketBuildStatusNotificationsTrait extends SCMSourceTrait {
4242

4343
/**
44-
* Should unstable builds be communicated as success to Bitbucket
44+
* Should unstable builds be communicated as success to Bitbucket.
4545
*/
4646
private boolean sendSuccessNotificationForUnstableBuild;
4747

4848
/**
49-
* Should not build jobs be communicated to Bitbucket
49+
* Aborted jobs must be communicated as stopped to Bitbucket.
50+
*/
51+
private boolean sendStoppedNotificationForAbortBuild;
52+
53+
/**
54+
* Should not build jobs be communicated to Bitbucket.
5055
*/
5156
private boolean disableNotificationForNotBuildJobs;
5257

@@ -73,6 +78,27 @@ public boolean getSendSuccessNotificationForUnstableBuild() {
7378
return this.sendSuccessNotificationForUnstableBuild;
7479
}
7580

81+
/**
82+
* Set if aborted builds will be communicated as stopped.
83+
*
84+
* @param sendStop comunicate Stop/Cancelled build status to Bitbucket for
85+
* aborted build.
86+
*/
87+
@DataBoundSetter
88+
public void setSendStoppedNotificationForAbortBuild(boolean sendStop) {
89+
sendStoppedNotificationForAbortBuild = sendStop;
90+
}
91+
92+
/**
93+
* Return if aborted builds will be communicated as stopped.
94+
*
95+
* @return true will be comunicate to Bitbucket as Stopped/Cancelled build
96+
* failed otherwise.
97+
*/
98+
public boolean getSendStoppedNotificationForAbortBuild() {
99+
return this.sendStoppedNotificationForAbortBuild;
100+
}
101+
76102
@DataBoundSetter
77103
public void setDisableNotificationForNotBuildJobs(boolean isNotificationDisabled) {
78104
disableNotificationForNotBuildJobs = isNotificationDisabled;
@@ -92,6 +118,7 @@ public boolean getDisableNotificationForNotBuildJobs() {
92118
protected void decorateContext(SCMSourceContext<?, ?> context) {
93119
((BitbucketSCMSourceContext) context).withDisableNotificationForNotBuildJobs(getDisableNotificationForNotBuildJobs());
94120
((BitbucketSCMSourceContext) context).withSendSuccessNotificationForUnstableBuild(getSendSuccessNotificationForUnstableBuild());
121+
((BitbucketSCMSourceContext) context).withSendStoppedNotificationForAbortBuild(getSendStoppedNotificationForAbortBuild());
95122
}
96123

97124
/**

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSourceContext.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public class BitbucketSCMSourceContext extends SCMSourceContext<BitbucketSCMSour
8181
*/
8282
@NonNull
8383
private WebhookConfiguration webhookConfiguration = new WebhookConfiguration();
84+
8485
/**
8586
* {@code true} if notifications should be disabled in this context.
8687
*/
@@ -92,7 +93,12 @@ public class BitbucketSCMSourceContext extends SCMSourceContext<BitbucketSCMSour
9293
private boolean sendSuccessNotificationForUnstableBuild;
9394

9495
/**
95-
* {@code false} if not build jobs should be send to Bitbucket.
96+
* {@code true} if aborted builds will be communicated as stopped/cancelled.
97+
*/
98+
private boolean sendStoppedNotificationForAbortBuild;
99+
100+
/**
101+
* {@code false} if not built jobs should be send to Bitbucket.
96102
*/
97103
private boolean disableNotificationForNotBuildJobs;
98104

@@ -208,7 +214,15 @@ public final WebhookConfiguration webhookConfiguration() {
208214
*/
209215
public final boolean notificationsDisabled() {
210216
return notificationsDisabled;
217+
}
211218

219+
/**
220+
* Returns if aborted builds should be passed as stopped/cancelled to Bitbucket.
221+
*
222+
* @return {@code true} if aborted builds should be passed as stopped/cancelled to Bitbucket.
223+
*/
224+
public final boolean sendStopNotificationForAbortBuild() {
225+
return sendStoppedNotificationForAbortBuild;
212226
}
213227

214228
/**
@@ -366,6 +380,18 @@ public final BitbucketSCMSourceContext withSendSuccessNotificationForUnstableBui
366380
return this;
367381
}
368382

383+
/**
384+
* Defines behaviour of unstable builds in Bitbucket.
385+
*
386+
* @param sendStopped {@code true} to consider aborted builds as stopped when notifying Bitbucket.
387+
* @return {@code this} for method chaining.
388+
*/
389+
@NonNull
390+
public final BitbucketSCMSourceContext withSendStoppedNotificationForAbortBuild(boolean sendStopped) {
391+
this.sendStoppedNotificationForAbortBuild = sendStopped;
392+
return this;
393+
}
394+
369395
/**
370396
* Defines behaviour of not-built jobs in Bitbucket.
371397
*

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/BitbucketBuildStatus.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ public class BitbucketBuildStatus {
3636
public enum Status {
3737
INPROGRESS("INPROGRESS"),
3838
FAILED("FAILED"),
39+
// available only in Cloud
3940
STOPPED("STOPPED"),
41+
// available only in Data Center
42+
CANCELLED("CANCELLED"),
4043
SUCCESSFUL("SUCCESSFUL");
4144

4245
private final String status;

src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/BitbucketBuildStatusNotificationsTrait/config.jelly

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
<f:entry title="${%Communicate unstable builds to Bitbucket as successful}" field="sendSuccessNotificationForUnstableBuild">
44
<f:checkbox/>
55
</f:entry>
6-
<f:entry title="${%Do not communicate not-built jobs to Bitbucket}" field="disableNotificationForNotBuildJobs">
6+
<f:entry title="${%Communicate not-built jobs to Bitbucket as stopped}" field="disableNotificationForNotBuildJobs">
7+
<f:checkbox/>
8+
</f:entry>
9+
<f:entry title="${%Communicate abort build to Bitbucket as stopped}" field="sendStoppedNotificationForAbortBuild">
710
<f:checkbox/>
811
</f:entry>
912
</j:jelly>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div>
2+
Not-built result by default is notificated as FAILED status to Bitbucket.
3+
If this option is enabled than not-built result is notified as STOPPED for Bitbucket Cloud or as CANCELLED for Bitbucket Data Center.
4+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div>
2+
Aborted build by default is notificated as FAILED status to Bitbucket.
3+
If this option is enabled than aborted build is notified as STOPPED for Bitbucket Cloud or as CANCELLED for Bitbucket Data Center.
4+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div>
2+
Unstable build by default is notificated as FAILED status to Bitbucket.
3+
If this option is enabled than unstable build is notified as SUCCESS to Bitbucket.
4+
</div>

0 commit comments

Comments
 (0)