Skip to content

Commit 34d8966

Browse files
committed
Extract action version from binding's API
1 parent 0301fd0 commit 34d8966

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

github-workflows-kt/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dependencies {
2626
implementation("it.krzeminski:snakeyaml-engine-kmp:4.0.1")
2727
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.9.0")
2828
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")
29+
implementation(kotlin("reflect"))
2930
implementation(projects.sharedInternal)
3031
ksp(projects.codeGenerator)
3132

github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/ConsistencyCheckJob.kt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ import io.github.typesafegithub.workflows.domain.Job
55
import io.github.typesafegithub.workflows.domain.JobOutputs
66
import io.github.typesafegithub.workflows.domain.RunnerType.UbuntuLatest
77
import io.github.typesafegithub.workflows.domain.actions.CustomAction
8+
import io.github.typesafegithub.workflows.domain.actions.RegularAction
89
import io.github.typesafegithub.workflows.dsl.WorkflowBuilder
910
import io.github.typesafegithub.workflows.dsl.expressions.expr
1011
import io.github.typesafegithub.workflows.internal.relativeToAbsolute
1112
import java.nio.file.Path
1213
import kotlin.io.path.invariantSeparatorsPathString
14+
import kotlin.reflect.KParameter
15+
import kotlin.reflect.full.memberProperties
16+
import kotlin.reflect.full.primaryConstructor
17+
import kotlin.reflect.jvm.isAccessible
1318

1419
@Suppress("LongMethod")
1520
internal fun WorkflowBuilder.consistencyCheckJob(
@@ -109,13 +114,14 @@ internal fun WorkflowBuilder.consistencyCheckJob(
109114

110115
private fun inferCheckoutActionVersionFromClasspath(): String {
111116
val clazz = Class.forName("io.github.typesafegithub.workflows.actions.actions.Checkout")
112-
// HACK: Ideally we'd instantiate the binding class and ask it for version, however
113-
// it turned out to be difficult due to default arguments in Kotlin. Using Java's
114-
// reflection, the constructors require passing ~40 arguments. As a workaround,
115-
// the version is extracted from JAR's name, like: 'checkout-v4.jar' -> 'v4'.
116-
val jarName =
117-
clazz.protectionDomain.codeSource.location
118-
.toString()
119-
.substringAfterLast("/")
120-
return jarName.substringAfterLast("-").substringBeforeLast(".")
117+
// It's easier to call the primary constructor, even though it's private, because
118+
// the public constructor requires named arguments, and I'm not sure how to call
119+
// it with these.
120+
val constructor =
121+
clazz.kotlin.primaryConstructor!!.also {
122+
it.isAccessible = true
123+
}
124+
val args: Map<KParameter, Any?> = constructor.parameters.associateWith { null }
125+
val bindingObject = constructor.callBy(args) as RegularAction<*>
126+
return bindingObject.actionVersion
121127
}

0 commit comments

Comments
 (0)