Skip to content

Commit 975e08e

Browse files
fix handling
1 parent 7d82a7f commit 975e08e

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

CodeQualityToGitlab/CodeQuality.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using System.Diagnostics.CodeAnalysis;
1+
using System.Diagnostics;
2+
using System.Diagnostics.CodeAnalysis;
23

34
namespace CodeQualityToGitlab;
45

6+
[DebuggerDisplay("{Description} {Severity} {Location.Path} {Location.Lines.Begin}")]
57
public class CodeQuality
68
{
79
public required string Description { get; set; }
@@ -10,12 +12,14 @@ public class CodeQuality
1012
public required LocationCq Location { get; set; }
1113
}
1214

15+
[DebuggerDisplay("{Path} {Lines.Begin}")]
1316
public class LocationCq
1417
{
1518
public required string Path { get; set; }
1619
public required Lines Lines { get; set; }
1720
}
1821

22+
[DebuggerDisplay("{Begin}")]
1923
public class Lines
2024
{
2125
public required int Begin { get; set; }

CodeQualityToGitlab/SarifConverters/Converter1.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public List<CodeQuality> Convert()
4444
Severity = GetSeverity(result.Level),
4545
Location = new()
4646
{
47-
Path = GetPath(pathRoot, begin),
47+
Path = GetPathOld(pathRoot, begin),
4848
Lines = new() { Begin = begin.ResultFile.Region.StartLine }
4949
},
5050
Fingerprint = Common.GetHash(
@@ -62,14 +62,14 @@ public List<CodeQuality> Convert()
6262
return cqrs;
6363
}
6464

65-
private static string GetPath(string? pathRoot, LocationVersionOne begin)
65+
private static string GetPathOld(string? pathRoot, LocationVersionOne begin)
6666
{
6767
// nullability says Uri is always set, but there are tools which omit this.
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
}
@@ -81,21 +81,13 @@ private static string GetPath(string? pathRoot, LocationVersionOne begin)
8181

8282
if (string.IsNullOrWhiteSpace(pathRoot))
8383
{
84-
return NormalizeSeparators(begin.ResultFile.Uri.LocalPath.Replace(@"\\", @"\"));
84+
return begin.ResultFile.Uri.LocalPath.Replace("//", "\\");
8585
}
86-
87-
var uri = GetUri(pathRoot);
88-
var absolutePath = begin.ResultFile.Uri.LocalPath;
89-
var rootPath = uri.LocalPath;
90-
91-
if (absolutePath.StartsWith(rootPath))
92-
{
93-
return NormalizeSeparators(absolutePath[rootPath.Length..]);
94-
}
95-
96-
return NormalizeSeparators(begin.ResultFile.Uri.MakeRelativeUri(uri).ToString());
86+
var rootUri = GetUri(pathRoot);
87+
return rootUri.MakeRelativeUri(begin.ResultFile.Uri).ToString().Replace("//", "\\");
9788
}
9889

90+
9991
private static Uri GetUri(string pathRoot)
10092
{
10193
if (Path.IsPathRooted(pathRoot))

Test/TestTransform.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,21 @@ public void TestTransformAllWorks()
3131
}
3232

3333
[Fact]
34-
public void TestTransformCreatesExpectedPath()
34+
public void TestTHandlesDotsInPathsForSarif1()
3535
{
3636
var target = new FileInfo(Path.GetTempFileName());
3737

38-
Transform.TransformAll("codeanalysis.sarif4.json", "", target, @"/builds/39753701/backend", true);
38+
Transform.TransformAll("codeanalysis.sarif4.json", "", target, "/builds/folder/backend/", true);
3939

4040
var options = JsonSerializerOptions;
4141

4242
using var r = new StreamReader(target.FullName);
4343
var json = r.ReadToEnd();
4444
var result = JsonSerializer.Deserialize<List<CodeQuality>>(json, options);
4545

46-
result.Should().HaveCount(8);
46+
result.Should().NotBeNull();
47+
result!.First().Location.Path.Should().Contain("SR.CLI");
48+
49+
result.Should().HaveCount(4);
4750
}
4851
}

0 commit comments

Comments
 (0)