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

PiJ*_*Jei 1 c# pull-request azure-devops 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;
        }
Run Code Online (Sandbox Code Playgroud)

PiJ*_*Jei 5

事实证明,默认情况下,这个获取拉取请求的调用只会返回有限数量的拉取请求(在我的例子中是 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;
 }
Run Code Online (Sandbox Code Playgroud)