Skip to content

Commit

Permalink
Create formatter for GitHub Actions (#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
Iron-Ham authored May 25, 2024
1 parent 00391e6 commit 47cf15b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Sources/PeripheryKit/Formatters/ActionsFormatter.swift
Original file line number Diff line number Diff line change
@@ -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)::"
}
}
2 changes: 2 additions & 0 deletions Sources/PeripheryKit/Formatters/OutputFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public extension OutputFormat {
return JsonFormatter.self
case .checkstyle:
return CheckstyleFormatter.self
case .actions:
return ActionsFormatter.self
}
}
}
1 change: 1 addition & 0 deletions Sources/Shared/OutputFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public enum OutputFormat: String, CaseIterable {
case json
case checkstyle
case codeclimate
case actions

public static let `default` = OutputFormat.xcode

Expand Down

0 comments on commit 47cf15b

Please sign in to comment.