Skip to content

Commit b98f06b

Browse files
committed
Rename option, refine docs, add tests
1 parent 34d8966 commit b98f06b

File tree

4 files changed

+73
-9
lines changed

4 files changed

+73
-9
lines changed

github-workflows-kt/api/github-workflows-kt.api

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3249,8 +3249,8 @@ public final class io/github/typesafegithub/workflows/yaml/CheckoutActionVersion
32493249
public final fun getVersion ()Ljava/lang/String;
32503250
}
32513251

3252-
public final class io/github/typesafegithub/workflows/yaml/CheckoutActionVersionSource$InferredFromClasspath : io/github/typesafegithub/workflows/yaml/CheckoutActionVersionSource {
3253-
public static final field INSTANCE Lio/github/typesafegithub/workflows/yaml/CheckoutActionVersionSource$InferredFromClasspath;
3252+
public final class io/github/typesafegithub/workflows/yaml/CheckoutActionVersionSource$InferFromClasspath : io/github/typesafegithub/workflows/yaml/CheckoutActionVersionSource {
3253+
public static final field INSTANCE Lio/github/typesafegithub/workflows/yaml/CheckoutActionVersionSource$InferFromClasspath;
32543254
}
32553255

32563256
public abstract interface class io/github/typesafegithub/workflows/yaml/ConsistencyCheckJobConfig {

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import io.github.typesafegithub.workflows.internal.relativeToAbsolute
1212
import java.nio.file.Path
1313
import kotlin.io.path.invariantSeparatorsPathString
1414
import kotlin.reflect.KParameter
15-
import kotlin.reflect.full.memberProperties
1615
import kotlin.reflect.full.primaryConstructor
1716
import kotlin.reflect.jvm.isAccessible
1817

@@ -46,7 +45,10 @@ internal fun WorkflowBuilder.consistencyCheckJob(
4645
when (consistencyCheckJobConfig.checkoutActionVersion) {
4746
CheckoutActionVersionSource.BundledWithLibrary -> "v4"
4847
is CheckoutActionVersionSource.Given -> consistencyCheckJobConfig.checkoutActionVersion.version
49-
CheckoutActionVersionSource.InferredFromClasspath -> inferCheckoutActionVersionFromClasspath()
48+
CheckoutActionVersionSource.InferFromClasspath ->
49+
inferCheckoutActionVersionFromClasspath(
50+
consistencyCheckJobConfig.checkoutActionClassFQN,
51+
)
5052
}
5153

5254
uses(
@@ -112,8 +114,17 @@ internal fun WorkflowBuilder.consistencyCheckJob(
112114
}
113115
}
114116

115-
private fun inferCheckoutActionVersionFromClasspath(): String {
116-
val clazz = Class.forName("io.github.typesafegithub.workflows.actions.actions.Checkout")
117+
private fun inferCheckoutActionVersionFromClasspath(checkoutActionClassFQN: String): String {
118+
val clazz: Class<*> =
119+
try {
120+
Class.forName(checkoutActionClassFQN)
121+
} catch (_: ClassNotFoundException) {
122+
error(
123+
"actions/checkout is not found in the classpath! " +
124+
"Either add a dependency on it (`@file:DependsOn(\"actions:checkout:<version>\")`), " +
125+
"or don't use CheckoutActionVersionSource.InferFromClasspath",
126+
)
127+
} as Class<*>
117128
// It's easier to call the primary constructor, even though it's private, because
118129
// the public constructor requires named arguments, and I'm not sure how to call
119130
// it with these.

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public val DEFAULT_CONSISTENCY_CHECK_JOB_CONFIG: ConsistencyCheckJobConfig.Confi
88
condition = null,
99
env = emptyMap(),
1010
checkoutActionVersion = CheckoutActionVersionSource.BundledWithLibrary,
11+
checkoutActionClassFQN = "io.github.typesafegithub.workflows.actions.actions.Checkout",
1112
additionalSteps = null,
1213
useLocalBindingsServerAsFallback = false,
1314
)
@@ -19,10 +20,15 @@ public sealed interface ConsistencyCheckJobConfig {
1920
val condition: String?,
2021
val env: Map<String, String>,
2122
/**
22-
* Configures what version of https://github.com/actions/checkout should be used in the consistency check job.
23+
* Configures what version of https://github.com/actions/checkout is used in the consistency check job.
2324
* Lets the user choose between convenience of automatic updates and more determinism and control if required.
2425
*/
2526
val checkoutActionVersion: CheckoutActionVersionSource,
27+
/**
28+
* Specifies the fully qualified name of a binding class for actions/checkout. Can be overridden if a custom
29+
* binding class is provided for this action, under a different FQN.
30+
*/
31+
val checkoutActionClassFQN: String,
2632
val additionalSteps: (JobBuilder<JobOutputs.EMPTY>.() -> Unit)?,
2733
/**
2834
* If the script execution step in the consistency check job fails, another attempt to execute is made with a
@@ -35,10 +41,27 @@ public sealed interface ConsistencyCheckJobConfig {
3541
}
3642

3743
public sealed interface CheckoutActionVersionSource {
44+
/**
45+
* Every version of github-workflows-kt library comes with a hardcoded version of this action.
46+
* Since bumping this version is possible only upon major releases (major version bumps), this
47+
* version often lags behind the newest one.
48+
*
49+
* This is the current default option.
50+
*/
3851
public object BundledWithLibrary : CheckoutActionVersionSource
3952

40-
public object InferredFromClasspath : CheckoutActionVersionSource
53+
/**
54+
* In the great majority of workflows, the checkout action is used in a preferred version.
55+
* We can utilize this fact and use the same version in the consistency check job.
56+
* Warning: this option won't work if a dependency on "action:checkout:vN" is not present.
57+
*
58+
* This is the most convenient option, and a candidate to become the default one.
59+
*/
60+
public object InferFromClasspath : CheckoutActionVersionSource
4161

62+
/**
63+
* Useful if it's desired to specify a concrete version by hand, right in your workflow.
64+
*/
4265
public class Given(
4366
public val version: String,
4467
) : CheckoutActionVersionSource

github-workflows-kt/src/test/kotlin/io/github/typesafegithub/workflows/IntegrationTest.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ class IntegrationTest :
384384
sourceFile = sourceTempFile,
385385
consistencyCheckJobConfig =
386386
DEFAULT_CONSISTENCY_CHECK_JOB_CONFIG.copy(
387-
checkoutActionVersion = CheckoutActionVersionSource.InferredFromClasspath,
387+
checkoutActionVersion = CheckoutActionVersionSource.InferFromClasspath,
388388
),
389389
) {
390390
job(
@@ -434,6 +434,36 @@ class IntegrationTest :
434434
""".trimIndent()
435435
}
436436

437+
test("actions/checkout's version inferred from classpath but binding class is not available") {
438+
// when
439+
shouldThrow<IllegalStateException> {
440+
workflow(
441+
name = "Test workflow",
442+
on = listOf(Push()),
443+
sourceFile = sourceTempFile,
444+
consistencyCheckJobConfig =
445+
DEFAULT_CONSISTENCY_CHECK_JOB_CONFIG.copy(
446+
checkoutActionVersion = CheckoutActionVersionSource.InferFromClasspath,
447+
checkoutActionClassFQN = "does.not.Exist",
448+
),
449+
) {
450+
job(
451+
id = "test_job",
452+
runsOn = RunnerType.UbuntuLatest,
453+
) {
454+
run(
455+
name = "Hello world!",
456+
command = "echo 'hello!'",
457+
)
458+
}
459+
}
460+
}.also {
461+
it.message shouldBe "actions/checkout is not found in the classpath! " +
462+
"Either add a dependency on it (`@file:DependsOn(\"actions:checkout:<version>\")`), " +
463+
"or don't use CheckoutActionVersionSource.InferFromClasspath"
464+
}
465+
}
466+
437467
test("with concurrency, default behavior") {
438468
// when
439469
workflow(

0 commit comments

Comments
 (0)