如何以编程方式获取AzureDevOps中所有已完成PullRequest的列表?

时间:2018-08-26 12:07:31

标签: c# azure-devops pull-request programmatically

我正在使用以下代码来获取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;
        }

1 个答案:

答案 0 :(得分:4)

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

注意:从方法的定义尚不清楚,skip的默认值和顶级参数是什么(但是通过更改这些值,我每次可以得到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;
 }