如何以编程方式获取 Azure DevOps 中所有已完成的拉取请求的列表?

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

我正在使用以下代码获取 VSTS 存储库中 所有 已完成拉取请求的列表。但是,获取的拉取请求列表仅包含有限的拉取请求列表,而不是全部。知道我做错了什么吗?

代码如下:

        /// <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;
        }

事实证明,默认情况下,这个获取拉取请求的调用只会 return 有限数量的拉取请求(在我的例子中是 101)。您需要做的是同时指定 skip 和 top 参数,这些参数在 GetPullRequestsAsync 方法的签名定义中声明为可选。以下代码显示如何使用这些参数,以 return 所有拉取请求:

注意:从方法的定义中不清楚,skip 和 top 参数的默认值是多少(但通过改变这些值我可以得到 1000每一次)。

/// <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;
 }