In AzureDevOps, we have three branches:
- feature
- develop
- main
We create a Pull Request (PR) from feature to develop. Once the PR is complete, we create a PR from develop to main. Is there a way to create a PR from develop to main automatically when the feature to develop PR is complete?
UPDATE V1:
I added a task in YAML as follows:
- task: PowerShell@2
displayName: 'PowerShell Script'
inputs:
targetType: filePath
filePath: ./Scripts/CreatePRBuildTask.ps1
arguments: '-token $(System.AccessToken)'
-- CreatePRBuildTask.ps1
$user = "PrivateToken"
$branchTarget = "refs/heads/main"
$branchSource = "refs/heads/develop"
$branchTragetPath = $branchTarget -replace "refs/heads/", ""
$teamProject = "team"
$repoName = "$(Build.Repository.Name)"
$organization = "org"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$uriBranchStatus = "https://dev.azure.com/$organization/$teamProject/_apis/git/repositories/$repoName/stats/branches?name=$branchTragetPath&api-version=5.1"
$uriCheckActivePR = "https://dev.azure.com/$organization/$teamProject/_apis/git/repositories/$repoName/pullrequests?searchCriteria.targetRefName=$branchTarget&searchCriteria.sourceRefName=$branchSource&api-version=5.1"
$uriCreatePR = "https://dev.azure.com/$organization/$teamProject/_apis/git/repositories/$repoName/pullrequests?api-version=5.1"
$resultStatus = Invoke-RestMethod -Uri $uriBranchStatus -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
if ($resultStatus.behindCount -eq 0)
{
Write-Host "Current branch contains last changes from master"
Return
}
$resultActivePR = Invoke-RestMethod -Uri $uriCheckActivePR -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
if ($resultActivePR.count -gt 0)
{
Write-Host "PR exists already"
Return
}
$bodyCreatePR = "{sourceRefName:'$branchSource',targetRefName:'$branchTarget',title:'Sync changes from $branchSource'}"
$result = Invoke-RestMethod -Uri $uriCreatePR -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $bodyCreatePR
Write-Host "Created PR" $result.pullRequestId
Looks like it is erroring out. What am I doing wrong?
UPDATE V2:
I see the script running successfully:
However, I see the log 'PR exists already' log even though there are no active PRs with 'develop' as source branch and 'main' as target branch.
On trying to run this part of script:
$bodyCreatePR = "{sourceRefName:'$branchSource',targetRefName:'$branchTarget',title:'Sync changes from $branchSource'}"
$result = Invoke-RestMethod -Uri $uriCreatePR -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $bodyCreatePR
Write-Host "Created PR" $result.pullRequestId
I see:
no pull request being created. What am I missing?
Here is my YAML file:
- task: PowerShell@2
displayName: 'PowerShell Script'
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
inputs:
targetType: filePath
filePath: ./Scripts/CreatePRBuildTask.ps1
arguments: '-token ${{ parameters.token }}'