3

I am trying to create an automation pipeline and in that, I want to create a pull request in bitbucket from my jenkins job. I found some document where I can create a pull request using rest api. But that is for api 2.0. I have old bitbucket and I am not sure which api version I have to use.

Thanks,

Mithil Amin
  • 207
  • 1
  • 6
  • 14

1 Answers1

6

You can create a pull request in Bitbucket using the REST API 1.0 doing the following:

curl -s --user USER:PASS --request POST --data @- --header Content-Type:application/json https://BITBUCKET-SERVER/rest/api/1.0/projects/TO-PROJECT/repos/TO-REPOSITORY/pull-requests << EOF
{
    "title": "SOME-TITTLE",
    "description": "SOME-DESCRIPTION",
    "state": "OPEN",
    "open": true,
    "closed": false,
    "fromRef": {
        "id": "refs/heads/FROM-BRANCH",
        "repository": {
            "slug": "FROM-REPO",
            "name": null,
            "project": {
                "key": "FROM-PROJECT"
            }
        }
    },
    "toRef": {
        "id": "refs/heads/TO-BRANCH",
        "repository": {
            "slug": "TO-REPO",
            "name": null,
            "project": {
                "key": "TO-PROJECT"
            }
        }
    },
    "locked": false,
    "reviewers": [
        {
            "user": {
                "name": "REVIEWER"
            }
        }
    ]
}
  • do you know if there is any jenkins plugin that would do this? also is there a way to let some of those defaults be selected, for example the reviewers assuming they are setup in the project just let the defaults take place? – Miguel Costa Oct 21 '20 at 07:10
  • This plugin claims to do so - however it wants me to disable an existing bitbucket plugin, and I have no idea what impact that'll have on my jobs, so never tried it https://plugins.jenkins.io/bitbucket-push-and-pull-request/ – Adam Hughes Jul 11 '22 at 22:45
  • worked for me with POST, as noted on the official documentation, thanks! – Shay Perlstein Aug 24 '22 at 10:42
  • I changed from "PUT" to "POST" in the answer. – Marcelo Ávila de Oliveira Aug 24 '22 at 12:33