Skip to content

Commit 941a84a

Browse files
committed
[JENKINS-75203] Include parent in build status notification
1 parent b043a46 commit 941a84a

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public String toString() {
8181
*/
8282
private String key;
8383

84+
/**
85+
* The parent key
86+
*/
87+
private String parent;
88+
8489
/**
8590
* A short name, usually #job-name, #build-number (will be shown as link
8691
* text in BB UI)
@@ -136,6 +141,7 @@ public BitbucketBuildStatus(@NonNull BitbucketBuildStatus other) {
136141
this.name = other.name;
137142
this.refname = other.refname;
138143
this.buildDuration = other.buildDuration;
144+
this.parent = other.parent;
139145
}
140146

141147
public String getHash() {
@@ -210,4 +216,11 @@ public void setBuildNumber(int buildNumber) {
210216
this.buildNumber = buildNumber;
211217
}
212218

219+
public void setParent(String parent) {
220+
this.parent = parent;
221+
}
222+
223+
public String getParent() {
224+
return parent;
225+
}
213226
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,15 @@ private static void createStatus(@NonNull Run<?, ?> build,
177177
if (state != null) {
178178
BitbucketDefaulNotifier notifier = new BitbucketDefaulNotifier(client);
179179
String notificationKey = DigestUtils.md5Hex(key);
180+
String notificationParentKey = null;
180181
if (context.useReadableNotificationIds() && !isCloud) {
181182
notificationKey = key.replace(' ', '_').toUpperCase();
183+
notificationParentKey = getBuildParentKey(build).replace(' ', '_').toUpperCase();
182184
}
183185
BitbucketBuildStatus buildStatus = new BitbucketBuildStatus(hash, statusDescription, state, url, notificationKey, name, refName);
184186
buildStatus.setBuildDuration(build.getDuration());
185187
buildStatus.setBuildNumber(build.getNumber());
188+
buildStatus.setParent(notificationParentKey);
186189
// TODO testResults should be provided by an extension point that integrates JUnit or anything else plugin
187190
notifier.notifyBuildStatus(buildStatus);
188191
if (result != null) {
@@ -296,6 +299,10 @@ private static String getBuildKey(@NonNull Run<?, ?> build, String branch, boole
296299
return key;
297300
}
298301

302+
private static String getBuildParentKey(@NonNull Run<?, ?> build) {
303+
return build.getParent().getParent().getFullName();
304+
}
305+
299306
/**
300307
* Sends notifications to Bitbucket on Checkout (for the "In Progress" Status).
301308
*/

src/test/java/com/cloudbees/jenkins/plugins/bitbucket/impl/notifier/BitbucketBuildStatusNotificationsJUnit5Test.java

+30
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,35 @@ void test_status_notification_name_when_UseReadableNotificationIds_is_true(@NonN
140140
ArgumentCaptor<BitbucketBuildStatus> captor = ArgumentCaptor.forClass(BitbucketBuildStatus.class);
141141
verify(apiClient).postBuildStatus(captor.capture());
142142
assertThat(captor.getValue().getKey()).isEqualTo("P/BRANCH-JOB");
143+
assertThat(captor.getValue().getParent()).isEqualTo("P");
144+
}
145+
146+
@Issue("JENKINS-75203")
147+
@Test
148+
void test_status_notification_parent_key_null_if_cloud_is_true(@NonNull JenkinsRule r) throws Exception {
149+
StreamBuildListener taskListener = new StreamBuildListener(System.out, StandardCharsets.UTF_8);
150+
URL jenkinsURL = new URL("http://example.com:" + r.getURL().getPort() + r.contextPath + "/");
151+
JenkinsLocationConfiguration.get().setUrl(jenkinsURL.toString());
152+
153+
String serverURL = BitbucketCloudEndpoint.SERVER_URL;
154+
155+
BitbucketBuildStatusNotificationsTrait trait = new BitbucketBuildStatusNotificationsTrait();
156+
157+
WorkflowRun build = prepareBuildForNotification(r, trait, serverURL);
158+
doReturn(Result.SUCCESS).when(build).getResult();
159+
160+
FilePath workspace = r.jenkins.getWorkspaceFor(build.getParent());
161+
162+
BitbucketApi apiClient = mock(BitbucketCloudApiClient.class);
163+
BitbucketMockApiFactory.add(serverURL, apiClient);
164+
165+
JobCheckoutListener listener = new JobCheckoutListener();
166+
listener.onCheckout(build, null, workspace, taskListener, null, SCMRevisionState.NONE);
167+
168+
ArgumentCaptor<BitbucketBuildStatus> captor = ArgumentCaptor.forClass(BitbucketBuildStatus.class);
169+
verify(apiClient).postBuildStatus(captor.capture());
170+
assertThat(captor.getValue().getKey()).isNotEmpty();
171+
assertThat(captor.getValue().getParent()).isNull();
143172
}
144173

145174
@Issue("JENKINS-74970")
@@ -173,6 +202,7 @@ void test_status_notification_on_fork(@NonNull JenkinsRule r) throws Exception {
173202
assertThat(captor.getValue()).satisfies(status -> {
174203
assertThat(status.getHash()).isEqualTo(prRevision.getHash());
175204
assertThat(status.getKey()).isEqualTo(DigestUtils.md5Hex("p/branch-job"));
205+
assertThat(status.getParent()).isNull();
176206
assertThat(status.getRefname()).isEqualTo("refs/heads/" + scmHead.getBranchName());
177207
});
178208
}

0 commit comments

Comments
 (0)