有没有办法 link 带有拉取请求的工作项?

Is there a way to link the work item with pull request?

我正在尝试使用 REST Api 的 PATCH 方法以编程方式 auto-approve 拉取请求。但它给出以下错误: "$id":"1","innerException":null,"message":"拉取请求必须 linked 到工作项之前 它可以完成。"

我尝试了以下方法,但它对我不起作用。

它正在 link 将 PR 用于工作项目,但反之则不然。 请让我知道我们是否可以 link 在创建它时将工作项添加到 PR(我正在使用 REST POST 方法)或在更新 PR(我正在使用 REST PATCH 方法使其 Status:Completed . 对于 PATCH,我的 JSON body 如下所示:

$JSON正文=@" {
“状态”:“完成”, “lastMergeSourceCommit”:{ "commitId": "$CommitId" }, “完成选项”:{ “旁路政策”:真实的, “deleteSourceBranch”:真 } } "@

编辑#1:

我在 powershell 中使用以下代码。下面的代码正在更新工作项,但不更新 PR 页面。请帮助:

[uri] $PRUri = "https://dev.azure.com/{Organization****}/{Project_ID***}/_apis/wit/workitems/****" + "?api-version=4.0-preview"

$JSONBody= '
    [
        {
            "op": 0,
            "path": "/relations/-",
            "value": {
                "attributes": {
                    "name": "Pull Request"
                },
                "rel": "ArtifactLink",
                "url": "vstfs:///Git/PullRequestId/{Project_ID***}/{REPO_ID**}/PR_ID***"
            }
        }
    ]'
    enter code here

$User = "" # Not needed when using PAT, can be set to anything

$PAT="****"

$Base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User,$PAT)))

$Response=Invoke-RestMethod -Uri $PRUri
                            -Method PATCH
                            -ContentType "application/json-patch+json"
                            -Headers @{Authorization=("Basic {0}" -f $Base64AuthInfo)}
                            -Body $JSONBody

我们可以使用api:

Post https://dev.azure.com/{Organization}/_apis/wit/$batch

帮助我们link 将 workItem 提交给 PR。 这是请求正文:

[
    {
        "method": "PATCH",
        "uri": "/_apis/wit/workItems/{WorkItemId}?api-version=4.0-preview",
        "headers": {
            "Content-Type": "application/json-patch+json"
        },
        "body": [
            {
                "op": 0,
                "path": "/relations/-",
                "value": {
                    "attributes": {
                        "name": "Pull Request"
                    },
                    "rel": "ArtifactLink",
                    "url": "vstfs:///Git/PullRequestId/{ProjectID}%2F{repositoryID}%2F{PullRequestId}"
                }
            }
        ]
    }
]

顺便说一句,我们可以使用 api: Projects - List And Repositories - List 来帮助我们找到 projectId 和 repositoryID。

更新:

Update2(使用我的演示脚本):

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic {YourPATxxxx}")
$headers.Add("Content-Type", "application/json")

$JSON = @'
    [
        {
            "method": "PATCH",
            "uri": "/_apis/wit/workItems/471?api-version=4.0-preview",
            "headers": {
                "Content-Type": "application/json-patch+json"
            },
            "body": [
                {
                    "op": 0,
                    "path": "/relations/-",
                    "value": {
                        "attributes": {
                            "name": "Pull Request"
                        },
                        "rel": "ArtifactLink",
                        "url": "vstfs:///Git/PullRequestId/{ProjectID}%2F{repositoryID}%2F{PullRequestId}"
                    }
                }
            ]
        }
    ]
'@

$response = Invoke-RestMethod 'https://dev.azure.com/{orgName}/_apis/wit/$batch' -Method 'POST' -Headers $headers -Body $JSON
$response | ConvertTo-Json

这里我使用workItem 471来测试: