private async Task IndexProjectAsync(
            SearchCode repoInfo,
            ConcurrentDictionary <PackageReference, HashSet <RepoInfo> > graph,
            IScanProgress scanProgress)
        {
            var projectContent = await _client.Repository.Content.GetAllContents(repoInfo.Repository.Id, repoInfo.Path);

            var repo      = RepoInfo.Parse(repoInfo.Repository.Name, repoInfo.HtmlUrl);
            var nugetRefs = ProjectFileParser.Parse(projectContent[0].Content);

            Console.WriteLine($"Repo name {repoInfo.Repository.Name} file name {repoInfo.Name}");

            foreach (var nugetRef in nugetRefs)
            {
                if (!graph.TryGetValue(nugetRef, out var repoInfos))
                {
                    repoInfos = new HashSet <RepoInfo>();
                }

                repoInfos.Add(repo);
                graph[nugetRef] = repoInfos;
            }

            scanProgress.UpdateProjectProgress();
        }
        private void IndexProject(
            SrcFileInfo searchFile,
            ConcurrentDictionary <PackageReference, HashSet <RepoInfo> > graph,
            IScanProgress scanProgress)
        {
            var fileName = Path.GetFileName(searchFile.path);

            if (!fileName.EndsWith(".csproj"))
            {
                return;
            }

            var repoSlug = ExtractSlugFromUrl(searchFile.links.self.href);

            if (_skipRepos.Contains(repoSlug))
            {
                Console.WriteLine($"Skipped {repoSlug}");
                return;
            }

            var repoResource = _client.RepositoriesEndPoint().RepositoryResource(_bbAccount, repoSlug);

            if (!_reposCache.TryGetValue(repoSlug, out var repo))
            {
                var repoInfo = repoResource.GetRepository();
                var fileLink = $"{repoInfo.links.html.href}/src/master/{searchFile.path}";
                repo = RepoInfo.Parse(repoInfo.slug, fileLink);
                _reposCache.Add(repoSlug, repo);
                scanProgress.UpdateRepoProgress();
            }

            var projectContent = repoResource.SrcResource().GetFileContent(searchFile.path);
            var nugetRefs      = ProjectFileParser.Parse(projectContent);

            foreach (var nugetRef in nugetRefs)
            {
                if (!graph.TryGetValue(nugetRef, out var repoInfos))
                {
                    repoInfos = new HashSet <RepoInfo>();
                }

                repoInfos.Add(repo);
                graph[nugetRef] = repoInfos;
            }

            scanProgress.UpdateProjectProgress();

            Console.WriteLine($"Processed {fileName} from {repo.Name}");
        }
예제 #3
0
        private async void Scan(object state)
        {
            _graph.Clear();
            var client = new GitHubClient(new ProductHeaderValue("MyAmazingApp2"), new InMemoryCredentialStore(new Credentials(_apiKey)));

            var repoClient = client.Repository;

            var scr = new SearchCodeRequest("PackageReference Lykke")
            {
                Organization = "LykkeCity",
                Extension    = "csproj"
            };

            try
            {
                _status = "Scanning";
                var searchResult = await client.Search.SearchCode(scr);

                _packagesFound = searchResult.TotalCount;

                for (int i = 0; i < _packagesFound; i += 100)
                {
                    var pageNumber = i / 100;
                    scr = new SearchCodeRequest("PackageReference Lykke")
                    {
                        Organization = "LykkeCity",
                        Extension    = "csproj",
                        Page         = pageNumber,
                    };

                    searchResult = await client.Search.SearchCode(scr);

                    Console.WriteLine($"Page {pageNumber} received {searchResult.Items.Count}");


                    foreach (var item in searchResult.Items)
                    {
                        var projectContent = await repoClient.Content.GetAllContents(item.Repository.Id, item.Path);

                        var repo      = RepoInfo.Parse(item.Repository.FullName, item.Repository.HtmlUrl);
                        var nugetRefs = ProjectFileParser.Parse(projectContent[0].Content);

                        Console.WriteLine($"Repo name {item.Repository.Name} file name {item.Name}");

                        foreach (var nugetRef in nugetRefs)
                        {
                            if (!_graph.TryGetValue(nugetRef, out var repoInfos))
                            {
                                repoInfos = new HashSet <RepoInfo>();
                            }
                            repoInfos.Add(repo);
                            _graph[nugetRef] = repoInfos;
                        }

                        Thread.Sleep(500);
                    }
                }
                _lastUpDateTime = DateTime.UtcNow;
                _timer.Change(TimeSpan.FromHours(1), Timeout.InfiniteTimeSpan);
                _status = "Idle";
            }
            catch (Exception ex)
            {
                _status = $"Error. Restarting in 20 min. {ex.Message}";
                _timer.Change(TimeSpan.FromMinutes(20), Timeout.InfiniteTimeSpan);
            }
        }