I'm trying to create a pull request on Bitbucket Server via Rest API, following this documentation. No matter what I try, I get a (400) Bad Request.
error. I found this answer and used the body text from the answer (of course, replacing my information like the repo, branch, etc.):
{
"title": "My new PR",
"description": "This is my new PR.",
"state": "OPEN",
"open": true,
"closed": false,
"fromRef": {
"id": "refs/heads/this-is-my-branch",
"repository": {
"slug": "my-repo",
"name": 'My repo',
"project": {
"key": "KE"
}
}
},
"toRef": {
"id": "refs/heads/master",
"repository": {
"slug": "my-repo",
"name": 'My repo',
"project": {
"key": "KE"
}
}
},
"locked": false,
"reviewers": [
{
"user": {
"name": "jeremywat"
}
}
]
}
This is the request (I'm using PowerShell):
Invoke-RestMethod -Headers @{Authorization = "Basic $BasicAuth"} -Body $JsonBody -ContentType 'application/json' -Method POST -Uri https://my-bitbucket-server.com/rest/api/1.0/projects/KE/repos/my-repo/pull-requests
The example provided in the Atlassian documentation includes some additional information in the request body and I tried including all that info as well, but still receive the same (400) Bad Request.
error.
I know the credentials ($BasicAuth
) are correct because I can get PRs, comment on PRs, etc. via the API with the same credentials. I can also create a new PR from the Bitbucket web interface.
So can anyone tell me what I'm doing wrong and what's the correct way to accomplish this?
The problem was I was putting the project
node inside the repository
node (which is how it's done in the documentation). Looking at this answer, the project and repository nodes are separate elements so I tried that and it worked this time:
{
"title": "My new PR",
"description": "This is my new PR.",
"state": "OPEN",
"open": true,
"closed": false,
"fromRef": {
"id": "refs/heads/this-is-my-branch",
"repository": "my-repo",
"project": {
"key": "KE"
}
},
"toRef": {
"id": "refs/heads/master",
"repository": "my-repo",
"project": {
"key": "KE"
}
},
"locked": false,
"reviewers": [
{
"user": {
"name": "jeremywat"
}
}
]
}