Create an Auto-Completing Pull Request in Azure DevOps CI/CD Pipeline Using PowerShell
Tags: devops, azure, git, powershell
Published: 2021-10-28 08:37:09

Ever thought of creating a pull request automatically using a CI/CD pipeline and setting the pull request auto-complete once all the required policies are met? Sometimes this is useful to keep one branch in sync with another, or you want to pull whatever changes are made to one branch automatically.

Azure DevOps provides a set of [REST APIs](https://docs.microsoft.com/en-us/rest/api/azure/devops/?view=azure-devops-rest-6.1) to interact with DevOps services programmatically. But there is no single API that allows us to create a PR with auto-complete enabled. So we have to make two API calls to achieve this. [Create PR](https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pull-requests/create?view=azure-devops-rest-5.0) for the pull request creation and [Update PR](https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pull-requests/update?view=azure-devops-rest-5.0) to set the pull request to autocomplete

We can call the REST APIs in many ways. I'm using PowerShell to make RSET calls and PowerShell pipeline tasks to integrate it with Azure DevOps CI/CD Pipeline. Below is the screenshot of a sample pipeline I have created and the PowerShell script I have used in the PowerShell pipeline task.

![Azure DevOps Build Pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6z9dq8tgxxbiauypq14h.png)

##PowerShell Script

```ps
# construct base URLs
$apisUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)/$($env:SYSTEM_TEAMPROJECT)/_apis"
$projectUrl = "$apisUrl/git/repositories/<Your Repository Name>"

# create common headers
$headers = @{}
$headers.Add("Authorization", "Bearer $env:SYSTEM_ACCESSTOKEN")
$headers.Add("Content-Type", "application/json")

# Create a Pull Request
$pullRequestUrl = "$projectUrl/pullrequests?api-version=5.1"
$pullRequest = @{
        "sourceRefName" = "refs/heads/develop"
        "targetRefName" = "refs/heads/master"
        "title" = "Pull from develop to master"
        "description" = ""
    }

$pullRequestJson = ($pullRequest | ConvertTo-Json -Depth 5)

Write-Output "Sending a REST call to create a new pull request from develop to master"

# REST call to create a Pull Request
$pullRequestResult = Invoke-RestMethod -Method POST -Headers $headers -Body $pullRequestJson -Uri $pullRequestUrl;
$pullRequestId = $pullRequestResult.pullRequestId

Write-Output "Pull request created. Pull Request Id: $pullRequestId"

# Set PR to auto-complete
$setAutoComplete = @{
    "autoCompleteSetBy" = @{
        "id" = $pullRequestResult.createdBy.id
    }
    "completionOptions" = @{       
        "deleteSourceBranch" = $False
        "bypassPolicy" = $False
    }
}

$setAutoCompleteJson = ($setAutoComplete | ConvertTo-Json -Depth 5)

Write-Output "Sending a REST call to set auto-complete on the newly created pull request"

# REST call to set auto-complete on Pull Request
$pullRequestUpdateUrl = ($projectUrl + '/pullRequests/' + $pullRequestId + '?api-version=5.1')

$setAutoCompleteResult = Invoke-RestMethod -Method PATCH -Headers $headers -Body $setAutoCompleteJson -Uri $pullRequestUpdateUrl

Write-Output "Pull request set to auto-complete"
```
Now, turn on the Continues integration in the Triggers tab. The pipeline will create an auto-complete enabled pull request to master automatically on every commit to the develop branch.

![Azure DevOps CI/CD Pipeline Trigger Enabled](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iupha99juln1oxfksfuc.png)