diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a1184f0..c36a3083 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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][] diff --git a/Dangerfile.swift b/Dangerfile.swift index aee8ccd0..4fbf8e7b 100644 --- a/Dangerfile.swift +++ b/Dangerfile.swift @@ -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") diff --git a/Sources/Danger/Danger.swift b/Sources/Danger/Danger.swift index dbe97a25..7c23e7f5 100644 --- a/Sources/Danger/Danger.swift +++ b/Sources/Danger/Danger.swift @@ -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 @@ -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 @@ -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)? diff --git a/Sources/Danger/DangerDSL.swift b/Sources/Danger/DangerDSL.swift index 785500ba..09c073ee 100644 --- a/Sources/Danger/DangerDSL.swift +++ b/Sources/Danger/DangerDSL.swift @@ -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 { @@ -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 + } +} diff --git a/Tests/DangerTests/DangerDSLTests.swift b/Tests/DangerTests/DangerDSLTests.swift index 1c9f0758..1925ec93 100644 --- a/Tests/DangerTests/DangerDSLTests.swift +++ b/Tests/DangerTests/DangerDSLTests.swift @@ -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") } @@ -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")