我如何使用Azure DevOps服务API跨福克斯创建PR?

2024-09-30 14:18:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Azure DevOps上有两个回购协议,我们称它们为parent_repochild_repochild_repo是父级的分支。我想做的是创建一个PR,使用Azure DevOps服务API,通过its Python librarychild_repo中的master合并到parent_repo中的master

根据these docsthis 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


Tags: fromimportmasteridchildrepository分支args
1条回答
网友
1楼 · 发布于 2024-09-30 14:18:46

好的,我深入研究了文档,从forkSource开始。在链接到here然后here之后,很明显forkSource必须这样表述:

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

奇怪的是,DevOpsAPI忽略了嵌套的repository参数,而不是(最好)抛出错误。这一变化解决了问题,并创建了一个类似于我所追求的公关

完整代码:

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)

相关问题 更多 >