How to programmatically make failed builds on Bitbucket successful

Here, we’ll use Python, and the user will provide us with the commit ID

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import requests
import json

bitbucketServerUrl = 'yourbitbucketsever'
commit = input("Enter commit id: ")
token = 'your bearer authentication token'
url = f"https://{bitbucketServerUrl}/rest/build-status/1.0/commits/{commit}"

headers = {
  'Content-Type': 'application/json',
  'Authorization': 'Bearer {token}'
}

Then we get all builds assosiated with this given commit.

1
2
3
4
response = requests.request("GET", url, headers=headers)

result =  json.loads(response.text.encode('utf8'))
builds = result['values']

After that, we will change all of the “FAILED” statuses to “SUCCESSFUL,” leaving all other properties, such as key, name, description, and url, unchanged.

1
2
for build in builds:
    build.update((k, "SUCCESSFUL") for k, v in build.items() if v == "FAILED")

Finally we send of all builds including the modfied once again to bitbucket in a post request.

1
2
3
for payload in builds:
  response = requests.request("POST", url, headers=headers, data = json.dumps(payload))
  print(response.text.encode('utf8'))
updatedupdated2024-01-172024-01-17