Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically create Pull Request on Bitbucket?

Update: Well, I was running this in a Bash script, but I wanted to see what error code I was getting, and now I can see I am getting a 401 Unauthorized. I am using my username and I created Personal Access Token with admin access bitbucket, so I should be able to create a PR right? I can do this through the web UI on the same repo?

I am running a bash script to create a pull request on Bitbucket. I already am programmatically cloning the repo, editing a file, doing a git add/commit, now I just need to use CURL to make the PR. It seems the bitbucket API exposes a endpoint to do this using a POST request:

Creates a new pull request where the destination repository is this repository and the author is the authenticated user.

The minimum required fields to create a pull request are title and source, specified by a branch name.

curl https://api.bitbucket.org/2.0/repositories/my-username/my-repository/pullrequests \
    -u my-username:my-password \
    --request POST \
    --header 'Content-Type: application/json' \
    --data '{
        "title": "My Title",
        "source": {
            "branch": {
                "name": "staging"
            }
        }
    }'

https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pullrequests#post

Here's how my repo looks on Bitbucket, I blocked out the real names, but the left format the same (the first name is the Project name, REACTOR2.0, while I believe the second name is the repository name, dat-repo):

enter image description here

I am trying many different variations, and am checking the remote bitbucket server for a new pull request, but I see nothing.

I'm certain my "title" and "branch" are correct. My only question is about the URL; I am inputting my username from bitbucket, if you go to "Manage Account" then "name", that is the field I'm using for the my-username part of the URL, and I'm adding the repository name for the my-repository part. However, I need to note that this is a repository on bitbucket nested inside a Project called "REACTOR2.0", so I wasn't sure if the project name needed to be specified in the URL somewhere.

Has anyone been successful with this API? I looked on google, but many questions were using the old 1.0 API and don't apply or people were doing GET request do simply get a list of pull requests....

like image 653
ennth Avatar asked Dec 18 '22 12:12

ennth


1 Answers

I was using the wrong API. There is a BitBucket cloud API, for repositories hosted on bitbucket with URLs like bitbucket.com/ and a BitBucket Server API for URLs like https://bitbucket.mycompanyname.com/, which is the API I needed to use.

In the end, the URL should be something like this (you need to fill in the YourCompanyName, YourProjectKey, and YourRepositoryName parameters):

https://bitbucket.YourCompanyHostName.com/rest/api/1.0/projects/YourProjectKey/repos/YourRepositoryName/pull-requests 

and JSON to post:

{
    "title": "Talking Nerdy",
    "description": "It’s a kludge, but put the tuple from the database in the cache.",
    "state": "OPEN",
    "open": true,
    "closed": false,
    "fromRef": {
        "id": "refs/heads/feature-ABC-123",
        "repository": {
            "slug": "my-repo",
            "name": null,
            "project": {
                "key": "PRJ"
            }
        }
    },
    "toRef": {
        "id": "refs/heads/master",
        "repository": {
            "slug": "my-repo",
            "name": null,
            "project": {
                "key": "PRJ"
            }
        }
    },
    "locked": false,
    "reviewers": [
        {
            "user": {
                "name": "reviewersName1"    
            }
        },
         {
            "user": {
                "name": "reviewerName2" 
            }
        }
    ]
}

If you choose to hit the API via CURL, you can formulate that request with something like this:

curl -H "Authorization: Basic EncryptedBase64UsernamePasswordHere" \
  -H "Content-Type: application/json" \
  "https://bitbucket.YourCompanyName.com/rest/api/1.0/projects/YourProjectKey/repos/YourRepositoryName/pull-requests" \
  -d JsonDataFromAboveHere

You can read more about the authentication in below, but you can either use -u username:password for authentication in the CURL request or you can take the username:password string and base64 encode it and then use -H "Authentication: Basic BASE64ENCODEDUSERNAMEPASSWORDHERE", up to you. You might need to escape the JSON double-quotes " with what I have below , depending on what type of machine you're making this request, something like this:

"{\"title\": \"Talking Nerdy\",
    \"description\": \"It’s a kludge, but put the tuple from the database in the cache.\",
    \"state\": \"OPEN\",
    \"open\": true,
    \"closed\": false,
    \"fromRef\": {
        \"id\": \"refs/heads/feature-ABC-123\",
        \"repository\": {
            \"slug\": \"my-repo\",
            \"name\": null,
            \"project\": {
                \"key\": \"PRJ\"
            }
        }
    },
    \"toRef\": {
        \"id\": \"refs/heads/master\",
        \"repository\": {
            \"slug\": \"my-repo\",
            \"name\": null,
            \"project\": {
                \"key\": \"PRJ\"
            }
        }
    },
    \"locked\": false,
    \"reviewers\": [
        {
            \"user\": {
                \"name\": \"reviewerName1\" 
            }
        },
         {
            \"user\": {
                \"name\": \"reviewerName2\" 
            }
        }
    ]
}"

If you want to add reviewers but don't know there names, there is a GET request you can make to the same URL above, and it will give a list of users and then can add their name to the reviewers array so when the PR gets created, they are added already as reviewers.

Authentication: https://developer.atlassian.com/server/bitbucket/how-tos/example-basic-authentication/

Rest API: https://developer.atlassian.com/server/bitbucket/how-tos/command-line-rest/

PullRequest: https://docs.atlassian.com/bitbucket-server/rest/7.6.0/bitbucket-rest.html#idp291

like image 52
ennth Avatar answered Dec 28 '22 13:12

ennth