void PruneBranches(GitPullRequestService service, Repository repo) { var gitHubRepositories = service.GetGitRepositories(repo); var prs = repo.Branches .Where(b => !b.IsRemote) .SelectMany(b => service.FindPullRequests(gitHubRepositories, b), (b, p) => (Branch: b, PullRequest: p)) .Where(bp => bp.PullRequest.IsDeleted) .Where(bp => PullRequestNumber == 0 || bp.PullRequest.Number == PullRequestNumber) .GroupBy(bp => bp.Branch) .Select(g => g.First()) // Select only one PR for each branch .ToList(); if (prs.Count == 0) { Console.WriteLine($"Couldn't find any pull requests with deleted remote branches to remove"); return; } foreach (var bp in prs) { if (bp.Branch.IsCurrentRepositoryHead) { Console.WriteLine($"Can't remove current repository head #{bp.PullRequest.Number} {bp.Branch.FriendlyName}"); } else { Console.WriteLine($"Removing #{bp.PullRequest.Number} {bp.Branch.FriendlyName}"); repo.Branches.Remove(bp.Branch); } } }
void ListBranches(GitPullRequestService service, Repository repo, string remoteName) { var gitRepositories = service.GetGitRepositories(repo); var prs = repo.Branches .Where(b => remoteName == null && !b.IsRemote || remoteName != null && b.IsRemote && b.RemoteName == remoteName) .SelectMany(b => service.FindPullRequests(gitRepositories, b), (b, p) => (Branch: b, PullRequest: p)) .Where(bp => PullRequestNumber == 0 || bp.PullRequest.Number == PullRequestNumber) .OrderBy(bp => bp.Branch.IsRemote) .ThenBy(bp => bp.PullRequest.Number) .ToList(); if (prs.Count == 0) { if (PullRequestNumber == 0) { Console.WriteLine("Couldn't find any branch with associated pull request in repository"); return; } Console.WriteLine($"Couldn't find branch associated with pull request #{PullRequestNumber} in repository"); return; } foreach (var bp in prs) { if (remoteName != null && bp.Branch.IsRemote && bp.Branch.RemoteName != bp.PullRequest.Repository.RemoteName && bp.PullRequest.Repository.References.ContainsKey(bp.Branch.UpstreamBranchCanonicalName)) { // Ignore branches forked from parent repository continue; } var isHead = bp.Branch.IsCurrentRepositoryHead ? "* " : " "; var remotePrefix = bp.PullRequest.Repository.RemoteName != "origin" ? bp.PullRequest.Repository.RemoteName : ""; var branchRemoteName = bp.Branch.RemoteName ?? ""; var postfix = (bp.PullRequest.IsDeleted ? "x " : "") + (branchRemoteName != "origin" ? branchRemoteName : ""); if (postfix.Length > 0) { postfix = $" ({postfix.TrimEnd()})"; } Console.WriteLine($"{isHead}{remotePrefix}#{bp.PullRequest.Number} {bp.Branch.FriendlyName}{postfix}"); } }
void BrowsePullRequest(GitPullRequestService service, Repository repo) { var gitRepositories = service.GetGitRepositories(repo); var prs = (PullRequestNumber == 0 ? service.FindPullRequests(gitRepositories, repo.Head) : repo.Branches .SelectMany(b => service.FindPullRequests(gitRepositories, b)) .Where(pr => pr.Number == PullRequestNumber) .Distinct()).ToList(); if (prs.Count > 0) { foreach (var pr in prs) { var url = pr.Repository.GetPullRequestUrl(pr.Number); Console.WriteLine(url); TryBrowse(url); } return; } if (PullRequestNumber != 0) { Console.WriteLine("Couldn't find pull request #" + PullRequestNumber); return; } var compareUrl = service.FindCompareUrl(gitRepositories, repo); if (compareUrl != null) { Console.WriteLine(compareUrl); TryBrowse(compareUrl); return; } Console.WriteLine("Couldn't find pull request or remote branch"); }