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

Feature/responsibility refactor #4

Merged
merged 16 commits into from
Dec 14, 2021
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
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,29 @@ A danger-swift plug-in to manage/post danger checking results with markdown styl

## Usage

- First of all create a result data structure with `CheckResult` initializer
Basically just use `.shoki` property from a `DangerDSL` instance to access all features provided by DangerSwiftShoki

Examples below assume you have initialized a `danger` using `Danger()` in your `Dangerfile.swift`

- First of all create a report data structure with `makeInitialReport` method

```swift
var checkResult = CheckResult(title: "My Check")
var report = danger.shoki.makeInitialReport(title: "My Report")
```

- Then you can perform any check with `check` method, by returning your check result in the trailing `execution` closure
- Then you can perform any checks with `check` method, by returning your check result in the trailing `execution` closure

```swift
checkResult.check("Test Result Check") {
danger.shoki.check("Test Result Check", into: &report) {
if testPassed {
return .good

} else {
if isAcceptable {
warn("Encouraged to make a change but OK at this time")
return .acceptable
return .acceptable(warningMessage: "Encouraged to make a change but OK at this time")

} else {
fail("Must fix")
return .rejected
return .rejected(failureMessage: "Must fix")
}
}
}
Expand All @@ -68,20 +70,20 @@ A danger-swift plug-in to manage/post danger checking results with markdown styl
- You can also ask reviewers not to forget to do some manual checks with `askReviewer` method if needed

```swift
checkResult.askReviewer(to: "Check whether commit messages are correctly formatted or not")
danger.shoki.askReviewer(to: "Check whether commit messages are correctly formatted or not", into: $report)
```

- At last post the whole check result with `shoki.report` method which is available for `DangerDSL` instances
- At last post the whole check result with `report` method

```swift
danger.shoki.report(checkResult) // Assume you have initialized `danger` by code like `let danger = Danger()`
danger.shoki.report(report)
```

## Preview

Code above will make danger producing markdown messages like below

> ## My Check
> ## My Report
>
> Checking Item | Result
> | ---| --- |
Expand Down
103 changes: 0 additions & 103 deletions Sources/DangerSwiftShoki/CheckResult.swift

This file was deleted.

8 changes: 6 additions & 2 deletions Sources/DangerSwiftShoki/DangerDSL+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ import Danger
extension DangerDSL {

public var shoki: Shoki {
return .init(markdownExecutor: { markdown($0) },
messageExecutor: { message($0) })
return .init(
markdownExecutor: { markdown($0) },
messageExecutor: { message($0) },
warningExecutor: { warn($0) },
failureExecutor: { fail($0) }
)
}

}
113 changes: 113 additions & 0 deletions Sources/DangerSwiftShoki/MarkdownConfiguration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//
// MarkdownConfiguration.swift
//
//
// Created by 史 翔新 on 2021/12/02.
//

public struct MarkdownConfiguration {

var titleMarkdownFormatter: (String) -> String
var messageMarkdownFormatter: ([Report.CheckItem]) -> String
var warningMarkdownFormatter: (Report.WarningMessage) -> String
var failureMarkdownFormatter: (Report.FailureMessage) -> String
var todosMarkdownFormatter: ([String]) -> String
var congratulationsMessage: String

public static func defaultTitleMarkdown(title: String) -> String {

"## " + title

}

public static func defaultMessageMarkdown(checkItems: [Report.CheckItem]) -> String {

guard !checkItems.isEmpty else {
return ""
}

let chartHeader = """
Checking Item | Result
| ---| --- |

"""
let chartContent = checkItems.map {
"\($0.title) | \($0.result.markdownSymbol)"
} .joined(separator: "\n")

return chartHeader + chartContent

}

public static func defaultWarningMarkdown(warning: Report.WarningMessage) -> String {

let messageSuffix = warning.message.map { ": \($0)" } ?? " has a warning."
return warning.title + messageSuffix

}

public static func defaultFailureMarkdown(failure: Report.FailureMessage) -> String {

let messageSuffix = failure.message.map { ": \($0)" } ?? " failed."
return failure.title + messageSuffix

}

public static func defaultTodosMarkdown(todos: [String]) -> String {

guard !todos.isEmpty else {
return ""
}

let todoContent = todos.map {
"- [ ] \($0)"
}

return todoContent.joined(separator: "\n")

}

public static var defaultCongratulationsMessage: String {

"Good Job :white_flower:"

}

public init(
titleMarkdownFormatter: @escaping (String) -> String = defaultTitleMarkdown(title:),
messageMarkdownFormatter: @escaping ([Report.CheckItem]) -> String = defaultMessageMarkdown(checkItems:),
warningMarkdownFormatter: @escaping (Report.WarningMessage) -> String = defaultWarningMarkdown(warning:),
failureMarkdownFormatter: @escaping (Report.FailureMessage) -> String = defaultFailureMarkdown(failure:),
todosMarkdownFormatter: @escaping ([String]) -> String = defaultTodosMarkdown(todos:),
congratulationsMessage: String = defaultCongratulationsMessage
) {
self.titleMarkdownFormatter = titleMarkdownFormatter
self.messageMarkdownFormatter = messageMarkdownFormatter
self.warningMarkdownFormatter = warningMarkdownFormatter
self.failureMarkdownFormatter = failureMarkdownFormatter
self.todosMarkdownFormatter = todosMarkdownFormatter
self.congratulationsMessage = congratulationsMessage
}

public static var `default`: MarkdownConfiguration {
.init()
}

}

private extension Report.CheckItem.Result {

var markdownSymbol: String {
switch self {
case .good:
return ":tada:"

case .acceptable:
return ":thinking:"

case .rejected:
return ":no_good:"
}
}

}
Loading