From 00add1bbbc1f44b0aa3e37c0238bd89a642d7f22 Mon Sep 17 00:00:00 2001 From: goat Date: Sat, 30 Jul 2022 16:33:04 +0200 Subject: [PATCH] add diffs --- Plogon/BuildProcessor.cs | 73 ++++++++++++++++++++++++++++++++++++++-- Plogon/Program.cs | 12 ++++--- 2 files changed, 78 insertions(+), 7 deletions(-) diff --git a/Plogon/BuildProcessor.cs b/Plogon/BuildProcessor.cs index 361eb69..91c82de 100644 --- a/Plogon/BuildProcessor.cs +++ b/Plogon/BuildProcessor.cs @@ -4,6 +4,9 @@ using System.IO; using System.Linq; using System.Net.Http; +using System.Net.Http.Json; +using System.Text.Json.Serialization; +using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Docker.DotNet; @@ -152,7 +155,67 @@ async Task GetDep(string name, NugetLockfile.Dependency dependency) }); } - public async Task ProcessTask(BuildTask task, bool commit) + private class HasteResponse + { + [JsonPropertyName("key")] + public string Key { get; set; } + }; + + private async Task GetDiffUrl(DirectoryInfo workDir, string haveCommit, string wantCommit) + { + if (string.IsNullOrEmpty(haveCommit)) + { + var revListPsi = new ProcessStartInfo("git", "rev-list --max-parents=0 HEAD") + { + RedirectStandardOutput = true, + WorkingDirectory = workDir.FullName, + }; + + var revListProcess = Process.Start(revListPsi); + if (revListProcess == null) + throw new Exception("Could not start rev-list"); + + haveCommit = Regex.Replace(revListProcess.StandardOutput.ReadToEnd(), @"\t|\n|\r", ""); + + if (revListProcess.ExitCode != 0) + throw new Exception("Rev-list did not succeed"); + + Log.Information("Using {NewRev} as have", haveCommit); + } + + var diffPsi = new ProcessStartInfo("git", + $"diff --submodule=diff {haveCommit}..{wantCommit}") + { + RedirectStandardOutput = true, + WorkingDirectory = workDir.FullName, + }; + + var process = Process.Start(diffPsi); + if (process == null) + throw new Exception("Diff process was null."); + + var output = await process.StandardOutput.ReadToEndAsync(); + + if (process.ExitCode != 0) + throw new Exception($"Git could not diff: {process.ExitCode} -- {diffPsi.Arguments}"); + + using var client = new HttpClient(); + var res = await client.PostAsync("https://haste.soulja-boy-told.me/documents", new StringContent(output)); + res.EnsureSuccessStatusCode(); + + var json = await res.Content.ReadFromJsonAsync(); + + return $"https://haste.soulja-boy-told.me/{json!.Key}.diff"; + } + + public class BuildResult + { + public bool Success { get; set; } + + public string DiffUrl { get; set; } + } + + public async Task ProcessTask(BuildTask task, bool commit) { var folderName = $"{task.InternalName}-{task.Manifest.Plugin.Commit}"; var work = this.workFolder.CreateSubdirectory($"{folderName}-work"); @@ -201,6 +264,8 @@ public async Task ProcessTask(BuildTask task, bool commit) } }); } + + var diffUrl = await GetDiffUrl(work, task.HaveCommit!, task.Manifest.Plugin.Commit); var dalamudAssemblyDir = await this.dalamudReleases.GetDalamudAssemblyDirAsync(task.Channel); @@ -314,7 +379,11 @@ await this.dockerClient.Containers.RemoveContainerAsync(containerCreateResponse. } } - return exitCode == 0; + return new BuildResult + { + DiffUrl = diffUrl, + Success = exitCode == 0, + }; } public class PluginCommitException : Exception diff --git a/Plogon/Program.cs b/Plogon/Program.cs index a65b69a..2ad9d61 100644 --- a/Plogon/Program.cs +++ b/Plogon/Program.cs @@ -89,16 +89,18 @@ static async Task Main(DirectoryInfo outputFolder, DirectoryInfo manifestFolder, task.HaveCommit ?? "nothing"); var status = await buildProcessor.ProcessTask(task, commit); - if (!status) + if (status.Success) { - Log.Error("Could not build: {Name} - {Sha}", task.InternalName, - task.Manifest.Plugin.Commit); + Log.Information("Built: {Name} - {Sha} - {DiffUrl}", task.InternalName, + task.Manifest.Plugin.Commit, status.DiffUrl); - buildsMd.AddRow("✔️", task.InternalName, task.Manifest.Plugin.Commit, string.Empty); + buildsMd.AddRow("✔️", task.InternalName, task.Manifest.Plugin.Commit, $"[Diff]({status.DiffUrl})"); } else { - buildsMd.AddRow("❌", task.InternalName, task.Manifest.Plugin.Commit, "Build failed"); + Log.Error("Could not build: {Name} - {Sha}", task.InternalName, + task.Manifest.Plugin.Commit); + buildsMd.AddRow("❌", task.InternalName, task.Manifest.Plugin.Commit, $"Build failed ([Diff]({status.DiffUrl}))"); } } catch (BuildProcessor.PluginCommitException ex)