-
Notifications
You must be signed in to change notification settings - Fork 1
/
PrepareSarifToUpload.swift
48 lines (39 loc) · 1.8 KB
/
PrepareSarifToUpload.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import Foundation
let fileName = "swiftlint.report.sarif"
let errorLevel = "error"
let warningThresholdRuleId = "warning_threshold"
let currentDirectoryURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
let fileURL = currentDirectoryURL.appendingPathComponent(fileName)
do {
// Load the file data
let data = try Data(contentsOf: fileURL)
// Decode JSON data
if var jsonObject = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
var runs = jsonObject["runs"] as? [[String: Any]],
!runs.isEmpty {
if var results = runs[0]["results"] as? [[String: Any]], !results.isEmpty {
// Filter out results based on the condition
results = results.filter { result in
let level = result["level"] as? String
let ruleId = result["ruleId"] as? String
// Keep results that don't match the deletion condition
return !(level == errorLevel && ruleId == warningThresholdRuleId)
}
// Update the modified results array back into the runs structure
runs[0]["results"] = results
jsonObject["runs"] = runs
// Encode the updated JSON data back to Data
let updatedData = try JSONSerialization.data(withJSONObject: jsonObject, options: [.prettyPrinted])
// Write the updated data back to the file
try updatedData.write(to: fileURL)
print("Filtered JSON file updated successfully.")
} else {
print("No results found to filter.")
}
} else {
print("The JSON structure is not in the expected format.")
}
} catch {
print("Error reading or writing JSON file: \(error.localizedDescription)")
}
exit(0)