通过编程在Bitbucket上创建拉取请求?

时间:2020-09-22 03:48:47

标签: git bitbucket pull-request programmatically

更新:嗯,我正在Bash脚本中运行此脚本,但是我想查看得到的错误代码,现在可以看到我得到了401 Unauthorized。我正在使用用户名,并且使用admin访问位存储桶创建了个人访问令牌,因此我应该能够创建PR吗?我可以通过同一回购上的Web UI来做到这一点吗?

我正在运行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

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

enter image description here

我正在尝试许多不同的变体,并且正在检查远程位桶服务器是否有新的请求请求,但是我什么都没看到。

我确定我的“标题”和“分支”是正确的。我唯一的问题是关于URL的。我正在从bitbucket输入我的用户名,如果您依次转到“管理帐户”和“名称”,那是我用于URL的my-username部分的字段,并且我要添加存储库名称my-repository部分。但是,我需要注意的是,这是一个位于名为“ REACTOR2.0”的项目内的bitbucket nested 上的存储库,因此我不确定项目名称是否需要在URL的某处指定。

有人成功使用此API吗?我在google上看过,但是很多问题都在使用旧的1.0 API,并且不适用,或者有人在执行GET请求,只是获得了一系列拉取请求...。

1 个答案:

答案 0 :(得分:2)

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

最后,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进行身份验证,也可以使用用户名:密码字符串和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/

PullRequest: https://docs.atlassian.com/bitbucket-server/rest/7.6.0/bitbucket-rest.html#idp291