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

Creating a pull request via API

perfecto25 October 4, 2016

hello, Im wondering if its possible to create a new pull req using Bitbuckets API? Im trying to automate a process where a JIRA requests sends a POST with a ticket Json, and my shell script then adds a new file to a repo, commits, creates a new branch, pushes to master, and then finally creates a Pull Request for review.

 

I browsed the docs and it seems like I can only do GET. Is it possible to create new pull requests?

Im avoiding using other solutions like the Stash CLI gem due to ruby version requirements. Thanks.  

 

Bbucket 4.2.0

3 answers

2 votes
G__Sylvie_Davies__bit-booster_com_
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.
October 4, 2016

POST against: /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/pull-requests

As the reference docs state: "Creates a new pull request between two branches."

https://developer.atlassian.com/static/rest/bitbucket-server/4.9.1/bitbucket-rest.html#idp2046528

Example POST from the reference docs:

{
    "title": "Talking Nerdy",
    "description": "It’s a kludge, but put the tuple from the database in the cache.",
    "state": "OPEN",
    "open": true,
    "closed": false,
    "fromRef": {
        "id": "refs/heads/feature-ABC-123",
        "repository": {
            "slug": "my-repo",
            "name": null,
            "project": {
                "key": "PRJ"
            }
        }
    },
    "toRef": {
        "id": "refs/heads/master",
        "repository": {
            "slug": "my-repo",
            "name": null,
            "project": {
                "key": "PRJ"
            }
        }
    },
    "locked": false,
    "reviewers": [
        {
            "user": {
                "name": "charlie"
            }
        }
    ],
    "links": {
        "self": [
            null
        ]
    }
}

 

p.s.  I invite you to try my Bitbucket and JIRA add-ons.  Bit-Booster Commit Graph and More (for Bitbucket), and Git Graph for JIRA.

perfecto25 October 4, 2016

thanks for that. 

Im running it like this,

 

curl -u username:MyPassword -H "Content-Type: application/json" https://mybitbucket.com/rest/api/1.0/projects/myProject/repos/myRepo/pull-requests -X POST --data req.json

 Json looks like this,
 

 

{
'title': 'testing-pull-req',
'description': 'testing testing',
'state': 'OPEN',
'open': true,
'closed': false,
'fromRef': {
'id': 'refs/heads/test-branch',
'repository': {
'slug': 'myRepo',
'name': null,
'project': {
'key': 'myProject'
}
}
},
'toRef': {
'id': 'refs/heads/master',
'repository': {
'slug': 'myRepo',
'name': null,
'project': {
'key': 'MyProject'
}
}
},
}
}

 

 

getting weird error, 

 

{"errors":[{"context":null,"message":"Unexpected character ('r' (code 114)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: com.atlassian.stash.internal.web.util.web.CountingServletInputStream@3e7de6ce; line: 1, column: 2]","exceptionName":"org.codehaus.jackson.JsonParseException"}]}

 

cant find anything on this, I checked the JSON syntax its valid. 

perfecto25 October 4, 2016

ok made it work, for some reason it didint like the json file, I formatted the whole thing as a compact 1-liner, 

 

{"title":"test","description":"test","fromRef":{"id":"refs/heads/test-branch","repository":{"slug":"test-repo","name":null,"project":{"key":"myProject"}}},"toRef":{"id":"refs/heads/master","repository":{"slug":"myRepo","name":null,"project":{"key":"MyProj"}}}}

 

ran it like this, 

 

curl -u user:myPW -H "Content-Type: application/json" https://bitbucket.server.com/rest/api/1.0/projects/myProject/repos/myRepo/pull-requests -X POST --data '{"title":"test","description":"test","fromRef":{"id":"refs/heads/test-branch","repository":{"slug":"test-repo","name":null,"project":{"key":"myProject"}}},"toRef":{"id":"refs/heads/master","repository":{"slug":"myRepo","name":null,"project":{"key":"MyProj"}}}}'

 

works

Like # people like this
G__Sylvie_Davies__bit-booster_com_
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.
October 4, 2016

Might have been the single-quotes messing things up.  Single-quotes are valid for JavaScript, but invalid for JSON.

Shubhra Kushwaha
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 29, 2017

Hi,

I tred above code but this is not working for me. Getting null for URI.

Yeldos
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 11, 2019

how can I use not password but key?

Amal Matty Antony
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 5, 2020

@perfecto25just created an account to thank you, your minified json solution worked :)

0 votes
Ramashankar Mishra
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 19, 2020

command executed successfully but no luck. Pull requests didn't get generated. Here is the command which I fired.

curl -u myUserName:myPwd -H "Content-Type: application/json" https://bitbucket.org/rest/api/1.0/projects/myProject/repos/myCore/pull-requests -X POST --data '{"title":"merge","description":"merge","fromRef":{"id":"refs/heads/testBranch","repository":{"slug":"myCore","name":null,"project":{"key":"testBranch"}}},"toRef":{"id":"refs/heads/toBranch","repository":{"slug":"myRepo","name":null,"project":{"key":"testBranch"}}}}'

0 votes
vshinde727
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 19, 2018

@perfecto25

Try to use like below... worked for me..

curl -u user:myPW -H "Content-Type: application/jso https://bitbucket.server.com/rest/api/1.0/projects/myProject/repos/myRepo/pull-requests -X POST --data @req.json

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events