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?

이해룡 Oct 03, 2018

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

3 answers

1 accepted

2 votes
Answer accepted
Sean Conaty 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!

이해룡 Oct 05, 2018 • edited

 

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}"}] }'
Sean Conaty 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.

이해룡 Oct 12, 2018 • edited

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.

Jin Wu Oct 17, 2018

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

0 votes
Ana Retamal Ortiz 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

0 votes
Jin Wu Oct 17, 2018

@Sean Conaty 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!

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Bitbucket Pipelines

Building a Bitbucket Pipe as a casual coder

...ipe.sh :  #!/bin/bash source "$(dirname "$0")/common.sh" enable_debug extra_args="" if [[ "${DEBUG}" == "true" ]]; then extra_args="--verbose" fi # mandatory variables R...

1,782 views 1 18
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