git - 以编程方式在 Bitbucket 上创建 pull 请求?

标签 git bitbucket pull-request programmatically

更新 :嗯,我在 Bash 脚本中运行它,但我想看看我得到了什么错误代码,现在我可以看到我得到了一个 401 Unauthorized 。我正在使用我的用户名,并使用 admin 访问 bitbucket 创建了个人访问 token ,所以我应该能够创建 PR 对吗?我可以通过同一存储库上的 Web UI 执行此操作吗?
我正在运行 bash 脚本以在 Bitbucket 上创建 pull 请求。我已经在以编程方式克隆 repo、编辑文件、执行 git add/commit,现在我只需要使用 CURL 来制作 PR。似乎 bitbucket API 公开了一个端点来使用 POST 请求执行此操作:

Creates a new pull request where the destination repository is this repository and the author is the authenticated user.

The minimum required fields to create a pull request are title and source, specified by a branch name.

curl https://api.bitbucket.org/2.0/repositories/my-username/my-repository/pullrequests \
    -u my-username:my-password \
    --request POST \
    --header 'Content-Type: application/json' \
    --data '{
        "title": "My Title",
        "source": {
            "branch": {
                "name": "staging"
            }
        }
    }'
https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pullrequests#post
这是我的 repo 在 Bitbucket 上的样子,我屏蔽了真实姓名,但左侧格式相同(第一个名称是项目名称 REACTOR2.0 ,而我相​​信第二个名称是存储库名称 dat-repo ):
enter image description here
我正在尝试许多不同的变体,并且正在检查远程 bitbucket 服务器是否有新的 pull 请求,但我什么也没看到。
我确定我的“标题”和“分支”是正确的。我唯一的问题是关于 URL;我正在从 bitbucket 输入我的用户名,如果您转到“管理帐户”然后是“名称”,这是我用于 URL 的 my-username 部分的字段,并且我正在为 my-repository 部分添加存储库名称。但是,我需要注意,这是一个嵌套在名为“REACTOR2.0”的项目中的 bitbucket 存储库,因此我不确定是否需要在 URL 中的某处指定项目名称。
有没有人成功使用这个 API?我在谷歌上看,但很多问题都在使用旧的 1.0 API 并且不适用,或者人们在做 GET 请求只是简单地获取 pull 请求列表....

最佳答案

我使用了错误的 API。有一个 BitBucket 云 API,用于托管在 bitbucket 上的存储库,其 URL 为 bitbucket.com/和一个 BitBucket 服务器 API,用于 https://bitbucket.mycompanyname.com/ 之类的 URL,这是我需要使用的 API。
最后,URL 应该是这样的(您需要填写 YourCompanyNameYourProjectKeyYourRepositoryName 参数):

https://bitbucket.YourCompanyHostName.com/rest/api/1.0/projects/YourProjectKey/repos/YourRepositoryName/pull-requests 
和 JSON 发布:
{
    "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": "reviewersName1"    
            }
        },
         {
            "user": {
                "name": "reviewerName2" 
            }
        }
    ]
}
如果您选择通过 CURL 访问 API,您可以使用以下内容制定该请求:
curl -H "Authorization: Basic EncryptedBase64UsernamePasswordHere" \
  -H "Content-Type: application/json" \
  "https://bitbucket.YourCompanyName.com/rest/api/1.0/projects/YourProjectKey/repos/YourRepositoryName/pull-requests" \
  -d JsonDataFromAboveHere
您可以在下面阅读有关 身份验证 的更多信息,但您可以在 CURL 请求中使用 -u username:password 进行身份验证,也可以使用 username:password 字符串和 base64 对其进行编码,然后使用 -H "Authentication: Basic BASE64ENCODEDUSERNAMEPASSWORDHERE" ,由您决定。您可能需要使用下面的内容来转义 JSON 双引号 ",具体取决于您发出此请求的机器类型,如下所示:
"{\"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\": \"reviewerName1\" 
            }
        },
         {
            \"user\": {
                \"name\": \"reviewerName2\" 
            }
        }
    ]
}"
如果你想添加评论者但不知道名字,你可以向上面相同的 URL 发出 GET 请求,它会给出一个用户列表,然后可以将他们的名字添加到评论者数组中,所以当PR 被创建,他们已经被添加为审阅者。
验证:
https://developer.atlassian.com/server/bitbucket/how-tos/example-basic-authentication/
休息API:
https://developer.atlassian.com/server/bitbucket/how-tos/command-line-rest/
pull 请求:
https://docs.atlassian.com/bitbucket-server/rest/7.6.0/bitbucket-rest.html#idp291

关于git - 以编程方式在 Bitbucket 上创建 pull 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64002827/

相关文章:

git - 如何修复推送失败意外 HTTP 状态代码 : 500 GitKraken

github - 从源代码中提取 # TODO 标签并将它们转换为 Bitbucket/Github 上的问题

azure-devops - 重新检查VSTS中的合并冲突(Azure DevOps)

linux - Gitolite 错误 : gitolite-admin not a repo

git - Git 的 Subversion 的 "use-commit-times"是什么?

git - 解释git删除远程分支的命令

java - 使用 Bitbucket 配置 Jenkins

git - 为什么恢复的提交不会显示在新的 PR 中?

git - 来自特定分支的 TFS 条件 pull 请求

git - 在 git 中撤消修改的文件不起作用