I have the following script to create a PR via Azure DevOps automatically:
CreatePRBuildTask.ps1
$user = ""
$branchTarget = "refs/heads/main"
$branchSource = "refs/heads/develop"
$branchTargetPath = $branchTarget -replace "refs/heads/", ""
$teamProject = "Project"
$repoName = "Repo"
$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=$branchTargetPath&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"
Write-Host $token
Write-Host $uriBranchStatus
Write-Host $uriCheckActivePR
Write-Host $uriCreatePR
# $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
Here is the PAT:
Which I added as a pipeline variable:
Here is my YAML:
name: 1.0
trigger:
- develop
- main
pool:
vmImage: "windows-latest"
variables:
"Token": $(CUSTOM_ACCESSTOKEN)
stages:
- template: build.yml
parameters:
token: $(Token)
build.yml
parameters:
- name: token
type: string
stages:
- stage: Build
jobs:
- job: Build
steps:
- task: PowerShell@2
displayName: 'PowerShell Script'
inputs:
targetType: filePath
filePath: ./Scripts/CreatePRBuildTask.ps1
arguments: '-token ${{ parameters.token }}'
I see this on running the build and it says Created PR but NO PR is created. What am I missing?
2023-03-09T20:37:12.4816590Z Generating script.
2023-03-09T20:37:12.4858807Z Formatted command: . 'D:\a\1\s\Scripts\CreatePRBuildTask.ps1' -token ***
2023-03-09T20:37:12.5727397Z ========================== Starting Command Output ===========================
2023-03-09T20:37:12.5943002Z ##[command]"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\e2c233da-bd23-4503-ae27-583e3bd96d7d.ps1'"
2023-03-09T20:37:12.8567819Z uriBranchStatus https://xxx/stats/branches?name=main&api-version=5.1
2023-03-09T20:37:12.8575187Z uriCheckActivePR https://xxx/pullrequests?searchCriteria.targetRefName=refs/heads/main&searchCriteria.sourceRefName=refs/heads/develop&api-version=5.1
2023-03-09T20:37:12.8581741Z uriCreatePR https://xxx/pullrequests?api-version=5.1
2023-03-09T20:37:13.4448437Z Created PR
2023-03-09T20:37:13.4448701Z
2023-03-09T20:37:13.4449812Z <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2023-03-09T20:37:13.4450257Z
2023-03-09T20:37:13.4450582Z
2023-03-09T20:37:13.4450762Z
2023-03-09T20:37:13.4450865Z
2023-03-09T20:37:13.4451054Z <html lang="en-US">
2023-03-09T20:37:13.4451370Z <head><title>
2023-03-09T20:37:13.4451555Z
2023-03-09T20:37:13.4451777Z Azure DevOps Services | Sign In
2023-03-09T20:37:13.4451996Z
2023-03-09T20:37:13.5697759Z ##[section]Finishing: PowerShell Script
Looks like it is unable to read the token. How should I use PAT in YAML? How do I fix that?