Exemplo n.º 1
0
        public async Task <IList <PullRequest> > GetPullRequestsRootAsync(ItemState stateFilter, DateTimeOffset lastCheck)
        {
            var prStateFilters = stateFilter.GetBbCloudStateFilter();
            var updateAtFilter = lastCheck.GetBbCloudUpdatedAtFilter(_clock);
            var completeArgs   = BitBucketCloudExtensions.BuildFullQuery(new[] { prStateFilters, updateAtFilter }, " AND ");

            var encoded = WebUtility.UrlEncode(completeArgs);
            var url     = $"{_prApiUrl}?q={encoded}";

            // Get the first page: https://bitbucket.org/!api/2.0/repositories/{repoOwner}/{repoName}/pullrequests
            // Follow `next` until it's null
            _logger.LogInformation($"Pulling first page of {_repoTag}");
            var timer = Stopwatch.StartNew();
            var page  = await GetBitbucketPullRequests(url);

            var aggregatePrs = new List <BitbucketPullRequest>(page.PullRequests);

            if (!string.IsNullOrWhiteSpace(page.next))
            {
                _logger.LogInformation($"Approximately {page.size:N0} pull requests to fetch in pages of size {page.pagelen:N0}");
                var requestsToMake = page.size / (float)page.pagelen;
                var approxPages    = Convert.ToInt32(Math.Ceiling(requestsToMake));
                var counter        = 0;
                var next           = page.next;
                while (!string.IsNullOrWhiteSpace(next))
                {
                    _logger.LogInformation($"{_repoTag} - pulling page {++counter:N0} / ~{approxPages:N0}");
                    var pageTimer = Stopwatch.StartNew();
                    var nextPage  = await GetBitbucketPullRequests(next);

                    pageTimer.Stop();
                    next = nextPage.next;
                    aggregatePrs.AddRange(nextPage.PullRequests);
                    _logger.LogInformation($"{_repoTag} - page {counter:N0} / ~{approxPages:N0} pulled in {pageTimer.ElapsedMilliseconds:N0}ms");
                }
            }
            timer.Stop();
            _logger.LogInformation($"{aggregatePrs.Count:N0} discovered in {timer.ElapsedMilliseconds:N0}ms");

            var pullRequests = aggregatePrs
                               .Select(bbPr => bbPr.ToPullRequest())
                               .ToList();

            return(pullRequests);
        }