使用Azure DevOps REST API添加PR审核人报错,如何解决?
问题原因及解决方法
你当前的REST API调用方式有误,导致无法正确添加审核人,具体问题和修复方案如下:
核心问题
你把审核人ID拼在了API路径末尾,这个端点实际是用于更新/删除单个指定审核人的(需配合PUT/DELETE方法),而添加审核人应该向/reviewers集合发起POST请求,并且通过请求体传递审核人信息,而非URL路径参数。
另外,你使用的Azure AD用户ID无法直接被Azure DevOps的PR审核人API识别,需要使用Azure DevOps内部的用户标识(identity descriptor),或者直接传递用户的邮箱/用户主体名(需放在请求体中)。
修复步骤
方法1:通过邮箱获取Azure DevOps用户标识并添加审核人
先调用API获取用户的identity descriptor,再用它添加审核人:
# 替换为实际值 $orgName = "你的组织名" $projName = "你的项目名" $repoId = "仓库ID" $prId = "47108" $reviewerEmail = "审核人邮箱" # 获取用户identity descriptor $identityUrl = "https://dev.azure.com/$orgName/_apis/identities?searchFilter=General&value=$reviewerEmail&api-version=6.0" $identity = Invoke-RestMethod -Uri $identityUrl -Headers $AzureDevOpsAuthenicationHeader $descriptor = $identity.value[0].descriptor # 添加审核人 $prReviewerUrl = "https://dev.azure.com/$orgName/$projName/_apis/git/repositories/$repoId/pullRequests/$prId/reviewers?api-version=6.0" $body = @{ descriptor = $descriptor } | ConvertTo-Json Invoke-RestMethod -Uri $prReviewerUrl -Method Post -Headers $AzureDevOpsAuthenicationHeader -ContentType application/json -Body $body
方法2:直接通过邮箱/用户主体名添加(无需提前获取descriptor)
如果你的Azure DevOps组织和Azure AD已集成,也可以直接在请求体中传递邮箱或用户主体名:
# 替换为实际值 $orgName = "你的组织名" $projName = "你的项目名" $repoId = "仓库ID" $prId = "47108" $reviewerPrincipal = "审核人邮箱或用户主体名" $prReviewerUrl = "https://dev.azure.com/$orgName/$projName/_apis/git/repositories/$repoId/pullRequests/$prId/reviewers?api-version=6.0" $body = @{ principalName = $reviewerPrincipal } | ConvertTo-Json Invoke-RestMethod -Uri $prReviewerUrl -Method Post -Headers $AzureDevOpsAuthenicationHeader -ContentType application/json -Body $body
为什么az命令能成功?
az repos pr reviewer add命令内部已经帮你完成了:
- 通过邮箱查询到对应的Azure DevOps用户标识
- 以正确的方式向
/reviewers集合发起POST请求并传递请求体
所以无需你手动处理这些细节。
内容的提问来源于stack exchange,提问作者Bionic Devnull
相关产品推荐
相关产品推荐

