Skip to content

Commit

Permalink
Merge pull request #3 from theScud/feature/filterWarnngsforIssues
Browse files Browse the repository at this point in the history
Adds filter Options for warnings similar to swift lint in danger
  • Loading branch information
el-hoshino authored Aug 22, 2022
2 parents 7322cb8 + e545595 commit 8d3283e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Sources/DangerSwiftKantoku/DangerDSL+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand Down
5 changes: 2 additions & 3 deletions Sources/DangerSwiftKantoku/Kantoku+IssueComments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -63,5 +62,5 @@ extension Kantoku {
}

}

}
41 changes: 39 additions & 2 deletions Sources/DangerSwiftKantoku/Kantoku.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import XCResultKit
public struct Kantoku {

let workingDirectoryPath: String
let modifiedFiles: [String]
let createdFiles: [String]

private let markdownCommentExecutor: (_ comment: String) -> Void

Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -77,6 +83,8 @@ extension Kantoku {

}



extension Kantoku {

private func postIssuesIfNeeded(from resultFile: XCResultFile, configuration: XCResultParsingConfiguration) {
Expand All @@ -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 {
Expand Down Expand Up @@ -155,3 +164,31 @@ extension XCResultParsingConfiguration.CodeCoverageRequirement {
}

}

extension Kantoku {

private func summaries<T: PostableIssueSummary>(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)
}

}

}
13 changes: 12 additions & 1 deletion Sources/DangerSwiftKantoku/XCResultParsingConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

}
Expand Down

0 comments on commit 8d3283e

Please sign in to comment.