From b5fae860b3872839fa17c05d2eeaaf25bef1dac4 Mon Sep 17 00:00:00 2001 From: Hesham Salman Date: Wed, 22 May 2024 00:10:40 -0400 Subject: [PATCH] Create formatter for GitHub Actions --- .../Formatters/ActionsFormatter.swift | 35 +++++++++++++++++++ .../Formatters/OutputFormatter.swift | 2 ++ Sources/Shared/OutputFormat.swift | 1 + 3 files changed, 38 insertions(+) create mode 100644 Sources/PeripheryKit/Formatters/ActionsFormatter.swift diff --git a/Sources/PeripheryKit/Formatters/ActionsFormatter.swift b/Sources/PeripheryKit/Formatters/ActionsFormatter.swift new file mode 100644 index 000000000..420c293bb --- /dev/null +++ b/Sources/PeripheryKit/Formatters/ActionsFormatter.swift @@ -0,0 +1,35 @@ +import Foundation +import Shared +import SystemPackage + +final class ActionsFormatter: OutputFormatter { + let configuration: Configuration + lazy var currentFilePath: FilePath = { .current }() + + init(configuration: Configuration) { + self.configuration = configuration + } + + func format(_ results: [ScanResult]) throws -> String { + guard results.count > 0 else { return "" } + guard configuration.relativeResults else { throw PeripheryError.usageError("`periphery scan` must be ran with `--relative-results` when using the actions formatter")} + + return results.flatMap { result in + describe(result, colored: false).map { (location, description) in + prefix(for: location, result: result) + description + } + } + .joined(separator: "\n") + } + + // MARK: - Private + + private func prefix(for location: SourceLocation, result: ScanResult) -> String { + let path = outputPath(location) + let lineNum = String(location.line) + let column = location.column + let title = describe(result.annotation) + + return "::warning file=\(path),line=\(lineNum),col=\(column),title=\(title)::" + } +} diff --git a/Sources/PeripheryKit/Formatters/OutputFormatter.swift b/Sources/PeripheryKit/Formatters/OutputFormatter.swift index 1c06e980a..a2b459033 100644 --- a/Sources/PeripheryKit/Formatters/OutputFormatter.swift +++ b/Sources/PeripheryKit/Formatters/OutputFormatter.swift @@ -105,6 +105,8 @@ public extension OutputFormat { return JsonFormatter.self case .checkstyle: return CheckstyleFormatter.self + case .actions: + return ActionsFormatter.self } } } diff --git a/Sources/Shared/OutputFormat.swift b/Sources/Shared/OutputFormat.swift index 1fe8f6fad..2f8ad5cb5 100644 --- a/Sources/Shared/OutputFormat.swift +++ b/Sources/Shared/OutputFormat.swift @@ -6,6 +6,7 @@ public enum OutputFormat: String, CaseIterable { case json case checkstyle case codeclimate + case actions public static let `default` = OutputFormat.xcode