Is there a command line bitbucket tool?
Asked by Robert Gelb:

I can push changes to bitbucket in the cloud via \`git\` command. I would also like to be able to create a pull request from the command line.

Is this possible?


Answers:
author: Jeroen Poismans
body: Hi Robert!

Git can't create pull request. If you want to do this from a command line you might want to look into the Bitbucket REST api.

Here is a link to the Bitbucket REST call you need:  
<https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-repositories-workspace-repo-slug-pullrequests-post>

Regards,

Jeroen

comments: author: Robert Gelb
body: P.S. I figured out what the problem was so hopefully the next person finds this. The username needs to be the username, not the login email address. And the password has to be an App Password, not your login password.

So I tried this with no success. I am not sure what I am missing. Here is my scenario:  

I have a branch \`feature/sc-6745-abs-accounting-a-p-ledger\` and I want to create a PR to merge it into \`develop\`.  

So I kick off the following from the command line (powershell in Windows):  

```

curl https://api.bitbucket.org/2.0/repositories/jwdev/adris-web/pullrequests `
-u MyUserName:MyPassword `
--request POST `
--header 'Content-Type: application/json' `
--data '{
  "title": "My Title",
  "source": {
    "branch": {
      "name": "feature/sc-6745-abs-accounting-a-p-ledger"
    }
  },
  "destination": {
    "branch": {
      "name": "develop"
    }
  }
}'
```

It came back with 401 unauthorized.   
For the username, I am using my username, e.g. myname@company.com. For password, I used my regular login password. I also tried creating an app password and gave it right to read/write pull requests. Both passwords came back as 401 unauthorized. I then created yet another app password and gave it every permission imaginable. Still didn't work.  

I think I got the URL right. The workspace is called JW Development, but in the URL it's jwdev (e.g. <https://bitbucket.org/jwdev/workspace/overview/>). And the repository is called adris-web.

<br />

Why isn't it working?


author: Jeroen Poismans
body: Robert,

Let me validate with my own repo, I will get back to this!


author: Robert Gelb
body: [@Jeroen Poismans](/t5/user/viewprofilepage/user-id/1708479)Were you able to make it work?


author: Jeroen Poismans
body: Hi [@Robert Gelb](/t5/user/viewprofilepage/user-id/5041136)

Sorry for the wait, busy times here :-). I got it working in Postman with this body:

```
{
  "title": "My Title",
  "source": {
   "branch": {
    "name": "develop"
    }
   },
   "destination": {
     "branch": {
      "name": "master"
      }
   }
}
```

So exactly what you had. The URL was also correct!  

What I did differently?  
Under the repository I added an Access Token with the correct permissions :   
![Screenshot 2023-11-21 at 17.11.31.png](https://community.atlassian.com/t5/image/serverpage/image-id/293027iBFABF69E6267767B/image-dimensions/297x316?v=v2 "Screenshot 2023-11-21 at 17.11.31.png")  

Then I user this access token in my request. In the request this is translated into an authorization header:

```
Authorization: Bearer <your token>
```

Give it a try!

Jeroen


author: Robert Gelb
body: I did get it working (see the edit to my post above yours). One thing that I found odd is that the resulting PR doesn't include the default reviewers. I got around by adding them manually in the body of the API call, but it should add them add them automatically.


author: Sanjen Bariki
body: Hi [@Robert Gelb](/t5/user/viewprofilepage/user-id/5041136) ,

Welcome to Atlassian community!

Thanks you [@Jeroen Poismans](/t5/user/viewprofilepage/user-id/1708479) ,

Please find the below link to do the pull request in Bitbucket.

* **[Pull request](https://stackoverflow.com/questions/8721730/how-can-i-send-a-pull-request-via-command-line-in-bitbucket)**
* **[Another way by the command line](https://github.com/Mergifyio/git-pull-request)**

Regards,

Sanjen


Tags: bitbucket-cloud

cloud