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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

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

3 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

Like # people like this

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.

how can I use not password but key?

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

@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

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"}}}}'

Suggest an answer

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

Calling any interview participants for Bitbucket Data Center

Hi everyone,  We are looking to learn more about development teams’ workflows and pain points, especially around DevOps, integrations, administration, scale, security, and the related challeng...

441 views 4 4
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