From 7096f780ed358e108e356dac5c32954bb98ef32f Mon Sep 17 00:00:00 2001 From: mr-miles Date: Sat, 18 Apr 2026 18:13:21 +0100 Subject: [PATCH] Add configurable SarifLevel property to ToolConfig Exposes a `sarifLevel` config option (default "warning") that controls the SARIF result level emitted by SarifReporter. Valid values mirror SARIF 2.1.0: error, warning, note, none. The example config uses "note". --- dotnet-diff-coverage.json | 3 ++- src/DotnetDiffCoverage/Commands/RootCommandBuilder.cs | 4 ++-- src/DotnetDiffCoverage/Config/ToolConfig.cs | 5 ++++- src/DotnetDiffCoverage/Output/SarifReporter.cs | 4 ++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/dotnet-diff-coverage.json b/dotnet-diff-coverage.json index 49e1206..d88e03b 100644 --- a/dotnet-diff-coverage.json +++ b/dotnet-diff-coverage.json @@ -25,5 +25,6 @@ "**/*.targets", "**/*.config" ], - "threshold": 0 + "threshold": 0, + "sarifLevel": "note" } diff --git a/src/DotnetDiffCoverage/Commands/RootCommandBuilder.cs b/src/DotnetDiffCoverage/Commands/RootCommandBuilder.cs index ba42769..249188d 100644 --- a/src/DotnetDiffCoverage/Commands/RootCommandBuilder.cs +++ b/src/DotnetDiffCoverage/Commands/RootCommandBuilder.cs @@ -245,12 +245,12 @@ private static async Task HandleAsync( var sarifReporter = services.GetRequiredService(); if (outputSarif.Name == "-") { - await sarifReporter.WriteAsync(result, Console.OpenStandardOutput()); + await sarifReporter.WriteAsync(result, Console.OpenStandardOutput(), config.SarifLevel); } else { using var stream = outputSarif.Open(FileMode.Create); - await sarifReporter.WriteAsync(result, stream); + await sarifReporter.WriteAsync(result, stream, config.SarifLevel); } } diff --git a/src/DotnetDiffCoverage/Config/ToolConfig.cs b/src/DotnetDiffCoverage/Config/ToolConfig.cs index 4a718dc..e618de3 100644 --- a/src/DotnetDiffCoverage/Config/ToolConfig.cs +++ b/src/DotnetDiffCoverage/Config/ToolConfig.cs @@ -12,7 +12,7 @@ public sealed class ToolConfig /// Glob patterns for non-source files to exclude from diff analysis entirely. public IReadOnlyList ExcludePatterns { get; init; } = DefaultExcludePatterns; - /// Coverage threshold (0â100). Exit code 1 if uncovered % exceeds this. + /// Coverage threshold (0–100). Exit code 1 if uncovered % exceeds this. public double Threshold { get; init; } = 0.0; /// Default coverage format if not specified on CLI. @@ -21,6 +21,9 @@ public sealed class ToolConfig /// Prefix to strip from coverage file paths before matching against diff paths. public string? CoveragePathPrefix { get; init; } + /// SARIF result level for uncovered lines. Valid values: "error", "warning", "note", "none". + public string SarifLevel { get; init; } = "warning"; + public static readonly IReadOnlyList DefaultTestFilePatterns = new[] { "tests/**", diff --git a/src/DotnetDiffCoverage/Output/SarifReporter.cs b/src/DotnetDiffCoverage/Output/SarifReporter.cs index dd86234..970bc9c 100644 --- a/src/DotnetDiffCoverage/Output/SarifReporter.cs +++ b/src/DotnetDiffCoverage/Output/SarifReporter.cs @@ -24,13 +24,13 @@ private sealed record SarifRoot( [property: JsonPropertyName("$schema")] string Schema, object[] Runs); - public async Task WriteAsync(CrossReferenceResult result, Stream stream) + public async Task WriteAsync(CrossReferenceResult result, Stream stream, string level = "warning") { var results = result.Files .SelectMany(f => f.UncoveredRanges.Select(range => new { ruleId = "DC001", - level = "warning", + level, message = new { text = range.Start == range.End