diff --git a/CHANGELOG.md b/CHANGELOG.md index 580ccef71..b1e0abb0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ##### Enhancements - Added the `--retain-unused-imported-modules` option. +- Added the `--format gitlab-codemagic` formatting option for GitLabs Code Quality artifact reports ##### Bug Fixes diff --git a/Sources/Configuration/OutputFormat.swift b/Sources/Configuration/OutputFormat.swift index 01be2fca7..cd0e7bac1 100644 --- a/Sources/Configuration/OutputFormat.swift +++ b/Sources/Configuration/OutputFormat.swift @@ -8,6 +8,7 @@ public enum OutputFormat: String, CaseIterable { case codeclimate case githubActions = "github-actions" case githubMarkdown = "github-markdown" + case gitlabCodeQuality = "gitlab-codequality" public static let `default` = OutputFormat.xcode diff --git a/Sources/PeripheryKit/Results/GitLabCodeQualityFormatter.swift b/Sources/PeripheryKit/Results/GitLabCodeQualityFormatter.swift new file mode 100644 index 000000000..f51a62cf4 --- /dev/null +++ b/Sources/PeripheryKit/Results/GitLabCodeQualityFormatter.swift @@ -0,0 +1,59 @@ +import Configuration +import Foundation +import SystemPackage + +final class GitLabCodeQualityFormatter: OutputFormatter { + let configuration: Configuration + lazy var currentFilePath: FilePath = .current + + init(configuration: Configuration) { + self.configuration = configuration + } + + func format(_ results: [ScanResult], colored: Bool) throws -> String? { + var jsonObject: [Any] = [] + + for result in results { + let violationFileLocation = declarationLocation(from: result.declaration) + + let description = describe(result, colored: colored) + .map(\.1) + .joined(separator: ", ") + + let checkName = describe(result.annotation) + + let fingerprint = result + .declaration + .usrs + .sorted() + .joined(separator: ".") + + let begin = violationFileLocation.line + let lines: [AnyHashable: Any] = [ + "begin": begin + ] + + let path = outputPath(violationFileLocation) + .url + .relativePath + + let location: [AnyHashable: Any] = [ + "path": path, + "lines": lines, + ] + + let object: [AnyHashable: Any] = [ + "description": description, + "check_name": checkName, + "fingerprint": fingerprint, + "location": location, + "severity": "info", + ] + + jsonObject.append(object) + } + + let data = try JSONSerialization.data(withJSONObject: jsonObject, options: [.prettyPrinted, .withoutEscapingSlashes]) + return String(bytes: data, encoding: .utf8) + } +} diff --git a/Sources/PeripheryKit/Results/OutputFormatter.swift b/Sources/PeripheryKit/Results/OutputFormatter.swift index 925022c55..5eff2c19e 100644 --- a/Sources/PeripheryKit/Results/OutputFormatter.swift +++ b/Sources/PeripheryKit/Results/OutputFormatter.swift @@ -178,6 +178,8 @@ public extension OutputFormat { GitHubActionsFormatter.self case .githubMarkdown: GitHubMarkdownFormatter.self + case .gitlabCodeQuality: + GitLabCodeQualityFormatter.self } } }