powershell 将工作项附加到Azure DevOps合并请求

qv7cva1a  于 10个月前  发布在  Shell
关注(0)|答案(1)|浏览(118)

我正在研究通过Azure DevOps中的API将工作项附加到合并请求的方法。
我已尝试使用以下方法,但未成功标记工作项。

$requestUri = "https://dev.azure.com/$org/$project/_apis/wit/workitems/12345?api-version=4.1"
$prapi1 = "https://dev.azure.com/$org/$project/_apis/git/repositories/$repo/pullRequests/12345"

$json = @"
[ {
  "op": "add",
  "path": "/relations/-",
  "value": {
    "rel": "ArtifactLink",
    "url": "$pr",
    "attributes": { "name": "pull request" }
  }
} ]
"@

$response = Invoke-RestMethod -Uri $requestUri -Headers $headers -ContentType "application/json-patch+json" -Method Patch -Body $json

字符串
但是,当使用指定的API时,我遇到一个错误,指示我:
{"$id”:“1”,“innerException”:null,“message”:“无效的资源链接目标:'https://dev.azure.com/org/project/_apis/git/repositories/repo/pullRequests/12345'.",“typeName”:“Microsoft.TeamFoundation.WorkItemTracking.Server.WorkItemResourceLinkException,Microsoft.TeamFoundation.WorkItemTracking.Server”,“typeKey”:“WorkItemResourceLinkException”,“errorCode”:0,“eventId”:3200}

agxfikkp

agxfikkp1#

根据您的脚本示例,问题的原因是pr url不正确。
正确格式:vstfs:///Git/PullRequestId/{projectId}/{repositoryId}/{pullRequestId}
您可以在Rest API中获取Pull Request URL:Pull Requests - Get Pull Request
它将在响应中显示artifactId
举例来说:
x1c 0d1x的数据
以下是PowerShell示例:

$token = "PAT"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$PullRequest="https://dev.azure.com/{org}/{projectname}/_apis/git/repositories/{reponame}/pullrequests/{PullRequestID}?api-version=7.1-preview.1"
$response = Invoke-RestMethod -Uri $PullRequest -Headers @{Authorization = "Basic $token"} -ContentType "application/json-patch+json" -Method Get

$url= $response.artifactId
echo $url

$requestUri = " https://dev.azure.com/{orgname}/_apis/wit/workitems/{workitemid}?api-version=7.1-preview.3"

$json = @"
[ {
  "op": "add",
  "path": "/relations/-",
  "value": {
    "rel": "ArtifactLink",
    "url": "$url",
    "attributes": { "name": "pull request" }
  }
} ]
"@

$response1 = Invoke-RestMethod -Uri $requestUri -Headers @{Authorization = "Basic $token"} -ContentType "application/json-patch+json" -Method Patch -Body $json

字符串

相关问题