From f73df0745d29a56a1d7e9e445ad2943ea4578b12 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Fri, 8 Nov 2024 12:41:16 +0100 Subject: [PATCH] Add github actions annotations --- .../Diagnostics/DiagnosticsChannel.cs | 3 +- .../ProcessorDiagnosticExtensions.cs | 7 +-- .../DocumentationGenerator.cs | 1 + .../Myst/Substitution/SubstitutionParser.cs | 2 +- src/docs-builder/Cli/Commands.cs | 2 +- .../Diagnostics/ErrorCollector.cs | 54 ++++++++++--------- 6 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/Elastic.Markdown/Diagnostics/DiagnosticsChannel.cs b/src/Elastic.Markdown/Diagnostics/DiagnosticsChannel.cs index 5f3526fe..14c4414b 100644 --- a/src/Elastic.Markdown/Diagnostics/DiagnosticsChannel.cs +++ b/src/Elastic.Markdown/Diagnostics/DiagnosticsChannel.cs @@ -42,7 +42,8 @@ public readonly record struct Diagnostic { public Severity Severity { get; init; } public int Line { get; init; } - public int? Position { get; init; } + public int? Column { get; init; } + public int? Length { get; init; } public string File { get; init; } public string Message { get; init; } } diff --git a/src/Elastic.Markdown/Diagnostics/ProcessorDiagnosticExtensions.cs b/src/Elastic.Markdown/Diagnostics/ProcessorDiagnosticExtensions.cs index a5be1bce..13a687fa 100644 --- a/src/Elastic.Markdown/Diagnostics/ProcessorDiagnosticExtensions.cs +++ b/src/Elastic.Markdown/Diagnostics/ProcessorDiagnosticExtensions.cs @@ -6,15 +6,16 @@ namespace Elastic.Markdown.Diagnostics; public static class ProcessorDiagnosticExtensions { - public static void EmitError(this InlineProcessor processor, int line, int position, string message) + public static void EmitError(this InlineProcessor processor, int line, int column, int length, string message) { var d = new Diagnostic { Severity = Severity.Error, File = processor.GetContext().Path.FullName, - Position = position, + Column = column, Line = line, - Message = message + Message = message, + Length = length }; processor.GetBuildContext().Collector.Channel.Write(d); } diff --git a/src/Elastic.Markdown/DocumentationGenerator.cs b/src/Elastic.Markdown/DocumentationGenerator.cs index efd9e18f..770693f1 100644 --- a/src/Elastic.Markdown/DocumentationGenerator.cs +++ b/src/Elastic.Markdown/DocumentationGenerator.cs @@ -131,6 +131,7 @@ await Parallel.ForEachAsync(DocumentationSet.Files, ctx, async (file, token) => await GenerateDocumentationState(ctx); await collectTask; + await Context.Collector.Channel.Reader.Completion; await Context.Collector.StopAsync(ctx); diff --git a/src/Elastic.Markdown/Myst/Substitution/SubstitutionParser.cs b/src/Elastic.Markdown/Myst/Substitution/SubstitutionParser.cs index 1c7c958b..7afa98f2 100644 --- a/src/Elastic.Markdown/Myst/Substitution/SubstitutionParser.cs +++ b/src/Elastic.Markdown/Myst/Substitution/SubstitutionParser.cs @@ -155,7 +155,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice) DelimiterCount = openSticks }; if (!found) - processor.EmitError(line + 1, column + 3 , $"Substitution key {{{key}}} is undefined"); + processor.EmitError(line + 1, column + 3, substitutionLeaf.Span.Length - 3, $"Substitution key {{{key}}} is undefined"); if (processor.TrackTrivia) { diff --git a/src/docs-builder/Cli/Commands.cs b/src/docs-builder/Cli/Commands.cs index 139a2233..ae00121a 100644 --- a/src/docs-builder/Cli/Commands.cs +++ b/src/docs-builder/Cli/Commands.cs @@ -56,7 +56,7 @@ public async Task Generate( Force = force ?? false, ReadFileSystem = fileSystem, WriteFileSystem = fileSystem, - Collector = new ConsoleDiagnosticsCollector(logger) + Collector = new ConsoleDiagnosticsCollector(logger, githubActionsService) }; var generator = DocumentationGenerator.Create(path, output, context, logger); await generator.GenerateAll(ctx); diff --git a/src/docs-builder/Diagnostics/ErrorCollector.cs b/src/docs-builder/Diagnostics/ErrorCollector.cs index ceee8905..430d83b9 100644 --- a/src/docs-builder/Diagnostics/ErrorCollector.cs +++ b/src/docs-builder/Diagnostics/ErrorCollector.cs @@ -1,9 +1,10 @@ using System.Diagnostics.CodeAnalysis; using System.Text; +using Actions.Core; +using Actions.Core.Services; using Cysharp.IO; using Elastic.Markdown.Diagnostics; using Errata; -using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Spectre.Console; using Diagnostic = Elastic.Markdown.Diagnostics.Diagnostic; @@ -21,8 +22,27 @@ public bool TryGet(string id, [NotNullWhen(true)] out Source? source) } } -public class ConsoleDiagnosticsCollector(ILoggerFactory loggerFactory) - : DiagnosticsCollector(loggerFactory, []) +public class GithubAnnotationOutput(ICoreService githubActions) : IDiagnosticsOutput +{ + public void Write(Diagnostic diagnostic) + { + if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("GITHUB_ACTION"))) return; + var properties = new AnnotationProperties + { + File = diagnostic.File, + StartColumn = diagnostic.Column, + StartLine = diagnostic.Line, + EndColumn = diagnostic.Column + diagnostic.Length ?? 1 + }; + if (diagnostic.Severity == Severity.Error) + githubActions.WriteError(diagnostic.Message, properties); + if (diagnostic.Severity == Severity.Warning) + githubActions.WriteWarning(diagnostic.Message, properties); + } +} + +public class ConsoleDiagnosticsCollector(ILoggerFactory loggerFactory, ICoreService? githubActions = null) + : DiagnosticsCollector(loggerFactory, githubActions != null ? [new GithubAnnotationOutput(githubActions)] : []) { private readonly List _items = new(); @@ -42,35 +62,17 @@ public override async Task StopAsync(Cancel ctx) { Severity.Error => Errata.Diagnostic.Error(item.Message) - .WithLabel(new Label(item.File, new Location(item.Line, item.Position ?? 0), "bad substitution") - .WithLength(8) - .WithPriority(1) - .WithColor(Color.Red)) - - , + .WithLabel(new Label(item.File, new Location(item.Line, item.Column ?? 0), "bad substitution") + .WithLength(item.Length ?? 3) + .WithPriority(1) + .WithColor(Color.Red)), Severity.Warning => Errata.Diagnostic.Warning(item.Message), _ => Errata.Diagnostic.Info(item.Message) }; report.AddDiagnostic(d); - /* - report.AddDiagnostic( - Errata.Diagnostic.Error("Operator '/' cannot be applied to operands of type 'string' and 'int'") - .WithCode("CS0019") - .WithNote("Try changing the type") - .WithLabel(new Label("Demo/Files/Program.cs", new Location(15, 23), "This is of type 'int'") - .WithLength(3) - .WithPriority(1) - .WithColor(Color.Yellow)) - .WithLabel(new Label("Demo/Files/Program.cs", new Location(15, 27), "Division is not possible") - .WithPriority(3) - .WithColor(Color.Red)) - .WithLabel(new Label("Demo/Files/Program.cs", new Location(15, 29), "This is of type 'string'") - .WithLength(3) - .WithPriority(2) - .WithColor(Color.Blue))); - */ } + // Render the report report.Render(AnsiConsole.Console); AnsiConsole.WriteLine();