Search code examples
c#azure-devopsazure-repos

azure devops REST API: How to check pull request status


I'm trying to find if a PR is closed, abandoned, or active, merging, or with conflicts. I'm trying GetPullRequestStatusesAsync, but I don't get anything back. The test ids cover various states, but the Statuses Result is simply empty.

using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
using Microsoft.TeamFoundation.SourceControl.WebApi;

var collection = "https://myurl";
var project = "pro";
var repo = "repo";

var codepat = Environment.GetEnvironmentVariable("code_pat");
if (codepat.IsNullOrEmpty()) { throw new Exception($"code_pat must be set in the environment."); }

var creds = new VssBasicCredential(string.Empty, codepat);
var conn = new VssConnection(new Uri(collection), creds);
using var gitClient = conn.GetClient<GitHttpClient>();
var srcRepo = gitClient.GetRepositoryAsync(project, repo).Result;

int[] prids = { 2345, 3254, 7645, 1357 };

foreach(var id in prids)
{
    Console.WriteLine($"id: {id}");
    var result = gitClient.GetPullRequestStatusesAsync(srcRepo.Id, id).Result;
    foreach (var r in result)
    {
        Console.WriteLine($"- {r.Id}: {r.State}, {r.Description}");
    }
    Console.WriteLine();
}

Solution

  • I got what I needed with GetPullRequestByIdAsync (but it would be nice to know what GetPullRequestStatusesAsync should return).

    var pr = gitClient.GetPullRequestByIdAsync(id).Result;
    Console.WriteLine($"- {pr.Status}, {pr.MergeStatus}");
    
    • Active, Conflicts

    • Active, Succeeded

    • Completed, Succeeded

    • Abandoned, NotSet