Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Programmatically create Pull Request on BitBucket repo using REST API?

Al Pizano September 21, 2020

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:
Capture2.PNGCapture2.PNG

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"):

Capture1.PNG

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....


[1]: https://i.stack.imgur.com/VQnSd.png

1 answer

0 votes
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 23, 2020

Hi Al,

I checked in our system the Bitbucket Cloud account linked with the email you use in this Community account, and I see that it doesn't own any repositories or have access to other repositories.

Do you perhaps have another Bitbucket Cloud account with a different email?

To answer your question: the "my-username" field in the URL should be the workspace ID of the workspace that owns the repo. While you are logged in https://bitbucket.org/ to the account that has access to this repo, select your avatar (bottom left corner) > All workspaces. Then select the workspace that owns this repo. After you do, you will find the workspace ID in the URL, which will have the format https://bitbucket.org/workspace-id/. This workspace-id should be in the place of "my-username" in the URL.

You don't need to specify the project for this API call.

I would recommend:
1. Checking that you have generated an app password for the Bitbucket Cloud account that has access to this repo
2. Make sure that the "my-username" field has the value of the workspace ID, as per my instructions above

An example call that works is the following:

curl -L -X POST --user username:password --header 'Content-Type: application/json' https://api.bitbucket.org/2.0/repositories/workspace-id/repo-slug/pullrequests/ --data '{"title": "My Title","source": {"branch": {"name": "feature-branch"}}, "destination": {"branch": {"name": "master"}}}'

where
username is the username of your Bitbucket account
password is the password of your Bitbucket account or an app-password
workspace-id is as I explained above
repo-slug is the repo-slug for the repo
in the data, replace the values for title, source and destination branch

Please feel free to let me know how it goes.

Kind regards,
Theodora

Al Pizano September 24, 2020

Thank you, yes it was a bitbucket Server account and I was able to figure out how to create the automated PR by another atlassian representative.

 

Thank you!

Like Theodora Boudale likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events