From 6e194ab243f67fb73078f0484bbbeeadeb762d3c Mon Sep 17 00:00:00 2001 From: James Ross Date: Thu, 21 Nov 2024 13:46:13 +0000 Subject: [PATCH 1/4] style: `dotnet format --severity info` --- Git/Project.cs | 2 +- GitHub/Query.cs | 2 +- Program.cs | 8 +++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Git/Project.cs b/Git/Project.cs index 0c7ffa9..26b8d23 100644 --- a/Git/Project.cs +++ b/Git/Project.cs @@ -9,7 +9,7 @@ namespace Open_Rails_Code_Bot.Git { public class Project { - string GitPath; + readonly string GitPath; public Project(string gitPath) { diff --git a/GitHub/Query.cs b/GitHub/Query.cs index 545fcc2..5c2a2c4 100644 --- a/GitHub/Query.cs +++ b/GitHub/Query.cs @@ -14,7 +14,7 @@ class Query readonly string Token; - HttpClient Client = new HttpClient(); + readonly HttpClient Client = new(); public Query(string token) { diff --git a/Program.cs b/Program.cs index b858585..8776d8d 100644 --- a/Program.cs +++ b/Program.cs @@ -103,9 +103,11 @@ static async Task AsyncMain(IConfigurationRoot config) var mergeBranchTree = git.ParseRef($"{mergeBranchCommit}^{{tree}}"); git.CheckoutDetached(baseBranchCommit); var baseBranchVersion = String.Format(gitHubConfig["versionFormat"] ?? "{0}", git.Describe(gitHubConfig["versionDescribeOptions"] ?? "")); - var mergeBranchParents = new List(); - mergeBranchParents.Add(mergeBranchCommit); - mergeBranchParents.Add(baseBranchCommit); + var mergeBranchParents = new List + { + mergeBranchCommit, + baseBranchCommit + }; var autoMergePullRequestsSuccess = new List(); var autoMergePullRequestsFailure = new List(); foreach (var pullRequest in autoMergePullRequests) From f750eb8fe0a6e8350f912efa7cebe861e0191ae4 Mon Sep 17 00:00:00 2001 From: James Ross Date: Thu, 21 Nov 2024 14:25:22 +0000 Subject: [PATCH 2/4] refactor: Clean up command runner and return all output --- Git/Project.cs | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/Git/Project.cs b/Git/Project.cs index 26b8d23..6df384a 100644 --- a/Git/Project.cs +++ b/Git/Project.cs @@ -129,45 +129,43 @@ public void SetBranchRef(string branch, string reference) RunCommand($"branch -f {branch} {reference}"); } - void RunCommand(string command) + void RunCommand(string arguments) { - foreach (var line in GetCommandOutput(command, true)) + foreach (var line in GetCommandOutput(arguments, true)) { } } - IEnumerable GetCommandOutput(string command, bool printOutput = false) + IEnumerable GetCommandOutput(string arguments, bool printOutput = false) { - var args = $"--no-pager {command}"; + arguments = $"--no-pager {arguments}"; if (printOutput) - Console.WriteLine($" > git {args}"); + Console.WriteLine($" > git {arguments}"); + var lines = new List(); var git = new Process(); git.StartInfo.WorkingDirectory = GitPath; git.StartInfo.FileName = "git"; - git.StartInfo.Arguments = args; + git.StartInfo.Arguments = arguments; git.StartInfo.UseShellExecute = false; git.StartInfo.RedirectStandardOutput = true; git.StartInfo.RedirectStandardError = true; git.StartInfo.StandardOutputEncoding = Encoding.UTF8; git.StartInfo.StandardErrorEncoding = Encoding.UTF8; - git.ErrorDataReceived += (sender, e) => - { - if (e.Data?.Length > 0) - Console.Error.WriteLine($" ! {e.Data}"); - }; + git.OutputDataReceived += (sender, e) => lines.Add($" < {e.Data}"); + git.ErrorDataReceived += (sender, e) => lines.Add($" ! {e.Data}"); git.Start(); + git.BeginOutputReadLine(); git.BeginErrorReadLine(); - while (!git.StandardOutput.EndOfStream) + git.WaitForExit(); + foreach (var line in lines) { - if (printOutput) - Console.WriteLine($" < {git.StandardOutput.ReadLine()}"); - else - yield return git.StandardOutput.ReadLine(); + if (printOutput && line.Length > 4) + Console.WriteLine(line); + yield return line[4..]; } - git.WaitForExit(); if (git.ExitCode != 0) { - throw new ApplicationException($"git {command} failed: {git.ExitCode}"); + throw new ApplicationException($"git {arguments} failed: {git.ExitCode}"); } } } From d580fd66d79e62d6099c130a6675529e14348ae0 Mon Sep 17 00:00:00 2001 From: James Ross Date: Thu, 21 Nov 2024 14:26:57 +0000 Subject: [PATCH 3/4] refactor: Clean up repository set up --- Git/Project.cs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Git/Project.cs b/Git/Project.cs index 6df384a..8aa17b4 100644 --- a/Git/Project.cs +++ b/Git/Project.cs @@ -18,13 +18,11 @@ public Project(string gitPath) public void Init(string repository) { - if (!Directory.Exists(GitPath)) - { - Directory.CreateDirectory(GitPath); - RunCommand($"init"); - RunCommand($"remote add origin {repository}"); - RunCommand($"config remote.origin.fetch +refs/*:refs/*"); - } + if (!Directory.Exists(GitPath)) Directory.CreateDirectory(GitPath); + if (!File.Exists(Path.Join(GitPath, ".git", "config"))) RunCommand($"init"); + + RunCommand($"config remove-section remote.origin"); + RunCommand($"remote add origin --mirror=fetch {repository}"); } public void Fetch() From 72f587daa16514bc2ba5859439e6945afc102f9d Mon Sep 17 00:00:00 2001 From: James Ross Date: Thu, 21 Nov 2024 14:29:50 +0000 Subject: [PATCH 4/4] feat: List all files in a pull request before attempting merge --- Git/Project.cs | 10 ++++++++++ Program.cs | 1 + 2 files changed, 11 insertions(+) diff --git a/Git/Project.cs b/Git/Project.cs index 8aa17b4..1acda0c 100644 --- a/Git/Project.cs +++ b/Git/Project.cs @@ -50,6 +50,16 @@ public void Clean() RunCommand("clean --force -d -x"); } + public void DiffStat(string reference1, string reference2) + { + foreach (var line in GetCommandOutput($"diff --numstat {reference1}...{reference2}")) + { + var parts = line.Split('\t'); + if (parts.Length == 3 && int.TryParse(parts[0], out var added) && int.TryParse(parts[1], out var deleted)) + Console.WriteLine(" {2} {0:+#,##0} {1:-#,##0}", added, deleted, parts[2]); + } + } + public void Merge(string reference) { RunCommand($"merge --quiet --no-edit --no-ff -Xignore-space-change {reference}"); diff --git a/Program.cs b/Program.cs index 8776d8d..f23cd58 100644 --- a/Program.cs +++ b/Program.cs @@ -113,6 +113,7 @@ static async Task AsyncMain(IConfigurationRoot config) foreach (var pullRequest in autoMergePullRequests) { Console.WriteLine($"Merging #{pullRequest.Number} {pullRequest.Title}..."); + git.DiffStat(baseBranchCommit, $"pull/{pullRequest.Number}/head"); var mergeCommit = git.ParseRef($"pull/{pullRequest.Number}/head"); try {