Skip to content

Commit f73df07

Browse files
committed
Add github actions annotations
1 parent a14e25b commit f73df07

File tree

6 files changed

+37
-32
lines changed

6 files changed

+37
-32
lines changed

src/Elastic.Markdown/Diagnostics/DiagnosticsChannel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public readonly record struct Diagnostic
4242
{
4343
public Severity Severity { get; init; }
4444
public int Line { get; init; }
45-
public int? Position { get; init; }
45+
public int? Column { get; init; }
46+
public int? Length { get; init; }
4647
public string File { get; init; }
4748
public string Message { get; init; }
4849
}

src/Elastic.Markdown/Diagnostics/ProcessorDiagnosticExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ namespace Elastic.Markdown.Diagnostics;
66

77
public static class ProcessorDiagnosticExtensions
88
{
9-
public static void EmitError(this InlineProcessor processor, int line, int position, string message)
9+
public static void EmitError(this InlineProcessor processor, int line, int column, int length, string message)
1010
{
1111
var d = new Diagnostic
1212
{
1313
Severity = Severity.Error,
1414
File = processor.GetContext().Path.FullName,
15-
Position = position,
15+
Column = column,
1616
Line = line,
17-
Message = message
17+
Message = message,
18+
Length = length
1819
};
1920
processor.GetBuildContext().Collector.Channel.Write(d);
2021
}

src/Elastic.Markdown/DocumentationGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ await Parallel.ForEachAsync(DocumentationSet.Files, ctx, async (file, token) =>
131131
await GenerateDocumentationState(ctx);
132132

133133
await collectTask;
134+
await Context.Collector.Channel.Reader.Completion;
134135
await Context.Collector.StopAsync(ctx);
135136

136137

src/Elastic.Markdown/Myst/Substitution/SubstitutionParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
155155
DelimiterCount = openSticks
156156
};
157157
if (!found)
158-
processor.EmitError(line + 1, column + 3 , $"Substitution key {{{key}}} is undefined");
158+
processor.EmitError(line + 1, column + 3, substitutionLeaf.Span.Length - 3, $"Substitution key {{{key}}} is undefined");
159159

160160
if (processor.TrackTrivia)
161161
{

src/docs-builder/Cli/Commands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public async Task<int> Generate(
5656
Force = force ?? false,
5757
ReadFileSystem = fileSystem,
5858
WriteFileSystem = fileSystem,
59-
Collector = new ConsoleDiagnosticsCollector(logger)
59+
Collector = new ConsoleDiagnosticsCollector(logger, githubActionsService)
6060
};
6161
var generator = DocumentationGenerator.Create(path, output, context, logger);
6262
await generator.GenerateAll(ctx);

src/docs-builder/Diagnostics/ErrorCollector.cs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System.Diagnostics.CodeAnalysis;
22
using System.Text;
3+
using Actions.Core;
4+
using Actions.Core.Services;
35
using Cysharp.IO;
46
using Elastic.Markdown.Diagnostics;
57
using Errata;
6-
using Microsoft.Extensions.Hosting;
78
using Microsoft.Extensions.Logging;
89
using Spectre.Console;
910
using Diagnostic = Elastic.Markdown.Diagnostics.Diagnostic;
@@ -21,8 +22,27 @@ public bool TryGet(string id, [NotNullWhen(true)] out Source? source)
2122
}
2223
}
2324

24-
public class ConsoleDiagnosticsCollector(ILoggerFactory loggerFactory)
25-
: DiagnosticsCollector(loggerFactory, [])
25+
public class GithubAnnotationOutput(ICoreService githubActions) : IDiagnosticsOutput
26+
{
27+
public void Write(Diagnostic diagnostic)
28+
{
29+
if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("GITHUB_ACTION"))) return;
30+
var properties = new AnnotationProperties
31+
{
32+
File = diagnostic.File,
33+
StartColumn = diagnostic.Column,
34+
StartLine = diagnostic.Line,
35+
EndColumn = diagnostic.Column + diagnostic.Length ?? 1
36+
};
37+
if (diagnostic.Severity == Severity.Error)
38+
githubActions.WriteError(diagnostic.Message, properties);
39+
if (diagnostic.Severity == Severity.Warning)
40+
githubActions.WriteWarning(diagnostic.Message, properties);
41+
}
42+
}
43+
44+
public class ConsoleDiagnosticsCollector(ILoggerFactory loggerFactory, ICoreService? githubActions = null)
45+
: DiagnosticsCollector(loggerFactory, githubActions != null ? [new GithubAnnotationOutput(githubActions)] : [])
2646
{
2747
private readonly List<Diagnostic> _items = new();
2848

@@ -42,35 +62,17 @@ public override async Task StopAsync(Cancel ctx)
4262
{
4363
Severity.Error =>
4464
Errata.Diagnostic.Error(item.Message)
45-
.WithLabel(new Label(item.File, new Location(item.Line, item.Position ?? 0), "bad substitution")
46-
.WithLength(8)
47-
.WithPriority(1)
48-
.WithColor(Color.Red))
49-
50-
,
65+
.WithLabel(new Label(item.File, new Location(item.Line, item.Column ?? 0), "bad substitution")
66+
.WithLength(item.Length ?? 3)
67+
.WithPriority(1)
68+
.WithColor(Color.Red)),
5169
Severity.Warning =>
5270
Errata.Diagnostic.Warning(item.Message),
5371
_ => Errata.Diagnostic.Info(item.Message)
5472
};
5573
report.AddDiagnostic(d);
56-
/*
57-
report.AddDiagnostic(
58-
Errata.Diagnostic.Error("Operator '/' cannot be applied to operands of type 'string' and 'int'")
59-
.WithCode("CS0019")
60-
.WithNote("Try changing the type")
61-
.WithLabel(new Label("Demo/Files/Program.cs", new Location(15, 23), "This is of type 'int'")
62-
.WithLength(3)
63-
.WithPriority(1)
64-
.WithColor(Color.Yellow))
65-
.WithLabel(new Label("Demo/Files/Program.cs", new Location(15, 27), "Division is not possible")
66-
.WithPriority(3)
67-
.WithColor(Color.Red))
68-
.WithLabel(new Label("Demo/Files/Program.cs", new Location(15, 29), "This is of type 'string'")
69-
.WithLength(3)
70-
.WithPriority(2)
71-
.WithColor(Color.Blue)));
72-
*/
7374
}
75+
7476
// Render the report
7577
report.Render(AnsiConsole.Console);
7678
AnsiConsole.WriteLine();

0 commit comments

Comments
 (0)