如何通过 API 创建新的 Bitbucket 拉取请求?

How to create a new Bitbucket pull request via API?

提问人:jeremywat 提问时间:11/5/2023 更新时间:11/6/2023 访问量:17

问:

我正在尝试按照此文档通过 Rest API 在 Bitbucket Server 上创建拉取请求。无论我尝试什么,我都会出错。我找到了这个答案并使用了答案中的正文文本(当然,替换了我的信息,如 repo、分支等):(400) Bad Request.

{
    "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"    
            }
        }
    ]
}

这是请求(我正在使用 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

Atlassian 文档中提供的示例在请求正文中包含了一些附加信息,我尝试也包含所有这些信息,但仍然收到相同的错误。(400) Bad Request.

我知道凭据 () 是正确的,因为我可以使用相同的凭据通过 API 获取 PR、对 PR 发表评论等。我还可以从 Bitbucket Web 界面创建新的 PR。$BasicAuth

那么,谁能告诉我我做错了什么,以及实现这一目标的正确方法是什么?

powershell bitbucket-server bitbucket-api

评论


答:

0赞 jeremywat 11/6/2023 #1

问题是我把节点放在节点内(这就是文档中的做法)。看看这个答案,项目和存储库节点是单独的元素,所以我尝试了一下,这次它起作用了:projectrepository

{
    "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"    
            }
        }
    ]
}