Skip to content

Commit 4cf1b37

Browse files
csharpier
1 parent 759acb0 commit 4cf1b37

File tree

6 files changed

+77
-70
lines changed

6 files changed

+77
-70
lines changed

CodeQualityToGitlab/Roslynator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class Diagnostic
3131
public class Summary
3232
{
3333
[XmlElement(ElementName = "Diagnostic")]
34-
public required List<Diagnostic> Diagnostic { get; set; } = [];
34+
public required List<Diagnostic> Diagnostic { get; set; } = [ ];
3535
}
3636

3737
[XmlRoot(ElementName = "Location")]
@@ -48,7 +48,7 @@ public class Location
4848
public class Diagnostics
4949
{
5050
[XmlElement(ElementName = "Diagnostic")]
51-
public List<Diagnostic> Diagnostic { get; set; } = [];
51+
public List<Diagnostic> Diagnostic { get; set; } = [ ];
5252
}
5353

5454
[XmlRoot(ElementName = "Project")]
@@ -68,7 +68,7 @@ public class Project
6868
public class Projects
6969
{
7070
[XmlElement(ElementName = "Project")]
71-
public List<Project> Project { get; set; } = [];
71+
public List<Project> Project { get; set; } = [ ];
7272
}
7373

7474
[XmlRoot(ElementName = "CodeAnalysis")]

CodeQualityToGitlab/RoslynatorConverter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ public static List<CodeQuality> ConvertToCodeQualityRaw(FileInfo source, string?
2929
Location = new()
3030
{
3131
Path = GetPath(diagnostic, project, pathRoot),
32-
Lines = new()
33-
{ Begin = lineNumber }
32+
Lines = new() { Begin = lineNumber }
3433
},
3534
Fingerprint = Common.GetHash($"{project.Name}{diagnostic.Id}{lineNumber}")
3635
};

CodeQualityToGitlab/SarifConverters/Converter1.cs

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,57 +9,57 @@ public class Converter1(FileInfo source, string? pathRoot)
99
{
1010
public List<CodeQuality> Convert()
1111
{
12-
Log.Information("Sarif Version 1 detected");
12+
Log.Information("Sarif Version 1 detected");
1313

14-
var logContents = File.ReadAllText(source.FullName);
14+
var logContents = File.ReadAllText(source.FullName);
1515

16-
var settings = new JsonSerializerSettings
17-
{
18-
ContractResolver = SarifContractResolverVersionOne.Instance
19-
};
16+
var settings = new JsonSerializerSettings
17+
{
18+
ContractResolver = SarifContractResolverVersionOne.Instance
19+
};
2020

21-
var log = JsonConvert.DeserializeObject<SarifLogVersionOne>(logContents, settings);
21+
var log = JsonConvert.DeserializeObject<SarifLogVersionOne>(logContents, settings);
2222

23-
var results = log?.Runs
24-
.SelectMany(x => x.Results)
25-
.Where(r => r.SuppressionStates == SuppressionStatesVersionOne.None) ?? [];
23+
var results =
24+
log?.Runs
25+
.SelectMany(x => x.Results)
26+
.Where(r => r.SuppressionStates == SuppressionStatesVersionOne.None) ?? [ ];
2627

27-
var cqrs = new List<CodeQuality>();
28-
foreach (var result in results)
29-
{
30-
var begin = result.Locations?.FirstOrDefault();
28+
var cqrs = new List<CodeQuality>();
29+
foreach (var result in results)
30+
{
31+
var begin = result.Locations?.FirstOrDefault();
3132

32-
if (begin == null)
33-
{
34-
Log.Warning("An issue has no location, skipping: {Result}", result.Message);
35-
continue;
36-
}
33+
if (begin == null)
34+
{
35+
Log.Warning("An issue has no location, skipping: {Result}", result.Message);
36+
continue;
37+
}
3738

38-
try
39+
try
40+
{
41+
var cqr = new CodeQuality
3942
{
40-
var cqr = new CodeQuality
43+
Description = $"{result.RuleId}: {result.Message}",
44+
Severity = GetSeverity(result.Level),
45+
Location = new()
4146
{
42-
Description = $"{result.RuleId}: {result.Message}",
43-
Severity = GetSeverity(result.Level),
44-
Location = new()
45-
{
46-
Path = GetPath(pathRoot, begin),
47-
Lines = new()
48-
{ Begin = begin.ResultFile.Region.StartLine }
49-
},
50-
Fingerprint = Common.GetHash(
47+
Path = GetPath(pathRoot, begin),
48+
Lines = new() { Begin = begin.ResultFile.Region.StartLine }
49+
},
50+
Fingerprint = Common.GetHash(
5151
$"{result.RuleId}|{begin.ResultFile.Uri}|{begin.ResultFile.Region.StartLine}"
52-
)
53-
};
54-
cqrs.Add(cqr);
55-
}
56-
catch (Exception e)
57-
{
58-
Log.Error(e, "Could not convert {@Result}, skipping", result);
59-
}
52+
)
53+
};
54+
cqrs.Add(cqr);
6055
}
56+
catch (Exception e)
57+
{
58+
Log.Error(e, "Could not convert {@Result}, skipping", result);
59+
}
60+
}
6161

62-
return cqrs;
62+
return cqrs;
6363
}
6464

6565
private static string GetPath(string? pathRoot, LocationVersionOne begin)
@@ -68,8 +68,8 @@ private static string GetPath(string? pathRoot, LocationVersionOne begin)
6868
if (begin.ResultFile.Uri == null)
6969
{
7070
Log.Error(
71-
"There is no valid Path for the issue {@Region}, cannot create a path. Check the source sarif for missing physicalLocation.uri",
72-
begin.ResultFile.Region
71+
"There is no valid Path for the issue {@Region}, cannot create a path. Check the source sarif for missing physicalLocation.uri",
72+
begin.ResultFile.Region
7373
);
7474
return "noPathInSourceSarif";
7575
}
@@ -114,4 +114,4 @@ private static string NormalizeSeparators(string source)
114114
{
115115
return source.Replace(@"\\", @"\").Replace("//", @"\");
116116
}
117-
}
117+
}

CodeQualityToGitlab/SarifConverters/Converter2.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public List<CodeQuality> Convert()
1212
var log = SarifLog.Load(source.FullName);
1313

1414
var results = log.Runs
15-
.SelectMany(x => x.Results)
16-
.Where(r => r.Suppressions == null || r.Suppressions.Any());
15+
.SelectMany(x => x.Results)
16+
.Where(r => r.Suppressions == null || r.Suppressions.Any());
1717

1818
var cqrs = new List<CodeQuality>();
1919
foreach (var result in results)
@@ -36,11 +36,10 @@ public List<CodeQuality> Convert()
3636
Location = new()
3737
{
3838
Path = GetPath(pathRoot, begin),
39-
Lines = new()
40-
{ Begin = startLine }
39+
Lines = new() { Begin = startLine }
4140
},
4241
Fingerprint = Common.GetHash(
43-
$"{result.RuleId}|{begin.PhysicalLocation.ArtifactLocation.Uri}|{startLine}"
42+
$"{result.RuleId}|{begin.PhysicalLocation.ArtifactLocation.Uri}|{startLine}"
4443
)
4544
};
4645
cqrs.Add(cqr);
@@ -61,8 +60,8 @@ private static string GetPath(string? pathRoot, Microsoft.CodeAnalysis.Sarif.Loc
6160
if (artifactLocationUri == null)
6261
{
6362
Log.Error(
64-
"There is no valid Path for the issue {@Region}, cannot create a path. Check the source sarif for missing physicalLocation.ArtifactLocation.uri",
65-
begin.PhysicalLocation.ArtifactLocation
63+
"There is no valid Path for the issue {@Region}, cannot create a path. Check the source sarif for missing physicalLocation.ArtifactLocation.uri",
64+
begin.PhysicalLocation.ArtifactLocation
6665
);
6766
return "noPathInSourceSarif";
6867
}
@@ -91,4 +90,4 @@ private static Severity GetSeverity(FailureLevel resultLevel)
9190
_ => throw new ArgumentOutOfRangeException(nameof(resultLevel), resultLevel, null)
9291
};
9392
}
94-
}
93+
}

CodeQualityToGitlab/SarifConverters/SarifConverter.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ public static List<CodeQuality> ConvertToCodeQualityRaw(FileInfo source, string?
66
{
77
var logContents = File.ReadAllText(source.FullName);
88

9-
return logContents.Contains(" \"$schema\": \"http://json.schemastore.org/sarif-2") ? new Converter2(source, pathRoot).Convert() : new Converter1(source, pathRoot).Convert();
9+
return logContents.Contains(" \"$schema\": \"http://json.schemastore.org/sarif-2")
10+
? new Converter2(source, pathRoot).Convert()
11+
: new Converter1(source, pathRoot).Convert();
1012
}
1113

12-
public static void ConvertToCodeQuality(FileInfo source, FileInfo target, string? pathRoot = null)
14+
public static void ConvertToCodeQuality(
15+
FileInfo source,
16+
FileInfo target,
17+
string? pathRoot = null
18+
)
1319
{
1420
var cqrs = ConvertToCodeQualityRaw(source, pathRoot);
1521
Common.WriteToDisk(target, cqrs);
1622
}
17-
}
23+
}

Test/TestSarif.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,22 @@ public void TestSarif21Works()
137137
var result = JsonSerializer.Deserialize<List<CodeQuality>>(json, options);
138138

139139
result.Should().HaveCount(1);
140-
result!.First().Should().BeEquivalentTo(new CodeQuality
141-
{
142-
Description = "no-unused-vars: Microsoft.CodeAnalysis.Sarif.Message",
143-
Fingerprint = "75510FEE03EDAC9F5241783C86ACBFEB",
144-
Severity = Severity.blocker,
145-
Location = new()
146-
{
147-
Path = @"C:\dev\sarif\sarif-tutorials\samples\Introduction\simple-example.js",
148-
Lines = new()
140+
result!
141+
.First()
142+
.Should()
143+
.BeEquivalentTo(
144+
new CodeQuality
149145
{
150-
Begin = 1
146+
Description = "no-unused-vars: Microsoft.CodeAnalysis.Sarif.Message",
147+
Fingerprint = "75510FEE03EDAC9F5241783C86ACBFEB",
148+
Severity = Severity.blocker,
149+
Location = new()
150+
{
151+
Path =
152+
@"C:\dev\sarif\sarif-tutorials\samples\Introduction\simple-example.js",
153+
Lines = new() { Begin = 1 }
154+
}
151155
}
152-
}
153-
});
156+
);
154157
}
155158
}

0 commit comments

Comments
 (0)