以编程方式在 Bitbucket 上创建 Pull Request?

问题描述 投票:0回答:1

更新:好吧,我在 Bash 脚本中运行它,但我想看看我得到了什么错误代码,现在我可以看到我得到了一个

401 Unauthorized
。我正在使用我的用户名,并使用
admin
access bitbucket 创建了个人访问令牌,所以我应该能够创建 PR,对吗?我可以通过同一个仓库上的网络用户界面来做到这一点吗?

我正在运行一个 bash 脚本来在 Bitbucket 上创建一个拉取请求。我已经以编程方式克隆存储库、编辑文件、执行 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
) :

我正在尝试许多不同的变体,并正在检查远程 bitbucket 服务器是否有新的拉取请求,但我看到nothing.

我确定我的“头衔”和“分支”是正确的。我唯一的问题是关于网址;我从 bitbucket 输入我的用户名,如果你去“管理帐户”然后“名称”,这是我用于 URL

my-username
部分的字段,我正在为
 添加存储库名称my-repository
部分。但是,我需要注意,这是 bitbucket 上的一个存储库nested 在一个名为“REACTOR2.0”的项目中,所以我不确定是否需要在某处的 URL 中指定项目名称。

有人成功使用过这个 API 吗?我查看了谷歌,但很多问题都在使用旧的 1.0 API 并且不适用,或者人们正在执行 GET 请求只是获取拉取请求列表....

git bitbucket pull-request
1个回答
15
投票

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

最后的URL应该是这样的(需要填写

YourCompanyName
YourProjectKey
YourRepositoryName
参数):

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

您可以在下面阅读有关authentication的更多信息,但是您可以在CURL请求中使用

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

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

© www.soinside.com 2019 - 2024. All rights reserved.