Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create formatter for GitHub Actions #746

Merged
merged 1 commit into from
May 25, 2024
Merged
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
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
Loading