Creating a pull request via API

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

2 answers

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.

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. 

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

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

Hi,

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

@mike r

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 Join to answer
Community showcase
Piotr Plewa
Published Dec 27, 2017 in Bitbucket

Recipe: Deploying AWS Lambda functions with Bitbucket Pipelines

Bitbucket Pipelines helps me manage and automate a number of serverless deployments to AWS Lambda and this is how I do it. I'm building Node.js Lambda functions using node-lambda&nbsp...

706 views 0 4
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot