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 to create bitbucket pull request using bitbucket rest api?

I wanna create bitbucket pull request using bitbucket rest api.

looks like the Rest API post request need very complex json data.

https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/pullrequests#post

I can get pull request very easylly, but looks post api is very hard.

is there any example of how to using bitbucket rest api to create pull request?
thanks

4 answers

1 accepted

2 votes
Answer accepted
seanaty Atlassian Team Oct 04, 2018

Hello,

The post API looks hard but I assure you it's not! The schema we show for the example has a lot of data because that is really the schema of the object that we return for an already-created pull request.

The amount of data necessary to create the pull request is very minimal. All you need is a title and a source (branch or commit).

curl -v https://api.bitbucket.org/2.0/repositories/my-username/my-repository/pullrequests \
  -u my-username:my-password \
  --request POST \
  --header 'Content-Type: application/json' \
  --data '{
    "title": "My Title",
    "source": {
      "branch": {
        "name": "staging"
      }
    }
  }'

The pull request destination repository will be the repository from the path name, so if you're working from a fork, you'll need to add some more data.

If you're not working with a fork, branch name isn't required for destination because it will default to the mainbranch, which is usually `master`.

The author of the pull request will be the the user who's authenticated when making the API call. You cannot change the author because you can't impersonate other users.

Here is an example of a pull request where the destination is not the default mainbranch. Say my mainbranch is `master` but I want to create a pull request from `my-feature-branch` to `staging`:

curl -v https://api.bitbucket.org/2.0/repositories/my-username/my-repository/pullrequests \
  -u my-username:my-password \
  --request POST \
  --header 'Content-Type: application/json' \
  --data '{
    "title": "My Title",
"description": "
"destination": {
"branch": {
"name": "staging"
}
},
    "source": {
      "branch": {
        "name": "my-feature-branch"
      }
    }
  }'

You can also reference a source or destination with a commit like this:

"source": {
"commit": {
"hash": "a3342b85a3fd24a40ea2ea254f40a0eadfd4ed42"
}
}

If you want to add reviewers can do so like:

curl -v https://api.bitbucket.org/2.0/repositories/my-username/my-repository/pullrequests \
  -u my-username:my-password \
  --request POST \
  --header 'Content-Type: application/json' \
  --data '{
"title": "My Title",
"description": "My description.",
"source": {
"branch": {
"name": "staging"
}
},
"reviewers": [
{
"uuid": "{504c3b62-8120-4f0c-a7bc-87800b9d6f70}"
}
]
}'

Hope this helps!

 

thanks, It's works for me.

create a pull request with one  reviewers it works good.

but if have more than one reviewers it fail. the response is like this:

<h1>
                Something went wrong
            </h1>              
            <p>
              While we check things on our end, refresh the page and
              <a class="support-ga"
                data-support-gaq-page="SACCreateIssue"
                href="https://support.atlassian.com/bitbucket-cloud/"
                target="_blank">contact support</a>
              if the problem persists.        </p>                                                                                                                                                                                                                                                                                                  <p>When contacting support, provide them with this code:</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <p><code>7ef948030ed1442685376e50d9e02fba</code></p> 

my curl command is like this:

curl https://api.bitbucket.org/2.0/repositories/xxxx/xxxx/pullrequests -u somebody:secret -X POST --header 'Content-Type: application/json'  -H "Accept: application/json"  --data '{                                          
    "title": "Just Title",
    "description": "Just description.",
    "source": {
      "branch": {
        "name": "mybranch"
      }
    },
     "destination": {
      "branch": {
        "name": "develop"
      }
    },
    "reviewers":  [{"uuid": "{xxxx-xxxx-xxxx-b5dc-ebbd5cd47005}"}, {"uuid": "{xxxx-821e-4c2c-9049-6f4d75e0f7c6}"}, {"uuid": "{xxxx-b6f8-434b-92b3-533151c97dc5}"}, {"uuid": "{xxxx-72b3-4e4e-bba7-34da0bd0b0f2}"}, {"uuid": "{xxxx-a4c1-47fc-9252-1c71a601043d}"}, {"uuid": "{xxxx-d005-455a-a53c-aaa6b2844864}"}, {"uuid": "{xxxx-0ca3-4a39-a9d9-129c46017d87}"}, {"uuid": "{xxxx-7173-4d45-9399-11d2736cc47e}"}] }'
seanaty Atlassian Team Oct 10, 2018

Hello,

Apologies for getting such a useless error! You didn't get a useful error because our the error message does not handle non-ascii characters well. I can put in a fix for this.

The real error is that you can't assign yourself to a pull request. Remove your UUID from the list and it should work.

it works great!

I reused the the reviewers in another pull-request that contains myself.

after filter mine uuid, it works well.

I'm very happy for using this API to automate some my tasks. 

thank you very much again, and have good weekend.

hi there, do you have any sample json for creating comments in commit? Thanks!

@seanaty Hoping you can help...  I have an automated Jenkins job to create pull requests.  Our Jenkins is setup with its own user. The job is able to create a PR as long as my request does not have reviewers.  When I add reviewers in the job, I get back a 400 Bad Request with no other message. The reviewers are being retrieved from the default reviewers API. Below is what works and what I have tried based on this post and https://community.atlassian.com/t5/Bitbucket-questions/Creating-a-pull-request-via-API/qaq-p/123913

I am a bit baffled at this point and dont know what else to try.

WORKS (no reviewers):

curl -X POST -H 'Content-Type: application/json' -u ****:**** https://bitbucket.org/api/2.0/repositories/xxxx/xxxx/pullrequests -d '{ "title": "Dependency Updates 2020-09-21", "description": "Automated Dependency Updates for 2020-09-21", "source": { "branch": { "name": "dependency-updates-2020-09-21" }, "repository": { "full_name": "xxxx/xxxx" } }, "destination": { "branch": { "name": "develop" } }, "close_source_branch": true }'

FAILS with 400 BAD REQUEST

curl -X POST -H 'Content-Type: application/json' -u ****:**** https://bitbucket.org/api/2.0/repositories/xxxx/xxxx/pullrequests -d '{ "title": "Dependency Updates 2020-09-21", "description": "Automated Dependency Updates for 2020-09-21", "source": { "branch": { "name": "dependency-updates-2020-09-21" }, "repository": { "full_name": "xxxx/xxxx" } }, "destination": { "branch": { "name": "develop" } }, "reviewers": [{"uuid": {xxxx-e492-427b-b3a1-5d93e77ad642}}, {"uuid": {xxxx-a43f-4c9a-8eac-708b9e3aa1bc}}], "close_source_branch": true }'

curl -X POST -H 'Content-Type: application/json' -u ****:**** https://bitbucket.org/api/2.0/repositories/xxxx/xxxx/pullrequests -d '{ "title": "Dependency Updates 2020-09-21", "description": "Automated Dependency Updates for 2020-09-21", "source": { "branch": { "name": "dependency-updates-2020-09-21" }, "repository": { "full_name": "xxxx/xxxx" } }, "destination": { "branch": { "name": "develop" } }, "reviewers": [{"user": {"uuid": {xxxx-e492-427b-b3a1-5d93e77ad642}}}, {"user": {"uuid": {xxxx-a43f-4c9a-8eac-708b9e3aa1bc}}}], "close_source_branch": true }'

Figured it out...  

I needed to wrap the uuid field value (with the curly braces) in double quotes. 

"reviewers": [{"uuid": "{xxxx-e492-427b-b3a1-5d93e77ad642}"}, {"uuid": "{xxxx-a43f-4c9a-8eac-708b9e3aa1bc}"}]

Like # people like this

...

@seanaty hi Sean, may I have a sample json to create comment in commits and create build status for commit? I have referred link https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/pullrequests/%7Bpull_request_id%7D/comments

but the response always said "400 error, missing necessary field".

even when i tried to update build status, so I first to get json of build status, then put the status json without changing anything, then error showed "b'{"type": "error", "error": {"fields": {"state": ["\\"state\\" must be one of set([\'SUCCESSFUL\', \'FAILED\', \'INPROGRESS\', \'STOPPED\'])"]}, "message": "state: \\"state\\" must be one of set([\'SUCCESSFUL\', \'FAILED\', \'INPROGRESS\', \'STOPPED\'])"}}'

thanks in advance!

0 votes
Ana Retamal Atlassian Team Oct 04, 2018

Hi, welcome to the Community!

For questions like this one, I'd like to point you to our Developer Community. You can also find more development resources in our Developer site.

Hope that helps!

Ana

Suggest an answer

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

New improvements to user management in Bitbucket Cloud 👥

Hey Community! We’re willing to wager that quite a few of you not only use Bitbucket, but administer it too. Our team is excited to share that we’ll be releasing improvements throughout this month of...

3,734 views 10 16
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