Skip to content

Commit 12685da

Browse files
committed
feat(abg): allow usage of version ranges by generating only major or minor version even with more detailed version
1 parent c1dc24a commit 12685da

File tree

12 files changed

+368
-15
lines changed

12 files changed

+368
-15
lines changed

action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ public data class ActionCoords(
1010
* A top-level action is an action with its `action.y(a)ml` file in the repository root, as opposed to actions stored
1111
* in subdirectories.
1212
*/
13-
public val ActionCoords.isTopLevel: Boolean get() = "__" !in name
13+
public val ActionCoords.isTopLevel: Boolean get() = "__" !in name.substringBefore("___")
1414

15-
public val ActionCoords.prettyPrint: String get() = "$owner/${
16-
name.replace("__", "/")
17-
}@$version"
15+
public val ActionCoords.prettyPrint: String
16+
get() = "$owner/${
17+
name.replace("___", " with ").replace("__", "/")
18+
}@$version"
1819

1920
/**
2021
* For most actions, it's the same as [ActionCoords.name].
@@ -28,8 +29,9 @@ public val ActionCoords.repoName: String get() =
2829
* For actions that aren't executed from the root of the repo, it returns the path relative to the repo root where the
2930
* action lives.
3031
*/
31-
public val ActionCoords.subName: String get() =
32-
if (isTopLevel) "" else "/${name.substringAfter("__").replace("__", "/")}"
32+
public val ActionCoords.subName: String
33+
get() =
34+
if (isTopLevel) "" else "/${name.substringAfter("__").substringBefore("___").replace("__", "/")}"
3335

3436
internal fun String.toActionCoords(): ActionCoords {
3537
val (ownerAndName, version) = this.split('@')

action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/generation/ClassNaming.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ package io.github.typesafegithub.workflows.actionbindinggenerator.generation
33
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
44
import io.github.typesafegithub.workflows.actionbindinggenerator.utils.toPascalCase
55

6-
internal fun ActionCoords.buildActionClassName(): String = this.name.toPascalCase()
6+
internal fun ActionCoords.buildActionClassName(): String = this.name.substringBefore("___").toPascalCase()

action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/generation/Generation.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,20 @@ private fun TypeSpec.Builder.inheritsFromRegularAction(
414414
return this
415415
.superclass(superclass)
416416
.addSuperclassConstructorParameter("%S", coords.owner)
417-
.addSuperclassConstructorParameter("%S", coords.name)
418-
.addSuperclassConstructorParameter("_customVersion ?: %S", coords.version)
417+
.addSuperclassConstructorParameter("%S", coords.name.substringBefore("___").replace("__", "/"))
418+
.addSuperclassConstructorParameter(
419+
"_customVersion ?: %S",
420+
when {
421+
coords.name.endsWith("___major") -> coords.version.majorVersion
422+
coords.name.endsWith("___minor") -> coords.version.minorVersion
423+
else -> coords.version
424+
},
425+
)
419426
}
420427

428+
private val String.majorVersion get() = substringBefore('.')
429+
private val String.minorVersion get() = split('.', limit = 3).take(2).joinToString(".")
430+
421431
private fun Metadata.primaryConstructor(
422432
inputTypings: Map<String, Typing>,
423433
coords: ActionCoords,

action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/metadata/MetadataReading.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ private fun ActionCoords.actionYmlUrl(gitRef: String) = "https://raw.githubuserc
4141

4242
private fun ActionCoords.actionYamlUrl(gitRef: String) = "https://raw.githubusercontent.com/$owner/$repoName/$gitRef$subName/action.yaml"
4343

44-
internal val ActionCoords.gitHubUrl: String get() = "https://github.com/$owner/$name"
45-
4644
public fun ActionCoords.fetchMetadata(
4745
metadataRevision: MetadataRevision,
4846
fetchUri: (URI) -> String = ::fetchUri,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// This file was generated using action-binding-generator. Don't change it by hand, otherwise your
2+
// changes will be overwritten with the next binding code regeneration.
3+
// See https://github.com/typesafegithub/github-workflows-kt for more info.
4+
@file:Suppress(
5+
"DataClassPrivateConstructor",
6+
"UNUSED_PARAMETER",
7+
)
8+
9+
package io.github.typesafegithub.workflows.actions.johnsmith
10+
11+
import io.github.typesafegithub.workflows.domain.actions.Action
12+
import io.github.typesafegithub.workflows.domain.actions.RegularAction
13+
import java.util.LinkedHashMap
14+
import kotlin.ExposedCopyVisibility
15+
import kotlin.String
16+
import kotlin.Suppress
17+
import kotlin.Unit
18+
import kotlin.collections.Map
19+
20+
/**
21+
* Action: Action With No Inputs
22+
*
23+
* Description
24+
*
25+
* [Action on GitHub](https://github.com/john-smith/action-with-no-inputs-with-major-version)
26+
*
27+
* @param _customInputs Type-unsafe map where you can put any inputs that are not yet supported by
28+
* the binding
29+
* @param _customVersion Allows overriding action's version, for example to use a specific minor
30+
* version, or a newer version that the binding doesn't yet know about
31+
*/
32+
@ExposedCopyVisibility
33+
public data class ActionWithNoInputsWithMajorVersion private constructor(
34+
/**
35+
* Type-unsafe map where you can put any inputs that are not yet supported by the binding
36+
*/
37+
public val _customInputs: Map<String, String> = mapOf(),
38+
/**
39+
* Allows overriding action's version, for example to use a specific minor version, or a newer
40+
* version that the binding doesn't yet know about
41+
*/
42+
public val _customVersion: String? = null,
43+
) : RegularAction<Action.Outputs>("john-smith", "action-with-no-inputs-with-major-version",
44+
_customVersion ?: "v3") {
45+
public constructor(
46+
vararg pleaseUseNamedArguments: Unit,
47+
_customInputs: Map<String, String> = mapOf(),
48+
_customVersion: String? = null,
49+
) : this(_customInputs = _customInputs, _customVersion = _customVersion)
50+
51+
@Suppress("SpreadOperator")
52+
override fun toYamlArguments(): LinkedHashMap<String, String> = LinkedHashMap(_customInputs)
53+
54+
override fun buildOutputObject(stepId: String): Action.Outputs = Outputs(stepId)
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// This file was generated using action-binding-generator. Don't change it by hand, otherwise your
2+
// changes will be overwritten with the next binding code regeneration.
3+
// See https://github.com/typesafegithub/github-workflows-kt for more info.
4+
@file:Suppress(
5+
"DataClassPrivateConstructor",
6+
"UNUSED_PARAMETER",
7+
)
8+
9+
package io.github.typesafegithub.workflows.actions.johnsmith
10+
11+
import io.github.typesafegithub.workflows.domain.actions.Action
12+
import io.github.typesafegithub.workflows.domain.actions.RegularAction
13+
import java.util.LinkedHashMap
14+
import kotlin.Deprecated
15+
import kotlin.ExposedCopyVisibility
16+
import kotlin.String
17+
import kotlin.Suppress
18+
import kotlin.Unit
19+
import kotlin.collections.Map
20+
21+
/**
22+
* ```text
23+
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
24+
* !!! WARNING !!!
25+
* !!! !!!
26+
* !!! This action binding has no typings provided. All inputs will !!!
27+
* !!! have a default type of String. !!!
28+
* !!! To be able to use this action in a type-safe way, ask the !!!
29+
* !!! action's owner to provide the typings using !!!
30+
* !!! !!!
31+
* !!! https://github.com/typesafegithub/github-actions-typing !!!
32+
* !!! !!!
33+
* !!! or if it's impossible, contribute typings to a community-driven !!!
34+
* !!! !!!
35+
* !!! https://github.com/typesafegithub/github-actions-typing-catalog !!!
36+
* !!! !!!
37+
* !!! This '_Untyped' binding will be available even once the typings !!!
38+
* !!! are added. !!!
39+
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
40+
* ```
41+
*
42+
* Action: Action With No Inputs
43+
*
44+
* Description
45+
*
46+
* [Action on GitHub](https://github.com/john-smith/action-with-no-inputs-with-major-version)
47+
*
48+
* @param _customInputs Type-unsafe map where you can put any inputs that are not yet supported by
49+
* the binding
50+
* @param _customVersion Allows overriding action's version, for example to use a specific minor
51+
* version, or a newer version that the binding doesn't yet know about
52+
*/
53+
@Deprecated(
54+
"Use the typed class instead",
55+
ReplaceWith("ActionWithNoInputsWithMajorVersion"),
56+
)
57+
@ExposedCopyVisibility
58+
public data class ActionWithNoInputsWithMajorVersion_Untyped private constructor(
59+
/**
60+
* Type-unsafe map where you can put any inputs that are not yet supported by the binding
61+
*/
62+
public val _customInputs: Map<String, String> = mapOf(),
63+
/**
64+
* Allows overriding action's version, for example to use a specific minor version, or a newer
65+
* version that the binding doesn't yet know about
66+
*/
67+
public val _customVersion: String? = null,
68+
) : RegularAction<Action.Outputs>("john-smith", "action-with-no-inputs-with-major-version",
69+
_customVersion ?: "v3") {
70+
public constructor(
71+
vararg pleaseUseNamedArguments: Unit,
72+
_customInputs: Map<String, String> = mapOf(),
73+
_customVersion: String? = null,
74+
) : this(_customInputs = _customInputs, _customVersion = _customVersion)
75+
76+
@Suppress("SpreadOperator")
77+
override fun toYamlArguments(): LinkedHashMap<String, String> = LinkedHashMap(_customInputs)
78+
79+
override fun buildOutputObject(stepId: String): Action.Outputs = Outputs(stepId)
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// This file was generated using action-binding-generator. Don't change it by hand, otherwise your
2+
// changes will be overwritten with the next binding code regeneration.
3+
// See https://github.com/typesafegithub/github-workflows-kt for more info.
4+
@file:Suppress(
5+
"DataClassPrivateConstructor",
6+
"UNUSED_PARAMETER",
7+
)
8+
9+
package io.github.typesafegithub.workflows.actions.johnsmith
10+
11+
import io.github.typesafegithub.workflows.domain.actions.Action
12+
import io.github.typesafegithub.workflows.domain.actions.RegularAction
13+
import java.util.LinkedHashMap
14+
import kotlin.ExposedCopyVisibility
15+
import kotlin.String
16+
import kotlin.Suppress
17+
import kotlin.Unit
18+
import kotlin.collections.Map
19+
20+
/**
21+
* Action: Action With No Inputs
22+
*
23+
* Description
24+
*
25+
* [Action on GitHub](https://github.com/john-smith/action-with-no-inputs-with-minor-version)
26+
*
27+
* @param _customInputs Type-unsafe map where you can put any inputs that are not yet supported by
28+
* the binding
29+
* @param _customVersion Allows overriding action's version, for example to use a specific minor
30+
* version, or a newer version that the binding doesn't yet know about
31+
*/
32+
@ExposedCopyVisibility
33+
public data class ActionWithNoInputsWithMinorVersion private constructor(
34+
/**
35+
* Type-unsafe map where you can put any inputs that are not yet supported by the binding
36+
*/
37+
public val _customInputs: Map<String, String> = mapOf(),
38+
/**
39+
* Allows overriding action's version, for example to use a specific minor version, or a newer
40+
* version that the binding doesn't yet know about
41+
*/
42+
public val _customVersion: String? = null,
43+
) : RegularAction<Action.Outputs>("john-smith", "action-with-no-inputs-with-minor-version",
44+
_customVersion ?: "v3.1") {
45+
public constructor(
46+
vararg pleaseUseNamedArguments: Unit,
47+
_customInputs: Map<String, String> = mapOf(),
48+
_customVersion: String? = null,
49+
) : this(_customInputs = _customInputs, _customVersion = _customVersion)
50+
51+
@Suppress("SpreadOperator")
52+
override fun toYamlArguments(): LinkedHashMap<String, String> = LinkedHashMap(_customInputs)
53+
54+
override fun buildOutputObject(stepId: String): Action.Outputs = Outputs(stepId)
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// This file was generated using action-binding-generator. Don't change it by hand, otherwise your
2+
// changes will be overwritten with the next binding code regeneration.
3+
// See https://github.com/typesafegithub/github-workflows-kt for more info.
4+
@file:Suppress(
5+
"DataClassPrivateConstructor",
6+
"UNUSED_PARAMETER",
7+
)
8+
9+
package io.github.typesafegithub.workflows.actions.johnsmith
10+
11+
import io.github.typesafegithub.workflows.domain.actions.Action
12+
import io.github.typesafegithub.workflows.domain.actions.RegularAction
13+
import java.util.LinkedHashMap
14+
import kotlin.Deprecated
15+
import kotlin.ExposedCopyVisibility
16+
import kotlin.String
17+
import kotlin.Suppress
18+
import kotlin.Unit
19+
import kotlin.collections.Map
20+
21+
/**
22+
* ```text
23+
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
24+
* !!! WARNING !!!
25+
* !!! !!!
26+
* !!! This action binding has no typings provided. All inputs will !!!
27+
* !!! have a default type of String. !!!
28+
* !!! To be able to use this action in a type-safe way, ask the !!!
29+
* !!! action's owner to provide the typings using !!!
30+
* !!! !!!
31+
* !!! https://github.com/typesafegithub/github-actions-typing !!!
32+
* !!! !!!
33+
* !!! or if it's impossible, contribute typings to a community-driven !!!
34+
* !!! !!!
35+
* !!! https://github.com/typesafegithub/github-actions-typing-catalog !!!
36+
* !!! !!!
37+
* !!! This '_Untyped' binding will be available even once the typings !!!
38+
* !!! are added. !!!
39+
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
40+
* ```
41+
*
42+
* Action: Action With No Inputs
43+
*
44+
* Description
45+
*
46+
* [Action on GitHub](https://github.com/john-smith/action-with-no-inputs-with-minor-version)
47+
*
48+
* @param _customInputs Type-unsafe map where you can put any inputs that are not yet supported by
49+
* the binding
50+
* @param _customVersion Allows overriding action's version, for example to use a specific minor
51+
* version, or a newer version that the binding doesn't yet know about
52+
*/
53+
@Deprecated(
54+
"Use the typed class instead",
55+
ReplaceWith("ActionWithNoInputsWithMinorVersion"),
56+
)
57+
@ExposedCopyVisibility
58+
public data class ActionWithNoInputsWithMinorVersion_Untyped private constructor(
59+
/**
60+
* Type-unsafe map where you can put any inputs that are not yet supported by the binding
61+
*/
62+
public val _customInputs: Map<String, String> = mapOf(),
63+
/**
64+
* Allows overriding action's version, for example to use a specific minor version, or a newer
65+
* version that the binding doesn't yet know about
66+
*/
67+
public val _customVersion: String? = null,
68+
) : RegularAction<Action.Outputs>("john-smith", "action-with-no-inputs-with-minor-version",
69+
_customVersion ?: "v3.1") {
70+
public constructor(
71+
vararg pleaseUseNamedArguments: Unit,
72+
_customInputs: Map<String, String> = mapOf(),
73+
_customVersion: String? = null,
74+
) : this(_customInputs = _customInputs, _customVersion = _customVersion)
75+
76+
@Suppress("SpreadOperator")
77+
override fun toYamlArguments(): LinkedHashMap<String, String> = LinkedHashMap(_customInputs)
78+
79+
override fun buildOutputObject(stepId: String): Action.Outputs = Outputs(stepId)
80+
}

action-binding-generator/src/test/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/generation/ClassNamingTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class ClassNamingTest :
1111
ActionCoords("irrelevant", "some-action-name", "v2") to "SomeActionName",
1212
ActionCoords("irrelevant", "some-action-name__subaction", "v2") to "SomeActionNameSubaction",
1313
ActionCoords("irrelevant", "some-action-name__foo__bar__baz", "v2") to "SomeActionNameFooBarBaz",
14+
ActionCoords("irrelevant", "some-action-name__foo__bar__baz___major", "v2") to "SomeActionNameFooBarBaz",
1415
).forEach { (input, output) ->
1516
test("should get '$input' and produce '$output'") {
1617
input.buildActionClassName() shouldBe output

0 commit comments

Comments
 (0)