@@ -55,12 +55,45 @@ public override void WriteResults(Result result, CLICommandOptions commandOption
55
55
56
56
if ( result is AnalyzeResult analyzeResult )
57
57
{
58
- SarifLog log = new ( ) ;
59
- var sarifVersion = SarifVersion . Current ;
60
- log . SchemaUri = sarifVersion . ConvertToSchemaUri ( ) ;
61
- log . Version = sarifVersion ;
58
+ SarifLog log = new ( )
59
+ {
60
+ Version = SarifVersion . Current
61
+ } ;
62
+
62
63
log . Runs = new List < Run > ( ) ;
63
- var run = new Run ( ) ;
64
+ // Convert Base Path to Forward Slashes to be a valid URI
65
+
66
+ var run = new Run ( )
67
+ {
68
+ Tool = new Tool
69
+ {
70
+ Driver = new ToolComponent
71
+ {
72
+ Name = "Application Inspector" ,
73
+ InformationUri = new Uri ( "https://github.com/microsoft/ApplicationInspector/" ) ,
74
+ Organization = "Microsoft" ,
75
+ Version = Helpers . GetVersionString ( )
76
+ }
77
+ }
78
+ } ;
79
+ if ( ! string . IsNullOrEmpty ( basePath ) )
80
+ {
81
+ if ( Path . DirectorySeparatorChar == '\\ ' )
82
+ {
83
+ basePath = basePath . Replace ( "\\ " , "/" ) ;
84
+ if ( ! basePath . EndsWith ( "/" ) )
85
+ {
86
+ basePath = $ "{ basePath } /";
87
+ }
88
+
89
+ }
90
+
91
+ run . OriginalUriBaseIds = new Dictionary < string , ArtifactLocation > ( )
92
+ {
93
+
94
+ { "ROOT" , new ArtifactLocation ( ) { Uri = new Uri ( $ "file://{ basePath } ") } }
95
+ } ;
96
+ }
64
97
65
98
if ( Uri . TryCreate ( cliAnalyzeCmdOptions . RepositoryUri , UriKind . RelativeOrAbsolute , out var uri ) )
66
99
{
@@ -69,7 +102,11 @@ public override void WriteResults(Result result, CLICommandOptions commandOption
69
102
new ( )
70
103
{
71
104
RepositoryUri = uri ,
72
- RevisionId = cliAnalyzeCmdOptions . CommitHash
105
+ RevisionId = cliAnalyzeCmdOptions . CommitHash ,
106
+ MappedTo = new ArtifactLocation ( )
107
+ {
108
+ UriBaseId = "ROOT"
109
+ }
73
110
}
74
111
} ;
75
112
}
@@ -81,22 +118,17 @@ public override void WriteResults(Result result, CLICommandOptions commandOption
81
118
{
82
119
RepositoryUri = analyzeResult . Metadata . RepositoryUri ,
83
120
RevisionId = analyzeResult . Metadata . CommitHash ?? string . Empty ,
84
- Branch = analyzeResult . Metadata . Branch ?? string . Empty
121
+ Branch = analyzeResult . Metadata . Branch ?? string . Empty ,
122
+ MappedTo = new ArtifactLocation ( )
123
+ {
124
+ UriBaseId = "ROOT"
125
+ }
85
126
}
86
127
} ;
87
128
}
88
129
89
130
var artifacts = new List < Artifact > ( ) ;
90
- run . Tool = new Tool
91
- {
92
- Driver = new ToolComponent
93
- {
94
- Name = "Application Inspector" ,
95
- InformationUri = new Uri ( "https://github.com/microsoft/ApplicationInspector/" ) ,
96
- Organization = "Microsoft" ,
97
- Version = Helpers . GetVersionString ( )
98
- }
99
- } ;
131
+
100
132
var reportingDescriptors = new List < ReportingDescriptor > ( ) ;
101
133
run . Results = new List < CodeAnalysis . Sarif . Result > ( ) ;
102
134
foreach ( var match in analyzeResult . Metadata . Matches )
@@ -114,14 +146,15 @@ public override void WriteResults(Result result, CLICommandOptions commandOption
114
146
Name = match . Rule . Name ,
115
147
DefaultConfiguration = new ReportingConfiguration
116
148
{
117
- Level = GetSarifFailureLevel ( match . Rule . Severity )
149
+ Level = FailureLevel . Note
118
150
}
119
151
} ;
120
152
reportingDescriptor . Tags . AddRange ( match . Rule . Tags ) ;
153
+ reportingDescriptor . SetProperty ( "AppInspector:Severity" , match . Rule . Severity . ToString ( ) ) ;
121
154
reportingDescriptors . Add ( reportingDescriptor ) ;
122
155
}
123
156
124
- sarifResult . Level = GetSarifFailureLevel ( match . Rule . Severity ) ;
157
+ sarifResult . Level = FailureLevel . Note ;
125
158
sarifResult . RuleId = match . Rule . Id ;
126
159
sarifResult . Tags . AddRange ( match . Rule . Tags ) ;
127
160
sarifResult . Message = new Message
@@ -134,7 +167,7 @@ public override void WriteResults(Result result, CLICommandOptions commandOption
134
167
var fileName = match . FileName ;
135
168
if ( basePath is not null )
136
169
{
137
- fileName = Path . GetRelativePath ( basePath , fileName ) ;
170
+ fileName = Path . GetRelativePath ( basePath , fileName ) . Replace ( " \\ " , "/" ) ;
138
171
}
139
172
140
173
if ( Uri . TryCreate ( fileName , UriKind . RelativeOrAbsolute , out var outUri ) )
@@ -150,6 +183,10 @@ public override void WriteResults(Result result, CLICommandOptions commandOption
150
183
Uri = outUri
151
184
}
152
185
} ;
186
+ if ( basePath != null )
187
+ {
188
+ artifact . Location . UriBaseId = "ROOT" ;
189
+ }
153
190
artifactIndex = artifact . Location . Index ;
154
191
artifact . Tags . AddRange ( match . Rule . Tags ) ;
155
192
if ( match . LanguageInfo is { } languageInfo )
@@ -164,30 +201,37 @@ public override void WriteResults(Result result, CLICommandOptions commandOption
164
201
artifacts [ artifactIndex ] . Tags . AddRange ( match . Rule . Tags ) ;
165
202
}
166
203
167
- sarifResult . Locations = new List < Location >
204
+ Location location = new ( )
168
205
{
169
- new ( )
206
+ PhysicalLocation = new PhysicalLocation
170
207
{
171
- PhysicalLocation = new PhysicalLocation
208
+ ArtifactLocation = new ArtifactLocation
172
209
{
173
- ArtifactLocation = new ArtifactLocation
174
- {
175
- Index = artifactIndex
176
- } ,
177
- Region = new Region
210
+ Index = artifactIndex ,
211
+ Uri = outUri
212
+ } ,
213
+ Region = new Region
214
+ {
215
+ StartLine = match . StartLocationLine ,
216
+ StartColumn = match . StartLocationColumn ,
217
+ EndLine = match . EndLocationLine ,
218
+ EndColumn = match . EndLocationColumn ,
219
+ Snippet = new ArtifactContent
178
220
{
179
- StartLine = match . StartLocationLine ,
180
- StartColumn = match . StartLocationColumn ,
181
- EndLine = match . EndLocationLine ,
182
- EndColumn = match . EndLocationColumn ,
183
- Snippet = new ArtifactContent
184
- {
185
- Text = match . Sample
186
- }
221
+ Text = match . Sample
187
222
}
188
223
}
189
224
}
190
225
} ;
226
+ if ( basePath != null )
227
+ {
228
+ location . PhysicalLocation . ArtifactLocation . UriBaseId = "ROOT" ;
229
+ }
230
+ sarifResult . SetProperty ( "AppInspector:Severity" , match . Rule . Severity . ToString ( ) ) ;
231
+ sarifResult . Locations = new List < Location >
232
+ {
233
+ location
234
+ } ;
191
235
}
192
236
}
193
237
}
@@ -200,7 +244,7 @@ public override void WriteResults(Result result, CLICommandOptions commandOption
200
244
log . Runs . Add ( run ) ;
201
245
try
202
246
{
203
- JsonSerializer . Serialize ( StreamWriter . BaseStream , log ) ;
247
+ log . Save ( StreamWriter . BaseStream ) ;
204
248
}
205
249
catch ( Exception e )
206
250
{
0 commit comments