Skip to content

Commit

Permalink
Add github actions annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz committed Nov 8, 2024
1 parent a14e25b commit f73df07
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 32 deletions.
3 changes: 2 additions & 1 deletion src/Elastic.Markdown/Diagnostics/DiagnosticsChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/Elastic.Markdown/DocumentationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/docs-builder/Cli/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task<int> 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);
Expand Down
54 changes: 28 additions & 26 deletions src/docs-builder/Diagnostics/ErrorCollector.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Diagnostic> _items = new();

Expand All @@ -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();
Expand Down

0 comments on commit f73df07

Please sign in to comment.