Skip to content

Commit 90527d8

Browse files
authored
feat(dsl): allow providing callback to use workflow object (#1535)
Part of #1464. It's the new API for using the `Workflow` domain object for whatever it may be useful for, before it's serialized to YAML. In version 3.0, the `workflow` DSL function will stop returning the `Workflow` object, and this new API will be the only way to access the object. Thanks to this, in most scripts, the workflow domain object won't be printed to standard output anymore.
1 parent e39c4ad commit 90527d8

File tree

4 files changed

+84
-80
lines changed

4 files changed

+84
-80
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,8 +1908,8 @@ public final class io/github/typesafegithub/workflows/dsl/WorkflowBuilder {
19081908

19091909
public final class io/github/typesafegithub/workflows/dsl/WorkflowBuilderKt {
19101910
public static final fun toBuilder (Lio/github/typesafegithub/workflows/domain/Workflow;)Lio/github/typesafegithub/workflows/dsl/WorkflowBuilder;
1911-
public static final fun workflow ([Lkotlin/Unit;Ljava/lang/String;Ljava/util/List;Ljava/util/Map;Ljava/io/File;Ljava/lang/String;Lio/github/typesafegithub/workflows/domain/Concurrency;Lio/github/typesafegithub/workflows/yaml/ConsistencyCheckJobConfig;Ljava/util/Map;Ljava/nio/file/Path;Lio/github/typesafegithub/workflows/yaml/Preamble;Lkotlin/jvm/functions/Function1;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Lio/github/typesafegithub/workflows/domain/Workflow;
1912-
public static synthetic fun workflow$default ([Lkotlin/Unit;Ljava/lang/String;Ljava/util/List;Ljava/util/Map;Ljava/io/File;Ljava/lang/String;Lio/github/typesafegithub/workflows/domain/Concurrency;Lio/github/typesafegithub/workflows/yaml/ConsistencyCheckJobConfig;Ljava/util/Map;Ljava/nio/file/Path;Lio/github/typesafegithub/workflows/yaml/Preamble;Lkotlin/jvm/functions/Function1;Ljava/util/Map;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lio/github/typesafegithub/workflows/domain/Workflow;
1911+
public static final fun workflow ([Lkotlin/Unit;Ljava/lang/String;Ljava/util/List;Ljava/util/Map;Ljava/io/File;Ljava/lang/String;Lio/github/typesafegithub/workflows/domain/Concurrency;Lio/github/typesafegithub/workflows/yaml/ConsistencyCheckJobConfig;Ljava/util/Map;Ljava/nio/file/Path;Lio/github/typesafegithub/workflows/yaml/Preamble;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Lio/github/typesafegithub/workflows/domain/Workflow;
1912+
public static synthetic fun workflow$default ([Lkotlin/Unit;Ljava/lang/String;Ljava/util/List;Ljava/util/Map;Ljava/io/File;Ljava/lang/String;Lio/github/typesafegithub/workflows/domain/Concurrency;Lio/github/typesafegithub/workflows/yaml/ConsistencyCheckJobConfig;Ljava/util/Map;Ljava/nio/file/Path;Lio/github/typesafegithub/workflows/yaml/Preamble;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Ljava/util/Map;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lio/github/typesafegithub/workflows/domain/Workflow;
19131913
}
19141914

19151915
public final class io/github/typesafegithub/workflows/dsl/expressions/Contexts : io/github/typesafegithub/workflows/dsl/expressions/contexts/FunctionsContext {

github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/dsl/WorkflowBuilder.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ public fun workflow(
191191
permissions: Map<Permission, Mode>? = null,
192192
gitRootDir: Path? = sourceFile?.toPath()?.absolute()?.findGitRoot(),
193193
preamble: Preamble? = null,
194+
useWorkflow: (Workflow) -> Unit = {},
194195
getenv: (String) -> String? = { System.getenv(it) },
195196
_customArguments: Map<String, @Contextual Any> = mapOf(),
196197
block: WorkflowBuilder.() -> Unit,
@@ -222,6 +223,7 @@ public fun workflow(
222223
return workflowBuilder
223224
.build()
224225
.also {
226+
useWorkflow(it)
225227
if (targetFileName != null) {
226228
it.writeToFile(
227229
gitRootDir = gitRootDir,

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

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -779,28 +779,26 @@ class IntegrationTest :
779779
}
780780

781781
test("return workflow as string and do not write to file") {
782-
// given
783-
val workflow =
784-
workflow(
785-
name = "Test workflow",
786-
on = listOf(Push()),
787-
sourceFile = sourceTempFile,
788-
targetFileName = null,
789-
consistencyCheckJobConfig = Disabled,
782+
// when
783+
var workflowYaml: String? = null
784+
workflow(
785+
name = "Test workflow",
786+
on = listOf(Push()),
787+
sourceFile = sourceTempFile,
788+
targetFileName = null,
789+
consistencyCheckJobConfig = Disabled,
790+
useWorkflow = { workflowYaml = it.generateYaml() },
791+
) {
792+
job(
793+
id = "test_job",
794+
runsOn = RunnerType.UbuntuLatest,
790795
) {
791-
job(
792-
id = "test_job",
793-
runsOn = RunnerType.UbuntuLatest,
794-
) {
795-
uses(
796-
name = "Check out",
797-
action = CheckoutV4(),
798-
)
799-
}
796+
uses(
797+
name = "Check out",
798+
action = CheckoutV4(),
799+
)
800800
}
801-
802-
// when
803-
val workflowYaml = workflow.generateYaml()
801+
}
804802

805803
// then
806804
workflowYaml shouldBe

github-workflows-kt/src/test/kotlin/io/github/typesafegithub/workflows/dsl/JobBuilderTest.kt

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import io.github.typesafegithub.workflows.actions.actions.CheckoutV4
44
import io.github.typesafegithub.workflows.actions.actions.SetupJavaV4
55
import io.github.typesafegithub.workflows.actions.actions.SetupJavaV4.Distribution.Adopt
66
import io.github.typesafegithub.workflows.domain.RunnerType
7+
import io.github.typesafegithub.workflows.domain.Workflow
78
import io.github.typesafegithub.workflows.domain.triggers.Push
89
import io.kotest.assertions.throwables.shouldThrow
910
import io.kotest.core.spec.style.FunSpec
@@ -23,48 +24,49 @@ class JobBuilderTest :
2324
context("job builder") {
2425
test("with custom step arguments") {
2526
// Given
26-
val workflow =
27-
workflow(
28-
name = "Test workflow",
29-
on = listOf(Push()),
30-
sourceFile = Paths.get(".github/workflows/some_workflow.main.kts").toFile(),
27+
var workflow: Workflow? = null
28+
workflow(
29+
name = "Test workflow",
30+
on = listOf(Push()),
31+
sourceFile = Paths.get(".github/workflows/some_workflow.main.kts").toFile(),
32+
useWorkflow = { workflow = it },
33+
) {
34+
job(
35+
id = "test_job",
36+
runsOn = RunnerType.UbuntuLatest,
3137
) {
32-
job(
33-
id = "test_job",
34-
runsOn = RunnerType.UbuntuLatest,
35-
) {
36-
run(
37-
name = "Hello world!",
38-
command = "echo 'hello!'",
39-
_customArguments = mapOf("foo0" to true),
40-
)
41-
run(
42-
command = "echo 'hello!'",
43-
_customArguments = mapOf("foo1" to true),
44-
)
45-
uses(
46-
name = "Check out",
47-
action = CheckoutV4(),
48-
_customArguments = mapOf("foo2" to true),
49-
)
50-
uses(
51-
action = CheckoutV4(),
52-
_customArguments = mapOf("foo3" to true),
53-
)
54-
uses(
55-
name = "Set up Java",
56-
action = SetupJavaV4(distribution = Adopt, javaVersion = "11"),
57-
_customArguments = mapOf("foo4" to true),
58-
)
59-
uses(
60-
action = SetupJavaV4(distribution = Adopt, javaVersion = "11"),
61-
_customArguments = mapOf("foo5" to true),
62-
)
63-
}
38+
run(
39+
name = "Hello world!",
40+
command = "echo 'hello!'",
41+
_customArguments = mapOf("foo0" to true),
42+
)
43+
run(
44+
command = "echo 'hello!'",
45+
_customArguments = mapOf("foo1" to true),
46+
)
47+
uses(
48+
name = "Check out",
49+
action = CheckoutV4(),
50+
_customArguments = mapOf("foo2" to true),
51+
)
52+
uses(
53+
action = CheckoutV4(),
54+
_customArguments = mapOf("foo3" to true),
55+
)
56+
uses(
57+
name = "Set up Java",
58+
action = SetupJavaV4(distribution = Adopt, javaVersion = "11"),
59+
_customArguments = mapOf("foo4" to true),
60+
)
61+
uses(
62+
action = SetupJavaV4(distribution = Adopt, javaVersion = "11"),
63+
_customArguments = mapOf("foo5" to true),
64+
)
6465
}
66+
}
6567

6668
// When
67-
val job = workflow.jobs.first()
69+
val job = workflow!!.jobs.first()
6870

6971
// Then
7072
job.steps.forEachIndexed { index, step ->
@@ -90,36 +92,38 @@ class JobBuilderTest :
9092

9193
test("use 'if'") {
9294
// When
93-
val workflow =
94-
workflow(
95-
name = "test",
96-
on = listOf(Push()),
97-
sourceFile = sourceTempFile,
98-
) {
99-
job(id = "test", runsOn = RunnerType.UbuntuLatest, `if` = "b") {
100-
run(command = "ls")
101-
}
95+
var workflow: Workflow? = null
96+
workflow(
97+
name = "test",
98+
on = listOf(Push()),
99+
sourceFile = sourceTempFile,
100+
useWorkflow = { workflow = it },
101+
) {
102+
job(id = "test", runsOn = RunnerType.UbuntuLatest, `if` = "b") {
103+
run(command = "ls")
102104
}
105+
}
103106

104107
// Then
105-
workflow.jobs[0].condition shouldBe "b"
108+
workflow!!.jobs[0].condition shouldBe "b"
106109
}
107110

108111
test("use 'condition'") {
109112
// When
110-
val workflow =
111-
workflow(
112-
name = "test",
113-
on = listOf(Push()),
114-
sourceFile = sourceTempFile,
115-
) {
116-
job(id = "test", runsOn = RunnerType.UbuntuLatest, condition = "b") {
117-
run(command = "ls")
118-
}
113+
var workflow: Workflow? = null
114+
workflow(
115+
name = "test",
116+
on = listOf(Push()),
117+
sourceFile = sourceTempFile,
118+
useWorkflow = { workflow = it },
119+
) {
120+
job(id = "test", runsOn = RunnerType.UbuntuLatest, condition = "b") {
121+
run(command = "ls")
119122
}
123+
}
120124

121125
// Then
122-
workflow.jobs[0].condition shouldBe "b"
126+
workflow!!.jobs[0].condition shouldBe "b"
123127
}
124128
}
125129
})

0 commit comments

Comments
 (0)