Mavi Domates
Mavi Domates

Reputation: 4521

Azure DevOps Rest API - List pull requests by tag/label

Is there any way to query Azure DevOps' REST API to return me a list of pull requests with a certain tag / label?

Been looking into the documentation here without much help: https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull%20requests/get%20pull%20requests?view=azure-devops-rest-6.0

Upvotes: 0

Views: 2831

Answers (1)

Walter
Walter

Reputation: 3058

According to the Get Pull Requests documentation, we can add some search criteria in the URI Parameters, but currently label is not available. If you would like that feature, please use this link and create a request for this feature. That will allow you to directly interact with the appropriate product group, and make it more convenient for the product group to collect and categorize your suggestions.

As a workaround, we can filter the results of the API. Please check if the following powershell script meets your needs. I used Get Pull Requests api and Get Pull Request By Id api in my sample (Please change the commented value):

$organization = "{organization name}" // organization name
$project = "{project name}"  // project name
$repo = "{repo name}" // repo name
$pat = "{PAT}"  //PAT
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))
$baseUrl = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repo /pullrequests?&api-version=6.0" 
$pullrequestlist = Invoke-RestMethod -Uri $baseUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Method GET 


$pullrequestid = @()
foreach($prid in $pullrequestlist.value){
$childprid = $prid.labels | Where-Object{$_.name -eq "{label name}" } | select $prid.url  //label name
$pullrequestid +=  $childprid | foreach{$prid.url.split("/")[-1]}
}


foreach($prid2 in $pullrequestid){
write-host $prid2  
$baseUrl2 = "https://dev.azure.com/$organization/$project/_apis/git/pullrequests/$($prid2)?api-version=6.0"
$pullrequestlist2 = Invoke-RestMethod -Uri $baseUrl2 -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Method GET
write-host $pullrequestlist2
} 

Here is my result: enter image description here

Upvotes: 1

Related Questions