4

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.

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
Ace
  • 831
  • 2
  • 8
  • 28
  • In the extension that mentioned in the link you provided there is an option to set "Auto complete" to the PR you create. (disclaimer: I'm the extension author) – Shayki Abramczyk Jun 28 '20 at 07:23
  • @ShaykiAbramczyk, Thanks for responding back. I can't use a 3rd party extension because of security issues so I'm trying to do it myself in the script. What exactly do I need to pass as the Id in order to complete the PR – Ace Jul 02 '20 at 10:31
  • Is "Project Collection Build Service (msazure)" the right service to use? – Ace Jul 02 '20 at 10:40
  • do you try to update with PowerShell? Does it's during a build? – Shayki Abramczyk Jul 02 '20 at 10:41
  • Yes, I'm trying to update via Powershell. It happens when I try to run that specific step which contains the powershell script so I assume it's on runtime and not build (correct me if I'm wrong) – Ace Jul 02 '20 at 10:48
  • Did you create the PR also in the script? I don't understand, is the script run in a build pipeline (you have task "PowerShell")? – Shayki Abramczyk Jul 02 '20 at 10:57
  • Yes, it's a powershell script task that contains both the PR creation and after that the PR completion part. It's run in Release pipeline. – Ace Jul 02 '20 at 11:04
  • Ok, check the code of my extension: https://github.com/shayki5/azure-devops-create-pr-task/blob/10a0dcfc4b90c3459eab0709378d1953a2e62b82/task/createPullRequest.ps1#L584 – Shayki Abramczyk Jul 02 '20 at 11:06
  • I'm not sure what was the issue, but I have added $Response.createdBy.id and compilationOptions = "" and it works. Thanks @ShaykiAbramczyk for your help! :) – Ace Jul 02 '20 at 11:20
  • Great! I added it also as an answer :) – Shayki Abramczyk Jul 02 '20 at 12:57

2 Answers2

2

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
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
0

Here after creation of PR, run the update operation to set Autocomplet. Check the below link

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

koushik
  • 320
  • 6
  • 14
  • Updated the original post - I'm trying using this API but get an error (described above) – Ace Jul 02 '20 at 08:13