git 是否有方法将工作项与拉取请求链接起来?

k3bvogb1  于 2023-05-27  发布在  Git
关注(0)|答案(1)|浏览(94)

我正在尝试使用REST API的PATCH方法以编程方式自动批准Pull请求。但它给出了以下错误:“$id”:“1”,“innerException”:null,“message”:“拉取请求必须链接到工作项才能完成。”
我尝试了下面的方法,但它对我不起作用。How to link a work item to a pull request using REST API in Azure DevOps?
它将PR链接到工作项,而不是相反。请让我知道我们是否可以在创建PR(我使用REST POST方法)或更新PR(我使用REST PATCH方法使其状态:Completed)时将工作项链接到PR。我的JSON body看起来像下面的PATCH:
$JSONBody= @”{
“status”:“completed”,“lastMergeSourceCommit”:{“commitId”:“$CommitId”},“completionOptions”:{“bypassPolicy”:true,“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
kse8i1jr

kse8i1jr1#

我们可以使用API:

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

帮助我们将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 - ListRepositories - 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来测试:

相关问题