60

I have to send a lot of pull requests, so I would rather use the bash command line than bitbucket's web interface.

Usage example: $ git-req username

Here is such a script for Github: http://pastebin.com/F9n3nPuu

Is there one for Bitbucket?

isherwood
  • 58,414
  • 16
  • 114
  • 157
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373

5 Answers5

62

Bitbucket with it's RESTful API 2.0 supports managing pull requests without interface. In CLI you can request it with CURL. This older version of the documentation has better interface details.

Get pull request data with CURL

To get full data about specific pull request:

$ curl --user s3m3n:bbpassword https://bitbucket.org/api/2.0/repositories/s3m3n/reponame/pullrequests/4

In return I get JSON with full info about my pull request #4 (put your username twice, password and reponame in command).

Create new pull request with RESTClient

To create new pull request we need to provide a lot of data with POST command, below how it looks in my RESTClient:

RESTClient Firefox

After firing Bitbucket shows pull request immediately:

Bitbucket screenshot

Create new pull request with CURL

You can still create the same pull request with one liner:

$ curl -X POST -H "Content-Type: application/json" -u s3m3n:bbpassword https://bitbucket.org/api/2.0/repositories/s3m3n/reponame/pullrequests -d '{ "title": "Merge some branches", "description": "stackoverflow example", "source": { "branch": { "name": "choose branch to merge with" }, "repository": { "full_name": "s3m3n/reponame" } }, "destination": { "branch": { "name": "choose branch that is getting changes" } }, "reviewers": [ { "username": "some other user needed to review changes" } ], "close_source_branch": false }'

REST browser tool (discontinued)

If you want to test all possible methods of API hop to REST browser tool of Bitbucket. It will show you all possible requests while returning your real repo's data.

keen
  • 816
  • 10
  • 11
s3m3n
  • 4,187
  • 1
  • 28
  • 24
  • 1
    +1 Great progress! I am sure an easy-to-use command-line client will appear soon. – Nicolas Raoul Dec 18 '13 at 06:06
  • What do you expect from such client? One line CURL to create new pull request is pretty simple, all trouble is json as POST body, but you have to pass all data somehow. How would you like to do that in other way? – s3m3n Dec 18 '13 at 11:42
  • I would at least create an alias for the simple case `git-req username`. – Nicolas Raoul Dec 18 '13 at 11:46
  • Sadly, the REST browser tool appears to have been discontinued. – myrcutio Mar 03 '16 at 16:31
  • @s3m3n thanks a lot for your help. your doc works!! I'd like to know where did you get the documentation of detailed POST json data ? in [official api doc](https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/pullrequests#post), I can't find how to create a PR. – Darktalker Jul 27 '16 at 07:36
  • 1
    @Darktalker it's very interesting. It seems that the new documentation is pretty bad. Look how my link at the top of answer looked like year before in [web.archive.org](http://web.archive.org/web/20150530151816/https://confluence.atlassian.com/display/BITBUCKET/pullrequests+Resource#pullrequestsResource-POST(create)anewpullrequest). – s3m3n Jul 28 '16 at 11:16
  • appreciate the curl API call to create PRs - helped me out today. a CLI tool would be nice, and maybe someone will get a chance to turn that into one.... – keen Oct 13 '16 at 02:21
  • Can you add attachments in any way? – netdigger Mar 18 '20 at 08:05
  • When I tried above curl request with Personal Access token, I'm getting error as `XSRF Security Token Missing`. Any idea on how to solve ? – Gopinath Shiva Jul 21 '20 at 13:54
  • For others stuck in authentication like me, try using `-u yourusername:your_app_password`. [app password document](https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/) – vampirewalk Feb 08 '21 at 03:42
10

Tried and tested :

  1. Generate personal access token by clicking here

  2. Save the Unique token id, append it after "Bearer in header".

For example: "Authorization : Bearer MDg4MzA4NTcfhtrhthyt/Thyythyh "

Complete JSON sample here:

Step 1 to enter the details and necessary headers

  1. Try running it Step 2

  2. Output on BitBucket, You will be able to see the pull request Final output

Command Line Syntax:

curl -i -X POST    -H "Authorization:Bearer MDg4MzA4NTk/TlMSS6Ea"    -H "X-Atlassian-Token:no-check"    -H "Content-Type:application/json"    -d '{"description":"1. Changes made 2. Changes made 3. Hello hanges","closed":false,"fromRef":{"id":"refs\/heads\/branch","repository":{"name":"From Repository ","project":{"key":"ProjectName"},"slug":"From Repository "}},"state":"OPEN","title":"Merge changes from branch to master","locked":false,"reviewers":[],"open":true,"toRef":{"id":"refs\/heads\/master","repository":{"name":"RepoName","project":{"key":"ProjectName"},"slug":"RepoName"}}}'  'https://bitbucket.agile.com/rest/api/1.0/projects/projectName/repos/repoName/pull-requests'
  • Seems the personal access token only works with Bitbucket Server? Is there a way to do this on bitbucket.org without sending in username and password? – Sasgorilla Jun 28 '21 at 21:04
9

There are 2 repos on bitbucket that could help:

the Attlassian team have stash (ruby): https://bitbucket.org/atlassian/bitbucket-server-cli

Zhemao has bitbucket-cli (python): https://bitbucket.org/zhemao/bitbucket-cli

both have pull request feature from command line.

itsadok
  • 28,822
  • 30
  • 126
  • 171
  • Looking at Zhemao, I don't see a way to do a pull request. Was this removed since you posted the answer? – David Welch Jul 25 '13 at 04:49
  • @Millisami Came here through Google looking voor the same feature and Stash seems to have it now (don't know since when). – Ivo Dancet Dec 02 '13 at 16:48
  • Does this feature exists for not Stash, for Bitbucket? – millisami Dec 03 '13 at 05:51
  • @Millisami I'm afraid I jumped to conclusions. After further investigating, with bitbucket, the api seems to be in place according to [this](https://bitbucket.org/site/master/issue/8195/rest-api-for-creating-pull-requests) and some folks seem to be [working on implementing it](https://bitbucket.org/zhemao/bitbucket-cli/issue/3/command-for-pull-requests) in bitbucket-cli. – Ivo Dancet Dec 03 '13 at 11:32
  • Accepted answer contains a 404 link. Sadness. – starmandeluxe May 11 '17 at 00:42
  • https://community.atlassian.com/t5/Bitbucket-questions/Re-Re-How-do-I-create-a-pull-request-from-the-command-l/qaq-p/816197/comment-id/28332#M28332 – dragon788 Jun 08 '18 at 00:57
  • 2
    @keen Zhemao/bitbucket-cli does provide the `pull_request` command since [6c50435](https://bitbucket.org/zhemao/bitbucket-cli/commits/6c50435bd2a67ad3c26e35b40b188d65d07964f0) but it is not documented in the readme. – pylipp Sep 20 '18 at 10:02
9

I wasn't too satisfied with the answers in this thread, so I created an package for it:

https://www.npmjs.com/package/bitbucket-pr

Instructions:

npm i -g bitbucket-pr

... Go to folder where you want to create a pull request ...

bitbucket-pr

Karamell
  • 1,023
  • 9
  • 23
  • Cool! How is it different from the others? What was not satisfying you? – Nicolas Raoul Feb 16 '18 at 13:16
  • The answer from karojosh doesn't have a proper solution, and the answer from s3m3n requires too much overhead. My package just opens the browser of the pull request with pre-filled information, which is probably the fastest way of doing it. – Karamell Feb 16 '18 at 13:20
  • Great tool! this saves me a few clicks! It will be possible to give as argument the destination branch? – Ryan Amaral Apr 28 '18 at 19:14
  • Nice that you find it useful. It will always go to the main branch of the repo, which you can change in the bitbucket settings. If you really want to have the target branch as an argument, you could do a pull request. The thing is like 5 lines of bash so should be easy to hack :) – Karamell May 10 '18 at 11:08
  • 4
    It would be great if you provide the source of your npm modules, that way, anyone can validate it's source code before using it, or even improving it – Luis Lobo Borobia Jun 15 '18 at 15:16
  • @LuisLoboBorobia couldn't you just view the source after downloading from npm? – iheanyi Nov 20 '18 at 22:25
  • Yes, you can. But my previous comment reasons are still valid. And one can even contribute changes and submit Pull Request for everyone to benefit from – Luis Lobo Borobia Dec 03 '18 at 17:03
  • Any idea how to make this work with PowerShell? – JoePC Mar 24 '21 at 00:34
  • Looks like this is now provided with a link (CTRL-Click) after doing a `git push`. `remote: Create a pull request for ${branch}:` – JoePC Mar 24 '21 at 00:40
3

I have created a pull request cli utility to ease my tasks.

Currently,

  • it can create/delete pr's right from the terminal
  • show basic diff for a raised PR

I have tested it with bitbucket enterprise 6.10.10

Source code: https://github.com/psadi/bbcli

Edit: I have added much more features over the time (inspired from gh utility) please do check it out!!

psadi
  • 149
  • 7