Skip to content

Commit 439ad99

Browse files
Bump version to 1.0.0
csharpier
1 parent 11cc55e commit 439ad99

File tree

12 files changed

+206
-152
lines changed

12 files changed

+206
-152
lines changed

CodeQualityToGitlab/CodeQualityToGitlab.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<PackAsTool>true</PackAsTool>
99
<ToolCommandName>cq</ToolCommandName>
1010
<PackageOutputPath>./nupkg</PackageOutputPath>
11-
<Version>0.9.0</Version>
11+
<Version>1.0.0</Version>
1212
<Company>codecentric</Company>
1313
<Copyright>(c) codecentric</Copyright>
1414
<PackageProjectUrl>https://github.com/codecentric/dotnet_gitlab_code_quality</PackageProjectUrl>

CodeQualityToGitlab/Common.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,11 @@ public static void WriteToDisk(FileInfo target, IEnumerable<CodeQuality> result)
2222
{
2323
WriteIndented = true,
2424
PropertyNamingPolicy = new LowerCaseNamingPolicy(),
25-
Converters =
26-
{
27-
new JsonStringEnumConverter()
28-
}
25+
Converters = { new JsonStringEnumConverter() }
2926
};
3027
using var fileStream = File.Create(target.FullName);
3128
using var utf8JsonWriter = new Utf8JsonWriter(fileStream);
3229
JsonSerializer.Serialize(utf8JsonWriter, result, options);
3330
Log.Information("Result written to: {TargetFullName}", target.FullName);
3431
}
35-
}
32+
}

CodeQualityToGitlab/Merger.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ public static void Merge(FileInfo[] sources, FileInfo target, bool bumpToMajor)
1414
{
1515
WriteIndented = true,
1616
PropertyNamingPolicy = new LowerCaseNamingPolicy(),
17-
Converters ={
18-
new JsonStringEnumConverter()
19-
}
17+
Converters = { new JsonStringEnumConverter() }
2018
};
2119

2220
foreach (var source in sources)
2321
{
2422
if (!source.Exists)
2523
{
26-
throw new FileNotFoundException($"The file '{source.FullName}' does not exist", source.FullName);
24+
throw new FileNotFoundException(
25+
$"The file '{source.FullName}' does not exist",
26+
source.FullName
27+
);
2728
}
2829

2930
using var f = source.OpenRead();
@@ -33,7 +34,9 @@ public static void Merge(FileInfo[] sources, FileInfo target, bool bumpToMajor)
3334

3435
if (data == null)
3536
{
36-
throw new ArgumentNullException($"could not deserialize content of {source.FullName}");
37+
throw new ArgumentNullException(
38+
$"could not deserialize content of {source.FullName}"
39+
);
3740
}
3841
result.AddRange(data);
3942
}
@@ -45,16 +48,17 @@ public static void Merge(FileInfo[] sources, FileInfo target, bool bumpToMajor)
4548
}
4649

4750
result = result.DistinctBy(x => x.Fingerprint).ToList();
48-
51+
4952
if (bumpToMajor)
5053
{
51-
foreach (var cqr in result
52-
.Where(cqr => cqr.Severity is Severity.minor or Severity.info))
54+
foreach (
55+
var cqr in result.Where(cqr => cqr.Severity is Severity.minor or Severity.info)
56+
)
5357
{
5458
cqr.Severity = Severity.major;
5559
}
5660
}
57-
61+
5862
Common.WriteToDisk(target, result);
5963
}
60-
}
64+
}

CodeQualityToGitlab/Program.cs

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,88 +7,113 @@ internal static class Program
77
{
88
private static async Task<int> Main(string[] args)
99
{
10-
Log.Logger = new LoggerConfiguration()
11-
.WriteTo.Console()
12-
.CreateLogger();
13-
10+
Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateLogger();
11+
1412
var sourceArgument = new Argument<FileInfo>(
1513
name: "source",
1614
description: "The file to convert"
17-
);
15+
);
1816

19-
var targetArgument = new Argument<FileInfo>(
20-
name: "target",
21-
description: "The target file"
17+
var targetArgument = new Argument<FileInfo>(name: "target", description: "The target file");
18+
19+
var bumpToMajorOption = new Option<bool>(
20+
name: "--all_major",
21+
"if true all info and minor issues are promoted to major for Gitlab"
2222
);
2323

24-
var bumpToMajorOption = new Option<bool>(name: "--all_major",
25-
"if true all info and minor issues are promoted to major for Gitlab");
26-
2724
var rootPathArgument = new Argument<string?>(
2825
name: "root",
29-
description: "The name root of the repository. Gitlab requires Code Quality issues to contain paths relative to the repository, " +
30-
"but the tools report them as absolute file paths. " +
31-
"Everything given in with this option will be removed. E.g. root is 'c:/dev' and the file name is something like 'c:/dev/myrepo/file.cs' it will transformed to 'myrepo/file.cs'. Can often be omitted. ",
26+
description: "The name root of the repository. Gitlab requires Code Quality issues to contain paths relative to the repository, "
27+
+ "but the tools report them as absolute file paths. "
28+
+ "Everything given in with this option will be removed. E.g. root is 'c:/dev' and the file name is something like 'c:/dev/myrepo/file.cs' it will transformed to 'myrepo/file.cs'. Can often be omitted. ",
3229
getDefaultValue: () => null
3330
);
34-
31+
3532
var rootCommand = new RootCommand("Tool to convert Dotnet-Formats to Gitlab code quality");
36-
var roslynatorToCodeQuality = new Command("roslynator", "Convert Roslynator file to Code Quality issue")
33+
var roslynatorToCodeQuality = new Command(
34+
"roslynator",
35+
"Convert Roslynator file to Code Quality issue"
36+
)
3737
{
3838
sourceArgument,
3939
targetArgument,
4040
rootPathArgument
4141
};
42-
42+
4343
var sarifToCodeQuality = new Command("sarif", "Convert Sarif files to Code Quality issue")
4444
{
4545
sourceArgument,
4646
targetArgument,
4747
rootPathArgument
4848
};
49-
49+
5050
var sourcesArgument = new Argument<FileInfo[]>(
5151
name: "sources",
5252
description: "The files to merge"
5353
);
54-
55-
var mergeCodeQuality = new Command("merge", "Merge multiple code quality files into one")
54+
55+
var mergeCodeQuality = new Command("merge", "Merge multiple code quality files into one")
5656
{
5757
targetArgument,
5858
sourcesArgument,
5959
bumpToMajorOption
6060
};
61-
61+
6262
var sourceGlobArgument = new Argument<string>(
6363
name: "sarifGlob",
6464
description: "Glob pattern for the sarif files",
6565
getDefaultValue: () => "**/*.sarif.json"
6666
);
67-
67+
6868
var sourceRoslynatorArgument = new Argument<string>(
6969
name: "roslynatorGlob",
7070
description: "Glob pattern for the roslynator files",
7171
getDefaultValue: () => "**/roslynator.xml"
7272
);
73-
74-
var transformCodeQuality = new Command("transform", "Transforms files from a glob mapping and merges them to one file")
73+
74+
var transformCodeQuality = new Command(
75+
"transform",
76+
"Transforms files from a glob mapping and merges them to one file"
77+
)
7578
{
7679
sourceGlobArgument,
7780
sourceRoslynatorArgument,
7881
targetArgument,
7982
rootPathArgument,
8083
bumpToMajorOption
8184
};
82-
83-
roslynatorToCodeQuality.SetHandler(RoslynatorConverter.ConvertToCodeQuality, sourceArgument, targetArgument, rootPathArgument);
84-
sarifToCodeQuality.SetHandler(SarifConverter.ConvertToCodeQuality, sourceArgument, targetArgument, rootPathArgument);
85-
mergeCodeQuality.SetHandler(Merger.Merge, sourcesArgument, targetArgument, bumpToMajorOption);
86-
transformCodeQuality.SetHandler(Transform.TransformAll, sourceGlobArgument, sourceRoslynatorArgument, targetArgument,rootPathArgument, bumpToMajorOption);
85+
86+
roslynatorToCodeQuality.SetHandler(
87+
RoslynatorConverter.ConvertToCodeQuality,
88+
sourceArgument,
89+
targetArgument,
90+
rootPathArgument
91+
);
92+
sarifToCodeQuality.SetHandler(
93+
SarifConverter.ConvertToCodeQuality,
94+
sourceArgument,
95+
targetArgument,
96+
rootPathArgument
97+
);
98+
mergeCodeQuality.SetHandler(
99+
Merger.Merge,
100+
sourcesArgument,
101+
targetArgument,
102+
bumpToMajorOption
103+
);
104+
transformCodeQuality.SetHandler(
105+
Transform.TransformAll,
106+
sourceGlobArgument,
107+
sourceRoslynatorArgument,
108+
targetArgument,
109+
rootPathArgument,
110+
bumpToMajorOption
111+
);
87112
rootCommand.Add(roslynatorToCodeQuality);
88113
rootCommand.Add(sarifToCodeQuality);
89114
rootCommand.Add(mergeCodeQuality);
90115
rootCommand.Add(transformCodeQuality);
91116

92117
return await rootCommand.InvokeAsync(args);
93118
}
94-
}
119+
}

CodeQualityToGitlab/RoslynatorConverter.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ public static class RoslynatorConverter
88
{
99
public static List<CodeQuality> ConvertToCodeQualityRaw(FileInfo source, string? pathRoot)
1010
{
11-
var serializer =
12-
new XmlSerializer(typeof(Roslynator));
11+
var serializer = new XmlSerializer(typeof(Roslynator));
1312

1413
var result = new List<CodeQuality>();
1514
using Stream reader = new FileStream(source.FullName, FileMode.Open);
16-
var roslynator = (Roslynator)(serializer.Deserialize(reader) ?? throw new ArgumentException("no data"));
15+
var roslynator = (Roslynator)(
16+
serializer.Deserialize(reader) ?? throw new ArgumentException("no data")
17+
);
1718
foreach (var project in roslynator.CodeAnalysis.Projects.Project)
1819
{
1920
Log.Information("Working on {ProjectName}", project.Name);
@@ -27,7 +28,7 @@ public static List<CodeQuality> ConvertToCodeQualityRaw(FileInfo source, string?
2728
Severity = GetSeverity(diagnostic.Severity),
2829
Location = new LocationCq
2930
{
30-
Path = GetPath(diagnostic, project, pathRoot),
31+
Path = GetPath(diagnostic, project, pathRoot),
3132
Lines = new Lines { Begin = lineNumber }
3233
},
3334
Fingerprint = Common.GetHash($"{project.Name}{diagnostic.Id}{lineNumber}")
@@ -39,7 +40,6 @@ public static List<CodeQuality> ConvertToCodeQualityRaw(FileInfo source, string?
3940

4041
return result;
4142
}
42-
4343

4444
public static void ConvertToCodeQuality(FileInfo source, FileInfo target, string? pathRoot)
4545
{
@@ -50,12 +50,12 @@ public static void ConvertToCodeQuality(FileInfo source, FileInfo target, string
5050
private static string GetPath(Diagnostic diagnostic, Project project, string? pathRoot)
5151
{
5252
var path = diagnostic.FilePath ?? project.FilePath;
53-
53+
5454
if (string.IsNullOrWhiteSpace(pathRoot))
5555
{
5656
return path;
5757
}
58-
58+
5959
var rv = path.Replace(pathRoot, "");
6060
return rv;
6161
}
@@ -73,13 +73,16 @@ private static Severity GetSeverity(string diagnosticSeverity)
7373
"Warning" => Severity.major,
7474
"Error" => Severity.critical,
7575
"Hidden" => Severity.minor,
76-
_ => throw new ArgumentOutOfRangeException(diagnosticSeverity, $"unknown: {diagnosticSeverity}")
76+
_
77+
=> throw new ArgumentOutOfRangeException(
78+
diagnosticSeverity,
79+
$"unknown: {diagnosticSeverity}"
80+
)
7781
};
7882
}
7983
}
8084

8185
public class LowerCaseNamingPolicy : JsonNamingPolicy
8286
{
83-
public override string ConvertName(string name) =>
84-
name.ToLower();
87+
public override string ConvertName(string name) => name.ToLower();
8588
}

CodeQualityToGitlab/SarifConverter.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
namespace CodeQualityToGitlab;
22

3-
using Serilog;
43
using Microsoft.CodeAnalysis.Sarif.Readers;
54
using Microsoft.CodeAnalysis.Sarif.VersionOne;
65
using Newtonsoft.Json;
6+
using Serilog;
77

88
public static class SarifConverter
99
{
@@ -23,8 +23,8 @@ public static List<CodeQuality> ConvertToCodeQualityRaw(FileInfo source, string?
2323
.SelectMany(x => x.Results)
2424
.Where(r => r.SuppressionStates == SuppressionStatesVersionOne.None);
2525

26-
var cqrs = new List<CodeQuality>();
27-
foreach (var result in results)
26+
var cqrs = new List<CodeQuality>();
27+
foreach (var result in results)
2828
{
2929
var begin = result.Locations?.FirstOrDefault();
3030

@@ -34,7 +34,7 @@ public static List<CodeQuality> ConvertToCodeQualityRaw(FileInfo source, string?
3434
continue;
3535
}
3636

37-
try
37+
try
3838
{
3939
var cqr = new CodeQuality
4040
{
@@ -45,7 +45,9 @@ public static List<CodeQuality> ConvertToCodeQualityRaw(FileInfo source, string?
4545
Path = GetPath(pathRoot, begin),
4646
Lines = new Lines { Begin = begin.ResultFile.Region.StartLine }
4747
},
48-
Fingerprint = Common.GetHash($"{result.RuleId}|{begin.ResultFile.Uri}|{begin.ResultFile.Region.StartLine}")
48+
Fingerprint = Common.GetHash(
49+
$"{result.RuleId}|{begin.ResultFile.Uri}|{begin.ResultFile.Region.StartLine}"
50+
)
4951
};
5052
cqrs.Add(cqr);
5153
}
@@ -57,6 +59,7 @@ public static List<CodeQuality> ConvertToCodeQualityRaw(FileInfo source, string?
5759

5860
return cqrs;
5961
}
62+
6063
public static void ConvertToCodeQuality(FileInfo source, FileInfo target, string? pathRoot)
6164
{
6265
var cqrs = ConvertToCodeQualityRaw(source, pathRoot);
@@ -68,7 +71,10 @@ private static string GetPath(string? pathRoot, LocationVersionOne begin)
6871
// nullability says Uri is always set, but there are tools which omit this.
6972
if (begin.ResultFile.Uri == null)
7073
{
71-
Log.Error("There is no valid Path for the issue {@Region}, cannot create a path. Check the source sarif for missing physicalLocation.uri", begin.ResultFile.Region);
74+
Log.Error(
75+
"There is no valid Path for the issue {@Region}, cannot create a path. Check the source sarif for missing physicalLocation.uri",
76+
begin.ResultFile.Region
77+
);
7278
return "noPathInSourceSarif";
7379
}
7480

@@ -98,4 +104,4 @@ private static Severity GetSeverity(ResultLevelVersionOne resultLevel)
98104
_ => throw new ArgumentOutOfRangeException(nameof(resultLevel), resultLevel, null)
99105
};
100106
}
101-
}
107+
}

0 commit comments

Comments
 (0)