Skip to content

Commit

Permalink
Merge pull request #110 from danger/suggestions
Browse files Browse the repository at this point in the history
Suggestions
  • Loading branch information
orta committed Nov 18, 2018
2 parents 8880a11 + d6ac342 commit ad2be9e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

## Master

* Suggestions support [#110](https://github.com/danger/danger-swift/pull/110) by [@f-meloni][]
* Separate the Danger library from the Runner [#109](https://github.com/danger/danger-swift/pull/109) by [@f-meloni][]
* Use danger-command for calls instead of danger command [#105](https://github.com/danger/danger-swift/pull/105) by [@f-meloni][]

Expand Down
2 changes: 2 additions & 0 deletions Dangerfile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ if danger.github.pullRequest.title.contains("WIP") {
warn("PR is classed as Work in Progress")
}

suggestion(code: "Test Suggestion", file: "Sources/Danger/Danger.swift", line: 222)

print("HI")
_ = danger.github.api.me { response in
print("OK")
Expand Down
25 changes: 24 additions & 1 deletion Sources/Danger/Danger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Logger
// MARK: - DangerRunner

private final class DangerRunner {
static let shared = DangerRunner()
fileprivate static let shared = DangerRunner()

let logger: Logger
let dsl: DangerDSL
Expand Down Expand Up @@ -136,6 +136,24 @@ extension DangerDSL {
public func markdown(message: String, file: String, line: Int) {
DangerRunner.shared.results.markdowns.append(Violation(message: message, file: file, line: line))
}

/// Adds an inline suggestion to the Danger report (sends a normal message if suggestions are not supported)
public func suggestion(code: String, file: String, line: Int) {
let message: String

if DangerRunner.shared.dsl.supportsSuggestions {
message = """
```suggestion
\(code)
```
"""
} else {
message = code
}

DangerRunner.shared.results.markdowns.append(Violation(message: message, file: file, line: line))
}

}

/// Fails on the Danger report
Expand Down Expand Up @@ -206,6 +224,11 @@ public func markdown(message: String, file: String, line: Int) {
DangerRunner.shared.dsl.markdown(message: message, file: file, line: line)
}

/// Adds an inline suggestion to the Danger report (sends a normal message if suggestions are not supported)
public func suggestion(code: String, file: String, line: Int) {
DangerRunner.shared.dsl.suggestion(code: code, file: file, line: line)
}

// MARK: - Private Functions

private var dumpInfo: (danger: DangerRunner, path: String)?
Expand Down
16 changes: 15 additions & 1 deletion Sources/Danger/DangerDSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public struct DangerDSL: Decodable {

let settings = try container.decode(Settings.self, forKey: .settings)

if github != nil {
if runningOnGithub {
let config: TokenConfiguration

if let baseURL = settings.github.baseURL {
Expand All @@ -45,3 +45,17 @@ public struct DangerDSL: Decodable {
}
}
}

extension DangerDSL {
var runningOnGithub: Bool {
return github != nil
}

var runningOnBitbucketServer: Bool {
return bitbucket_server != nil
}

var supportsSuggestions: Bool {
return runningOnGithub
}
}
4 changes: 4 additions & 0 deletions Tests/DangerTests/DangerDSLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ final class DangerDSLTests: XCTestCase {

XCTAssertNil(danger.bitbucket_server)
XCTAssertNotNil(danger.github)
XCTAssertTrue(danger.runningOnGithub)
XCTAssertTrue(danger.supportsSuggestions)
XCTAssertNotNil(danger.git)
XCTAssert(danger.github.api.configuration.accessToken == "7bd263f8e4becaa3d29b25d534fe6d5f3b555ccf")
}
Expand All @@ -47,6 +49,8 @@ final class DangerDSLTests: XCTestCase {

XCTAssertNil(danger.bitbucket_server)
XCTAssertNotNil(danger.github)
XCTAssertTrue(danger.runningOnGithub)
XCTAssertTrue(danger.supportsSuggestions)
XCTAssertNotNil(danger.git)
XCTAssert(danger.github.api.configuration.accessToken == "7bd263f8e4becaa3d29b25d534fe6d5f3b555ccf")
XCTAssert(danger.github.api.configuration.apiEndpoint == "https://base.url.io")
Expand Down

0 comments on commit ad2be9e

Please sign in to comment.