Skip to content

Instantly share code, notes, and snippets.

@jrolstad
Created October 5, 2015 21:57
Show Gist options
  • Save jrolstad/282880f4ddbdac210284 to your computer and use it in GitHub Desktop.
Save jrolstad/282880f4ddbdac210284 to your computer and use it in GitHub Desktop.
Search Github Organization Branches
class Program
{
static void Main(string[] args)
{
var userName = "<user name>";
var password = "<password>";
var organization = "<user name or Organization Name";
var filter = "<what I'm looking for>";
Console.WriteLine("Querying Github...");
var branches = GetBranches(userName, password, organization, filter)
.ToList()
.OrderBy(b => b.RepositoryName);
foreach (var branch in branches)
{
Console.WriteLine("{0}|{1}",branch.RepositoryName, branch.BranchName);
}
Console.WriteLine("");
Console.WriteLine("Querying complete. Press Enter to exit.");
Console.ReadLine();
}
private static IEnumerable<RepositoryBranch> GetBranches(string userName, string password, string organization, string branchFilter)
{
var connection = new Connection(new ProductHeaderValue("wheres_my_code"),new InMemoryCredentialStore(new Credentials(userName, password)));
var apiConnection = new ApiConnection(connection);
var repoClient = new RepositoriesClient(apiConnection);
var repositories = repoClient.GetAllForOrg(organization).Result;
var branches = repositories
.AsParallel()
.SelectMany(r => GetBranchesForRepository(repoClient, r, organization));
var matchingBranches = branches
.Where(b => b.BranchName.ToLower().Contains(branchFilter.ToLower().Trim()));
return matchingBranches;
}
private static IEnumerable<RepositoryBranch> GetBranchesForRepository(RepositoriesClient repoClient, Repository repo, string organization)
{
var repoBranches = repoClient.GetAllBranches(organization, repo.Name).Result;
return repoBranches.Select(branch => new RepositoryBranch
{
Owner = organization,
RepositoryName = repo.Name,
BranchName = branch.Name
});
}
}
public class RepositoryBranch
{
public string Owner { get; set; }
public string RepositoryName { get; set; }
public string BranchName { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment