如何使用 Azure DevOps 服务 API 跨分支创建 PR?

How can I create a PR across forks using the Azure DevOps Service API?

我在 Azure DevOps 上有两个存储库,我们称它们为 parent_repochild_repochild_repo 是父级的一个分支。我想做的是创建一个 PR,使用 Azure DevOps 服务 API 将 child_repomaster 合并到 parent_repomaster,通过its Python library.

根据these docs and this threadforkSource 是指示源分支在分叉中并提供该分叉的repo_id 所需的参数。

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication

_connection = Connection(base_url=URL, creds=BasicAuthentication("", PAT))
CLIENT = _connection.clients.get_git_client()

args = {
    "git_pull_request_to_create": {
        "sourceRefName": f"refs/heads/master",
        "targetRefName": f"refs/heads/master",
        "forkSource": {"repository": {"repository": child_repo_id}},
        "title": "...",
        "description": "...",
    },
    "repository_id": parent_repo_id,
}

res = CLIENT.create_pull_request(**args)

我为 forkSource 提供的嵌套字典是库反复试验的结果,它确实成功地创建了 PR。然而,这个创建的PR是将parent:master合并到parent:master,所以它没有用。

如何更改 args 以便为 child:master 创建 PR 到 parent:master

好吧,我对文档进行了更深入的研究,从 here with forkSource. Following that link into here then here 开始,很明显 forkSource 必须像这样表述:

"forkSource": {"repository": {"id": child_repo_id}}

奇怪的是,DevOps API 忽略了嵌套的 repository 参数而不是(最好)抛出错误。此更改解决了问题并创建了一个类似于我所追求的 PR。

完整代码:

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication

_connection = Connection(base_url=URL, creds=BasicAuthentication("", PAT))
CLIENT = _connection.clients.get_git_client()

args = {
    "git_pull_request_to_create": {
        "sourceRefName": f"refs/heads/master",
        "targetRefName": f"refs/heads/master",
        "forkSource": {"repository": {"id": child_repo_id}}, # the only change required
        "title": "...",
        "description": "...",
    },
    "repository_id": parent_repo_id,
}

res = CLIENT.create_pull_request(**args)