diff --git a/.gitignore b/.gitignore index dfcfd56..5677221 100644 --- a/.gitignore +++ b/.gitignore @@ -348,3 +348,6 @@ MigrationBackup/ # Ionide (cross platform F# VS Code tools) working folder .ionide/ + +.idea/ +.env \ No newline at end of file diff --git a/GitHubLabelSync.sln b/GitHubLabelSync.sln new file mode 100644 index 0000000..cab2dcf --- /dev/null +++ b/GitHubLabelSync.sln @@ -0,0 +1,48 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30114.105 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitHubLabelSync", "src\GitHubLabelSync.csproj", "{2F338383-5B7C-4398-A375-E005DFD09A3F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitHubLabelSync.Tests", "test\GitHubLabelSync.Tests.csproj", "{CBF71974-EAE3-49DD-A8BC-0DFDC2D9FC26}" +EndProject +Global + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2F338383-5B7C-4398-A375-E005DFD09A3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2F338383-5B7C-4398-A375-E005DFD09A3F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2F338383-5B7C-4398-A375-E005DFD09A3F}.Debug|x64.ActiveCfg = Debug|Any CPU + {2F338383-5B7C-4398-A375-E005DFD09A3F}.Debug|x64.Build.0 = Debug|Any CPU + {2F338383-5B7C-4398-A375-E005DFD09A3F}.Debug|x86.ActiveCfg = Debug|Any CPU + {2F338383-5B7C-4398-A375-E005DFD09A3F}.Debug|x86.Build.0 = Debug|Any CPU + {2F338383-5B7C-4398-A375-E005DFD09A3F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2F338383-5B7C-4398-A375-E005DFD09A3F}.Release|Any CPU.Build.0 = Release|Any CPU + {2F338383-5B7C-4398-A375-E005DFD09A3F}.Release|x64.ActiveCfg = Release|Any CPU + {2F338383-5B7C-4398-A375-E005DFD09A3F}.Release|x64.Build.0 = Release|Any CPU + {2F338383-5B7C-4398-A375-E005DFD09A3F}.Release|x86.ActiveCfg = Release|Any CPU + {2F338383-5B7C-4398-A375-E005DFD09A3F}.Release|x86.Build.0 = Release|Any CPU + {CBF71974-EAE3-49DD-A8BC-0DFDC2D9FC26}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CBF71974-EAE3-49DD-A8BC-0DFDC2D9FC26}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CBF71974-EAE3-49DD-A8BC-0DFDC2D9FC26}.Debug|x64.ActiveCfg = Debug|Any CPU + {CBF71974-EAE3-49DD-A8BC-0DFDC2D9FC26}.Debug|x64.Build.0 = Debug|Any CPU + {CBF71974-EAE3-49DD-A8BC-0DFDC2D9FC26}.Debug|x86.ActiveCfg = Debug|Any CPU + {CBF71974-EAE3-49DD-A8BC-0DFDC2D9FC26}.Debug|x86.Build.0 = Debug|Any CPU + {CBF71974-EAE3-49DD-A8BC-0DFDC2D9FC26}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CBF71974-EAE3-49DD-A8BC-0DFDC2D9FC26}.Release|Any CPU.Build.0 = Release|Any CPU + {CBF71974-EAE3-49DD-A8BC-0DFDC2D9FC26}.Release|x64.ActiveCfg = Release|Any CPU + {CBF71974-EAE3-49DD-A8BC-0DFDC2D9FC26}.Release|x64.Build.0 = Release|Any CPU + {CBF71974-EAE3-49DD-A8BC-0DFDC2D9FC26}.Release|x86.ActiveCfg = Release|Any CPU + {CBF71974-EAE3-49DD-A8BC-0DFDC2D9FC26}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/README.md b/README.md index bb9e1a9..2569781 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# GitHubLabelSync +# GitHub Label Sync Synchronize GitHub issue labels across repositories diff --git a/src/App.cs b/src/App.cs new file mode 100644 index 0000000..458eceb --- /dev/null +++ b/src/App.cs @@ -0,0 +1,41 @@ +using System; +using System.Threading.Tasks; + +namespace GitHubLabelSync +{ + public class App + { + private readonly ISynchronizer _sync; + private readonly Action _setStatus; + private readonly Action _log; + + public App(ISynchronizer sync, Action setStatus, Action log) + { + _sync = sync; + _setStatus = setStatus; + _log = log; + } + + public async Task Run(Settings settings) + { + _setStatus($"Starting..."); + + var account = await _sync.GetAccount(settings.Name); + _log(string.Empty); + + var repos = await _sync.GetRepositories(account); + _log(string.Empty); + + var labels = await _sync.GetAccountLabels(account); + _log(string.Empty); + + foreach (var repo in repos) + { + await _sync.SyncRepo(repo, settings, labels); + _log(string.Empty); + } + + _log("Done!"); + } + } +} \ No newline at end of file diff --git a/src/Command.cs b/src/Command.cs new file mode 100644 index 0000000..66efefb --- /dev/null +++ b/src/Command.cs @@ -0,0 +1,35 @@ +using System; +using System.Threading.Tasks; +using Spectre.Console; +using Spectre.Console.Cli; + +namespace GitHubLabelSync +{ + public class Command : AsyncCommand + { + private readonly IAnsiConsole _console; + + public Command(IAnsiConsole console) + => _console = console; + + public override async Task ExecuteAsync(CommandContext context, Settings settings) + { + try + { + await _console.Status().StartAsync("Running...", Run(settings)); + return 0; + } + catch (Exception e) + { + _console.WriteException(e); + return 1; + } + } + + private Func Run(Settings settings) + => async ctx + => await Factory + .App(settings.APIKey, s => _console.WriteLine(s), s => ctx.Status(s)) + .Run(settings); + } +} \ No newline at end of file diff --git a/src/Factory.cs b/src/Factory.cs new file mode 100644 index 0000000..f99a013 --- /dev/null +++ b/src/Factory.cs @@ -0,0 +1,20 @@ +using System; +using System.Reflection; +using Octokit; + +namespace GitHubLabelSync +{ + public static class Factory + { + public static App App(string apiKey, Action setStatus, Action log) + { + var name = Assembly.GetExecutingAssembly().GetName().Name; + var value = new ProductHeaderValue(name); + var client = new GitHubClient(value) { Credentials = new Credentials(apiKey) }; + + var gitHub = new GitHub(client, setStatus, log); + var sync = new Synchronizer(gitHub, new Random(), setStatus, log); + return new App(sync, setStatus, log); + } + } +} \ No newline at end of file diff --git a/src/GitHub.cs b/src/GitHub.cs new file mode 100644 index 0000000..2b8f6d0 --- /dev/null +++ b/src/GitHub.cs @@ -0,0 +1,113 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Octokit; + +namespace GitHubLabelSync +{ + public class GitHub : IGitHub + { + private readonly IGitHubClient _client; + private readonly Action _setStatus; + private readonly Action _log; + + public GitHub(IGitHubClient client, Action setStatus, Action log) + { + _client = client; + _setStatus = setStatus; + _log = log; + } + + public async Task GetOrganization(string name) + { + _setStatus($"Finding organization for {name}..."); + var account = await _client.Organization.Get(name); + _log($"Organization ID for {name}: {account.Id}"); + return account; + } + + public async Task GetUser(string name) + { + _setStatus($"Finding user for {name}..."); + var account = await _client.User.Get(name); + _log($"User ID for {name}: {account.Id}"); + return account; + } + + public async Task> GetRepositoriesForOrganization(Account account) + { + _setStatus($"Finding repositories for {account.Login}..."); + var repos = await _client.Repository.GetAllForOrg(account.Login); + var repoNames = string.Join(", ", repos.Select(l => l.Name)); + _log($"{repos.Count} repositories for {account.Login}: {repoNames}"); + return repos; + } + + public async Task> GetRepositoriesForUser(Account account) + { + _setStatus($"Finding repositories for {account.Login}..."); + var repos = await _client.Repository.GetAllForUser(account.Login); + var repoNames = string.Join(", ", repos.Select(l => l.Name)); + _log($"{repos.Count} repositories for {account.Login}: {repoNames}"); + return repos; + } + + public async Task CreateTempRepoForOrganization(Account account, string repoName) + { + _setStatus($"Creating temp repository {repoName}..."); + var newRepo = new NewRepository(repoName) { Private = true }; + var repo = await _client.Repository.Create(account.Login, newRepo); + _log($"Created temp repository {repoName}"); + return repo; + } + + public async Task CreateTempRepoForUser(Account account, string repoName) + { + _setStatus($"Creating temp repository {repoName}..."); + var newRepo = new NewRepository(repoName) { Private = true }; + var repo = await _client.Repository.Create(newRepo); + _log($"Created temp repository {repoName}"); + return repo; + } + + public async Task DeleteTempRepo(Account account, string repoName) + { + _setStatus($"Deleting temp repository {repoName}..."); + await _client.Repository.Delete(account.Login, repoName); + _log($"Deleted temp repository {repoName}"); + } + + public async Task> GetLabels(Repository repo) + { + _setStatus($"Finding labels for {repo.Name}..."); + var repoLabels = await _client.Issue.Labels.GetAllForRepository(repo.Id); + var repoLabelNames = string.Join(", ", repoLabels.Select(l => l.Name)); + _log($"{repoLabels.Count,2} labels : {repoLabelNames}"); + return repoLabels; + } + + public async Task AddLabel(Repository repo, Label label) + { + _setStatus($"Adding {label.Name} to {repo.Name}..."); + var newLabel = new NewLabel(label.Name, label.Color) { Description = label.Description }; + await _client.Issue.Labels.Create(repo.Id, newLabel); + _log($"Added {label.Name}"); + } + + public async Task EditLabel(Repository repo, Label label) + { + _setStatus($"Editing {label.Name} in {repo.Name}..."); + var newLabel = new LabelUpdate(label.Name, label.Color) { Description = label.Description }; + await _client.Issue.Labels.Update(repo.Id, label.Name, newLabel); + _log($"Edited {label.Name}"); + } + + public async Task DeleteLabel(Repository repo, Label label) + { + _setStatus($"Deleting {label.Name} from {repo.Name}..."); + await _client.Issue.Labels.Delete(repo.Id, label.Name); + _log($"Deleted {label.Name}"); + } + } +} \ No newline at end of file diff --git a/src/GitHubLabelSync.csproj b/src/GitHubLabelSync.csproj new file mode 100644 index 0000000..f5a69c5 --- /dev/null +++ b/src/GitHubLabelSync.csproj @@ -0,0 +1,25 @@ + + + 0.1.0 + Exe + true + sync-labels + net5.0 + Synchronize GitHub issue labels across repositories + ecoAPM LLC + ecoAPM LLC + ecoAPM LLC + GitHubLabelSync + README.md + https://github.com/ecoAPM/GitHubLabelSync + MIT + https://github.com/ecoAPM/GitHubLabelSync + true + true + true + + + + + + \ No newline at end of file diff --git a/src/IGitHub.cs b/src/IGitHub.cs new file mode 100644 index 0000000..bdf08f9 --- /dev/null +++ b/src/IGitHub.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Octokit; + +namespace GitHubLabelSync +{ + public interface IGitHub + { + Task GetOrganization(string name); + Task GetUser(string name); + + Task> GetRepositoriesForOrganization(Account account); + Task> GetRepositoriesForUser(Account account); + + Task CreateTempRepoForOrganization(Account account, string repoName); + Task CreateTempRepoForUser(Account account, string repoName); + Task DeleteTempRepo(Account account, string repoName); + + Task> GetLabels(Repository repo); + Task AddLabel(Repository repo, Label label); + Task EditLabel(Repository repo, Label label); + Task DeleteLabel(Repository repo, Label label); + } +} \ No newline at end of file diff --git a/src/ISynchronizer.cs b/src/ISynchronizer.cs new file mode 100644 index 0000000..9a228ac --- /dev/null +++ b/src/ISynchronizer.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Octokit; + +namespace GitHubLabelSync +{ + public interface ISynchronizer + { + Task GetAccount(string name); + Task> GetRepositories(Account account); + Task> GetAccountLabels(Account account); + Task SyncRepo(Repository repo, Settings settings, IReadOnlyList