You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure DevOps:PowerShell创建分支关联工作项后出现链接读取错误

Azure DevOps工作项关联分支后显示“分支链接无法读取”问题

操作背景

我有一个全新的Azure DevOps工作项(#24468),未关联任何分支,通过PowerShell脚本执行以下操作:

1. 设置认证令牌

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

2. 创建分支

以下脚本成功创建分支,在AzDO网页端可正常查看:

$targetBranchName = "ci"
$targetBranchId = (az repos ref list -p "myProjectName" -r "myRepoName" --filter heads/$($targetBranchName) | ConvertFrom-Json).ObjectId
$branchFullName ="feature/0024468-New_program_to_do_something"
$branchJSON = az repos ref create --name refs/heads/$($branchFullName) --object-id $targetBranchId --organization "https://dev.azure.com/myOrgName" --project myProjectName --repository myRepoName

3. 获取工作项信息

脚本成功获取工作项详情:

$taskNo = "24468"
$urlWI = -join("https://dev.azure.com/myOrgName/myProjectName/_apis/wit/workitems/",$taskNo,"?api-version=5.1")
$wiJSON = Invoke-RestMethod -Uri $urlWI -Headers @{Authorization = "Basic $token"} -Method Get 
$wiJSON._links # 显示链接
$wiJSON.fields # 显示字段

$taskURL = $wiJSON._links.self.href

4. 获取项目和仓库ID

  • 获取项目ID:
$url1="https://dev.azure.com/myOrgName/_apis/projects/myProjectName?api-version=6.0"
$response1 = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = "Basic $token"} -Method Get 
$projectid = $response1.id
  • 获取仓库ID:
$url2=-join("https://dev.azure.com/myOrgName/myProjectName/_apis/git/repositories/","myRepoName","?api-version=4.1")
$response2 = Invoke-RestMethod -Uri $url2 -Headers @{Authorization = "Basic $token"} -Method Get 
$repoid = $response2.id

5. 关联分支到工作项

构造请求体并调用Patch API,无报错,返回结果已包含新的关联链接:

$urlBranch = "vstfs:///Git/Ref/$projectid/$repoid/GB$branchFullName" 
$body = "[{
    `"op`": `"add`",
    `"path`": `"/relations/-`",
    `"value`": {
      `"rel`": `"ArtifactLink`",
      `"url`": `"$urlBranch`",
      `"attributes`": {
        `"name`": `"Branch`", `"comment`": `"工作项已关联分支`"
      }
    }
  }]"
$response = Invoke-RestMethod -Uri "$($TaskURL)?api-version=5.1" -Headers @{Authorization = "Basic $($token)"} -Method Patch -Body $body -ContentType application/json-patch+json

问题现象

API返回结果显示分支已成功关联,但在Azure DevOps网页端的工作项页面“开发/分支”区域,显示红色感叹号提示:(!) 分支链接无法读取。


内容的提问来源于stack exchange,提问作者Frank Monroe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 04:21:02