Skip to content

Commit a7694a5

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

File tree

12 files changed

+376
-26
lines changed

12 files changed

+376
-26
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: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import com.squareup.kotlinpoet.asTypeName
1717
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
1818
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.MetadataRevision
1919
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.TypingActualSource
20+
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.isTopLevel
21+
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.repoName
22+
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.subName
2023
import io.github.typesafegithub.workflows.actionbindinggenerator.generation.Properties.CUSTOM_INPUTS
2124
import io.github.typesafegithub.workflows.actionbindinggenerator.generation.Properties.CUSTOM_VERSION
2225
import io.github.typesafegithub.workflows.actionbindinggenerator.metadata.Input
@@ -417,10 +420,20 @@ private fun TypeSpec.Builder.inheritsFromRegularAction(
417420
return this
418421
.superclass(superclass)
419422
.addSuperclassConstructorParameter("%S", coords.owner)
420-
.addSuperclassConstructorParameter("%S", coords.name)
421-
.addSuperclassConstructorParameter("_customVersion ?: %S", coords.version)
423+
.addSuperclassConstructorParameter("%S", coords.name.substringBefore("___").replace("__", "/"))
424+
.addSuperclassConstructorParameter(
425+
"_customVersion ?: %S",
426+
when {
427+
coords.name.endsWith("___major") -> coords.version.majorVersion
428+
coords.name.endsWith("___minor") -> coords.version.minorVersion
429+
else -> coords.version
430+
},
431+
)
422432
}
423433

434+
private val String.majorVersion get() = substringBefore('.')
435+
private val String.minorVersion get() = split('.', limit = 3).take(2).joinToString(".")
436+
424437
private fun Metadata.primaryConstructor(
425438
inputTypings: Map<String, Typing>,
426439
coords: ActionCoords,
@@ -622,9 +635,7 @@ private fun actionKdoc(
622635
|
623636
|${metadata.description.escapedForComments.removeTrailingWhitespacesForEachLine()}
624637
|
625-
|[Action on GitHub](https://github.com/${coords.owner}/${coords.name.substringBefore(
626-
'/',
627-
)}${if ("/" in coords.name) "/tree/${coords.version}/${coords.name.substringAfter('/')}" else ""})
638+
|[Action on GitHub](https://github.com/${coords.owner}/${coords.repoName}${if (!coords.isTopLevel) "/tree/${coords.version}${coords.subName}" else ""})
628639
""".trimMargin()
629640

630641
private fun Map<String, Typing>?.getInputTyping(key: String) = this?.get(key) ?: StringTyping

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCo
55
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.CommitHash
66
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.MetadataRevision
77
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.NewestForVersion
8+
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.repoName
9+
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.subName
810
import kotlinx.serialization.Serializable
911
import kotlinx.serialization.decodeFromString
1012
import java.io.IOException
@@ -35,17 +37,9 @@ public data class Output(
3537
val description: String = "",
3638
)
3739

38-
private fun ActionCoords.actionYmlUrl(gitRef: String) =
39-
"https://raw.githubusercontent.com/$owner/${name.substringBefore(
40-
'/',
41-
)}/$gitRef/${if ("/" in name) "${name.substringAfter('/')}/" else ""}action.yml"
40+
private fun ActionCoords.actionYmlUrl(gitRef: String) = "https://raw.githubusercontent.com/$owner/$repoName/$gitRef$subName/action.yml"
4241

43-
private fun ActionCoords.actionYamlUrl(gitRef: String) =
44-
"https://raw.githubusercontent.com/$owner/${name.substringBefore(
45-
'/',
46-
)}/$gitRef/${if ("/" in name) "${name.substringAfter('/')}/" else ""}action.yaml"
47-
48-
internal val ActionCoords.gitHubUrl: String get() = "https://github.com/$owner/$name"
42+
private fun ActionCoords.actionYamlUrl(gitRef: String) = "https://raw.githubusercontent.com/$owner/$repoName/$gitRef$subName/action.yaml"
4943

5044
public fun ActionCoords.fetchMetadata(
5145
metadataRevision: MetadataRevision,
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)