예제 #1
0
        public async Task <string> GetLatestVersionAsync(string owner, string repository)
        {
            string key = $"{owner}/{repository}";

            if (LocalCache.ContainsKey(key))
            {
                _streamer.PushLine <MemoryCacheVersionProvider>(new StreamOptions
                {
                    Level    = LogLevel.Debug,
                    Messages = new StreamMessageBuilder()
                               .Add($"{owner}/{repository} latest release is retrieved from cache").Build()
                });
            }
            else
            {
                await _semaphore.WaitAsync();

                try
                {
                    if (!LocalCache.ContainsKey(key))
                    {
                        LocalCache.Add(key, _provider.GetLatestVersionAsync(owner, repository));
                    }
                }
                finally
                {
                    _semaphore.Release();
                }
            }

            return(await LocalCache[key]);
        }
예제 #2
0
        public IDictionary <WorkflowInfo, IEnumerable <GitHubAction> > GetOutdated(IEnumerable <WorkflowInfo> items)
        {
            int totalCount = items
                             .SelectMany(wfi => wfi.Workflow.Jobs)
                             .SelectMany(j => j.Value.Steps)
                             .Count(s => s.Action.IsValidForUpgrade);

            OnRepositoryCheckedStarted(totalCount);
            var index = 0;
            Dictionary <WorkflowInfo, IEnumerable <GitHubAction> > result = items
                                                                            .AsParallel()
                                                                            .ToDictionary(
                wfi => wfi,
                wfi => wfi.Workflow.Jobs
                .SelectMany(job => job.Value.Steps)
                .AsParallel()
                .Select(step => step.Action)
                .Where(action => action.IsValidForUpgrade)
                .Select(async action =>
            {
                if (action.LatestVersion == null)
                {
                    await _semaphore.WaitAsync();
                    try
                    {
                        if (action.LatestVersion == null)
                        {
                            OnRepositoryChecked(++index, totalCount);
                            action.LatestVersion = await _provider.GetLatestVersionAsync(action.Owner, action.Repository);
                        }
                    }
                    catch (GitHubVersionNotFoundException e)
                    {
                        _streamer.PushLine <GitHubService>(new StreamOptions
                        {
                            Exception = e,
                            Level     = LogLevel.Warning,
                            Messages  = new StreamMessageBuilder().Add(e.Message, ConsoleColor.Yellow).Build()
                        });
                    }
                    catch (Exception e)
                    {
                        _streamer.PushLine <GitHubService>(new StreamOptions
                        {
                            Exception = e,
                            Level     = LogLevel.Error,
                            Messages  = new StreamMessageBuilder().Add(e.Message, ConsoleColor.Red).Build()
                        });
                    }
                    finally
                    {
                        _semaphore.Release();
                    }
                }

                return(action);
            })
                .Select(t => t.Result)
                .Where(action => !action.IsUpToDate))
                                                                            .Where(p => p.Value.Any())
                                                                            .ToDictionary(p => p.Key, p => p.Value.AsEnumerable());

            OnRepositoryCheckedFinished();
            return(result);
        }