Azure DevOps: find the pull request that brought a commit in
You have a commit ID, or a line in blame that looks wrong, and you want the pull request it came through: the discussion, who approved it, the linked work items. Azure DevOps answers this with one REST call, Pull Request Query. Below are the call, which of its two query types you need, a git command for when the API is not at hand, and the same thing in IntelliJ IDEA. From Microsoft's REST reference, Pull Request Query - Get, and Complete a pull request.
The REST call: Pull Request Query
POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequestquery?api-version=7.1- The body is a list of queries, each with a type and a list of commit IDs:
{"queries": [ {"type": "commit", "items": ["<commit ID>"]}, {"type": "lastMergeCommit", "items": ["<commit ID>"]} ]} - The answer's
resultslist is in the same order asqueries. Each entry maps every commit ID you asked about to the pull requests found for it. - The token needs Code (Read) (the
vso.codescope).
commit or lastMergeCommit
- commit finds the pull requests that merged the commit: the commit was one of the pull request's own commits, on its source branch.
- lastMergeCommit finds the pull request that created the commit as its merge commit.
- Which one finds your commit depends on how the pull request was completed. With a merge (no fast-forward), the branch's commits reach the target branch as they are, so a blame line on
mainusually shows one of them, and commit finds it. With a squash merge, only one new commit reaches the target branch; blame shows that new commit, and only lastMergeCommit finds it. - Sending both queries in one call, as in the body above, covers both cases. The same pull request can come back from both; keep it once.
Without the API: git
When pull requests are completed with a merge commit, you can find the merge on the target branch that brought a commit in, then ask Pull Request Query with lastMergeCommit and that merge's ID. In Bash (Git Bash on Windows), replace <commit>:
for m in $(git rev-list --first-parent --merges --reverse origin/main); do
git merge-base --is-ancestor <commit> $m && { git log -1 --oneline $m; break; }
done
It walks the merges of origin/main from the oldest and prints the first one that contains the commit. The shorter git log --merges --ancestry-path <commit>..origin/main can stop at the wrong merge when main was merged into the branch after the commit; with --first-parent added it can print nothing for the same reason. After a squash merge the commit is not on main at all, so this finds nothing: use the commit query above with the original commit ID.
Find Pull Request on Azure DevOps in IntelliJ IDEA
Azure DevOps Pull Requests, a plugin by Lesstep for JetBrains IDEs 2024.1 or later and Android Studio, lists the pull requests of Azure DevOps Services and Azure DevOps Server in an IDE tool window. From version 1.0.104, Find Pull Request on Azure DevOps is on the annotate (blame) gutter and on a commit in the Git log. It sends both queries above and opens the pull request that brought the commit in; when there are several it lists them to choose from, completed ones first, and when there is none it says so. An active one that is in your list opens in the tool window, where voting, commenting and completing are. Finding the pull request is free; voting, commenting, completing and creating pull requests are Pro, with a 30-day trial.
The plugin named Find Pull Request on JetBrains Marketplace, by another developer, lists GitHub, GitLab and Bitbucket as the services it supports; Azure DevOps is not among them.
Get it on JetBrains Marketplace
Azure DevOps is a trademark of Microsoft Corporation; IntelliJ IDEA is a trademark of JetBrains s.r.o. This page is not affiliated with or endorsed by Microsoft or JetBrains.
More: merge types · revert and cherry-pick · work items linked to a pull request · 日本語