public void Pull_Request_To_Upstream_Repository() { var number = 777; var originUrl = "https://github.com/origin/repo"; var upstreamRemoteName = "upstream"; var upstreamUrl = "https://github.com/upstream/repo"; var prSha = "prSha"; var originRemote = CreateRemote("origin", originUrl); var upstreamRemote = CreateRemote(upstreamRemoteName, upstreamUrl); var repo = CreateRepository(prSha, "origin", "refs/heads/one", new[] { originRemote, upstreamRemote }); AddRemoteReferences(repo, originRemote, new Dictionary <string, string> { { "refs/heads/one", prSha } }); AddRemoteReferences(repo, upstreamRemote, new Dictionary <string, string> { { $"refs/pull/{number}/head", prSha } }); var target = new GitPullRequestService(); var gitHubRepositories = target.GetGitHubRepositories(repo); var prs = target.FindPullRequests(gitHubRepositories, repo.Head); var pr = prs.FirstOrDefault(); Assert.That(pr.Repository.Url, Is.EqualTo(upstreamUrl)); Assert.That(pr.Repository.RemoteName, Is.EqualTo(upstreamRemoteName)); Assert.That(pr.Number, Is.EqualTo(number)); }
void PruneBranches(GitPullRequestService service, Repository repo, Action <Exception> exceptionLogger) { var remoteRepositoryCache = service.GetRemoteRepositoryCache(repo, exceptionLogger); var upstreamRepositoires = CreateUpstreamRepositoires(remoteRepositoryCache, repo); var prs = repo.Branches .Where(b => !b.IsRemote) .SelectMany(b => service.FindPullRequests(remoteRepositoryCache, upstreamRepositoires, 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 BrowsePullRequest(GitPullRequestService service, Repository repo) { var gitHubRepositories = service.GetGitHubRepositories(repo); var prs = service.FindPullRequests(gitHubRepositories, repo.Head); if (prs.Count > 0) { foreach (var pr in prs) { var prUrl = service.GetPullRequestUrl(pr.Repository, pr.Number); Browse(prUrl); } return; } var compareUrl = service.FindCompareUrl(gitHubRepositories, repo); if (compareUrl != null) { Browse(compareUrl); return; } Console.WriteLine("Couldn't find pull request or remote branch"); }
public void No_Pull_Request() { var repo = CreateRepository("sha", null, null, Array.Empty <Remote>()); var target = new GitPullRequestService(); var gitHubRepositories = target.GetGitHubRepositories(repo); var prs = target.FindPullRequests(gitHubRepositories, repo.Head); Assert.That(prs, Is.Empty); }
void BrowsePullRequest(GitPullRequestService service, Repository repo, Action <Exception> exceptionLogger) { var remoteRepositoryCache = service.GetRemoteRepositoryCache(repo, exceptionLogger); var upstreamRepositoires = CreateUpstreamRepositoires(remoteRepositoryCache, repo); var prs = (PullRequestNumber == 0 ? service.FindPullRequests(remoteRepositoryCache, upstreamRepositoires, repo.Head) : repo.Branches .SelectMany(b => service.FindPullRequests(remoteRepositoryCache, upstreamRepositoires, 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(remoteRepositoryCache, repo); if (compareUrl != null) { Console.WriteLine(compareUrl); TryBrowse(compareUrl); return; } Console.WriteLine("Couldn't find pull request or remote branch"); }
void ListBranches(GitPullRequestService service, Repository repo, string remoteName, Action <Exception> exceptionLogger) { var remoteRepositoryCache = service.GetRemoteRepositoryCache(repo, exceptionLogger); var upstreamRepositoires = CreateUpstreamRepositoires(remoteRepositoryCache, repo); var prs = repo.Branches .Where(b => remoteName == null && !b.IsRemote || remoteName != null && b.IsRemote && b.RemoteName == remoteName) .SelectMany(b => service.FindPullRequests(remoteRepositoryCache, upstreamRepositoires, 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 ListBranches(GitPullRequestService service, Repository repo) { var gitHubRepositories = service.GetGitHubRepositories(repo); foreach (var branch in repo.Branches) { if (branch.IsRemote) { continue; } var prs = service.FindPullRequests(gitHubRepositories, branch); var pr = prs.FirstOrDefault(); if (pr == default) { continue; } var remotePrefix = pr.Repository.RemoteName != "origin" ? pr.Repository.RemoteName : ""; var remotePostfix = branch.RemoteName != "origin" ? $" ({branch.RemoteName})" : ""; Console.WriteLine($"{remotePrefix}#{pr.Number} {branch.FriendlyName}{remotePostfix}"); } }
public void Live_Pull_Request(string headSha, string prSha) { var number = 777; var remoteName = "origin"; var remoteUrl = "https://github.com/owner/repo"; var remote = CreateRemote(remoteName, remoteUrl); var repo = CreateRepository(headSha, "origin", "refs/heads/one", new[] { remote }); AddRemoteReferences(repo, remote, new Dictionary <string, string> { { "refs/heads/one", prSha }, { $"refs/pull/{number}/head", prSha } }); var target = new GitPullRequestService(); var gitHubRepositories = target.GetGitHubRepositories(repo); var prs = target.FindPullRequests(gitHubRepositories, repo.Head); var pr = prs.FirstOrDefault(); Assert.That(pr.Repository.RemoteName, Is.EqualTo(remoteName)); Assert.That(pr.Repository.Url, Is.EqualTo(remoteUrl)); Assert.That(pr.Number, Is.EqualTo(number)); }