简体   繁体   中英

How can I get all pull requests Ids (from merged commits) for a given commit Id using VSTS Git API for C#

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!

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM