Skip to content

Commit b42e5e5

Browse files
committed
fix: bitbucket service pull request build status from different source
The commit 7ac886d switched to the newer repository commit build status api. This introduced a regression in the case the PR source repository is not the same as the target, for example a person fork. In this case the bitbucket api created from the PR head will point to the fork repo and may result in a 401 error if the credentials don't have the necessary permissions. ``` ERROR: Could not send notifications com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRequestException: HTTP request error. Status: HTTP/1.1 401 Response: {"errors":[{"context":null,"message":"You are not permitted to access this resource","exceptionName":"com.atlassian.bitbucket.AuthorisationException"}]} ``` This change will force the bitbucket api instance to be the target repo which is the appropriate target for the notification. The old api was commit hash centric, not project centric, so the api client owner/repo didn't matter. No changes were made to the Bitbucket Cloud code path.
1 parent 1492cd1 commit b42e5e5

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -225,22 +225,25 @@ private static void sendNotifications(BitbucketSCMSource source, Run<?, ?> build
225225
listener.getLogger().println("[Bitbucket] Notifying pull request build result");
226226
PullRequestSCMHead head = (PullRequestSCMHead) rev.getHead();
227227
key = getBuildKey(build, head.getOriginName(), shareBuildKeyBetweenBranchAndPR);
228-
/*
229-
* Poor documentation for bitbucket cloud at:
230-
* https://community.atlassian.com/t5/Bitbucket-questions/Re-Builds-not-appearing-in-pull-requests/qaq-p/1805991/comment-id/65864#M65864
231-
* that means refName null or valued with only head.getBranchName()
232-
*
233-
* For Bitbucket Server, refName should be "refs/heads/" + the name
234-
* of the source branch of the pull request, and the build status
235-
* should be posted to the repository that contains that branch.
236-
* If refName is null, then Bitbucket Server does not show the
237-
* build status in the list of pull requests, but still shows it
238-
* on the web page of the individual pull request.
239-
*/
240-
bitbucket = source.buildBitbucketClient(head);
241-
if (BitbucketApiUtils.isCloud(bitbucket)) {
228+
if (BitbucketApiUtils.isCloud(source.getServerUrl())) {
229+
/*
230+
* Poor documentation for bitbucket cloud at:
231+
* https://community.atlassian.com/t5/Bitbucket-questions/Re-Builds-not-appearing-in-pull-requests/qaq-p/1805991/comment-id/65864#M65864
232+
* that means refName null or valued with only head.getBranchName()
233+
*/
234+
bitbucket = source.buildBitbucketClient(head);
242235
refName = null;
243236
} else {
237+
// For Bitbucket Server, notify the target of the PR. Head may point to a forked source repo that the
238+
// credentials don't have access to resulting in a 401 error.
239+
bitbucket = source.buildBitbucketClient();
240+
// For Bitbucket Server, refName should be "refs/heads/" + the name
241+
// of the source branch of the pull request, and the build status
242+
// should be posted to the repository of the pull request target.
243+
// If refName is null, then Bitbucket Server shows the build status
244+
// for all pull requests for the commit id. When refName is provided
245+
// the build status is only shown for the pull request that matches
246+
// the refName and commit id.
244247
refName = "refs/heads/" + head.getBranchName();
245248
}
246249
} else {

0 commit comments

Comments
 (0)