azure-devops

AZDO API - Get Pull Request for Builds


I am stumped as to how to get the PR relating to each build. This is pretty critical stuff, as we need to know who approved each build (PR Approvals).

Have pulled the build data (https://dev.azure.com/{org}/{acc}/_apis/build/builds/{bid}?api-version=7.1)

.. but nothing in there that's obvious to me :(

Also done a lot of googling, maybe I'm not searching for the right terms ? One post mentioned that there was a parameter object on the build output with the PR id, but that was back on v5 of the API and does not appear for me now in v7.

Appreciate any wisdom.


Solution

  • To get the Pull Request (PR) information associated with a build in Azure DevOps, you're correct that it isn't directly exposed in the build API response in version 7.1. However, you can retrieve the PR information by leveraging the sourceVersion from the build data and then querying the PRs associated with that commit.

    Step 1: Get the sourceVersion from the Build

    From the build data you pulled using the API, look for the sourceVersion field. This represents the commit hash that triggered the build.

    Example response from the build API:

    {
      "id": 12345,
      "sourceVersion": "abcdef1234567890abcdef1234567890abcdef12",
      // other fields
    }
    

    Step 2: Query for PRs Associated with the Commit

    Now, using the sourceVersion (commit hash), you can query the Pull Request API to find the PR that contains that commit. You will need to call the Pull Requests API like this:

    API Endpoint:

    GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests?searchCriteria.sourceRefName=refs/heads/{sourceBranch}&searchCriteria.status=completed&api-version=7.1
    

    Alternatively, you can also filter by commit ID:

    GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests?searchCriteria.sourceCommitId={commitId}&api-version=7.1
    

    Replace {commitId} with the sourceVersion from the previous step.

    Step 3: Extract PR Details

    From the response of the PR API, you will get the PR that is associated with the build. This response will include details like PR ID, title, description, and approval details.

    Example PR response:

    {
      "value": [
        {
          "pullRequestId": 101,
          "title": "Fix bug in feature X",
          "status": "completed",
          "createdBy": {
            "displayName": "John Doe"
          },
          "reviewers": [
            {
              "displayName": "Jane Smith",
              "vote": 10 // Approved
            }
          ]
        }
      ]
    }
    

    In this response, you can see the reviewers and their approval statuses.

    Summary of Steps:

    1. Get the sourceVersion (commit hash) from the build details.
    2. Use the PR API to query for pull requests associated with that commit.
    3. Retrieve the reviewers and their approval status from the PR response.

    This will give you the PR that triggered the build, along with the approvals.