Skip to content

Commit

Permalink
Show exception (if one was thrown) within the GitHub markdown summary
Browse files Browse the repository at this point in the history
  • Loading branch information
thomhurst committed Jul 27, 2024
1 parent f072afb commit 5b5f5cf
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
9 changes: 4 additions & 5 deletions GitVersion.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
mode: Mainline
branches: {}
mode: ContinuousDeployment
strategies:
- Mainline
ignore:
sha:
- f7ef4687260996a62c4fed816462ab9592556115
- 606fab3291986219f315be3940c910405420b3c6

merge-message-formats: {}
- 606fab3291986219f315be3940c910405420b3c6
1 change: 0 additions & 1 deletion src/ModularPipelines.Build/Modules/PackProjectsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace ModularPipelines.Build.Modules;
[DependsOn<PackageFilesRemovalModule>]
[DependsOn<CodeFormattedNicelyModule>]
[DependsOn<FindProjectDependenciesModule>]

[DependsOn<ChangedFilesInPullRequestModule>]
[RunOnLinuxOnly]
public class PackProjectsModule : Module<CommandResult[]>
Expand Down
1 change: 1 addition & 0 deletions src/ModularPipelines.Build/ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Show exception (if one was thrown) within the GitHub markdown summary
32 changes: 31 additions & 1 deletion src/ModularPipelines.Git/GitVersioning.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
using System.Text.Json;
using Microsoft.Extensions.Logging;
using ModularPipelines.Context;
using ModularPipelines.FileSystem;
using ModularPipelines.Git.Models;
using ModularPipelines.Logging;
using ModularPipelines.Options;
using File = ModularPipelines.FileSystem.File;

namespace ModularPipelines.Git;

internal class GitVersioning : IGitVersioning
{
private readonly IGitInformation _gitInformation;
private readonly ICommand _command;
private readonly IModuleLoggerProvider _moduleLoggerProvider;

private readonly Folder _temporaryFolder;

private static readonly SemaphoreSlim _semaphoreSlim = new(1, 1);
private static GitVersionInformation? _prefetchedGitVersionInformation;

public GitVersioning(IFileSystemContext fileSystemContext, IGitInformation gitInformation, ICommand command)
public GitVersioning(IFileSystemContext fileSystemContext, IGitInformation gitInformation, ICommand command, IModuleLoggerProvider moduleLoggerProvider)
{
_gitInformation = gitInformation;
_command = command;
_moduleLoggerProvider = moduleLoggerProvider;
_temporaryFolder = fileSystemContext.CreateTemporaryFolder();
}

Expand All @@ -42,6 +47,8 @@ await _command.ExecuteCommandLineTool(new CommandLineToolOptions("dotnet")
},
});

await TryWriteConfigurationFile();

var gitVersionOutput = await _command.ExecuteCommandLineTool(
new CommandLineToolOptions(Path.Combine(_temporaryFolder, "dotnet-gitversion"))
{
Expand All @@ -60,4 +67,27 @@ await _command.ExecuteCommandLineTool(new CommandLineToolOptions("dotnet")
_semaphoreSlim.Release();
}
}

private async Task TryWriteConfigurationFile()
{
try
{
var file = new File(Path.Combine(_gitInformation.Root.Path, "GitVersion.yml"));

if (!file.Exists)
{
await file.WriteAsync(
"""
mode: ContinuousDeployment
strategies:
- Mainline
"""
);
}
}
catch (Exception e)
{
_moduleLoggerProvider.GetLogger().LogWarning(e, "Error defining GitVersion.yml configuration");
}
}
}
20 changes: 19 additions & 1 deletion src/ModularPipelines.GitHub/GitHubMarkdownSummaryGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public async Task OnEndAsync(IPipelineHookContext pipelineContext, PipelineSumma
{
var mermaid = await GenerateMermaidSummary(pipelineSummary);
var table = await GenerateTableSummary(pipelineSummary);
var exception = await GetException(pipelineSummary);

var stepSummaryVariable = pipelineContext.Environment.EnvironmentVariables.GetEnvironmentVariable("GITHUB_STEP_SUMMARY");

Expand All @@ -32,7 +33,24 @@ public async Task OnEndAsync(IPipelineHookContext pipelineContext, PipelineSumma
return;
}

await pipelineContext.FileSystem.GetFile(stepSummaryVariable).WriteAsync($"{mermaid}\n\n{table}\n\n{_afterPipelineLogger.GetOutput()}");
await pipelineContext.FileSystem.GetFile(stepSummaryVariable).WriteAsync($"{mermaid}\n\n{table}\n\n{_afterPipelineLogger.GetOutput()}{exception}");
}

private async Task<string> GetException(PipelineSummary pipelineSummary)
{
var results = await pipelineSummary.GetModuleResultsAsync();

var exception = results
.FirstOrDefault(x => x.ModuleStatus == Status.Failed)
?.Exception
?? results.Select(x => x.Exception).FirstOrDefault();

if (exception is null)
{
return string.Empty;
}

return $"\n\n```\n{exception}\n```";
}

private async Task<string> GenerateMermaidSummary(PipelineSummary pipelineSummary)
Expand Down

0 comments on commit 5b5f5cf

Please sign in to comment.