azure-devopsazure-rest-api

Trying to get all PRs from "branch/*" using Rest API


I need to get all the pull requests from /hotfix branches to /main (e.g. refs/heads/hotfix/branch1, refs/heads/hotfix/branch2, etc. to /main).

https://dev.azure.com/{ORG}/{PROJECT}/_apis/git/pullrequests?searchCriteria.reviewerId={ID}&searchCriteria.status=completed&searchCriteria.sourceRefName=refs/heads/hotfix/&searchCriteria.targetRefName=refs/heads/main

I get no branches under hotfix, however if I put the specific branch under hotfix/, like "hotfix/branch" it works. I would like to get all branches that are under "refs/heads/hotfix".

https://learn.microsoft.com/es-es/rest/api/azure/devops/git/pull-requests/get-pull-requests?view=azure-devops-rest-7.1&tabs=HTTP

Tried with refs/heads/hotfix/* (this doesn't work). I also tried with &includeChildren=true , also doesn't work.


Solution

  • searchCriteria.targetRefName and searchCriteria.sourceRefName do not support wildcards. You can remove searchCriteria.sourceRefName from your request URL and filter based on the source branch in the response body. Refer to the example below.

    PowerShell scripts:

    $token = "{PAT}"
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
    $url="https://dev.azure.com/{Org}/{Project}/_apis/git/repositories/{RepoId}/pullrequests?searchCriteria.targetRefName=refs/heads/main&api-version=7.1-preview.1"
    $head = @{ Authorization =" Basic $token" }
    $response = Invoke-RestMethod -Uri $url -Method GET -Headers $head -ContentType application/json
    
    # Filter all PRs which source branch is `refs/heads/hotfix/*`
    $filteredPullRequests = $response.value | Where-Object { $_.sourceRefName -like "refs/heads/hotfix/*" }
    $filteredPullRequests
    

    Results:

    enter image description here