Skip to content

Commit 0d125b3

Browse files
authored
feat(dsl): accept maps and sets instead of linked versions (#1428)
It turned out that iterating over a map/set returned by a common map builder `mapOf`/`setOf` corresponds to the order given to this function. It wasn't obvious since the builder returns a regular `Map`/`Set` where the order is not specified. Thanks to this observation, we can simplify the API and not require `linkedMapOf`/`linkedSetOf` which is more verbose. Action bindings still operate on `LinkedHashMap` because making this change would break the Maven-based bindings server. Leaving this type there is not that painful, it's only visible if someone decides to create a local action binding. Otherwise, the binding code is auto-generated.
1 parent 0cbcdb1 commit 0d125b3

File tree

17 files changed

+109
-109
lines changed

17 files changed

+109
-109
lines changed

.github/workflows/end-to-end-tests.main.kts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ workflow(
4949
Push(branches = listOf("main")),
5050
PullRequest(),
5151
),
52-
yamlConsistencyJobEnv = linkedMapOf(
52+
yamlConsistencyJobEnv = mapOf(
5353
"GITHUB_TOKEN" to expr("secrets.GITHUB_TOKEN")
5454
),
5555
yamlConsistencyJobAdditionalSteps = {
@@ -67,7 +67,7 @@ workflow(
6767
job(
6868
id = "test_job_1",
6969
runsOn = RunnerType.UbuntuLatest,
70-
env = linkedMapOf(
70+
env = mapOf(
7171
GREETING to "World",
7272
),
7373
permissions = mapOf(
@@ -180,14 +180,14 @@ workflow(
180180

181181
run(
182182
name = "Custom environment variable",
183-
env = linkedMapOf(
183+
env = mapOf(
184184
FIRST_NAME to "Patrick",
185185
),
186186
command = "echo $GREETING $FIRST_NAME",
187187
)
188188
run(
189189
name = "Encrypted secret",
190-
env = linkedMapOf(
190+
env = mapOf(
191191
SECRET to expr { SUPER_SECRET },
192192
TOKEN to expr { secrets.GITHUB_TOKEN },
193193
),

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

Lines changed: 66 additions & 66 deletions
Large diffs are not rendered by default.

github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/domain/Container.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import kotlinx.serialization.Contextual
1111
public data class Container(
1212
val image: String,
1313
val credentials: Credentials? = null,
14-
val env: LinkedHashMap<String, String> = linkedMapOf(),
14+
val env: Map<String, String> = mapOf(),
1515
val ports: List<PortMapping> = emptyList(),
1616
val volumes: List<VolumeMapping> = emptyList(),
1717
val options: List<String> = emptyList(),

github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/domain/Job.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public data class Job<OUTPUT : JobOutputs>(
1111
val steps: List<Step>,
1212
val needs: List<Job<*>> = emptyList(),
1313
val outputs: OUTPUT,
14-
val env: LinkedHashMap<String, String> = linkedMapOf(),
14+
val env: Map<String, String> = mapOf(),
1515
val condition: String? = null,
1616
val strategyMatrix: Map<String, List<String>>? = null,
1717
val permissions: Map<Permission, Mode>? = null,

github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/domain/RunnerType.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public sealed interface RunnerType {
88
// Custom runner. Could be an expression `runsOn = expr("github.event.inputs.run-on")`
99
public data class Custom(val runsOn: String) : RunnerType
1010

11-
public data class Labelled(val labels: LinkedHashSet<String>) : RunnerType {
11+
public data class Labelled(val labels: Set<String>) : RunnerType {
1212
public constructor(vararg labels: String) : this(LinkedHashSet(labels.toList()))
1313

1414
init {
@@ -20,7 +20,7 @@ public sealed interface RunnerType {
2020
* @see <a href="https://docs.github.com/en/actions/using-jobs/choosing-the-runner-for-a-job#choosing-runners-in-a-group">Choosing runners in a group</a>
2121
*/
2222
@Suppress("MaxLineLength")
23-
public data class Group(val name: String, val labels: LinkedHashSet<String>? = null) : RunnerType {
23+
public data class Group(val name: String, val labels: Set<String>? = null) : RunnerType {
2424
public constructor(name: String, vararg labels: String) : this(name, LinkedHashSet(labels.toList()))
2525
}
2626

github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/domain/Step.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import kotlinx.serialization.Contextual
88

99
public sealed class Step(
1010
public open val id: String,
11-
public open val env: LinkedHashMap<String, String> = linkedMapOf(),
11+
public open val env: Map<String, String> = mapOf(),
1212
public open val condition: String? = null,
1313
public open val continueOnError: Boolean? = null,
1414
public open val timeoutMinutes: Int? = null,
@@ -30,7 +30,7 @@ public data class CommandStep(
3030
override val id: String,
3131
val name: String? = null,
3232
val command: String,
33-
override val env: LinkedHashMap<String, String> = linkedMapOf(),
33+
override val env: Map<String, String> = mapOf(),
3434
override val condition: String? = null,
3535
override val continueOnError: Boolean? = null,
3636
override val timeoutMinutes: Int? = null,
@@ -50,7 +50,7 @@ public data class KotlinLogicStep(
5050
override val id: String,
5151
val name: String? = null,
5252
val command: String,
53-
override val env: LinkedHashMap<String, String> = linkedMapOf(),
53+
override val env: Map<String, String> = mapOf(),
5454
override val condition: String? = null,
5555
override val continueOnError: Boolean? = null,
5656
override val timeoutMinutes: Int? = null,
@@ -72,7 +72,7 @@ public open class ActionStep<out OUTPUTS : Outputs>(
7272
override val id: String,
7373
public open val name: String? = null,
7474
public open val action: Action<OUTPUTS>,
75-
override val env: LinkedHashMap<String, String> = linkedMapOf(),
75+
override val env: Map<String, String> = mapOf(),
7676
override val condition: String? = null,
7777
override val continueOnError: Boolean? = null,
7878
override val timeoutMinutes: Int? = null,

github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/domain/Workflow.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import java.nio.file.Path
99
public data class Workflow(
1010
val name: String,
1111
val on: List<Trigger>,
12-
val env: LinkedHashMap<String, String>,
12+
val env: Map<String, String>,
1313
val sourceFile: Path?,
1414
val targetFileName: String?,
1515
val concurrency: Concurrency? = null,
1616
val permissions: Map<Permission, Mode>? = null,
1717
val yamlConsistencyJobCondition: String? = null,
18-
val yamlConsistencyJobEnv: LinkedHashMap<String, String> = linkedMapOf(),
18+
val yamlConsistencyJobEnv: Map<String, String> = mapOf(),
1919
val yamlConsistencyJobAdditionalSteps: (JobBuilder<JobOutputs.EMPTY>.() -> Unit)? = null,
2020
val jobs: List<Job<*>>,
2121
override val _customArguments: Map<String, @Contextual Any?> = mapOf(),

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class JobBuilder<OUTPUT : JobOutputs>(
2626
public val name: String?,
2727
public val runsOn: RunnerType,
2828
public val needs: List<Job<*>>,
29-
public val env: LinkedHashMap<String, String>,
29+
public val env: Map<String, String>,
3030
public val condition: String?,
3131
public val strategyMatrix: Map<String, List<String>>?,
3232
public val permissions: Map<Permission, Mode>? = null,
@@ -64,7 +64,7 @@ public class JobBuilder<OUTPUT : JobOutputs>(
6464
vararg pleaseUseNamedArguments: Unit,
6565
command: String,
6666
name: String? = null,
67-
env: LinkedHashMap<String, String> = linkedMapOf(),
67+
env: Map<String, String> = mapOf(),
6868
@SuppressWarnings("FunctionParameterNaming")
6969
`if`: String? = null,
7070
condition: String? = null,
@@ -101,7 +101,7 @@ public class JobBuilder<OUTPUT : JobOutputs>(
101101
@Suppress("UNUSED_PARAMETER")
102102
vararg pleaseUseNamedArguments: Unit,
103103
name: String? = null,
104-
env: LinkedHashMap<String, String> = linkedMapOf(),
104+
env: Map<String, String> = mapOf(),
105105
@SuppressWarnings("FunctionParameterNaming")
106106
`if`: String? = null,
107107
condition: String? = null,
@@ -142,7 +142,7 @@ public class JobBuilder<OUTPUT : JobOutputs>(
142142
// simplified implementation is used.
143143
command = "GHWKT_RUN_STEP='${this.id}:$id' '.github/workflows/${sourceFile.name}'",
144144
logic = logic,
145-
env = LinkedHashMap(env.toMap() + mapOf("GHWKT_GITHUB_CONTEXT_JSON" to "${'$'}{{ toJSON(github) }}")),
145+
env = env + mapOf("GHWKT_GITHUB_CONTEXT_JSON" to "${'$'}{{ toJSON(github) }}"),
146146
condition =
147147
conditionCheckingStep?.let {
148148
expr { "steps.${conditionCheckingStep.id}.outputs.$evaluationResultOutput" }
@@ -162,7 +162,7 @@ public class JobBuilder<OUTPUT : JobOutputs>(
162162
vararg pleaseUseNamedArguments: Unit,
163163
action: Action<T>,
164164
name: String? = null,
165-
env: LinkedHashMap<String, String> = linkedMapOf(),
165+
env: Map<String, String> = mapOf(),
166166
@SuppressWarnings("FunctionParameterNaming")
167167
`if`: String? = null,
168168
condition: String? = null,

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ import java.nio.file.Path
1818
public class WorkflowBuilder(
1919
name: String,
2020
on: List<Trigger>,
21-
env: LinkedHashMap<String, String> = linkedMapOf(),
21+
env: Map<String, String> = mapOf(),
2222
sourceFile: Path?,
2323
targetFileName: String?,
2424
concurrency: Concurrency? = null,
2525
yamlConsistencyJobCondition: String? = null,
26-
yamlConsistencyJobEnv: LinkedHashMap<String, String> = linkedMapOf(),
26+
yamlConsistencyJobEnv: Map<String, String> = mapOf(),
2727
yamlConsistencyJobAdditionalSteps: (JobBuilder<JobOutputs.EMPTY>.() -> Unit)? = null,
2828
jobs: List<Job<*>> = emptyList(),
2929
permissions: Map<Permission, Mode>? = null,
@@ -55,7 +55,7 @@ public class WorkflowBuilder(
5555
needs: List<Job<*>> = emptyList(),
5656
`if`: String? = null,
5757
condition: String? = null,
58-
env: LinkedHashMap<String, String> = linkedMapOf(),
58+
env: Map<String, String> = mapOf(),
5959
strategyMatrix: Map<String, List<String>>? = null,
6060
permissions: Map<Permission, Mode>? = null,
6161
_customArguments: Map<String, @Contextual Any?> = mapOf(),
@@ -109,7 +109,7 @@ public class WorkflowBuilder(
109109
needs: List<Job<*>> = emptyList(),
110110
`if`: String? = null,
111111
condition: String? = null,
112-
env: LinkedHashMap<String, String> = linkedMapOf(),
112+
env: Map<String, String> = mapOf(),
113113
strategyMatrix: Map<String, List<String>>? = null,
114114
permissions: Map<Permission, Mode>? = null,
115115
_customArguments: Map<String, @Contextual Any?> = mapOf(),
@@ -163,12 +163,12 @@ public fun workflow(
163163
vararg pleaseUseNamedArguments: Unit,
164164
name: String,
165165
on: List<Trigger>,
166-
env: LinkedHashMap<String, String> = linkedMapOf(),
166+
env: Map<String, String> = mapOf(),
167167
sourceFile: Path? = null,
168168
targetFileName: String? = sourceFile?.fileName?.let { it.toString().substringBeforeLast(".main.kts") + ".yaml" },
169169
concurrency: Concurrency? = null,
170170
yamlConsistencyJobCondition: String? = null,
171-
yamlConsistencyJobEnv: LinkedHashMap<String, String> = linkedMapOf(),
171+
yamlConsistencyJobEnv: Map<String, String> = mapOf(),
172172
yamlConsistencyJobAdditionalSteps: (JobBuilder<JobOutputs.EMPTY>.() -> Unit)? = null,
173173
permissions: Map<Permission, Mode>? = null,
174174
_customArguments: Map<String, @Contextual Any> = mapOf(),

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class IntegrationTest : FunSpec({
118118
name = "Test workflow",
119119
on = listOf(Push()),
120120
sourceFile = sourceTempFile.toPath(),
121-
yamlConsistencyJobEnv = linkedMapOf("GITHUB_TOKEN" to expr("secrets.GITHUB_TOKEN")),
121+
yamlConsistencyJobEnv = mapOf("GITHUB_TOKEN" to expr("secrets.GITHUB_TOKEN")),
122122
) {
123123
job(
124124
id = "test_job",
@@ -187,7 +187,7 @@ class IntegrationTest : FunSpec({
187187
name = "Test workflow",
188188
on = listOf(Push()),
189189
env =
190-
linkedMapOf(
190+
mapOf(
191191
"SIMPLE_VAR" to "simple-value-workflow",
192192
"MULTILINE_VAR" to
193193
"""
@@ -208,7 +208,7 @@ class IntegrationTest : FunSpec({
208208
runsOn = RunnerType.UbuntuLatest,
209209
condition = "\${{ always() }}",
210210
env =
211-
linkedMapOf(
211+
mapOf(
212212
"SIMPLE_VAR" to "simple-value-job",
213213
"MULTILINE_VAR" to
214214
"""
@@ -229,7 +229,7 @@ class IntegrationTest : FunSpec({
229229
name = "Check out",
230230
action = CheckoutV4(),
231231
env =
232-
linkedMapOf(
232+
mapOf(
233233
"SIMPLE_VAR" to "simple-value-uses",
234234
"MULTILINE_VAR" to
235235
"""
@@ -245,7 +245,7 @@ class IntegrationTest : FunSpec({
245245
name = "Hello world!",
246246
command = "echo 'hello!'",
247247
env =
248-
linkedMapOf(
248+
mapOf(
249249
"SIMPLE_VAR" to "simple-value-run",
250250
"MULTILINE_VAR" to
251251
"""

0 commit comments

Comments
 (0)