Exemplo n.º 1
0
 private RepositoryManager(
     string repoOwner,
     string repoName,
     string repoUrl,
     string fullName,
     IRepoPullRequestReader prReader,
     IPullRequestCacheManager cacheManager,
     TimeSpan prApiDosBuffer,
     Dictionary <int, PullRequest> byNumber,
     ILogger logger)
 {
     RepoOwner     = repoOwner;
     RepoName      = repoName;
     RepoUrl       = repoUrl;
     _fullName     = fullName;
     _prReader     = prReader;
     _cacheManager = cacheManager;
     _dosBuffer    = prApiDosBuffer;
     _logger       = logger;
     _byNumber     = byNumber;
 }
Exemplo n.º 2
0
        public static async Task <IRepoManager> InitializeAsync(
            string repoOwner,
            string repoName,
            string repoUrl,
            IRepoPullRequestReader prReader,
            IPullRequestCacheManager cacheManager,
            TimeSpan prApiDosBuffer,
            bool refreshFromUpstream,
            ILogger logger)
        {
            if (string.IsNullOrWhiteSpace(repoOwner))
            {
                throw new ArgumentNullException(nameof(repoOwner));
            }

            if (string.IsNullOrWhiteSpace(repoName))
            {
                throw new ArgumentNullException(nameof(repoName));
            }

            if (string.IsNullOrWhiteSpace(repoUrl))
            {
                throw new ArgumentNullException(nameof(repoUrl));
            }

            if (!Uri.TryCreate(repoUrl, UriKind.Absolute, out var _))
            {
                throw new ArgumentException($"'{repoUrl}' is not a valid URL");
            }

            if (prReader is null)
            {
                throw new ArgumentNullException(nameof(prReader));
            }

            if (cacheManager is null)
            {
                throw new ArgumentNullException(nameof(cacheManager));
            }

            if (prApiDosBuffer < TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(prApiDosBuffer));
            }

            if (logger is null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            var fullName = $"{repoOwner}:{repoName}";

            logger.LogInformation($"{fullName} initializing the repository manager");
            var timer = Stopwatch.StartNew();

            IList <PullRequest> prs = new List <PullRequest>();

            try
            {
                logger.LogInformation($"{fullName} reading the cache");
                prs = await cacheManager.LoadAsync(repoOwner, repoName);
            }
            catch (Exception)
            {
                logger.LogInformation($"{fullName} cache file does not exist, therefore one will be created.");
            }
            var byNumber = prs.ToDictionary(pr => pr.Number);

            timer.Stop();
            logger.LogInformation($"{fullName} initialized the cache with {byNumber.Count:N0} pull requests in {timer.ElapsedMilliseconds:N0}ms");

            var repoHistoryMgr = new RepositoryManager(repoOwner, repoName, repoUrl, fullName, prReader, cacheManager, prApiDosBuffer, byNumber, logger);

            if (!refreshFromUpstream)
            {
                return(repoHistoryMgr);
            }

            // If there's nothing in the cache, go look for stuff.
            await repoHistoryMgr.RefreshFromUpstreamAsync(ItemState.Closed);

            return(repoHistoryMgr);
        }