diff --git a/Sources/DangerSwiftKantoku/DangerDSL+.swift b/Sources/DangerSwiftKantoku/DangerDSL+.swift index 719443b..5c4bb9e 100644 --- a/Sources/DangerSwiftKantoku/DangerDSL+.swift +++ b/Sources/DangerSwiftKantoku/DangerDSL+.swift @@ -12,6 +12,8 @@ extension DangerDSL { public var kantoku: Kantoku { .init( workingDirectoryPath: utils.exec("pwd"), + modifiedFiles: git.modifiedFiles, + createdFiles: git.createdFiles, markdownCommentExecutor: { markdown($0) }, inlineCommentExecutor: { message(message: $0, file: $1, line: $2) }, normalCommentExecutor: { message($0) }, diff --git a/Sources/DangerSwiftKantoku/Kantoku+IssueComments.swift b/Sources/DangerSwiftKantoku/Kantoku+IssueComments.swift index c70a63c..4b0f205 100644 --- a/Sources/DangerSwiftKantoku/Kantoku+IssueComments.swift +++ b/Sources/DangerSwiftKantoku/Kantoku+IssueComments.swift @@ -46,11 +46,10 @@ extension Kantoku { } func post(_ summaries: [PostableIssueSummary], as level: CommentLevel) { - for summary in summaries { let message = summary.issueMessage let filePath = summary.documentLocation?.relativePath(against: workingDirectoryPath) - + if let filePath = filePath { let lineNumber = filePath.queries?.endingLineNumber // Line numbers in XCResult starts from `0`, while on web pages like GitHub starts from `1` @@ -63,5 +62,5 @@ extension Kantoku { } } - + } diff --git a/Sources/DangerSwiftKantoku/Kantoku.swift b/Sources/DangerSwiftKantoku/Kantoku.swift index 4ff4ed9..ff8f498 100644 --- a/Sources/DangerSwiftKantoku/Kantoku.swift +++ b/Sources/DangerSwiftKantoku/Kantoku.swift @@ -11,6 +11,8 @@ import XCResultKit public struct Kantoku { let workingDirectoryPath: String + let modifiedFiles: [String] + let createdFiles: [String] private let markdownCommentExecutor: (_ comment: String) -> Void @@ -25,6 +27,8 @@ public struct Kantoku { init( workingDirectoryPath: String, + modifiedFiles: [String], + createdFiles: [String], markdownCommentExecutor: @escaping (_ comment: String) -> Void, inlineCommentExecutor: @escaping (_ comment: String, _ filePath: String, _ lineNumber: Int) -> Void, normalCommentExecutor: @escaping (_ comment: String) -> Void, @@ -34,6 +38,8 @@ public struct Kantoku { normalFailureExecutor: @escaping (_ comment: String) -> Void ) { self.workingDirectoryPath = workingDirectoryPath + self.modifiedFiles = modifiedFiles + self.createdFiles = createdFiles self.markdownCommentExecutor = markdownCommentExecutor self.inlineCommentExecutor = inlineCommentExecutor self.normalCommentExecutor = normalCommentExecutor @@ -77,6 +83,8 @@ extension Kantoku { } + + extension Kantoku { private func postIssuesIfNeeded(from resultFile: XCResultFile, configuration: XCResultParsingConfiguration) { @@ -87,9 +95,10 @@ extension Kantoku { warn("Failed to get invocation record from \(resultFile.url.absoluteString)") return } - + if configuration.parseBuildWarnings { - post(issues.warningSummaries, as: .warning) + let filteredSummaries = summaries(of: issues.warningSummaries, filteredBy: configuration.reportingFileType) + post(filteredSummaries, as: .warning) } if configuration.parseBuildErrors { @@ -155,3 +164,31 @@ extension XCResultParsingConfiguration.CodeCoverageRequirement { } } + +extension Kantoku { + + private func summaries(of summaries: [T], filteredBy fileType: XCResultParsingConfiguration.ReportingFileType) -> [T] { + + let filteringPredicate: (XCResultParsingConfiguration.RelativeFilePath) -> Bool + + switch fileType { + case .all: + return summaries + + case .modifiedAndCreatedFiles: + filteringPredicate = { (modifiedFiles + createdFiles).contains($0) } + + case .custom(predicate: let predicate): + filteringPredicate = predicate + } + + return summaries.filter { summary in + guard let relativePath = summary.documentLocation?.relativePath(against: workingDirectoryPath) else { + return false + } + return filteringPredicate(relativePath.filePath) + } + + } + +} diff --git a/Sources/DangerSwiftKantoku/XCResultParsingConfiguration.swift b/Sources/DangerSwiftKantoku/XCResultParsingConfiguration.swift index 927a8a4..3af2be7 100644 --- a/Sources/DangerSwiftKantoku/XCResultParsingConfiguration.swift +++ b/Sources/DangerSwiftKantoku/XCResultParsingConfiguration.swift @@ -8,6 +8,14 @@ import Foundation public struct XCResultParsingConfiguration { + + public typealias RelativeFilePath = String + + public enum ReportingFileType { + case all + case modifiedAndCreatedFiles + case custom(predicate: (RelativeFilePath) -> Bool) + } public enum CodeCoverageRequirement { public struct CoverageThreshold { @@ -28,19 +36,22 @@ public struct XCResultParsingConfiguration { public var parseTestFailures: Bool public var codeCoverageRequirement: CodeCoverageRequirement + public var reportingFileType: ReportingFileType public init( parseBuildWarnings: Bool = true, parseBuildErrors: Bool = true, parseAnalyzerWarnings: Bool = true, parseTestFailures: Bool = true, - codeCoverageRequirement: CodeCoverageRequirement = .required(.init(acceptable: 0, recommended: 0.6)) + codeCoverageRequirement: CodeCoverageRequirement = .required(.init(acceptable: 0, recommended: 0.6)), + reportingFileType: ReportingFileType = .all ) { self.parseBuildWarnings = parseBuildWarnings self.parseBuildErrors = parseBuildErrors self.parseAnalyzerWarnings = parseAnalyzerWarnings self.parseTestFailures = parseTestFailures self.codeCoverageRequirement = codeCoverageRequirement + self.reportingFileType = reportingFileType } }