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 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 for the pull request creation and Update PR 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.
PowerShell Script
# 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.
Top comments (4)
Nice!
Another option is to use the "Create Pull Request" extension that does it :)
marketplace.visualstudio.com/items...
(Disclaimer: I'm the author)
I am unable run the pipeline successfully. It give me below error
@rineshpk please help on this
Hi after autocomplete it's still need to mark it as complete (Complete Merge), is there a way to automate.