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

Coroutines compatibility #177

Merged
merged 8 commits into from
Sep 2, 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
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ jobs:

- name: Install Kotlin
run: |
curl -o kotlin-compiler.zip -L https://github.com/JetBrains/kotlin/releases/download/v1.4.31/kotlin-compiler-1.4.31.zip
curl -o kotlin-compiler.zip -L https://github.com/JetBrains/kotlin/releases/download/v1.5.10/kotlin-compiler-1.5.10.zip

if [[ "$OSTYPE" != "darwin"* ]]
then
sudo chmod -R a+rwx /usr/local/

unzip -d /usr/local/bin kotlin-compiler.zip
echo "/usr/local/bin/kotlinc/bin" >> $GITHUB_PATH
rm -rf kotlin-compiler.zip
fi

unzip -d /usr/local/bin kotlin-compiler.zip
echo "/usr/local/bin/kotlinc/bin" >> $GITHUB_PATH
rm -rf kotlin-compiler.zip

- uses: actions/setup-node@v2
with:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
-->
## Master

- Coroutines compatibility [@gianluz] - [#177](https://github.com/danger/kotlin/pull/177)
- Improving error message for when a DangerPlugin was not registered [@rojanthomas] - [#181](https://github.com/danger/kotlin/pull/181)
- Fix body parameter in github models [@tegorov] - [#175](https://github.com/danger/kotlin/pull/175)

Expand Down
27 changes: 27 additions & 0 deletions Dangerfile_ci.df.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
//Testing plugin
@file:DependsOn("danger-kotlin-sample-plugin-sample.jar")

import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.datetime.Clock
import org.apache.commons.text.WordUtils
import systems.danger.kotlin.*
import systems.danger.kotlin.models.danger.DangerDSL
import systems.danger.samples.plugin.SamplePlugin

register plugin SamplePlugin
Expand Down Expand Up @@ -49,4 +54,26 @@ danger(args) {
warn(WordUtils.capitalize("please consider to create new files in Kotlin"), it, 1)
}
}

// Coroutines checks in parallel test
val current = Clock.System.now()
runBlocking {
val task1 = async { expensiveCheck("1", 1000) }
val task2 = async { expensiveCheck("2", 3000) }
val task3 = async { expensiveCheck("3", 2000) }
val task4 = async { expensiveCheck("4", 5000) }
task1.await()
task2.await()
task3.await()
task4.await()
}
val after = Clock.System.now()
val runningTime = after.minus(current)
message("Coroutines checks terminated - runningFor $runningTime")
}

suspend fun DangerDSL.expensiveCheck(name: String, runForMillis: Long) {
// Example expensive check
delay(runForMillis)
message("Coroutine $name terminated in $runForMillis ms")
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package systems.danger.kotlin

import systems.danger.kotlin.json.JsonParser
import systems.danger.kotlin.models.danger.ConcurrentDangerResults
import systems.danger.kotlin.models.danger.DSL
import systems.danger.kotlin.models.danger.DangerDSL
import systems.danger.kotlin.models.danger.DangerResults
import systems.danger.kotlin.models.git.FilePath
import systems.danger.kotlin.sdk.DangerContext
import systems.danger.kotlin.sdk.Violation
import java.io.File
import java.util.concurrent.atomic.AtomicReference

/**
* Main Danger runner
Expand All @@ -23,23 +24,23 @@ internal class MainDangerRunner(jsonInputFilePath: FilePath, jsonOutputPath: Fil

val danger: DangerDSL = JsonParser.decodeJson<DSL>(jsonInputFilePath).danger

private val dangerResults: DangerResults = DangerResults()
private val concurrentDangerResults: ConcurrentDangerResults = ConcurrentDangerResults()

override val fails: List<Violation>
get() {
return dangerResults.fails.toList()
return concurrentDangerResults.fails.get()
}
override val warnings: List<Violation>
get() {
return dangerResults.warnings.toList()
return concurrentDangerResults.warnings.get()
}
override val messages: List<Violation>
get() {
return dangerResults.messages.toList()
return concurrentDangerResults.messages.get()
}
override val markdowns: List<Violation>
get() {
return dangerResults.markdowns.toList()
return concurrentDangerResults.markdowns.get()
}


Expand Down Expand Up @@ -95,27 +96,34 @@ internal class MainDangerRunner(jsonInputFilePath: FilePath, jsonOutputPath: Fil
}

private fun warn(violation: Violation) {
dangerResults.warnings.add(violation)
commit()
concurrentDangerResults.warnings.updateWith(violation)
}

private fun fail(violation: Violation) {
dangerResults.fails.add(violation)
commit()
concurrentDangerResults.fails.updateWith(violation)
}

private fun message(violation: Violation) {
dangerResults.messages.add(violation)
commit()
concurrentDangerResults.messages.updateWith(violation)
}

private fun markdown(violation: Violation) {
dangerResults.markdowns.add(violation)
concurrentDangerResults.markdowns.updateWith(violation)
}

private fun AtomicReference<MutableList<Violation>>.updateWith(violation: Violation) {
this.getAndUpdate {
it.apply {
add(violation)
}
}
commit()
}

// commit all the inline violations into the json output file
private fun commit() {
JsonParser.encodeJson(dangerResults, jsonOutputFile)
synchronized(this) {
JsonParser.encodeJson(concurrentDangerResults.toDangerResults(), jsonOutputFile)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
import systems.danger.kotlin.sdk.Violation
import systems.danger.kotlin.models.serializers.ViolationSerializer
import java.util.concurrent.atomic.AtomicReference

@Serializable
internal data class Meta(
Expand All @@ -15,9 +16,20 @@ internal data class Meta(

@Serializable
internal data class DangerResults(
var fails: MutableList<Violation> = mutableListOf(),
var warnings: MutableList<Violation> = mutableListOf(),
var messages: MutableList<Violation> = mutableListOf(),
var markdowns: MutableList<Violation> = mutableListOf(),
val fails: List<Violation>,
val warnings: List<Violation>,
val messages: List<Violation>,
val markdowns: List<Violation>,
val meta: Meta = Meta()
)

internal data class ConcurrentDangerResults(
val fails: AtomicReference<MutableList<Violation>> = AtomicReference(mutableListOf()),
val warnings: AtomicReference<MutableList<Violation>> = AtomicReference(mutableListOf()),
val messages: AtomicReference<MutableList<Violation>> = AtomicReference(mutableListOf()),
val markdowns: AtomicReference<MutableList<Violation>> = AtomicReference(mutableListOf())
) {
fun toDangerResults() = DangerResults(
fails.get(), warnings.get(), messages.get(), markdowns.get()
)
}
12 changes: 8 additions & 4 deletions dependencyVersions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,28 @@ project.ext.groupIdOkio = 'com.squareup.okio'
project.ext.artifactIdOkio = 'okio'

// Kotlin
project.ext.versionKotlin = '1.5.0'
project.ext.versionKotlin = '1.5.10'
project.ext.groupIdKotlin = 'org.jetbrains.kotlin'
project.ext.groupIdKotlinx = 'org.jetbrains.kotlinx'
project.ext.artifactIdKotlinMain = 'kotlin-main-kts'
project.ext.artifactIdKotlinSerializationJson = 'kotlinx-serialization-json'
project.ext.artifactIdKotlinDatetime = "kotlinx-datetime"
project.ext.versionKotlinSerializationJson = '1.0.1'
project.ext.versionKotlinDatetime = '0.1.1'
project.ext.artifactIdKotlinCoroutines = "kotlinx-coroutines-core"
project.ext.versionKotlinSerializationJson = '1.2.1'
project.ext.versionKotlinDatetime = '0.2.0'
project.ext.versionKotlinCoroutines = '1.5.0'

ext.kotlin = [
mainKts: "$groupIdKotlin:$artifactIdKotlinMain:$versionKotlin",
serialization: "$groupIdKotlinx:$artifactIdKotlinSerializationJson:$versionKotlinSerializationJson",
datetime: "$groupIdKotlinx:$artifactIdKotlinDatetime:$versionKotlinDatetime"
datetime: "$groupIdKotlinx:$artifactIdKotlinDatetime:$versionKotlinDatetime",
coroutines: "$groupIdKotlinx:$artifactIdKotlinCoroutines:$versionKotlinCoroutines"
]
ext.kotlinDependencies = group {
includeJar kotlin.mainKts
includeRecursiveJar kotlin.serialization
includeRecursiveJar kotlin.datetime
includeRecursiveJar kotlin.coroutines
}

// Testing
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
kotlinVersion=1.5.0
kotlinVersion=1.5.10
kotlin.code.style=official
systemProp.org.gradle.internal.publish.checksums.insecure=true