Search code examples
azure-devopsazure-pipelinesazure-pipelines-release-pipelineazure-devops-rest-apiazure-devops-extensions

Create an automatic PR and complete it Azure DevOps


I'm trying to create a flow where when a PR from branch a -> b is completed, an automatic PR from branch c -> d is created and completed.

I looked in this question but it doesn't mention auto complete

CI/CD pipelines Azure devops automatic merge after deploy release

Also, can I set a specific policy to the automated PR so it would be auto complete?

Update

I tried using the following call:

https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull%20requests/update?view=azure-devops-rest-5.1

But I got the error:

Invoke-RestMethod : {"$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)."

I tried to get the creator ID from the response but it's empty.

I tried to assign Project Collection Build Service (msazure) like I saw here: https://developercommunity.visualstudio.com/content/problem/298596/autocompletesetby-ignored-in-pull-request-rest-api.html

But I'm getting the error.


Solution

  • After you create the PR you can get the creator ID:

    $response = Invoke-RestMethod -Uri $url -Method Post -Headers $head -Body $jsonBody -ContentType "application/json;charset=UTF-8"
    
    $currentUserId = $response.createdBy.id
    

    And send him in the update json body:

    $body = @{
        autoCompleteSetBy = @{ id = "$buildUserId" }
        completionOptions = ""
    }    
    
    $response = Invoke-RestMethod -Uri $url -Method Patch -Headers $head -Body $jsonBody -ContentType application/json