Skip to content

Commit 30546e6

Browse files
authored
refactor(library): extract consistency job check creation to separate file (#1768)
Part of #1765.
1 parent dced05e commit 30546e6

File tree

2 files changed

+69
-49
lines changed

2 files changed

+69
-49
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package io.github.typesafegithub.workflows.yaml
2+
3+
import io.github.typesafegithub.workflows.domain.Job
4+
import io.github.typesafegithub.workflows.domain.JobOutputs
5+
import io.github.typesafegithub.workflows.domain.RunnerType.UbuntuLatest
6+
import io.github.typesafegithub.workflows.domain.actions.CustomAction
7+
import io.github.typesafegithub.workflows.dsl.WorkflowBuilder
8+
import io.github.typesafegithub.workflows.internal.relativeToAbsolute
9+
import java.nio.file.Path
10+
import kotlin.io.path.invariantSeparatorsPathString
11+
12+
internal fun WorkflowBuilder.consistencyCheckJob(
13+
sourceFilePath: String?,
14+
targetFileName: String?,
15+
gitRootDir: Path?,
16+
consistencyCheckJobConfig: ConsistencyCheckJobConfig.Configuration,
17+
): Job<JobOutputs.EMPTY> {
18+
check(gitRootDir != null && sourceFilePath != null) {
19+
"consistency check requires a valid sourceFile and Git root directory"
20+
}
21+
22+
val targetFilePath =
23+
gitRootDir
24+
.resolve(".github")
25+
.resolve("workflows")
26+
.resolve(targetFileName)
27+
.relativeToAbsolute(gitRootDir)
28+
.invariantSeparatorsPathString
29+
30+
return this.job(
31+
id = "check_yaml_consistency",
32+
name = "Check YAML consistency",
33+
runsOn = UbuntuLatest,
34+
condition = consistencyCheckJobConfig.condition,
35+
env = consistencyCheckJobConfig.env,
36+
) {
37+
uses(
38+
name = "Check out",
39+
// Since this action is used in a simple way, and we actually don't want to update the version
40+
// because it causes YAML regeneration, let's not use the type-safe binding here. It will also
41+
// let us avoid depending on a Maven-based action binding once bundled bindings are deprecated.
42+
action =
43+
CustomAction(
44+
actionOwner = "actions",
45+
actionName = "checkout",
46+
actionVersion = "v4",
47+
),
48+
)
49+
50+
consistencyCheckJobConfig.additionalSteps?.also { block ->
51+
block()
52+
}
53+
54+
run(
55+
name = "Execute script",
56+
command = "rm '$targetFilePath' && '$sourceFilePath'",
57+
)
58+
run(
59+
name = "Consistency check",
60+
command = "git diff --exit-code '$targetFilePath'",
61+
)
62+
}
63+
}

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

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import io.github.typesafegithub.workflows.domain.Job
44
import io.github.typesafegithub.workflows.domain.KotlinLogicStep
55
import io.github.typesafegithub.workflows.domain.Mode
66
import io.github.typesafegithub.workflows.domain.Permission
7-
import io.github.typesafegithub.workflows.domain.RunnerType.UbuntuLatest
87
import io.github.typesafegithub.workflows.domain.Workflow
9-
import io.github.typesafegithub.workflows.domain.actions.CustomAction
108
import io.github.typesafegithub.workflows.domain.contexts.Contexts
119
import io.github.typesafegithub.workflows.domain.contexts.GithubContext
1210
import io.github.typesafegithub.workflows.dsl.toBuilder
@@ -103,54 +101,13 @@ public fun Workflow.generateYaml(
103101

104102
val jobsWithConsistencyCheck =
105103
if (consistencyCheckJobConfig is ConsistencyCheckJobConfig.Configuration) {
106-
check(gitRootDir != null && sourceFile != null) {
107-
"consistency check requires a valid sourceFile and Git root directory"
108-
}
109-
110-
val targetFilePath =
111-
gitRootDir
112-
.resolve(".github")
113-
.resolve("workflows")
114-
.resolve(targetFileName)
115-
.relativeToAbsolute(gitRootDir)
116-
.invariantSeparatorsPathString
117-
118104
val consistencyCheckJob =
119-
this.toBuilder().job(
120-
id = "check_yaml_consistency",
121-
name = "Check YAML consistency",
122-
runsOn = UbuntuLatest,
123-
condition = consistencyCheckJobConfig.condition,
124-
env = consistencyCheckJobConfig.env,
125-
) {
126-
uses(
127-
name = "Check out",
128-
// Since this action is used in a simple way, and we actually don't want to update the version
129-
// because it causes YAML regeneration, let's not use the type-safe binding here. It will also
130-
// let us avoid depending on a Maven-based action binding once bundled bindings are deprecated.
131-
action =
132-
CustomAction(
133-
actionOwner = "actions",
134-
actionName = "checkout",
135-
actionVersion = "v4",
136-
),
137-
)
138-
139-
consistencyCheckJobConfig.additionalSteps?.also { block ->
140-
block()
141-
}
142-
143-
run(
144-
name = "Execute script",
145-
command =
146-
"rm '$targetFilePath' " +
147-
"&& '$sourceFilePath'",
148-
)
149-
run(
150-
name = "Consistency check",
151-
command = "git diff --exit-code '$targetFilePath'",
152-
)
153-
}
105+
this.toBuilder().consistencyCheckJob(
106+
sourceFilePath = sourceFilePath,
107+
targetFileName = targetFileName,
108+
gitRootDir = gitRootDir,
109+
consistencyCheckJobConfig = consistencyCheckJobConfig,
110+
)
154111
listOf(consistencyCheckJob) +
155112
jobs.map {
156113
it.copy(needs = it.needs + consistencyCheckJob)

0 commit comments

Comments
 (0)