PiJei
PiJei

Reputation: 604

How to programmatically get the list of all the Completed PullRequests in AzureDevOps?

I am using the following code to get the list of All the completed pull requests on a VSTS repository. However, the list of pull requests that is fetched, contains only a limited list of the pull requests, and not all of them. Any idea what am I doing wrong?

Here is the code:

        /// <summary>
        /// Gets all the completed pull requests that are created by the given identity, for the given repository
        /// </summary>
        /// <param name="gitHttpClient">GitHttpClient that is created for accessing vsts.</param>
        /// <param name="repositoryId">The unique identifier of the repository</param>
        /// <param name="identity">The vsts Identity of a user on Vsts</param>
        /// <returns></returns>
        public static List<GitPullRequest> GetAllCompletedPullRequests(
            GitHttpClient gitHttpClient, Guid repositoryId, Identity identity)
        {
            var pullRequestSearchCriteria = new GitPullRequestSearchCriteria
            {
                Status = PullRequestStatus.Completed,
                CreatorId = identity.Id,
            };

            List<GitPullRequest> allPullRequests =  gitHttpClient.GetPullRequestsAsync(
                repositoryId,
                pullRequestSearchCriteria).Result;

            return allPullRequests;
        }

Upvotes: 1

Views: 2040

Answers (1)

PiJei
PiJei

Reputation: 604

It turns out that by default, this call to getting pull requests, will only return a limited number of pull requests ( in my case was 101). What you need to do, is to rather specify both skip, and top parameters which are stated as optional in GetPullRequestsAsync method's signature definition. The following code shows how to use these parameters, to return all pull requests:

Note: It is not clear from the definition of the method, what are the default values for skip, and top parameters ( but by varying these values I could get 1000 at each time).

/// <summary>
/// Gets all the completed pull requests that are created by the given identity, for the given repository
/// </summary>
/// <param name="gitHttpClient">GitHttpClient that is created for accessing vsts.</param>
/// <param name="repositoryId">The unique identifier of the repository</param>
/// <param name="identity">The vsts Identity of a user on Vsts</param>
/// <returns></returns>
public static List<GitPullRequest> GetAllCompletedPullRequests(
     GitHttpClient gitHttpClient, Guid repositoryId, Identity identity)
 {
     var pullRequestSearchCriteria = new GitPullRequestSearchCriteria
     {
         Status = PullRequestStatus.Completed,
         CreatorId = identity.Id,
     };
     List<GitPullRequest> allPullRequests = new List<GitPullRequest>();
     int skip = 0;
     int threshold = 1000;
     while(true){
         List<GitPullRequest> partialPullRequests =  gitHttpClient.GetPullRequestsAsync(
             repositoryId,
             pullRequestSearchCriteria,
             skip:skip, 
             top:threshold 
             ).Result;
         allPullRequests.AddRange(partialPullRequests);
         if(partialPullRequests.Length < threshold){break;}
         skip += threshold
    }
     return allPullRequests;
 }

Upvotes: 5

Related Questions