Merge Pull Requests programmatically in BitBucket

Oliver Schoenborn
1 min readDec 19, 2019

In Creating PR programmatically in Bitbucket, I showed how to create a large number of identical pull requests, ie identical across a set of bitbucket repositories. The other task that is cumbersome is merging the set of create PRs.

In the final curl command in that post, extract the merge link from the responses, like this:

BB_URL=https://api.bitbucket.org/2.0/repositories/YOUR_ORG_NAME
for repo in $REPOS; do
curl -s $BB_URL/$repo/pullrequests --user USERNAME:PASSWORD \
--request POST --header 'Content-Type: application/json' \
--data @pr_get.json \
| jq -r .links.merge.href
done > pr_merges.txt

Then add a line for the PR that you created manually (all you need is the PR number, which you can find in the URL in the browser where you created it). When you have all PRs approved and are ready to merge, execute the following command:

for merge in $( cat pr_merges.txt ); do
curl $merge --user USERNAME:PASSWORD --request POST
done

Done!

--

--