How to overwrite bitbucket notification status for pull requests
Asked by Jimmy.Diep:

I have a webhook that will trigger Jenkins build when there is a new or updated pull requests for the repos.

It will trigger the bitbucket notification to show the build status.

However, when the PR has multiple updates, it will trigger multiple builds that are tied to the PR. This is a problem as if any of the builds fails or never completes for a reason, the PR is stuck as there is a branch restriction set:

"Check the last commit for at least 1 successful build, no failed builds, and no in progress builds"

ie.

<br />

![Screenshot 2023-09-20 at 11.10.00 AM.png](/t5/image/serverpage/image-id/281845i80A57AABAB0444FB/image-dimensions/259x191?v=v2 "Screenshot 2023-09-20 at 11.10.00 AM.png")

4 builds were successful after the 1st build got stuck in a never completed state. This prevented the PR from being merged

![Screenshot 2023-09-20 at 11.25.57 AM.png](/t5/image/serverpage/image-id/281846i25221CFD44104044/image-dimensions/248x239?v=v2 "Screenshot 2023-09-20 at 11.25.57 AM.png")  

This PR has 6 builds that were unsuccessful, but the last one is successful. However, since it doesn't pass the PR requirements for merging:

"Check the last commit for at least 1 successful build, no failed builds, and no in progress builds"

It is not allowed to merge.

I am using the bitbucketStatusNotify function, passing in buildState, reposSlug, and commitID.

ie.

bitbucketStatusNotify(buildState: "SUCCESS", repoSlug: "my-repos", commitId: "xyzzy")

Is there a way to cancel previous build states and only have the latest one tied to the PR?

Thanks.


Answers:
author: Theodora Boudale
body: Hi Jimmy,

You can get info about the commit statuses (including their key) of a certain commit with the following API endpoint:

* <https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commit-statuses/#api-repositories-workspace-repo-slug-commit-commit-statuses-get>

Example call:

```
curl -u username:app-password --request GET \
--url 'https://api.bitbucket.org/2.0/repositories/workspace-id/repo/commit/7c47933/statuses' \
--header 'Accept: application/json'
```

You can then use the following API endpoint to update a certain status:

* <https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commit-statuses/#api-repositories-workspace-repo-slug-commit-commit-statuses-build-key-put>

Example call:

```
curl -u username:app-password --request PUT \
--url 'https://api.bitbucket.org/2.0/repositories/workspace-id/repo/commit/7c47933/statuses/build/some-key' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{
"state": "SUCCESSFUL"
}'
```

You can see all available values for state in the last link I shared if you select **+ Show child properties** for **Commit Status** \> expand the **state**property.

Please feel free to let me know if you have any questions!

Kind regards,  
Theodora

comments: author: Jimmy.Diep
body: Awesome! Thank you very much!


author: Jimmy.Diep
body: I found there is a buildKey field that is unique for the link.

<br />

<https://github.com/jenkinsci/bitbucket-build-status-notifier-plugin>

Is there a way to find the buildKey value for previous builds? Knowing this value, I can change the build state of the existing builds linked to the PR.


Tags: bitbucket-cloud

cloud

jenkins

notification