Skip to content

Instantly share code, notes, and snippets.

@eduherminio
Last active June 13, 2020 19:43
Show Gist options
  • Save eduherminio/506f1663fdc9bb2fbf43ffb02df57c30 to your computer and use it in GitHub Desktop.
Save eduherminio/506f1663fdc9bb2fbf43ffb02df57c30 to your computer and use it in GitHub Desktop.
Finds repositories with a given package as a dependency within a GitHub organization
<Query Kind="Program">
<NuGetReference>Octokit</NuGetReference>
<Namespace>Octokit</Namespace>
</Query>
void Main()
{
const string packageName = "MyPackage";
var searchResult = new RepositoryFinder()
.FindReposThatUse(packageName);
var repos = searchResult.Items
.Where(r => r.Repository.Name != packageName)
.GroupBy(it => it.Repository.Id)
.Select(gr => gr.FirstOrDefault()?.Repository)
.ToList();
$"Projects dependant of {packageName}:".Dump();
repos.Select(r => r.Name).Dump();
}
public class RepositoryFinder
{
private const string GitHubUrl = @"https://github.xxxxxxxx.com/";
private const string OrganizationName = @"xxxxxxxx";
private readonly IGitHubClient _client;
public RepositoryFinder()
{
Uri url = new Uri(GitHubUrl);
_client = new GitHubClient(new ProductHeaderValue("DependenciesSearchTool"), url);
}
public SearchCodeResult FindReposThatUse(string packageName)
{
SearchCodeRequest request = new SearchCodeRequest($"org:{OrganizationName} \"{packageName}\"")
{
Extensions = new[] { "csproj", "config" }
};
return _client.Search.SearchCode(request).Result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment