-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import Foundation | ||
import SourceKittenFramework | ||
|
||
/// To some tools (i.e. Datadog), code quality findings are reported in the SARIF format: | ||
/// - Full Spec: https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html | ||
/// - Samples: https://github.com/microsoft/sarif-tutorials/blob/main/samples/ | ||
/// - JSON Validator: https://sarifweb.azurewebsites.net/Validation | ||
struct SARIFReporter: Reporter { | ||
// MARK: - Reporter Conformance | ||
static let identifier = "sarif" | ||
static let isRealtime = false | ||
static let description = "Reports violations in the Static Analysis Results Interchange Format (SARIF)" | ||
static let swiftlintVersion = "https://github.com/realm/SwiftLint/blob/\(Version.current.value)/README.md" | ||
|
||
static func generateReport(_ violations: [StyleViolation]) -> String { | ||
let SARIFJson = [ | ||
"version": "2.1.0", | ||
"$schema": "https://docs.oasis-open.org/sarif/sarif/v2.1.0/cos02/schemas/sarif-schema-2.1.0.json", | ||
"runs": [ | ||
[ | ||
"tool": [ | ||
"driver": [ | ||
"name": "SwiftLint", | ||
"semanticVersion": Version.current.value, | ||
"informationUri": swiftlintVersion | ||
] | ||
], | ||
"results": violations.map(dictionary(for:)) | ||
] | ||
] | ||
] as [String: Any] | ||
|
||
return toJSON(SARIFJson) | ||
} | ||
|
||
// MARK: - Private | ||
|
||
private static func dictionary(for violation: StyleViolation) -> [String: Any] { | ||
return [ | ||
"level": violation.severity.rawValue, | ||
"ruleId": violation.ruleIdentifier, | ||
"message": [ | ||
"text": violation.reason | ||
], | ||
"locations": [ | ||
dictionary(for: violation.location) | ||
] | ||
] | ||
} | ||
|
||
private static func dictionary(for location: Location) -> [String: Any] { | ||
// According to SARIF specification JSON1008, minimum value for line is 1 | ||
if let line = location.line, line > 0 { | ||
return [ | ||
"physicalLocation": [ | ||
"artifactLocation": [ | ||
"uri": location.file ?? "" | ||
], | ||
"region": [ | ||
"startLine": line, | ||
"startColumn": location.character ?? "1" | ||
] | ||
] | ||
] | ||
} | ||
|
||
return [ | ||
"physicalLocation": [ | ||
"artifactLocation": [ | ||
"uri": location.file ?? "" | ||
] | ||
] | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
Tests/SwiftLintFrameworkTests/Resources/CannedSARIFReporterOutput.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
{ | ||
"$schema" : "https:\/\/docs.oasis-open.org\/sarif\/sarif\/v2.1.0\/cos02\/schemas\/sarif-schema-2.1.0.json", | ||
"runs" : [ | ||
{ | ||
"results" : [ | ||
{ | ||
"level" : "warning", | ||
"locations" : [ | ||
{ | ||
"physicalLocation" : { | ||
"artifactLocation" : { | ||
"uri" : "filename" | ||
}, | ||
"region" : { | ||
"startColumn" : 2, | ||
"startLine" : 1 | ||
} | ||
} | ||
} | ||
], | ||
"message" : { | ||
"text" : "Violation Reason" | ||
}, | ||
"ruleId" : "line_length" | ||
}, | ||
{ | ||
"level" : "error", | ||
"locations" : [ | ||
{ | ||
"physicalLocation" : { | ||
"artifactLocation" : { | ||
"uri" : "filename" | ||
}, | ||
"region" : { | ||
"startColumn" : 2, | ||
"startLine" : 1 | ||
} | ||
} | ||
} | ||
], | ||
"message" : { | ||
"text" : "Violation Reason" | ||
}, | ||
"ruleId" : "line_length" | ||
}, | ||
{ | ||
"level" : "error", | ||
"locations" : [ | ||
{ | ||
"physicalLocation" : { | ||
"artifactLocation" : { | ||
"uri" : "filename" | ||
}, | ||
"region" : { | ||
"startColumn" : 2, | ||
"startLine" : 1 | ||
} | ||
} | ||
} | ||
], | ||
"message" : { | ||
"text" : "Shorthand syntactic sugar should be used, i.e. [Int] instead of Array<Int>" | ||
}, | ||
"ruleId" : "syntactic_sugar" | ||
}, | ||
{ | ||
"level" : "error", | ||
"locations" : [ | ||
{ | ||
"physicalLocation" : { | ||
"artifactLocation" : { | ||
"uri" : "" | ||
} | ||
} | ||
} | ||
], | ||
"message" : { | ||
"text" : "Colons should be next to the identifier when specifying a type and next to the key in dictionary literals" | ||
}, | ||
"ruleId" : "colon" | ||
} | ||
], | ||
"tool" : { | ||
"driver" : { | ||
"informationUri" : "https:\/\/github.com\/realm\/SwiftLint\/blob\/0.54.0\/README.md", | ||
"name" : "SwiftLint", | ||
"semanticVersion" : "0.54.0" | ||
} | ||
} | ||
} | ||
], | ||
"version" : "2.1.0" | ||
} |