-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AddPipelineGlobalHooks<GitHubMarkdownSummaryGenerator>
- Loading branch information
Showing
3 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
null | ||
- GitHub Actions Markdown Summary - Thanks to @MattParkerDev ! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/ModularPipelines.GitHub/GitHubMarkdownSummaryGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
using ModularPipelines.Context; | ||
using ModularPipelines.Enums; | ||
using ModularPipelines.Interfaces; | ||
using ModularPipelines.Models; | ||
|
||
namespace ModularPipelines.GitHub; | ||
|
||
internal class GitHubMarkdownSummaryGenerator : IPipelineGlobalHooks | ||
{ | ||
public Task OnStartAsync(IPipelineHookContext pipelineContext) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public async Task OnEndAsync(IPipelineHookContext pipelineContext, PipelineSummary pipelineSummary) | ||
{ | ||
var mermaid = await GenerateMermaidSummary(pipelineSummary); | ||
var table = await GenerateTableSummary(pipelineSummary); | ||
|
||
var stepSummaryVariable = pipelineContext.Environment.EnvironmentVariables.GetEnvironmentVariable("GITHUB_STEP_SUMMARY"); | ||
|
||
if (string.IsNullOrEmpty(stepSummaryVariable)) | ||
{ | ||
return; | ||
} | ||
|
||
await pipelineContext.FileSystem.GetFile(stepSummaryVariable).WriteAsync($"{mermaid}\n\n{table}"); | ||
} | ||
|
||
private async Task<string> GenerateMermaidSummary(PipelineSummary pipelineSummary) | ||
{ | ||
var results = await pipelineSummary.GetModuleResultsAsync(); | ||
|
||
var stepStringList = results | ||
.OrderBy(x => x.ModuleEnd) | ||
.ThenBy(s => s.ModuleStart) | ||
.Select(x => | ||
{ | ||
var (startTime, endTime) = (x.ModuleStart, x.ModuleEnd); | ||
return $"{x.ModuleName} :{AddCritIfFailed(x)} {startTime:mm:ss:fff}, {endTime:mm:ss:fff}"; | ||
}).ToList(); | ||
|
||
var text = $""" | ||
```mermaid | ||
--- | ||
config: | ||
theme: base | ||
themeVariables: | ||
primaryColor: "#007d15" | ||
primaryTextColor: "#fff" | ||
primaryBorderColor: "#02ad1e" | ||
lineColor: "#F8B229" | ||
secondaryColor: "#006100" | ||
tertiaryColor: "#fff" | ||
darkmode: "true" | ||
titleColor: "#fff" | ||
gantt: | ||
leftPadding: 40 | ||
rightPadding: 120 | ||
--- | ||
gantt | ||
dateFormat mm:ss:SSS | ||
title Run Summary | ||
axisFormat %M:%S | ||
{string.Join("\n", stepStringList)} | ||
``` | ||
"""; | ||
|
||
return text; | ||
} | ||
|
||
private async Task<string> GenerateTableSummary(PipelineSummary pipelineSummary) | ||
{ | ||
var results = await pipelineSummary.GetModuleResultsAsync(); | ||
|
||
var stepStringList = results.OrderBy(x => x.ModuleEnd) | ||
.ThenBy(s => s.ModuleStart) | ||
.Select(stepContainer => | ||
{ | ||
var (startTime, endTime, duration) = (stepContainer.ModuleStart, stepContainer.ModuleEnd, stepContainer.ModuleDuration); | ||
var text = $"| {stepContainer.ModuleName} | {GetStatusString(stepContainer.ModuleStatus)} | {startTime:HH:mm:ss} | {endTime:HH:mm:ss} | {duration} |"; | ||
return text; | ||
} | ||
).ToList(); | ||
|
||
var (globalStartTime, globalEndTime, globalDuration) = (pipelineSummary.Start, pipelineSummary.End, pipelineSummary.TotalDuration); | ||
var pipelineStatusString = GetStatusString(pipelineSummary.Status); | ||
var overallSummaryString = $"| **Total** | **{pipelineStatusString}** | **{globalStartTime:HH:mm:ss}** | **{globalEndTime:HH:mm:ss}** | **{globalDuration}** |"; | ||
var text = $""" | ||
### Run Summary | ||
| Step | Status | Start | End | Duration | | ||
| --- | --- | --- | --- | --- | | ||
{string.Join("\n", stepStringList)} | ||
{overallSummaryString} | ||
"""; | ||
|
||
return text; | ||
} | ||
|
||
private static string AddCritIfFailed(IModuleResult moduleResult) | ||
{ | ||
return moduleResult.ModuleResultType is ModuleResultType.Failure | ||
? "crit," | ||
: string.Empty; | ||
} | ||
|
||
private static string GetStatusString(Status status) | ||
{ | ||
return status switch | ||
{ | ||
Status.Successful or Status.UsedHistory => $$$"""${\textsf{\color{lightgreen}{{{status}}}}}$""", | ||
Status.NotYetStarted or Status.IgnoredFailure or Status.Processing or Status.Skipped => | ||
$$$"""${\textsf{\color{orange}{{{status}}}}}$""", | ||
Status.PipelineTerminated or Status.TimedOut or Status.Failed or Status.Unknown => | ||
$$$"""${\textsf{\color{red}{{{status}}}}}$""", | ||
_ => throw new ArgumentOutOfRangeException(nameof(status), status, null), | ||
}; | ||
} | ||
} |