Search code examples
azurepowershellazure-devopsbranchpull-request

In Azure Dev Ops, given a branch name, how I do I get the corresponding Pull Request (if one exists)


In Azure Dev Ops, given a branch name, how I do I get the corresponding Pull Request (if one exists).

I have a local branch named FooBranch which I've pushed to the remote repo BarRepo which lives in Azure DevOps. I created a Pull Request in the BarRepo repo from the branch FooBranch.

The is the branch's Url https://{org}.visualstudio.com/{project}/_git/BarRepo?version=GBFooBranch

The PR url looks like this https://{org}.visualstudio.com/{project}/_git/BarRepo/pullrequest/{prID} where {prID} is an 8-digit number.

Given the branch BarRepo how do I determine {prID}? I would like to do this in a PowerShell script if possible. Maybe thru the Azure CLI. Or via an HTTP req.


I know not all branches will have an associated PR. Can a branch be in several PRs?


Solution

  • I know not all branches will have an associated PR. Can a branch be in several PRs?

    Yes, a branch can be linked to multiple PRs if it's used as the source for different target branches or if multiple PRs are created from it. For example, a feature branch might have PRs for both the development and release branches, each with a different PR ID but sharing the same source branch.

    Given the branch BarRepo how do I determine {prID}? I would like to do this in a PowerShell script if possible. Maybe the Azure CLI. Or via an HTTP req.

    1. Using Azure CLI az repos pr list sample PowerShell task:
    steps:
      - powershell:  |
          $organization = ""
          $project = ""
          $repositoryName= ""
          $sourcebranch = ""     
    
          $prs = az repos pr list --organization https://dev.azure.com/$organization --project $project --repository $repositoryName --source-branch $sourcebranch --output json
    
          # Parse the JSON response
          $prsJson = $prs | ConvertFrom-Json
    
          # Check if any PRs are found
          if ($prsJson.Count -eq 0) {
              Write-Output "No Pull Requests found for branch: $sourcebranch"
          } else {
              # Get all PR IDs
              $prIds = $prsJson.pullRequestId
              Write-Output "Pull Request IDs: $prIds"
          }
        displayName: 'az repos pr list'
        env:
          AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
    
    

    Test result:

    result1

    1. Using REST API Pull Requests - Get Pull Requests sample PowerShell task:
    steps:
      - task: PowerShell@2
        displayName: 'REST API'
        inputs:
          targetType: 'inline'
          script: |
            # Set variables
            $organization = ""
            $project = ""
            $repositoryName= ""
            $sourcebranch = ""
            
            # Create the API URL
            $apiUrl = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repositoryName/pullrequests?searchCriteria.sourceRefName=refs/heads/$sourcebranch&api-version=7.1-preview.1"
            
            # Make the HTTP GET request
            $response = Invoke-RestMethod -Uri $apiUrl -Method Get -Headers @{Authorization = "Bearer $(System.AccessToken)"} 
            
            # Check if any PRs are found
            if ($response.value.Count -eq 0) {
                Write-Output "No Pull Requests found for branch: $sourcebranch"
            } else {
                # Get all PR IDs
                $prIds = $response.value.pullRequestId
                Write-Output "Pull Request IDs: $prIds"
            }
    

    Test result:

    result