Skip to content

Commit 414e1e5

Browse files
committed
feat(library): allow specifying checkout action's version in consistency check job
1 parent 9698245 commit 414e1e5

File tree

3 files changed

+149
-2
lines changed

3 files changed

+149
-2
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ internal fun WorkflowBuilder.consistencyCheckJob(
3737
condition = consistencyCheckJobConfig.condition,
3838
env = consistencyCheckJobConfig.env,
3939
) {
40+
val checkoutActionVersion = when (consistencyCheckJobConfig.checkoutActionVersion) {
41+
CheckoutActionVersionSource.BundledWithLibrary -> "v4"
42+
is CheckoutActionVersionSource.Given -> consistencyCheckJobConfig.checkoutActionVersion.version
43+
CheckoutActionVersionSource.InferredFromClasspath -> inferCheckoutActionVersionFromClasspath()
44+
}
45+
4046
uses(
4147
name = "Check out",
4248
// Since this action is used in a simple way, and we actually don't want to update the version
@@ -46,7 +52,7 @@ internal fun WorkflowBuilder.consistencyCheckJob(
4652
CustomAction(
4753
actionOwner = "actions",
4854
actionName = "checkout",
49-
actionVersion = "v4",
55+
actionVersion = checkoutActionVersion,
5056
),
5157
)
5258

@@ -99,3 +105,16 @@ internal fun WorkflowBuilder.consistencyCheckJob(
99105
)
100106
}
101107
}
108+
109+
private fun inferCheckoutActionVersionFromClasspath(): String {
110+
val clazz = Class.forName("io.github.typesafegithub.workflows.actions.actions.Checkout")
111+
println("Constructors!")
112+
clazz.declaredConstructors.forEach {
113+
println(it)
114+
}
115+
// TODO: how to call the constructor with default args?
116+
// Or: how to get the version?
117+
val instance = clazz.declaredConstructors.first().newInstance()
118+
val version = clazz.getDeclaredMethod("getVersion").invoke(instance)
119+
return version as String
120+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public val DEFAULT_CONSISTENCY_CHECK_JOB_CONFIG: ConsistencyCheckJobConfig.Confi
77
ConsistencyCheckJobConfig.Configuration(
88
condition = null,
99
env = emptyMap(),
10+
checkoutActionVersion = CheckoutActionVersionSource.BundledWithLibrary,
1011
additionalSteps = null,
1112
useLocalBindingsServerAsFallback = false,
1213
)
@@ -17,6 +18,11 @@ public sealed interface ConsistencyCheckJobConfig {
1718
public data class Configuration(
1819
val condition: String?,
1920
val env: Map<String, String>,
21+
/**
22+
* Configures what version of https://github.com/actions/checkout should be used in the consistency check job.
23+
* Lets the user choose between convenience of automatic updates and more determinism and control if required.
24+
*/
25+
val checkoutActionVersion: CheckoutActionVersionSource,
2026
val additionalSteps: (JobBuilder<JobOutputs.EMPTY>.() -> Unit)?,
2127
/**
2228
* If the script execution step in the consistency check job fails, another attempt to execute is made with a
@@ -27,3 +33,9 @@ public sealed interface ConsistencyCheckJobConfig {
2733
val useLocalBindingsServerAsFallback: Boolean,
2834
) : ConsistencyCheckJobConfig
2935
}
36+
37+
public sealed interface CheckoutActionVersionSource {
38+
public object BundledWithLibrary : CheckoutActionVersionSource
39+
public object InferredFromClasspath : CheckoutActionVersionSource
40+
public class Given(public val version: String) : CheckoutActionVersionSource
41+
}

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

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import io.github.typesafegithub.workflows.actions.endbug.AddAndCommit
66
import io.github.typesafegithub.workflows.annotations.ExperimentalKotlinLogicStep
77
import io.github.typesafegithub.workflows.domain.Concurrency
88
import io.github.typesafegithub.workflows.domain.RunnerType
9-
import io.github.typesafegithub.workflows.domain.actions.Action
109
import io.github.typesafegithub.workflows.domain.actions.Action.Outputs
1110
import io.github.typesafegithub.workflows.domain.actions.RegularAction
1211
import io.github.typesafegithub.workflows.domain.triggers.Push
1312
import io.github.typesafegithub.workflows.dsl.expressions.expr
1413
import io.github.typesafegithub.workflows.dsl.workflow
14+
import io.github.typesafegithub.workflows.yaml.CheckoutActionVersionSource
1515
import io.github.typesafegithub.workflows.yaml.ConsistencyCheckJobConfig.Disabled
1616
import io.github.typesafegithub.workflows.yaml.DEFAULT_CONSISTENCY_CHECK_JOB_CONFIG
1717
import io.github.typesafegithub.workflows.yaml.Preamble.Just
@@ -318,6 +318,122 @@ class IntegrationTest :
318318
""".trimIndent()
319319
}
320320

321+
test("actions/checkout's version given explicitly") {
322+
// when
323+
workflow(
324+
name = "Test workflow",
325+
on = listOf(Push()),
326+
sourceFile = sourceTempFile,
327+
consistencyCheckJobConfig =
328+
DEFAULT_CONSISTENCY_CHECK_JOB_CONFIG.copy(
329+
checkoutActionVersion = CheckoutActionVersionSource.Given("v123"),
330+
),
331+
) {
332+
job(
333+
id = "test_job",
334+
runsOn = RunnerType.UbuntuLatest,
335+
) {
336+
run(
337+
name = "Hello world!",
338+
command = "echo 'hello!'",
339+
)
340+
}
341+
}
342+
343+
// then
344+
targetTempFile.readText() shouldBe
345+
"""
346+
# This file was generated using Kotlin DSL (.github/workflows/some_workflow.main.kts).
347+
# If you want to modify the workflow, please change the Kotlin file and regenerate this YAML file.
348+
# Generated with https://github.com/typesafegithub/github-workflows-kt
349+
350+
name: 'Test workflow'
351+
on:
352+
push: {}
353+
jobs:
354+
check_yaml_consistency:
355+
name: 'Check YAML consistency'
356+
runs-on: 'ubuntu-latest'
357+
steps:
358+
- id: 'step-0'
359+
name: 'Check out'
360+
uses: 'actions/checkout@v123'
361+
- id: 'step-1'
362+
name: 'Execute script'
363+
run: 'rm ''.github/workflows/some_workflow.yaml'' && ''.github/workflows/some_workflow.main.kts'''
364+
- id: 'step-2'
365+
name: 'Consistency check'
366+
run: 'git diff --exit-code ''.github/workflows/some_workflow.yaml'''
367+
test_job:
368+
runs-on: 'ubuntu-latest'
369+
needs:
370+
- 'check_yaml_consistency'
371+
steps:
372+
- id: 'step-0'
373+
name: 'Hello world!'
374+
run: 'echo ''hello!'''
375+
376+
""".trimIndent()
377+
}
378+
379+
test("actions/checkout's version inferred from classpath") {
380+
// when
381+
workflow(
382+
name = "Test workflow",
383+
on = listOf(Push()),
384+
sourceFile = sourceTempFile,
385+
consistencyCheckJobConfig =
386+
DEFAULT_CONSISTENCY_CHECK_JOB_CONFIG.copy(
387+
checkoutActionVersion = CheckoutActionVersionSource.InferredFromClasspath
388+
),
389+
) {
390+
job(
391+
id = "test_job",
392+
runsOn = RunnerType.UbuntuLatest,
393+
) {
394+
run(
395+
name = "Hello world!",
396+
command = "echo 'hello!'",
397+
)
398+
}
399+
}
400+
401+
// then
402+
targetTempFile.readText() shouldBe
403+
"""
404+
# This file was generated using Kotlin DSL (.github/workflows/some_workflow.main.kts).
405+
# If you want to modify the workflow, please change the Kotlin file and regenerate this YAML file.
406+
# Generated with https://github.com/typesafegithub/github-workflows-kt
407+
408+
name: 'Test workflow'
409+
on:
410+
push: {}
411+
jobs:
412+
check_yaml_consistency:
413+
name: 'Check YAML consistency'
414+
runs-on: 'ubuntu-latest'
415+
steps:
416+
- id: 'step-0'
417+
name: 'Check out'
418+
uses: 'actions/checkout@v4'
419+
- id: 'step-1'
420+
name: 'Execute script'
421+
run: 'rm ''.github/workflows/some_workflow.yaml'' && ''.github/workflows/some_workflow.main.kts'''
422+
- id: 'step-2'
423+
name: 'Consistency check'
424+
run: 'git diff --exit-code ''.github/workflows/some_workflow.yaml'''
425+
test_job:
426+
runs-on: 'ubuntu-latest'
427+
needs:
428+
- 'check_yaml_consistency'
429+
steps:
430+
- id: 'step-0'
431+
name: 'Hello world!'
432+
run: 'echo ''hello!'''
433+
434+
""".trimIndent()
435+
}
436+
321437
test("with concurrency, default behavior") {
322438
// when
323439
workflow(

0 commit comments

Comments
 (0)