Fetch AzureDevops build details by Pull Request

Viewed 127

My requirement is to tell the user if his/her PR got deployed successfully or not So I thought,

  1. I can ask user about his PR#
  2. Using PR# - Fetch Build Details
  3. Using BuildID from Step 2, find deployment information

I stuck at step 2 I am using below documentation to fetch build details. https://docs.microsoft.com/en-us/rest/api/azure/devops/build/builds/list?view=azure-devops-rest-6.0

Not able to find correct usage of reasonFilter parameter to pass pull request id to fetch build details API URL Info

Build Reson

Can someone help how exactly to use reasonFilter parameter here tried this _apis/build/builds?api-version=5.1&$top=1&reasonFilter=pullrequest=20056

2 Answers

Regarding the point you're stuck with: The reasonFilter is simply a number of predefined string values you can choose from. For instance, if you specify reasonFilter=pullrequest, you get all builds started by a pull request. Thus, you can't specify the exact PR ID here.

Regarding your requirement in general: I don't think there's an API to strictly tie the PR ID and its build(s). You should look into the Status API instead. The build is just one of the statuses that can be associated with the pull request or, more specifically, with the commit.

So, I would approach your task the following way (note that it's not an exact algorithm, but a number of steps I would try to find the solution):

  • get the pull request by the PR ID provided by user
  • find the lastMergeCommit in the response
  • get the statuses of that merge commit
  • investigate the collection of GitStatus objects returned back and find out how to filter the build you're looking for (I don't know the format of that response for sure, but I'm confident there's some attribute to judge by)

Hope this can lead you to the right direction.

how exactly to use reasonFilter parameter here tried this _apis/build/builds?api-version=5.1&$top=1&reasonFilter=pullrequest=20056

Based on your requirement, you need to filter the build via build reason and Pull Request ID.

I'm afraid there is no out-of-box parameter to filter by pull request ID in the Rest API.

For a workaround, you can try to add the tag to your build.

Here are the steps:

  1. You can add a step in your build to use Pull Request ID as Build tag. Refer to this doc: AddBuildTag: Add a tag to the build

For example:

- powershell: |
  
   
   Write-Host "##vso[build.addbuildtag]$env:SYSTEM_PULLREQUEST_PULLREQUESTID"
   
  displayName: 'PowerShell Script'
  condition: eq(variables['Build.reason'], 'pullrequest')

You can set the condition to make sure that when the pipeline is triggered by pull request, it will add build tag.

  1. You can add the tagFilters in Rest API to filter the related build.

For example:

Get https://dev.azure.com/org/project/_apis/build/builds?reasonFilter=pullrequest&tagFilters=PullrequestID&api-version=6.0
Related