Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Create the Danger main instance only once #185

Merged
merged 2 commits into from
Sep 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import java.util.concurrent.atomic.AtomicReference
* @param jsonOutputPath the output json file path used to publish the danger results on your Pull Request
*/
internal class MainDangerRunner(jsonInputFilePath: FilePath, jsonOutputPath: FilePath) : DangerContext {

private val jsonOutputFile: File = File(jsonOutputPath)

val danger: DangerDSL = JsonParser.decodeJson<DSL>(jsonInputFilePath).danger
Expand Down Expand Up @@ -86,7 +85,7 @@ internal class MainDangerRunner(jsonInputFilePath: FilePath, jsonOutputPath: Fil
}

override fun suggest(code: String, file: FilePath, line: Int) {
if (dangerRunner.danger.onGitHub) {
if (runnerInstance.danger.onGitHub) {
val message = "```suggestion\n $code \n```"
markdown(Violation(message, file, line))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ package systems.danger.kotlin
import systems.danger.kotlin.models.danger.DangerDSL
import systems.danger.kotlin.models.git.FilePath

internal lateinit var dangerRunner: MainDangerRunner
internal var dangerRunner: MainDangerRunner? = null

internal val runnerInstance: MainDangerRunner
get() {
if(dangerRunner != null) {
return dangerRunner!!
} else {
throw IllegalArgumentException("Danger must be initialised before accessing it")
}
}

/**
* Syntactic sugar that allows you to work with a [DangerDSL] descriptor in a single Danger block.
Expand Down Expand Up @@ -31,14 +40,16 @@ inline fun danger(args: Array<String>, block: DangerDSL.() -> Unit) = Danger(arg
* @return a new [DangerDSL] descriptor
*/
fun Danger(args: Array<String>): DangerDSL {
val argsCount = args.count()
if (dangerRunner == null) {
val argsCount = args.count()

val jsonInputFilePath = args[argsCount - 2]
val jsonOutputPath = args[argsCount - 1]
val jsonInputFilePath = args[argsCount - 2]
val jsonOutputPath = args[argsCount - 1]

dangerRunner = MainDangerRunner(jsonInputFilePath, jsonOutputPath)
dangerRunner = MainDangerRunner(jsonInputFilePath, jsonOutputPath)
}

return dangerRunner.danger
return dangerRunner!!.danger
}

/**
Expand All @@ -47,7 +58,7 @@ fun Danger(args: Array<String>): DangerDSL {
* @param message the standard message
*/
fun message(message: String) =
dangerRunner.message(message)
runnerInstance.message(message)

/**
* Adds an inline message message to the Danger report
Expand All @@ -57,15 +68,15 @@ fun message(message: String) =
* @param line the line number into the target file
*/
fun message(message: String, file: FilePath, line: Int) =
dangerRunner.message(message, file, line)
runnerInstance.message(message, file, line)

/**
* Adds an inline markdown message to the Danger report
*
* @param message the markdown formatted message
*/
fun markdown(message: String) =
dangerRunner.markdown(message)
runnerInstance.markdown(message)

/**
* Adds an inline markdown message to the Danger report
Expand All @@ -75,15 +86,15 @@ fun markdown(message: String) =
* @param line the line number into the target file
*/
fun markdown(message: String, file: FilePath, line: Int) =
dangerRunner.markdown(message, file, line)
runnerInstance.markdown(message, file, line)

/**
* Adds an inline warning message to the Danger report
*
* @param message the warning message
*/
fun warn(message: String) =
dangerRunner.warn(message)
runnerInstance.warn(message)

/**
* Adds an inline warning message to the Danger report
Expand All @@ -93,15 +104,15 @@ fun warn(message: String) =
* @param line the line number into the target file
*/
fun warn(message: String, file: FilePath, line: Int) =
dangerRunner.warn(message, file, line)
runnerInstance.warn(message, file, line)

/**
* Adds an inline fail message to the Danger report
*
* @param message the fail message
*/
fun fail(message: String) =
dangerRunner.fail(message)
runnerInstance.fail(message)

/**
* Adds an inline fail message to the Danger report
Expand All @@ -111,7 +122,7 @@ fun fail(message: String) =
* @param line the line number into the target file
*/
fun fail(message: String, file: FilePath, line: Int) =
dangerRunner.fail(message, file, line)
runnerInstance.fail(message, file, line)

/**
* Adds an inline suggested code message to the Danger report
Expand All @@ -121,4 +132,4 @@ fun fail(message: String, file: FilePath, line: Int) =
* @param line the line number into the target file
*/
fun suggest(code: String, file: FilePath, line: Int) =
dangerRunner.suggest(code, file, line)
runnerInstance.suggest(code, file, line)