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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

6 answers

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

 

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.

There is - the web UI...

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!

 

 

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

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

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"
}

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

Jonas may drop the part "command line"

Jonas dropped everything but "pull".

Like # people like this

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

 

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

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

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

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

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

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

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

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.

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 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

Powering DevOps with Bitbucket Server & Data Center

Hi everyone, The Cloud team recently announced 12 new DevOps features that help developers ship better code, faster   ! While we’re all excited about the new improvements to Bitbucket ...

1,826 views 0 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you