From 97960a33e1d15bdac1732ecd44cd293ea57624fa Mon Sep 17 00:00:00 2001 From: Sudeep Kini Date: Thu, 4 Aug 2022 12:06:37 +0530 Subject: [PATCH 1/2] Adds filter Options for warnings 1. Adds a enum to specify which files should show warnings using Danger 2. Add code to generates list of file names from the options 3. Add code to filter warning issues based on file names list generate above. --- .../Kantoku+IssueComments.swift | 18 +++++++--- Sources/DangerSwiftKantoku/Kantoku.swift | 35 ++++++++++++++++--- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/Sources/DangerSwiftKantoku/Kantoku+IssueComments.swift b/Sources/DangerSwiftKantoku/Kantoku+IssueComments.swift index c70a63c..abda3bc 100644 --- a/Sources/DangerSwiftKantoku/Kantoku+IssueComments.swift +++ b/Sources/DangerSwiftKantoku/Kantoku+IssueComments.swift @@ -45,23 +45,33 @@ extension Kantoku { } - func post(_ summaries: [PostableIssueSummary], as level: CommentLevel) { - + func post(_ summaries: [PostableIssueSummary], + as level: CommentLevel, + targetFilesNmes: [File]? = nil) { for summary in summaries { let message = summary.issueMessage let filePath = summary.documentLocation?.relativePath(against: workingDirectoryPath) - + + if let filePath = filePath { let lineNumber = filePath.queries?.endingLineNumber + if let targetFilesNmes = targetFilesNmes { + if !(targetFilesNmes.contains(filePath.filePath.finalFileName)) { + continue + } + } // Line numbers in XCResult starts from `0`, while on web pages like GitHub starts from `1` post(as: level)(message, filePath.filePath, lineNumber.map({ $0 + 1 }) ?? 0) } else { + if targetFilesNmes != nil { + continue + } post(as: level)(message) } } } - + } diff --git a/Sources/DangerSwiftKantoku/Kantoku.swift b/Sources/DangerSwiftKantoku/Kantoku.swift index 4ff4ed9..16e4686 100644 --- a/Sources/DangerSwiftKantoku/Kantoku.swift +++ b/Sources/DangerSwiftKantoku/Kantoku.swift @@ -7,6 +7,7 @@ import Foundation import XCResultKit +import Danger public struct Kantoku { @@ -77,9 +78,23 @@ extension Kantoku { } +public typealias File = String +extension File { + var finalFileName: String { + return self.components(separatedBy: "/").last ?? "" + + } +} + extension Kantoku { - private func postIssuesIfNeeded(from resultFile: XCResultFile, configuration: XCResultParsingConfiguration) { + public enum WariningFilter { + case all + case modifiedAndCreatedFiles + case files([File]) + } + + private func postIssuesIfNeeded(from resultFile: XCResultFile, configuration: XCResultParsingConfiguration, warnFor: WariningFilter = .all) { if configuration.needsIssues { @@ -87,9 +102,19 @@ extension Kantoku { warn("Failed to get invocation record from \(resultFile.url.absoluteString)") return } - + + var targetFileNames: [String]? = nil + switch warnFor { + case .all : break + case .files(let files) : + targetFileNames = files.map{ $0.finalFileName } + case .modifiedAndCreatedFiles : + let allFilePaths = Danger().git.modifiedFiles + Danger().git.createdFiles + targetFileNames = allFilePaths.map { $0.finalFileName } + } + if configuration.parseBuildWarnings { - post(issues.warningSummaries, as: .warning) + post(issues.warningSummaries, as: .warning, targetFilesNmes: targetFileNames) } if configuration.parseBuildErrors { @@ -123,11 +148,11 @@ extension Kantoku { } - public func parseXCResultFile(at filePath: String, configuration: XCResultParsingConfiguration) { + public func parseXCResultFile(at filePath: String, configuration: XCResultParsingConfiguration, warnFor: WariningFilter) { let resultFile = XCResultFile(url: .init(fileURLWithPath: filePath)) - postIssuesIfNeeded(from: resultFile, configuration: configuration) + postIssuesIfNeeded(from: resultFile, configuration: configuration, warnFor: warnFor) postCoverageIfNeeded(from: resultFile, configuration: configuration) } From e5455950d8c8c46131ec8648d31db181fcb5347a Mon Sep 17 00:00:00 2001 From: Sudeep Kini Date: Thu, 18 Aug 2022 14:57:26 +0530 Subject: [PATCH 2/2] Updates as per PR comments --- Sources/DangerSwiftKantoku/DangerDSL+.swift | 2 + .../Kantoku+IssueComments.swift | 13 +--- Sources/DangerSwiftKantoku/Kantoku.swift | 66 +++++++++++-------- .../XCResultParsingConfiguration.swift | 13 +++- 4 files changed, 54 insertions(+), 40 deletions(-) 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 abda3bc..4b0f205 100644 --- a/Sources/DangerSwiftKantoku/Kantoku+IssueComments.swift +++ b/Sources/DangerSwiftKantoku/Kantoku+IssueComments.swift @@ -45,28 +45,17 @@ extension Kantoku { } - func post(_ summaries: [PostableIssueSummary], - as level: CommentLevel, - targetFilesNmes: [File]? = nil) { + 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 - if let targetFilesNmes = targetFilesNmes { - if !(targetFilesNmes.contains(filePath.filePath.finalFileName)) { - continue - } - } // Line numbers in XCResult starts from `0`, while on web pages like GitHub starts from `1` post(as: level)(message, filePath.filePath, lineNumber.map({ $0 + 1 }) ?? 0) } else { - if targetFilesNmes != nil { - continue - } post(as: level)(message) } diff --git a/Sources/DangerSwiftKantoku/Kantoku.swift b/Sources/DangerSwiftKantoku/Kantoku.swift index 16e4686..ff8f498 100644 --- a/Sources/DangerSwiftKantoku/Kantoku.swift +++ b/Sources/DangerSwiftKantoku/Kantoku.swift @@ -7,11 +7,12 @@ import Foundation import XCResultKit -import Danger public struct Kantoku { let workingDirectoryPath: String + let modifiedFiles: [String] + let createdFiles: [String] private let markdownCommentExecutor: (_ comment: String) -> Void @@ -26,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, @@ -35,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 @@ -78,23 +83,11 @@ extension Kantoku { } -public typealias File = String -extension File { - var finalFileName: String { - return self.components(separatedBy: "/").last ?? "" - } -} extension Kantoku { - public enum WariningFilter { - case all - case modifiedAndCreatedFiles - case files([File]) - } - - private func postIssuesIfNeeded(from resultFile: XCResultFile, configuration: XCResultParsingConfiguration, warnFor: WariningFilter = .all) { + private func postIssuesIfNeeded(from resultFile: XCResultFile, configuration: XCResultParsingConfiguration) { if configuration.needsIssues { @@ -103,18 +96,9 @@ extension Kantoku { return } - var targetFileNames: [String]? = nil - switch warnFor { - case .all : break - case .files(let files) : - targetFileNames = files.map{ $0.finalFileName } - case .modifiedAndCreatedFiles : - let allFilePaths = Danger().git.modifiedFiles + Danger().git.createdFiles - targetFileNames = allFilePaths.map { $0.finalFileName } - } - if configuration.parseBuildWarnings { - post(issues.warningSummaries, as: .warning, targetFilesNmes: targetFileNames) + let filteredSummaries = summaries(of: issues.warningSummaries, filteredBy: configuration.reportingFileType) + post(filteredSummaries, as: .warning) } if configuration.parseBuildErrors { @@ -148,11 +132,11 @@ extension Kantoku { } - public func parseXCResultFile(at filePath: String, configuration: XCResultParsingConfiguration, warnFor: WariningFilter) { + public func parseXCResultFile(at filePath: String, configuration: XCResultParsingConfiguration) { let resultFile = XCResultFile(url: .init(fileURLWithPath: filePath)) - postIssuesIfNeeded(from: resultFile, configuration: configuration, warnFor: warnFor) + postIssuesIfNeeded(from: resultFile, configuration: configuration) postCoverageIfNeeded(from: resultFile, configuration: configuration) } @@ -180,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 } }