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"

$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

This creates the PR. $result.createdBy.url show a page containing this info:

enter image description here

From where do I get the actual link to the created PR?

user989988
  • 3,006
  • 7
  • 44
  • 91
  • 1
    Is this what you want? `https://dev.azure.com/$organization/$teamProject/_git/$repoName/pullrequest/$result.pullRequestId` – jdfa Apr 10 '23 at 18:36

0 Answers0