Skip to content

Commit

Permalink
Merge pull request #83 from nils-a/feature/GH-82
Browse files Browse the repository at this point in the history
#82 and #77
  • Loading branch information
nils-a authored Oct 27, 2023
2 parents a22ba6a + e2f513b commit 3fa52ab
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
38 changes: 32 additions & 6 deletions src/GitHubMilestoneCleaner/Commands/AutoCleanupCommand.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
Expand Down Expand Up @@ -113,16 +114,41 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
return 0;
}

foreach (var issue in toRemove)
async Task DoRemove(Action? callback = null)
{
var group = grouped.First(g => g.MainIssue.Id == issue.Id || g.SubIssues.Any(i => i.Id == issue.Id));
var comment = issue == group.MainIssue ? settings.TopIssueComment : settings.SubIssueComment;
if (!string.IsNullOrEmpty(comment))
foreach (var issue in toRemove)
{
comment = string.Format(comment, group.MainIssue.HtmlUrl);
var group = grouped.First(g => g.MainIssue.Id == issue.Id || g.SubIssues.Any(i => i.Id == issue.Id));
var comment = issue == group.MainIssue ? settings.TopIssueComment : settings.SubIssueComment;
if (!string.IsNullOrEmpty(comment))
{
comment = string.Format(comment, group.MainIssue.HtmlUrl);
}

await adapter.RemoveMilestone(repo, issue, comment);
callback?.Invoke();
}
}

if (settings.NonInteractive)
{
await DoRemove();
}
else
{
await AnsiConsole.Progress()
.StartAsync(async ctx =>
{
var inc = 100d / toRemove.Count;
var task = ctx.AddTask($"Cleaning {toRemove.Count} issues");
await DoRemove(() =>
{
task.Increment(inc);
});

await adapter.RemoveMilestone(repo, issue, comment);
var rest = 100 - task.Percentage;
task.Increment(rest);
});
}

return 0;
Expand Down
18 changes: 14 additions & 4 deletions src/GitHubMilestoneCleaner/Engines/IssueGroupEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@ namespace GitHubMilestoneCleaner.Engines;
public class IssueGroupEngine
{
private readonly Regex _versionMatcher =
new Regex(
@"(0|[1-9]\d*)(\.(0|[1-9]\d*))+(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?");
new(
@"\s*(from|to) v?(0|[1-9]\d*)(\.(0|[1-9]\d*))+(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?");
private readonly Regex _digestMatcher = new(@"\s*digest to [0-9a-fA-F]+$");


public IEnumerable<IssueGroup> GroupIssues(IEnumerable<Issue> issues)
{

var matchers = new[]
{
_versionMatcher,
_digestMatcher
};

return issues
.Select(x => new
{
Issue = x,
VersionAgnosticName = _versionMatcher.Replace(x.Title, string.Empty)
VersionAgnosticName = (matchers
.FirstOrDefault(m => m.IsMatch(x.Title))?
.Replace(x.Title, string.Empty) ?? x.Title)
.TrimEnd(),
})
.GroupBy(x => x.VersionAgnosticName)
.Select(x =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ public static class GitHubClientExtensions

return TimeSpan.FromSeconds(secondsToWait);
},
async (ex, _, _, ctx) =>
(ex, _, _, ctx) =>
{
var cancellationTokenSource = (CancellationTokenSource)ctx[CancellationTokenSourceKey];
if (ex is Octokit.NotFoundException)
{
AnsiConsole.MarkupLine($"[red]{ex.GetType().Name}: {ex.Message}[/]");
cancellationTokenSource.Cancel();
}

return Task.CompletedTask;
});

public static async Task<T> WithRetry<T>(
Expand Down

0 comments on commit 3fa52ab

Please sign in to comment.