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

通过Azure DevOps REST API设置PR自动完成报错求助

Azure Pipeline REST API设置PR自动完成报错解决

问题描述

尝试在Azure Pipeline中通过REST API将用户手动创建的PR(拉取请求)设置为自动完成时,触发错误,需要解决该问题。

错误信息

Invoke-RestMethod:
/home/vsts/work/_temp/57dec5bf-46fb-449d-9fd7-86e123fc352e.ps1:50
Line | 50 | … eteResult = Invoke-RestMethod -Method PATCH -Headers $headers -Body $ … |

```json
{"$id":"1","innerException":null,"message":"Invalid argument value.\r\nParameter name: Invalid pull request auto complete set by id. Valid values are either the current user identity id, or an empty guid (to unset auto complete).","typeName":"Microsoft.TeamFoundation.SourceControl.WebServer.InvalidArgumentValueException, Microsoft.TeamFoundation.SourceControl.WebServer","typeKey":"InvalidArgumentValueException","errorCode":0,"eventId":0}
```
##[error]PowerShell exited with code '1'.

原YAML配置

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # construct base URLs
      $apisUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECT)/_apis"
      $projectUrl = "$apisUrl/git/repositories/XXXXX"
      Write-Output "Print out content of projectUrl $projectUrl"

      # create common headers
      $headers = @{}
      $headers.Add("Authorization", "Bearer $env:SYSTEM_ACCESSTOKEN")
      $headers.Add("Content-Type", "application/json")
    
      $baseUrl = "https://dev.azure.com/XXXXX/XXXXXX/_apis/git/pullrequests/XXXX?api-version=6.0"
      Write-Output "Print out content of baseUrl $baseUrl"
    
      $pullRequestJsonStr = Invoke-RestMethod -Uri $baseUrl -Headers $headers -Method GET 
      Write-Output "Print out content of pullRequestJsonStr $pullRequestJsonStr"
      $pullRequestObj = ConvertTo-Json $pullRequestJsonStr -Depth 5
      Write-Output "pullRequestJsonStr to object $pullRequestObj"
    
      #Write-Output "Print out content of Build.Respository.ID $(Build.Repository.ID)"
    
      $autoCompleteSetId = $pullRequestJsonStr.createdBy.id
      Write-Output "*****Print out content of autoCompleteSetId $autoCompleteSetId"
        
      #Auto complete call
      $setAutoComplete = @{
        "autoCompleteSetBy" = @{
          "id" = $autoCompleteSetId
        }
        # "completionOptions" = @{       
        #   "deleteSourceBranch" = $False
        #   "bypassPolicy" = $False
        # }
      }
      try {
        $setAutoCompleteJson = ($setAutoComplete | ConvertTo-Json -Depth 5)
      } catch {
        Write-Host "Unable to create json due to $_"
      }
      Write-Output "Print out content of setAutoCompleteJson  $setAutoCompleteJson"
    
      Write-Output "***Sending a REST call to set auto-complete on pull request***"
      
      # REST call to set auto-complete on Pull Request
      # if ($autoCompleteSetId -ne $null) {
        $pullRequestUpdateUrl = ($projectUrl + '/pullRequests/' + $pullRequestJsonStr.pullRequestId + '?api-version=6.0')
        Write-Output "Print out content of pullRequestUpdateUrl $pullRequestUpdateUrl"
    
        $setAutoCompleteResult = Invoke-RestMethod -Method PATCH -Headers $headers -Body $setAutoCompleteJson -Uri $pullRequestUpdateUrl
        Write-Output "Pull request set to auto-complete"
      # }
      # else {
      #   Write-Output "Pull request already set to auto-complete"
      # }
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)
  displayName: 'PowerShell Script'

解决方法

错误提示明确指出:autoCompleteSetBy.id的合法值只能是当前发起请求的用户身份ID(即Pipeline使用的系统AccessToken对应的身份),或者空GUID(用于取消自动完成)。原脚本使用PR创建者的ID,不符合要求。

修改步骤

  1. 添加获取当前Pipeline执行用户ID的逻辑,替换原有的PR创建者ID获取代码
  2. 使用获取到的当前用户ID设置autoCompleteSetBy.id

修改后的完整YAML

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # construct base URLs
      $apisUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECT)/_apis"
      $projectUrl = "$apisUrl/git/repositories/XXXXX"
      Write-Output "Print out content of projectUrl $projectUrl"

      # create common headers
      $headers = @{}
      $headers.Add("Authorization", "Bearer $env:SYSTEM_ACCESSTOKEN")
      $headers.Add("Content-Type", "application/json")
    
      $baseUrl = "https://dev.azure.com/XXXXX/XXXXXX/_apis/git/pullrequests/XXXX?api-version=6.0"
      Write-Output "Print out content of baseUrl $baseUrl"
    
      $pullRequestJsonStr = Invoke-RestMethod -Uri $baseUrl -Headers $headers -Method GET 
      Write-Output "Print out content of pullRequestJsonStr $pullRequestJsonStr"
    
      # 获取当前执行请求的用户ID
      $currentUserUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)_apis/connectionData?api-version=6.0"
      $currentUser = Invoke-RestMethod -Uri $currentUserUrl -Headers $headers -Method GET
      $autoCompleteSetId = $currentUser.authenticatedUser.id
      Write-Output "*****当前执行用户ID: $autoCompleteSetId"
        
      #Auto complete call
      $setAutoComplete = @{
        "autoCompleteSetBy" = @{
          "id" = $autoCompleteSetId
        }
        "completionOptions" = @{       
          "deleteSourceBranch" = $False
          "bypassPolicy" = $False
        }
      }
      try {
        $setAutoCompleteJson = ($setAutoComplete | ConvertTo-Json -Depth 5)
      } catch {
        Write-Host "Unable to create json due to $_"
      }
      Write-Output "Print out content of setAutoCompleteJson  $setAutoCompleteJson"
    
      Write-Output "***Sending a REST call to set auto-complete on pull request***"
      
      $pullRequestUpdateUrl = ($projectUrl + '/pullRequests/' + $pullRequestJsonStr.pullRequestId + '?api-version=6.0')
      Write-Output "Print out content of pullRequestUpdateUrl $pullRequestUpdateUrl"
    
      $setAutoCompleteResult = Invoke-RestMethod -Method PATCH -Headers $headers -Body $setAutoCompleteJson -Uri $pullRequestUpdateUrl
      Write-Output "Pull request set to auto-complete"
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)
  displayName: 'PowerShell Script'

额外说明

  • 确保System.AccessToken拥有拉取请求管理的权限(可在Pipeline的权限设置中配置)
  • 若需取消自动完成,将autoCompleteSetBy.id设置为空GUID:"00000000-0000-0000-0000-000000000000"

内容的提问来源于stack exchange,提问作者Abhishek bagur

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 09:37:22