2
votes

We are trying to gather information about our pull requests to establish certain metrices (how long does it take until a pull request is completed, ...) for our department.

Azure DevOps provides an API to query a lot of stuff, including pull requests. I looked up the usage here: https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pull%20requests/get%20pull%20request?view=azure-devops-rest-6.0 I have also looked into this client provided by Microsoft: https://docs.microsoft.com/en-us/dotnet/api/microsoft.teamfoundation.sourcecontrol.webapi.githttpclient?view=azure-devops-dotnet

I have the issue that it seems like my results I get through both the API and the GitHttpClient are not matching with the information I think should exist. One of our latest pull requests has the ID 1086. I assume, this means that there have been already 1085 pull requests before and this is the 1086th pull request.

When I query the API, I get weird results: Using the API, without any filter criteria I get 5 pull requests. Using the filter criteria "status completed" I get 101 pull requests. Using top = 5000 still delivers 101. Using the GitHttpClient I get different results. Without any filter, I get 4. Using the filter criteria "status completed" I get 101 pull requests. Using top = 5000 I get 490.

That's still not 1086. I don't want to go through all the pull requests in Azure DevOps manually and check whether the API returns the correct number of pull requests or not. So, how is the pull request id calculated? Is it just counting up? Are there actually 1086 pull requests and the api / client just delivers less? Are the gaps in the pull request ids and there are not actually 1086 pull requests?

The personal access token I am using has the required permissions. I have even tried with an access token that has all the permissions and still the same results. So that should not be the issue.

So is there a good and easy / quick way to verifiy whether the data I get from the API / GitHttpClient is complete? Also why do I have to put top=x to get more than 101 results. To my understanding, this is not how rest apis work...

Code Samples API:

public class Client
{
    private const string _azureDevopsUri = "https://dev.azure.com/companyName";
    private const string _query = "projectName/_apis/git/pullrequests?searchCriteria.status=completed&api-version=6.0";

    public async Task<PullRequests> GetPullRequestReport(DateTime? fromTime, DateTime? toTime)
    {
        string personalAccessToken = "HereIsMyToken";
        string credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalAccessToken)));

        using (var httpClient = new HttpClient())
        {
            httpClient.BaseAddress = new Uri(_azureDevopsUri);
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);

            var response = await httpClient.GetAsync(_query);
            if (response.IsSuccessStatusCode)
            {
                var responseContentAsJsonString = await response.Content.ReadAsStringAsync();
                var pullRequests = JsonConvert.DeserializeObject<PullRequests>(responseContentAsJsonString);

GitHttpClient

public class Client
{
    private const string _azureDevopsUri = "https://dev.azure.com/companyName";

    public async Task<PullRequests> GetPullRequestReport(DateTime? fromTime, DateTime? toTime)
    {
        string personalAccessToken = "HereIsMyToken";

        var connection = new VssConnection(new Uri(_azureDevopsUri), new VssBasicCredential(string.Empty, personalAccessToken));
        var gitHttpClient = connection.GetClient<GitHttpClient>();

        var pullRequestsGit = await gitHttpClient.GetPullRequestsByProjectAsync(projectName", new GitPullRequestSearchCriteria { Status = PullRequestStatus.Completed }, top: 5000);
1

1 Answers

2
votes

I don't think you need "searchCriteria", and you need dollar signs for "top" and "skip".

For example, this works for me:

https://dev.azure.com/accountname/project/_apis/git/repositories/repo/pullRequests?api-version=3.0&$top=1000&$skip=0&status=Completed

Does that get you the top 1000?