Search code examples
bitbucket-api

bitbucket API Rest - Getting branches from a repository


I am looking for the list of endpoints available for bitbucket API regarding get the branches from a repository and a specific branch.

I was expecting to see something like:

GET /2.0/repositories/{workspace}/{repo_slug}/branches/

GET /2.0/repositories/{workspace}/{repo_slug}/branches/{branch}

So I can get the commits from a specific branch. 

I know I can get commits but with this endpoint, its scope is under repository perspective. 

Do you know if there are endpoints to work with branches and recursively into its hierarchy? I looked over the documentation for API 2.0 but I did not see what I was looking for so that is why I am posting this question here.

In addition, I see some time ago that was not possible according to this answer, but it belongs to the version 1.0 of the API. Is it still true? 


Solution

  • When hitting this endpoint:

    https://api.bitbucket.org/2.0/repositories/<workspace>/<repository-name>`
    # GET /2.0/repositories/{workspace}/{repo_slug}
    

    You get as a result a JSON document. In the links attribute you got a key called branches. It is something like this:

    {
        "scm": "git",
        "has_wiki": false,
        "links": {
            "watchers": {
                "href": "https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/watchers"
            },
            "branches": {
                "href": "https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/refs/branches"
            },
         ....
    

    So you can hit the endpoint and get the branches:

    https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/refs/branches
    # GET /2.0/repositories/{workspace}/{repo_slug}/refs/branches
    

    And get a specific branch with

    https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/refs/branches/<branch-name>
    # GET /2.0/repositories/{workspace}/{repo_slug}/refs/branches/<branch-name>