bitbucket

Updating build status for commits in Bitbucket Cloud version

The issue

When setting up bitbucket i came across a situation where I renames one of the Jenkins pipeline. Unfortunately that pipeline has failed and the status was already set as failed in bitbucket. I am using “Bitbucket Build Status Notifier” plugin in jenkins to send the status to bitbucket. After that the renamed pipeline passed and the status is green. But this will not change the old pipeline status any more and I see the 1 failed status all the time. So, let’s see updating build status for commits in Bitbucket Cloud version.

Updating build status
Build status

What options do I have to resolve this issue ? Luckily we have access to Bitbucket API (Reference : https://developer.atlassian.com/bitbucket/api/2/reference/ )

Updating build status using REST API

To start with you will need to create a oAuth credential. Go to bibucket settings

Bitbucket Settings
Bitbucket Settings

Once settings page is open go to OAuth -> Add consumer. Use a callbackURL. Anything will work. Add read permission to the repository. I am using https://getthekt.com.callback here. You can use it too.

Add consumer
Add consumer

This will give you a Key and Secret. Note them down , will need it later.

Using Postman

Copy and paste the below URL to your browser window. This will give you access_token, which will be used in the next step.

https://bitbucket.org/site/oauth2/authorize?client_id=<Paste your Key you got from previous steps>&response_type=token

Browser response
Browser response

Will use POSTMAN to consume the API.

This will be a GET method https://api.bitbucket.org/2.0/repositories/<your work space>/<Your project>/refs/branches

Under Header use the Authorization key and use the bearer token you got from the browser.

This will give you a list of all your branches. Now you can use your branch name at the end of the endpoint to get details about the branch you are looking at. I needed to update the status of master branch so used master here. You can use the one you need to use.

https://api.bitbucket.org/2.0/repositories/workspace/project/commits/master

You will notice the statuses link for that branch

Status link
Status link

Copy that link and use as endpoint now. This will give you all the statuses with the key for each of them. Copy the key of the build you need to change the status. The response would give you a build details in the JSON.

Build response
Build response

Use the self , href as the end point now and use GET method to get the details. Which will look like https://api.bitbucket.org/2.0/repositories/workspace/sfdc-dev/commit/684cc802asdasdasdaasasda726b320e04/statuses/build/4c8729dfasdasdasdasda575951d7

The response will look like below :

{
    "key": "keyasdasdasdas",
    "description": "",
    "repository": {
        "links": {
            "self": {
                "href": "https://api.bitbucket.org/2.0/repositories/workspace/projectname"
            },
            "html": {
                "href": "https://bitbucket.org/workspace/projectname"
            },
            "avatar": {
                "href": "https://bytebucket.org/ravatar/%7ewrwerwerwewc320a7%7D?ts=default"
            }
        },
        "type": "repository",
        "name": "projectname",
        "full_name": "workspace/projectname",
        "uuid": "{99werwerwerwerwerw320a7}"
    },
    "url": "http://werwerwerwerwerwe/jenkins/job/Temp-pipeline/15/display/redirect",
    "links": {
        "commit": {
            "href": "https://api.bitbucket.org/2.0/repositories/workspace/projectname/commit/684cc8werwerwerw6150726b320e04"
        },
        "self": {
            "href": "https://api.bitbucket.org/2.0/repositories/workspace/projectname/commit/684cc8werwerwerw6150726b320e04/statuses/build/keyasdasdasdas"
        }
    },
    "refname": null,
    "state": "SUCCESSFUL",
    "created_on": "2020-02-06T17:07:58.681866+00:00",
    "commit": {
        "hash": "werwerwerwerwerwe",
        "type": "commit",
        "links": {
            "self": {
                "href": "https://api.bitbucket.org/2.0/repositories/workspace/projectname/commit/684cc80werwerwerwe6150726b320e04"
            },
            "html": {
                "href": "https://bitbucket.org/workspace/projectname/commits/684cc802dwerwerwerw6150726b320e04"
            }
        }
    },
    "updated_on": "2020-02-13T18:49:10.584503+00:00",
    "type": "build",
    "name": "Temp-pipeline"
}

Copy this entire JSON and replace the status as you like for the JSON node “state”.

I needed this to be successful so used “state”: “SUCCESSFUL”. Now just change the postman method from GET to PUT . Paste the updated JSON in the body and click send

Updated response
Updated response

This will update the status of the specific build to the one you wanted. Now I can see the invalid pipeline as Passed:

Conclusion

This is the end of the post for today. Hope someone will get help from this anytime. Please let me know if you face any issues while following the steps or anything did not work. Would be happy to help. Keep reading and sharing….


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *