以编程方式在 Bitbucket 上创建拉取请求?

enn*_*nth 3 git bitbucket pull-request programmatically

更新:嗯,我在 Bash 脚本中运行它,但我想看看我得到了什么错误代码,现在我可以看到我得到了一个401 Unauthorized. 我正在使用我的用户名并使用admin访问 bitbucket创建了个人访问令牌,所以我应该能够创建一个 PR 对吗?我可以通过同一存储库上的 Web UI 执行此操作吗?

我正在运行 bash 脚本以在 Bitbucket 上创建拉取请求。我已经在以编程方式克隆 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"
            }
        }
    }'
Run Code Online (Sandbox Code Playgroud)

https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pullrequests#post

这是我的 repo 在 Bitbucket 上的样子,我屏蔽了真实姓名,但左侧格式相同(第一个名称是项目名称REACTOR2.0,而我相信第二个名称是存储库名称,dat-repo):

在此处输入图片说明

我正在尝试许多不同的变体,并且正在检查远程 bitbucket 服务器以获取新的拉取请求,但我什么也没看到。

我确定我的“标题”和“分支”是正确的。我唯一的问题是关于 URL;我正在从 bitbucket 输入我的用户名,如果您转到“管理帐户”,然后转到“名称”,这是我用于my-usernameURL 部分的字段,我正在为该部分添加存储库名称my-repository。但是,我需要注意,这是一个嵌套在名为“REACTOR2.0”的项目中的bitbucket 存储库,因此我不确定是否需要在 URL 中的某处指定项目名称。

有没有人成功使用这个 API?我在谷歌上看,但很多问题都在使用旧的 1.0 API 并且不适用,或者人们在做 GET 请求只是简单地获取拉取请求列表....

enn*_*nth 6

我使用了错误的 API。有一个 BitBucket 云 API,用于托管在 bitbucket 上的存储库,其 URL 为 bitbucket.com/ 和一个 BitBucket Server API,用于诸如https://bitbucket.mycompanyname.com/ 之类的URL ,这是我需要使用的 API。

最后,该URL应该是这样的(你需要填写的YourCompanyNameYourProjectKey以及YourRepositoryName参数):

https://bitbucket.YourCompanyHostName.com/rest/api/1.0/projects/YourProjectKey/repos/YourRepositoryName/pull-requests 
Run Code Online (Sandbox Code Playgroud)

和 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" 
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

如果您选择通过 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
Run Code Online (Sandbox Code Playgroud)

你可以阅读更多有关认证在下面,但你可以使用-u 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\" 
            }
        }
    ]
}"
Run Code Online (Sandbox Code Playgroud)

如果你想添加评论者但不知道名字,你可以向上面相同的 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/

拉取请求:https ://docs.atlassian.com/bitbucket-server/rest/7.6.0/bitbucket-rest.html#idp291