Skip to content

Commit

Permalink
repo commit, ownership checks
Browse files Browse the repository at this point in the history
  • Loading branch information
goaaats committed Jul 30, 2022
1 parent 28fb827 commit 224cbd4
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 43 deletions.
34 changes: 33 additions & 1 deletion Plogon/BuildProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ async Task GetDep(string name, NugetLockfile.Dependency dependency)
});
}

public async Task<bool> ProcessTask(BuildTask task)
public async Task<bool> ProcessTask(BuildTask task, bool commit)
{
var folderName = $"{task.InternalName}-{task.Manifest.Plugin.Commit}";
var work = this.workFolder.CreateSubdirectory($"{folderName}-work");
Expand Down Expand Up @@ -290,6 +290,38 @@ await this.dockerClient.Containers.RemoveContainerAsync(containerCreateResponse.
Force = true,
});

if (exitCode == 0 && commit)
{
var dpOutput = new DirectoryInfo(Path.Combine(output.FullName, task.InternalName));

if (!dpOutput.Exists)
throw new Exception("DalamudPackager output not found?");

try
{
this.pluginRepository.UpdatePluginHave(task.Channel, task.InternalName, task.Manifest.Plugin.Commit);
var repoOutputDir = this.pluginRepository.GetPluginOutputDirectory(task.Channel, task.InternalName);

foreach (var file in dpOutput.GetFiles())
{
file.CopyTo(Path.Combine(repoOutputDir.FullName, file.Name), true);
}
}
catch (Exception ex)
{
Log.Error(ex, "Error during plugin commit");
throw new PluginCommitException(ex);
}
}

return exitCode == 0;
}

public class PluginCommitException : Exception
{
public PluginCommitException(Exception inner)
: base("Could not commit plugin.", inner)
{
}
}
}
129 changes: 92 additions & 37 deletions Plogon/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,67 +17,122 @@ class Program
/// <param name="workFolder">The folder to store temporary files and build output in.</param>
/// <param name="staticFolder">The 'static' folder that holds script files.</param>
/// <param name="ci">Running in CI.</param>
/// <param name="commit">Commit to repo.</param>
/// <param name="ownerId">Creator of the request.</param>
static async Task Main(DirectoryInfo outputFolder, DirectoryInfo manifestFolder, DirectoryInfo workFolder,
DirectoryInfo staticFolder, bool ci = false)
DirectoryInfo staticFolder, bool ci = false, bool commit = false, int ownerId = -1)
{
SetupLogging();

var githubSummary = "## Build Summary";
var githubSummary = "## Build Summary\n### Base Images\n";
GitHubOutputBuilder.SetActive(ci);

var buildProcessor = new BuildProcessor(outputFolder, manifestFolder, workFolder, staticFolder);
var tasks = buildProcessor.GetTasks();
var aborted = false;

if (!tasks.Any())
try
{
Log.Information("Nothing to do, goodbye...");
githubSummary += "No tasks were detected, this is probably an issue on our side, please report.";
}
else
{
GitHubOutputBuilder.StartGroup("Get images");
var images = await buildProcessor.SetupDockerImage();
Debug.Assert(images.Any(), "No images returned");
var buildProcessor = new BuildProcessor(outputFolder, manifestFolder, workFolder, staticFolder);
var tasks = buildProcessor.GetTasks();

var imagesMd = MarkdownTableBuilder.Create("Tags", "Created");
foreach (var imageInspectResponse in images)
if (!tasks.Any())
{
imagesMd.AddRow(string.Join(",", imageInspectResponse.RepoTags), imageInspectResponse.Created.ToLongDateString());
Log.Information("Nothing to do, goodbye...");
githubSummary += "No tasks were detected, this is probably an issue on our side, please report.";
}
GitHubOutputBuilder.EndGroup();
else
{
GitHubOutputBuilder.StartGroup("Get images");
var images = await buildProcessor.SetupDockerImage();
Debug.Assert(images.Any(), "No images returned");

githubSummary += imagesMd.ToString();
var imagesMd = MarkdownTableBuilder.Create("Tags", "Created");
foreach (var imageInspectResponse in images)
{
imagesMd.AddRow(string.Join(",", imageInspectResponse.RepoTags),
imageInspectResponse.Created.ToLongDateString());
}

foreach (var task in tasks)
{
GitHubOutputBuilder.StartGroup($"Build {task.InternalName} ({task.Manifest.Plugin.Commit})");
GitHubOutputBuilder.EndGroup();

githubSummary += imagesMd.ToString();
githubSummary += "\n### Build Results";

var buildsMd = MarkdownTableBuilder.Create("", "Name", "Commit", "Status");

try
foreach (var task in tasks)
{
Log.Information("Need: {Name} - {Sha} (have {HaveCommit})", task.InternalName,
task.Manifest.Plugin.Commit,
task.HaveCommit ?? "nothing");
var status = await buildProcessor.ProcessTask(task);
GitHubOutputBuilder.StartGroup($"Build {task.InternalName} ({task.Manifest.Plugin.Commit})");

if (!status)
if (ownerId > 0 && task.Manifest.Plugin.Owners.All(x => x != ownerId))
{
Log.Error("Could not build: {Name} - {Sha}", task.InternalName, task.Manifest.Plugin.Commit);
Log.Information("Not owned: {Name} - {Sha} (have {HaveCommit})", task.InternalName,
task.Manifest.Plugin.Commit,
task.HaveCommit ?? "nothing");

buildsMd.AddRow("👽", task.InternalName, task.Manifest.Plugin.Commit, "Not your plugin");
}
}
catch (Exception ex)
{
Log.Error(ex, "Could not build");

if (aborted)
{
Log.Information("Aborted, won't run: {Name} - {Sha} (have {HaveCommit})", task.InternalName,
task.Manifest.Plugin.Commit,
task.HaveCommit ?? "nothing");

buildsMd.AddRow("", task.InternalName, task.Manifest.Plugin.Commit, "Not ran");
continue;
}

try
{
Log.Information("Need: {Name} - {Sha} (have {HaveCommit})", task.InternalName,
task.Manifest.Plugin.Commit,
task.HaveCommit ?? "nothing");
var status = await buildProcessor.ProcessTask(task, commit);

if (!status)
{
Log.Error("Could not build: {Name} - {Sha}", task.InternalName,
task.Manifest.Plugin.Commit);

buildsMd.AddRow("✔️", task.InternalName, task.Manifest.Plugin.Commit, string.Empty);
}
else
{
buildsMd.AddRow("", task.InternalName, task.Manifest.Plugin.Commit, "Build failed");
}
}
catch (BuildProcessor.PluginCommitException ex)
{
// We just can't make sure that the state of the repo is consistent here...
// Need to abort.

Log.Error(ex, "Repo consistency can't be guaranteed, aborting...");
buildsMd.AddRow("⁉️", task.InternalName, task.Manifest.Plugin.Commit, "Could not commit to repo");
aborted = true;
}
catch (Exception ex)
{
Log.Error(ex, "Could not build");
buildsMd.AddRow("😰", task.InternalName, task.Manifest.Plugin.Commit, $"Build system error: {ex.Message}");
}

GitHubOutputBuilder.EndGroup();
}

GitHubOutputBuilder.EndGroup();
githubSummary += buildsMd.ToString();
}
}

var githubSummaryFilePath = Environment.GetEnvironmentVariable("GITHUB_STEP_SUMMARY");
if (!string.IsNullOrEmpty(githubSummaryFilePath))
finally
{
await File.WriteAllTextAsync(githubSummaryFilePath, githubSummary);
var githubSummaryFilePath = Environment.GetEnvironmentVariable("GITHUB_STEP_SUMMARY");
if (!string.IsNullOrEmpty(githubSummaryFilePath))
{
await File.WriteAllTextAsync(githubSummaryFilePath, githubSummary);
}
}

if (aborted)
Environment.ExitCode = -1;
}

private static void SetupLogging()
Expand Down
45 changes: 40 additions & 5 deletions Plogon/Repo/PluginRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using Serilog;
using Tomlyn;
Expand All @@ -11,6 +12,7 @@ namespace Plogon.Repo;
public class PluginRepository
{
private readonly DirectoryInfo repoDirectory;
private FileInfo StateFile => new FileInfo(Path.Combine(repoDirectory.FullName, "State.toml"));

private State state;

Expand All @@ -21,11 +23,10 @@ public class PluginRepository
public PluginRepository(DirectoryInfo repoDirectory)
{
this.repoDirectory = repoDirectory;

var stateFile = new FileInfo(Path.Combine(repoDirectory.FullName, "State.toml"));
if (stateFile.Exists)

if (StateFile.Exists)
{
this.state = Toml.ToModel<State>(stateFile.OpenText().ReadToEnd());
this.state = Toml.ToModel<State>(StateFile.OpenText().ReadToEnd());
}
else
{
Expand All @@ -35,6 +36,16 @@ public PluginRepository(DirectoryInfo repoDirectory)

Log.Information("Plugin repository at {repo} initialized", repoDirectory.FullName);
}

private void SaveState()
{
File.WriteAllText(this.StateFile.FullName, Toml.FromModel(this.state));
}

public DirectoryInfo GetPluginOutputDirectory(string channelName, string plugin)
{
return this.repoDirectory.CreateSubdirectory(channelName).CreateSubdirectory(plugin);
}

public State.Channel.PluginState? GetPluginState(string channelName, string plugin)
{
Expand All @@ -52,4 +63,28 @@ public PluginRepository(DirectoryInfo repoDirectory)

return null;
}

public void UpdatePluginHave(string channelName, string plugin, string haveCommit)
{
if (!this.state.Channels.ContainsKey(channelName))
{
this.state.Channels[channelName] = new State.Channel();
}

var channel = this.state.Channels[channelName];
if (channel.Plugins.TryGetValue(plugin, out var pluginState))
{
pluginState.BuiltCommit = haveCommit;
}
else
{
var newState = new State.Channel.PluginState()
{
BuiltCommit = haveCommit,
};
channel.Plugins[plugin] = newState;
}

SaveState();
}
}

0 comments on commit 224cbd4

Please sign in to comment.