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

问题描述 投票:1回答:1

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

这是代码:

        /// <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;
        }
c# azure-devops pull-request programmatically
1个回答
4
投票

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

注意:从方法的定义中可以看出,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;
 }
© www.soinside.com 2019 - 2024. All rights reserved.