Reputation: 663
If I have a commit that was merged to 20 branches (can have 20 corresponding Pull requests), is there an API I can use to find the list of 20 commit Ids by using the original leaf commit Id as a parameter?
GetPullRequests(string commitId)
Ideally there would be a git rest API method to do this, but I can't seem to find it.
Thanks!
Upvotes: 4
Views: 5185
Reputation: 1479
I found this question and answer useful (even though I was using postman, not C#) because the microsoft doc, for my money, lacks a helpful explanation or example of what the input / body looks like for this request.
So, in addition to the answers here and the docs, this is how the input / resulting request body should be formatted:
{
"queries": [
{
"type": "commit",
"items": [ COMMIT_ID ]
}
]
}
This can be verified if you head to Azure DevOps > your repository > commits, and click on a commit that's part of a PR. When this page loads, the network tab shows a request to PullRequestQuery with the body/payload formatted as above.
Upvotes: 1
Reputation: 663
I was able to figure it out. We're supposed to use a PullRequestQuery and pass a list of commits as the input. It will return a dictionary of merged commit Ids for each commit we input. Below is the code to get all PRs for one commit Id.
public void GetAllPullRequestsForCommit(Guid repoId, string commitId)
{
var query = new GitPullRequestQuery();
var input = new GitPullRequestQueryInput() { Type = GitPullRequestQueryType.Commit, Items = new List<string>() { commitId } };
query.QueryInputs = new List<GitPullRequestQueryInput>() { input };
var response = _gitClient.GetPullRequestQueryAsync(query, repoId).Result;
var samplePullRequest = response.Results.SelectMany(x => x.Values).First().First().PullRequestId;
}
More information here
Upvotes: 6