1
+ using Microsoft . CodeAnalysis . Sarif . Readers ;
2
+ using Microsoft . CodeAnalysis . Sarif . VersionOne ;
3
+ using Newtonsoft . Json ;
4
+ using Serilog ;
5
+
6
+ namespace CodeQualityToGitlab . SarifConverters ;
7
+
8
+ public class Converter1 ( FileInfo source , string ? pathRoot )
9
+ {
10
+ public List < CodeQuality > Convert ( )
11
+ {
12
+ Log . Information ( "Sarif Version 1 detected" ) ;
13
+
14
+ var logContents = File . ReadAllText ( source . FullName ) ;
15
+
16
+ var settings = new JsonSerializerSettings
17
+ {
18
+ ContractResolver = SarifContractResolverVersionOne . Instance
19
+ } ;
20
+
21
+ var log = JsonConvert . DeserializeObject < SarifLogVersionOne > ( logContents , settings ) ;
22
+
23
+ var results = log . Runs
24
+ . SelectMany ( x => x . Results )
25
+ . Where ( r => r . SuppressionStates == SuppressionStatesVersionOne . None ) ;
26
+
27
+ var cqrs = new List < CodeQuality > ( ) ;
28
+ foreach ( var result in results )
29
+ {
30
+ var begin = result . Locations ? . FirstOrDefault ( ) ;
31
+
32
+ if ( begin == null )
33
+ {
34
+ Log . Warning ( "An issue has no location, skipping: {Result}" , result . Message ) ;
35
+ continue ;
36
+ }
37
+
38
+ try
39
+ {
40
+ var cqr = new CodeQuality
41
+ {
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 (
51
+ $ "{ 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
+ }
60
+ }
61
+
62
+ return cqrs ;
63
+ }
64
+
65
+ private static string GetPath ( string ? pathRoot , LocationVersionOne begin )
66
+ {
67
+ // nullability says Uri is always set, but there are tools which omit this.
68
+ if ( begin . ResultFile . Uri == null )
69
+ {
70
+ 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
73
+ ) ;
74
+ return "noPathInSourceSarif" ;
75
+ }
76
+
77
+ if ( ! begin . ResultFile . Uri ! . IsAbsoluteUri )
78
+ {
79
+ return begin . ResultFile . Uri . ToString ( ) ;
80
+ }
81
+
82
+ if ( string . IsNullOrWhiteSpace ( pathRoot ) )
83
+ {
84
+ return begin . ResultFile . Uri . LocalPath . Replace ( "//" , "\\ " ) ;
85
+ }
86
+ var uri = new Uri ( pathRoot ) ;
87
+ return uri . MakeRelativeUri ( begin . ResultFile . Uri ) . ToString ( ) . Replace ( "//" , "\\ " ) ;
88
+ }
89
+
90
+ private static Severity GetSeverity ( ResultLevelVersionOne resultLevel )
91
+ {
92
+ return resultLevel switch
93
+ {
94
+ ResultLevelVersionOne . NotApplicable => Severity . minor ,
95
+ ResultLevelVersionOne . Pass => Severity . minor ,
96
+ ResultLevelVersionOne . Note => Severity . minor ,
97
+ ResultLevelVersionOne . Warning => Severity . major ,
98
+ ResultLevelVersionOne . Default => Severity . major ,
99
+ ResultLevelVersionOne . Error => Severity . blocker ,
100
+ _ => throw new ArgumentOutOfRangeException ( nameof ( resultLevel ) , resultLevel , null )
101
+ } ;
102
+ }
103
+ }
0 commit comments