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

How do I create a pull request from the command line?

oyvindharboe September 26, 2016

With Gerrit I type "git push origin HEAD:refs/for/master", what's the equivalent for bitbucket?

6 answers

3 votes
Laurent Boivin September 27, 2016

If you want to create a Pull Request from the command line, you can use the REST API :

curl -s -u USER:PASSWD -X POST -H 'Content-Type: application/json' -d 'JSON_struct_with_data' http://SERVER:PORT/rest/api/1.0/projects/PRJKEY/repos/REPONAME/pull-requests

Check the doc of REST API for the details on the JSON data block.

HTH

 

oyvindharboe September 27, 2016

Ouch! I get a lot of groans about the the arcane Gerrit syntax (git push origin HEAD:refs/for/MAIN) and the above is not an improvement.

I was hoping that there was something no more complicated in terms of syntax, configurations, required tools to be installed, passwords, etc. than Gerrit for creating a Bitbucket pull requests.

Tim Crall
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 28, 2016

There is - the web UI...

oyvindharboe September 28, 2016

I'm up against Gerrit, compared to Gerrit that's just one additional step to get wrong:

 

  1. Create branch
  2. Go to GUI create pull request

 

Instead of:

git push origin HEAD:refs/for/master

 

=> done!

 

 

binki June 17, 2018

At least you could write a custom script since there’s a REST API. So this is better than nothing.

Josh Clark March 18, 2020

Because opening a browser and clicking a kajillion times is faster than a CLI alias... e_e

1 vote
Jose Alban March 26, 2020

function bbpr {

  local yourOrg

  yourOrg=eg


  local rootDir
  rootDir=$(git rev-parse --show-toplevel | rev | cut -d'/' -f 1 | rev)


  local branch
  branch=$(git rev-parse --abbrev-ref HEAD)


  local ffbin
  ffbin='/Applications/Firefox.app/Contents/MacOS/firefox-bin'


  $ffbin --new-tab "https://bitbucket.org/${yourOrg}/${rootDir}/pull-requests/new?source=${branch}&t=1"
}

0 votes
oyvindharboe June 14, 2019

Still no news matching Gerrit w.r.t. ease of use on this?

 

All I want is:

 

  • A simple "git push origin HEAD:refs/for/master" command line
  • No new tools to install
  • No REST API docs to read
  • No messing with 'curl'
  • ssh authentication
  • No named branches to clean up when the pull request is closed
  • No forking of the repository on the server necessary
  • pull requests should be closed automatically if the pull request is pushed/merged by a push rather than going via REST API or the web page
Giannis Tampakakis February 6, 2020

Hello!
So did you end up finding anything?
I'm kinda in that same spot right now.

oyvindharboe February 6, 2020

Sadly, no.

 

I've moved on to github for lack of out of the box integration with e.g. Google Cloud Build, smee.io and whatnot.

Miguel Costa October 20, 2020

have you tried something like this?

https://git-scm.com/docs/git-request-pull

Miguel Costa October 20, 2020

yeah I was looking for the same...

unfortunately some companies have managers decided to eat up all this Atlassian crap and we have to deal with all this nonsense 

Like Andres LeonRangel likes this
0 votes
Robert Szalai April 4, 2019

Hi,

 

I am getting this:

/var/lib/gems/2.5.0/gems/commander-4.1.6/lib/commander/user_interaction.rb:328: warning: constant ::NIL is deprecated
/var/lib/gems/2.5.0/gems/commander-4.1.6/lib/commander/user_interaction.rb:328: warning: constant ::Data is deprecated
/var/lib/gems/2.5.0/gems/commander-4.1.6/lib/commander/user_interaction.rb:328: warning: constant ::TRUE is deprecated
/var/lib/gems/2.5.0/gems/commander-4.1.6/lib/commander/user_interaction.rb:328: warning: constant ::FALSE is deprecated
/var/lib/gems/2.5.0/gems/commander-4.1.6/lib/commander/user_interaction.rb:328: warning: constant ::Fixnum is deprecated
/var/lib/gems/2.5.0/gems/commander-4.1.6/lib/commander/user_interaction.rb:328: warning: constant ::Bignum is deprecated
/var/lib/gems/2.5.0/gems/commander-4.1.6/lib/commander/user_interaction.rb:328: warning: constant ::TimeoutError is deprecated
Traceback (most recent call last):
11: from /var/lib/gems/2.5.0/gems/commander-4.1.6/lib/commander/import.rb:10:in `block in <top (required)>'
10: from /var/lib/gems/2.5.0/gems/commander-4.1.6/lib/commander/delegates.rb:8:in `run!'
9: from /var/lib/gems/2.5.0/gems/commander-4.1.6/lib/commander/runner.rb:82:in `run!'
8: from /var/lib/gems/2.5.0/gems/commander-4.1.6/lib/commander/runner.rb:422:in `run_active_command'
7: from /var/lib/gems/2.5.0/gems/commander-4.1.6/lib/commander/command.rb:155:in `run'
6: from /var/lib/gems/2.5.0/gems/commander-4.1.6/lib/commander/command.rb:180:in `call'
5: from /var/lib/gems/2.5.0/gems/atlassian-stash-0.7.0/bin/stash:142:in `block (2 levels) in <top (required)>'
4: from /var/lib/gems/2.5.0/gems/atlassian-stash-0.7.0/lib/atlassian/stash/git.rb:50:in `ensure_within_git!'
3: from /var/lib/gems/2.5.0/gems/atlassian-stash-0.7.0/bin/stash:144:in `block (3 levels) in <top (required)>'
2: from /var/lib/gems/2.5.0/gems/atlassian-stash-0.7.0/lib/atlassian/stash/pull_request.rb:101:in `create_pull_request'
1: from /usr/lib/ruby/2.5.0/json/common.rb:156:in `parse'
/usr/lib/ruby/2.5.0/json/common.rb:156:in `parse': 765: unexpected token at '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> (JSON::ParserError)
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="https://bitbucket...">here</a>.</p>
</body></html>
'

 

Any idea how to resolve it?

BR

Robert

Laurent Boivin April 4, 2019

Hi

You are calling an URL that redirects to another one for some (hopefully good) reason.

The library you are using is not handling this redirect automatically and you get the response "302 - what you are looking for is not here, but it's there".

So you should either find the option in the library you are using to follow the redirection when it occurs, or you must check first your HTTP response code before processing the HTTP response message.

On HTTP code 302, you should call the new URL.

HTH.
Laurent

0 votes
dragon788 May 10, 2017

The best option is probably to use a couple different tools developed for Stash and/or BItBucket Cloud to do this. The REST API should be similar between them (even though their codebases aren't necessarily the same).

See the excellent utilities I came across from this StackOverflow answer.

http://stackoverflow.com/a/15046966

the Attlassian team have stash (ruby): https://bitbucket.org/atlassian/stash-command-line-tools

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

 

Israel Tsadok November 15, 2017

You have a broken link: stash is now at https://bitbucket.org/atlassian/bitbucket-server-cli

dragon788 June 7, 2018

Good spot. It is also easy to install them via `gem install atlassian-stash`. Once you've run `stash configure` you can easily operate on a large number of repos in a loop if you are dealing with a massive find/replace for example.

Sadly it only operates on https remote-url configured repos even though it should be able to map ssh:// or git repos to the SSH counterpart since the `stash configure` requires the web URL and the repo URL should largely match.

Given a local folder like PROJECTNAME if you are updating a duplicated entry across multiple repositories you can `cd PROJECTNAME` and then run the script below (assuming you setup the `git create-pull-request` alias.

for repo in `ls -A`; do 
pushd $repo;
git remote set-url origin https://stash.internal:8444/scm/PROJECTNAME/$(basename $(pwd)).git ;
git create-pull-request branches/JIRA-123 master @reviewer1 @reviewer2 ;
git remote set-url origin ssh://git@stash.internal:7999/PROJECTNAME/$(basename $(pwd)).git;
popd 2>&1 >/dev/null ;
done
Raviteja Badisa July 18, 2018

Is there any other way we can perform this action without having stash installed?

Simon Storr March 27, 2019

I would be keen to find this out. We have 27 repositories which need e.g. their AssemblyInfo.cs updating with the new major version number so would like to automate creating pull requests for all these in one go - the UI takes far too long :/

Like # people like this
0 votes
Jonas Andersson
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 27, 2016
oyvindharboe September 27, 2016

I can't see anything in that link that shows me how to create a pull request from the command line. I have created pull requests in the Bitbucket web-interface, I've downloaded pull requests from the command line, but I've never found a way to create pull requests from the command line.

Like # people like this
Nam VU October 27, 2018

Jonas may drop the part "command line"

jmila April 9, 2019

Jonas dropped everything but "pull".

Like # people like this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events