Skip to content

Commit 0f70fd1

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 some changes introduced by jenkinsci#954 to the refName parameter. While local testing has shown that by changing the bitbucket client to point to the target repo, setting the refName to null works in all cases, this change has been restricted to only cases when the source repo is the same as the target. No changes were made to the Bitbucket Cloud code path.
1 parent 1492cd1 commit 0f70fd1

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,19 +229,31 @@ private static void sendNotifications(BitbucketSCMSource source, Run<?, ?> build
229229
* Poor documentation for bitbucket cloud at:
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()
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.
239232
*/
240-
bitbucket = source.buildBitbucketClient(head);
241-
if (BitbucketApiUtils.isCloud(bitbucket)) {
242-
refName = null;
233+
refName = null;
234+
if (BitbucketApiUtils.isCloud(source.getServerUrl())) {
235+
bitbucket = source.buildBitbucketClient(head);
243236
} else {
244-
refName = "refs/heads/" + head.getBranchName();
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 that contains that branch.
243+
// If refName is null, then Bitbucket Server does not show the
244+
// build status in the list of pull requests, but still shows it
245+
// on the web page of the individual pull request.
246+
//
247+
// While the above behavior has been reported by some users, it only
248+
// applies where the target branch exists on the target repo.
249+
// Other tests have shown that refName null works no matter
250+
// the source of the pull request when updating the build status
251+
// on the pull request target, but will continue to set refName
252+
// to account for the reported behavior.
253+
// https://developer.atlassian.com/server/bitbucket/rest/v905/api-group-builds-and-deployments/#api-api-latest-projects-projectkey-repos-repositoryslug-commits-commitid-builds-post
254+
if (StringUtils.equals(head.getRepoOwner(), source.getRepoOwner()) && StringUtils.equals(head.getRepository(), source.getRepository())) {
255+
refName = "refs/heads/" + head.getBranchName();
256+
}
245257
}
246258
} else {
247259
listener.getLogger().println("[Bitbucket] Notifying commit build result");

0 commit comments

Comments
 (0)