0

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:

enter image description here

Which I added as a pipeline variable:

enter image description here

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?

user989988
  • 3,006
  • 7
  • 44
  • 91
  • Do you see your token here: `Write-Host $token`? – Shamrai Aleksander Apr 07 '23 at 08:19
  • If you look at the results of your script, it's obvious that it's **not** creating the PR. The output is an HTML document, which usually indicates that authentication failed. You should use the `$(System.AccessToken)` variable in automation scripts, which provides you with an access token using the build service's identity as opposed to a user's access token which will eventually expire. I'd also be curious why you're having pipeline create pull requests, which feels to me like it's probably using automation to solve a process problem, when you should focus on solving the process problem. – Daniel Mann Apr 07 '23 at 17:01

0 Answers0