Skip to content

Commit

Permalink
Merge pull request #10 from jcansdale/feature/prune
Browse files Browse the repository at this point in the history
Add --prune option to remove PRs with deleted remote branches
  • Loading branch information
jcansdale authored Dec 3, 2018
2 parents 9eaeb26 + cc9b7a6 commit df40f59
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions GitPullRequest/GitPullRequest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

<Version>1.0.5</Version>
<Version>1.0.6</Version>

<LangVersion>7.3</LangVersion>

</PropertyGroup>

<ItemGroup>
<PackageReference Include="LibGit2Sharp" Version="0.26.0-preview-0062" />
<PackageReference Include="LibGit2Sharp" Version="0.26.0-preview-0070" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.3.0-alpha" />
<PackageReference Include="Microsoft.Alm.Authentication" Version="4.3.0" />
</ItemGroup>
Expand Down
39 changes: 39 additions & 0 deletions GitPullRequest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public static int Main(string[] args)
[Option("--all", Description = "List all branches with associated pull requests")]
public bool All { get; }

[Option("--prune", Description = "Remove pull requests with deleted remote branches")]
public bool Prune { get; }

void OnExecute()
{
var repoPath = Repository.Discover(TargetDir);
Expand All @@ -39,6 +42,12 @@ void OnExecute()
var service = new GitPullRequestService();
using (var repo = new Repository(repoPath))
{
if (Prune)
{
PruneBranches(service, repo);
return;
}

if (List || All)
{
ListBranches(service, repo);
Expand Down Expand Up @@ -122,6 +131,36 @@ void ListBranches(GitPullRequestService service, Repository repo)
}
}

void PruneBranches(GitPullRequestService service, Repository repo)
{
var gitHubRepositories = service.GetGitHubRepositories(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)
.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 Browse(string pullUrl)
{
Process.Start(new ProcessStartInfo
Expand Down

0 comments on commit df40f59

Please sign in to comment.