Created
October 5, 2015 21:57
-
-
Save jrolstad/282880f4ddbdac210284 to your computer and use it in GitHub Desktop.
Search Github Organization Branches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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