Skip to content

Commit f6d7273

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. This change also reverts changes introduced by jenkinsci#954 to the refName parameter. Since notification is on the targget repo, the head ref branch may not exist on the target and result in no status update. No changes were made to the Bitbucket Cloud code path.
1 parent 1492cd1 commit f6d7273

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,18 +230,18 @@ private static void sendNotifications(BitbucketSCMSource source, Run<?, ?> build
230230
* https://community.atlassian.com/t5/Bitbucket-questions/Re-Builds-not-appearing-in-pull-requests/qaq-p/1805991/comment-id/65864#M65864
231231
* that means refName null or valued with only head.getBranchName()
232232
*
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.
233+
* For Bitbucket Server, refName is optional.
234+
* https://developer.atlassian.com/server/bitbucket/rest/v905/api-group-builds-and-deployments/#api-api-latest-projects-projectkey-repos-repositoryslug-commits-commitid-builds-post
235+
* The PR head revision may point to a different source repository, for example a personal fork. We will
236+
* be notifying the PR target repo status where the head branch may not exist, so a null refName will
237+
* suffice.
239238
*/
239+
refName = null;
240240
bitbucket = source.buildBitbucketClient(head);
241-
if (BitbucketApiUtils.isCloud(bitbucket)) {
242-
refName = null;
243-
} else {
244-
refName = "refs/heads/" + head.getBranchName();
241+
if (!BitbucketApiUtils.isCloud(bitbucket)) {
242+
// For Bitbucket Server, notify the target of the PR. Head may point to a source repo that the
243+
// credentials don't have access to resulting in a 401 error.
244+
bitbucket = source.buildBitbucketClient();
245245
}
246246
} else {
247247
listener.getLogger().println("[Bitbucket] Notifying commit build result");

0 commit comments

Comments
 (0)