|
1 | | -using System.Text.Encodings.Web; |
2 | 1 | using System.Text.Json; |
3 | | -using System.Text.Json.Serialization; |
4 | 2 | using JulianVerdurmen.SlnxValidator.Core.FileSystem; |
5 | 3 | using JulianVerdurmen.SlnxValidator.Core.Reporting; |
6 | 4 | using JulianVerdurmen.SlnxValidator.Core.ValidationResults; |
7 | 5 |
|
8 | 6 | namespace JulianVerdurmen.SlnxValidator.Core.SarifReporting; |
9 | 7 |
|
10 | | -public sealed class SarifReporter(IFileSystem fileSystem) : ISarifReporter |
| 8 | +public sealed class SarifReporter(IFileSystem fileSystem) : ReporterBase(fileSystem), ISarifReporter |
11 | 9 | { |
12 | 10 | private const string SarifSchema = "https://json.schemastore.org/sarif-2.1.0.json"; |
13 | 11 | private const string SarifVersion = "2.1.0"; |
14 | 12 | private const string ToolName = "slnx-validator"; |
15 | 13 | private const string ToolInformationUri = "https://github.com/304NotModified/SLNX-validator"; |
16 | 14 |
|
17 | | - private static readonly JsonSerializerOptions JsonOptions = new() |
| 15 | + public override async Task WriteReportAsync(ReportResults reportResults, Stream outputStream) |
18 | 16 | { |
19 | | - WriteIndented = true, |
20 | | - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
21 | | - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
22 | | - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, |
23 | | - Converters = { new JsonStringEnumConverter() } |
24 | | - }; |
25 | | - |
26 | | - public async Task WriteReportAsync(IReadOnlyList<FileValidationResult> results, string outputPath, |
27 | | - IReadOnlyDictionary<ValidationErrorCode, RuleSeverity?>? severityOverrides = null) |
28 | | - { |
29 | | - var directory = Path.GetDirectoryName(outputPath); |
30 | | - if (!string.IsNullOrEmpty(directory)) |
31 | | - fileSystem.CreateDirectory(directory); |
32 | | - |
33 | | - await using var stream = fileSystem.CreateFile(outputPath); |
34 | | - await WriteReportAsync(results, stream, severityOverrides); |
35 | | - } |
36 | | - |
37 | | - public async Task WriteReportAsync(IReadOnlyList<FileValidationResult> results, Stream outputStream, |
38 | | - IReadOnlyDictionary<ValidationErrorCode, RuleSeverity?>? severityOverrides = null) |
39 | | - { |
40 | | - var usedCodes = results |
41 | | - .SelectMany(r => r.Errors) |
42 | | - .Select(e => e.Code) |
43 | | - .Where(c => !IsIgnored(c, severityOverrides)) |
44 | | - .Distinct() |
45 | | - .OrderBy(c => (int)c) |
46 | | - .ToList(); |
| 17 | + var usedCodes = reportResults.UsedCodes; |
47 | 18 |
|
48 | 19 | var rules = usedCodes |
49 | | - .Select(c => BuildRule(c, severityOverrides)) |
| 20 | + .Select(c => BuildRule(c, reportResults.Overrides)) |
50 | 21 | .ToList(); |
51 | 22 |
|
52 | | - var sarifResults = results |
| 23 | + var sarifResults = reportResults.Results |
53 | 24 | .SelectMany(r => r.Errors |
54 | | - .Where(e => !IsIgnored(e.Code, severityOverrides)) |
55 | | - .Select(e => BuildResult(r.File, e, severityOverrides))) |
| 25 | + .Where(e => !reportResults.Overrides.IsIgnored(e.Code)) |
| 26 | + .Select(e => BuildResult(r.File, e, reportResults.Overrides))) |
56 | 27 | .ToList(); |
57 | 28 |
|
58 | 29 | var log = new SarifLog |
@@ -80,38 +51,24 @@ public async Task WriteReportAsync(IReadOnlyList<FileValidationResult> results, |
80 | 51 | await JsonSerializer.SerializeAsync(outputStream, log, JsonOptions); |
81 | 52 | } |
82 | 53 |
|
83 | | - private static bool IsIgnored(ValidationErrorCode code, IReadOnlyDictionary<ValidationErrorCode, RuleSeverity?>? overrides) => |
84 | | - overrides is not null && overrides.TryGetValue(code, out var severity) && severity is null; |
85 | | - |
86 | | - private static RuleSeverity GetEffectiveSeverity(ValidationErrorCode code, |
87 | | - IReadOnlyDictionary<ValidationErrorCode, RuleSeverity?>? overrides) |
88 | | - { |
89 | | - if (overrides is not null && overrides.TryGetValue(code, out var severity) && severity.HasValue) |
90 | | - return severity.Value; |
91 | | - return RuleProvider.Get(code).DefaultSeverity; |
92 | | - } |
93 | | - |
94 | | - private static SarifReportingDescriptor BuildRule(ValidationErrorCode code, |
95 | | - IReadOnlyDictionary<ValidationErrorCode, RuleSeverity?>? overrides) |
| 54 | + private static SarifReportingDescriptor BuildRule(ValidationErrorCode code, SeverityOverrides overrides) |
96 | 55 | { |
97 | | - var meta = RuleProvider.Get(code); |
98 | | - var effectiveSeverity = GetEffectiveSeverity(code, overrides); |
| 56 | + var resolved = RuleProvider.Resolve(code, overrides); |
99 | 57 | return new SarifReportingDescriptor |
100 | 58 | { |
101 | | - Id = meta.Id, |
102 | | - ShortDescription = new SarifMessage { Text = meta.Name }, |
103 | | - FullDescription = new SarifMessage { Text = meta.Description }, |
| 59 | + Id = resolved.Id, |
| 60 | + ShortDescription = new SarifMessage { Text = resolved.Name }, |
| 61 | + FullDescription = new SarifMessage { Text = resolved.Description }, |
104 | 62 | DefaultConfiguration = new SarifDefaultConfiguration |
105 | 63 | { |
106 | | - Level = MapToSarifLevel(effectiveSeverity) |
| 64 | + Level = MapToSarifLevel(resolved.EffectiveSeverity) |
107 | 65 | } |
108 | 66 | }; |
109 | 67 | } |
110 | 68 |
|
111 | | - private static SarifResult BuildResult(string filePath, ValidationError error, |
112 | | - IReadOnlyDictionary<ValidationErrorCode, RuleSeverity?>? overrides) |
| 69 | + private static SarifResult BuildResult(string filePath, ValidationError error, SeverityOverrides overrides) |
113 | 70 | { |
114 | | - var effectiveSeverity = GetEffectiveSeverity(error.Code, overrides); |
| 71 | + var effectiveSeverity = overrides.GetEffectiveSeverity(error.Code); |
115 | 72 | return new SarifResult |
116 | 73 | { |
117 | 74 | RuleId = error.Code.ToCode(), |
|
0 commit comments