1. Bitbucket
  2. Public Issue Tracker
  3. master
  4. Issues

Issues

Issue #8195 resolved

REST API for creating pull requests

stephen mathieson
created an issue

it'd be really super cool if there was an API for creating pull requests :)

see github's api

Comments (20)

  1. Erik van Zijst staff
    curl -X POST -u username:password \
        https://api.bitbucket.org/2.0/repositories/{user}/{slug}/pullrequests/ \
        -d src=my_branch \
        -d title="Short description" \
        -d dest=master \
        -d description="Long description"
    

    The last 2 parameters are optional. The default destination is the repo's main branch.

    If you're creating a PR between forks, make the POST on the destination repo's URL, and add:

    -d src_repo=evzijst/fork
    

    Additionally you can add reviewers by repeating the reviewers param 1 or more times:

    -d reviewers=evzijst -d reviewers=nvenegas -d reviewers=...
    
  2. Erik van Zijst staff

    To merge a PR, just do:

    curl -X POST https://api.bitbucket.org/2.0/repositories/{user}/{slug}/pullrequests/{id}/accept
    

    or:

    curl -X POST https://api.bitbucket.org/2.0/repositories/{user}/{slug}/pullrequests/{id}/decline
    

    Both take an optional message parameter that allow you to explain why you're accepting or declining. In the former case that becomes the commit message of the merge.

  3. Dan Shumaker

    Does this still work? Can you give an example for use with POSTMAN. I'm trying to send a json object like:

    {
        src: "t1",
        title: "Woo Hoo",
        dest: "master",
        description: "this could be the one",
        reviewers: "dan_shumaker"
    }
    

    And it's not working, the response is "Bad request".

  4. Dan Shumaker

    I got it with some guess work:

    {
        "destination": {
            "branch": {
                "name": "master"
            }
        },
        "source": {
            "branch": {
              "name": "t1"
            }
        },
        "title": "R with POSTMAN BITBUCKET API"
    }
    
  5. Adam Y

    Has anyone had luck with this working? I keep getting an error message:

    {
      "type": "error",
      "error": {
        "fields": {
          "source": [
            "This field is required."
          ],
          "title": [
            "This field is required."
          ]
        },
        "message": "Bad request"
      }
    }
    
  6. Dan Shumaker

    [~Adam Y] Try using a json payload like I mentioned above. As your response message indicates it looks like you did not include a source or title field in your payload, but it could also be that the json structure was wrong in some way.

  7. Iluschenko Dima

    Hey

    I do request via guzzle on php. But got this error:

    {"type": "error", "error": {"fields": {"source": ["This field is required."]}, "message": "Bad request"}}

    Code:

    <?php
        $data = array(
          'destination' => array(
            'branch' => array('name' => 'master'),
          ),
          'source' => array(
            'branch' => array('name' => 'feature/4507'),
            'repository' => array(
              'full_name' => 'diluschenko/beloglazov.pro.v2',
            ),
          ),
          'title' => $title,
          'access_token' => $this->accessToken,
        );
    
        $params = array(
          'type' => 'POST',
          'url' => 'https://api.bitbucket.org/2.0/repositories/diluschenko/beloglazov.pro.v2/pullrequests',
          'options' => array(
            'headers' => array(
              'Accept' => 'application/json',
            ),
            'form_params' => $data,
          ),
        );
    
        $response = $this->connector->sendRequest($params);
    

    What's wrong?

  8. Iluschenko Dima

    Answer:

    <?php
        $data = array(
          'destination' => array(
            'branch' => array('name' => 'master'),
          ),
          'source' => array(
            'branch' => array('name' => 'feature/4507'),
          ),
          'title' => $title,
        );
    
        $params = array(
          'type' => 'POST',
          'url' => 'https://api.bitbucket.org/2.0/repositories/diluschenko/beloglazov.pro.v2/pullrequests?access_token=$this->accessToken',
          'options' => array(
            'headers' => array(
              'Accept' => 'application/json',
            ),
            'json' => $data,
          ),
        );
    
        $response = $this->connector->sendRequest($params);
    
  9. Log in to comment