Skip to content

Commit da24678

Browse files
authored
Avoid "Success" Bitbucket notification, case build was aborted (#448)
* Add notification trait for NOT_BUILD jobs. Signed-off-by: Alexander Falkenstern <[email protected]> * Avoid NPE, if repository is not set.
1 parent ebd4f2d commit da24678

File tree

6 files changed

+103
-33
lines changed

6 files changed

+103
-33
lines changed

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

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@
5858
*/
5959
public 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

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public class BitbucketBuildStatusNotificationsTrait extends SCMSourceTrait {
4545
*/
4646
private boolean sendSuccessNotificationForUnstableBuild;
4747

48+
/**
49+
* Should not build jobs be communicated to Bitbucket
50+
*/
51+
private boolean disableNotificationForNotBuildJobs;
52+
4853
/**
4954
* Constructor.
5055
*
@@ -68,11 +73,24 @@ public boolean getSendSuccessNotificationForUnstableBuild() {
6873
return this.sendSuccessNotificationForUnstableBuild;
6974
}
7075

76+
@DataBoundSetter
77+
public void setDisableNotificationForNotBuildJobs(boolean isNotificationDisabled) {
78+
disableNotificationForNotBuildJobs = isNotificationDisabled;
79+
}
80+
81+
/**
82+
* @return if unstable builds will be communicated
83+
*/
84+
public boolean getDisableNotificationForNotBuildJobs() {
85+
return this.disableNotificationForNotBuildJobs;
86+
}
87+
7188
/**
7289
* {@inheritDoc}
7390
*/
7491
@Override
7592
protected void decorateContext(SCMSourceContext<?, ?> context) {
93+
((BitbucketSCMSourceContext) context).withDisableNotificationForNotBuildJobs(getDisableNotificationForNotBuildJobs());
7694
((BitbucketSCMSourceContext) context).withSendSuccessNotificationForUnstableBuild(getSendSuccessNotificationForUnstableBuild());
7795
}
7896

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,8 @@ protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHe
552552
CredentialsNameProvider.name(scanCredentials));
553553
}
554554
// this has the side effect of ensuring that repository type is always populated.
555-
listener.getLogger().format("Repository type: %s%n", WordUtils.capitalizeFully(getRepositoryType().name()));
555+
final BitbucketRepositoryType repositoryType = getRepositoryType();
556+
listener.getLogger().format("Repository type: %s%n", WordUtils.capitalizeFully(repositoryType != null ? repositoryType.name() : "Unknown"));
556557

557558
// populate the request with its data sources
558559
if (request.isFetchPRs()) {

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public class BitbucketSCMSourceContext extends SCMSourceContext<BitbucketSCMSour
9191
*/
9292
private boolean sendSuccessNotificationForUnstableBuild;
9393

94+
/**
95+
* {@code false} if not build jobs should be send to Bitbucket.
96+
*/
97+
private boolean disableNotificationForNotBuildJobs;
98+
9499
/**
95100
* Constructor.
96101
*
@@ -215,6 +220,15 @@ public final boolean sendSuccessNotificationForUnstableBuild() {
215220
return sendSuccessNotificationForUnstableBuild;
216221
}
217222

223+
/**
224+
* Returns {@code false} if not build jobs should be passed to Bitbucket.
225+
*
226+
* @return {@code false} if not build jobs should be passed to Bitbucket.
227+
*/
228+
public boolean disableNotificationForNotBuildJobs() {
229+
return disableNotificationForNotBuildJobs;
230+
}
231+
218232
/**
219233
* Adds a requirement for branch details to any {@link BitbucketSCMSourceRequest} for this context.
220234
*
@@ -352,6 +366,18 @@ public final BitbucketSCMSourceContext withSendSuccessNotificationForUnstableBui
352366
return this;
353367
}
354368

369+
/**
370+
* Defines behaviour of unstable builds in Bitbucket.
371+
*
372+
* @param disabled {@code false} to report not build jobs to Bitbucket.
373+
* @return {@code this} for method chaining.
374+
*/
375+
@NonNull
376+
public final BitbucketSCMSourceContext withDisableNotificationForNotBuildJobs(boolean disabled) {
377+
this.disableNotificationForNotBuildJobs = disabled;
378+
return this;
379+
}
380+
355381
/**
356382
* {@inheritDoc}
357383
*/

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@
3030

3131
public class BitbucketBuildStatus {
3232

33+
/**
34+
* Enumeration of possible Bitbucket commit notification states
35+
*/
36+
public enum Status {
37+
INPROGRESS("INPROGRESS"),
38+
FAILED("FAILED"),
39+
STOPPED("STOPPED"),
40+
SUCCESSFUL("SUCCESSFUL");
41+
42+
private final String status;
43+
44+
Status(final String status) {
45+
this.status = status;
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return status;
51+
}
52+
}
53+
3354
/**
3455
* The commit hash to set the status on
3556
*/
@@ -42,9 +63,9 @@ public class BitbucketBuildStatus {
4263
private String description;
4364

4465
/**
45-
* One of: INPROGRESS|SUCCESSFUL|FAILED
66+
* One of: INPROGRESS|FAILED|STOPPED|SUCCESSFUL
4667
*/
47-
private String state;
68+
private Status state;
4869

4970
/**
5071
* The URL to link from the status details
@@ -66,7 +87,7 @@ public class BitbucketBuildStatus {
6687
@Restricted(DoNotUse.class)
6788
public BitbucketBuildStatus() {}
6889

69-
public BitbucketBuildStatus(String hash, String description, String state, String url, String key, String name) {
90+
public BitbucketBuildStatus(String hash, String description, Status state, String url, String key, String name) {
7091
this.hash = hash;
7192
this.description = description;
7293
this.state = state;
@@ -92,10 +113,10 @@ public void setDescription(String description) {
92113
}
93114

94115
public String getState() {
95-
return state;
116+
return state.toString();
96117
}
97118

98-
public void setState(String state) {
119+
public void setState(Status state) {
99120
this.state = state;
100121
}
101122

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
<f:entry title="${%Communicate Unstable builds to Bitbucket as Successful}" field="sendSuccessNotificationForUnstableBuild">
44
<f:checkbox/>
55
</f:entry>
6+
<f:entry title="${%Don't Communicate not build jobs to Bitbucket}" field="disableNotificationForNotBuildJobs">
7+
<f:checkbox/>
8+
</f:entry>
69
</j:jelly>

0 commit comments

Comments
 (0)