Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions Sources/Configuration/OutputFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
59 changes: 59 additions & 0 deletions Sources/PeripheryKit/Results/GitLabCodeQualityFormatter.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 2 additions & 0 deletions Sources/PeripheryKit/Results/OutputFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ public extension OutputFormat {
GitHubActionsFormatter.self
case .githubMarkdown:
GitHubMarkdownFormatter.self
case .gitlabCodeQuality:
GitLabCodeQualityFormatter.self
}
}
}