Skip to content

Commit ebe6791

Browse files
authored
feat(abg): support generating action bindings with comment in usage site (#2187)
Part of #1691. Thanks to this, the user can request an action binding that, when used in a workflow, will have a requested comment next to the place in the YAML where the action is used.
1 parent e228b0a commit ebe6791

5 files changed

Lines changed: 122 additions & 5 deletions

File tree

action-binding-generator/api/action-binding-generator.api

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
public final class io/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords {
2-
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/SignificantVersion;Ljava/lang/String;)V
3-
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/SignificantVersion;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
2+
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/SignificantVersion;Ljava/lang/String;Ljava/lang/String;)V
3+
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/SignificantVersion;Ljava/lang/String;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
44
public final fun component1 ()Ljava/lang/String;
55
public final fun component2 ()Ljava/lang/String;
66
public final fun component3 ()Ljava/lang/String;
77
public final fun component4 ()Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/SignificantVersion;
88
public final fun component5 ()Ljava/lang/String;
9-
public final fun copy (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/SignificantVersion;Ljava/lang/String;)Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords;
10-
public static synthetic fun copy$default (Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/SignificantVersion;Ljava/lang/String;ILjava/lang/Object;)Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords;
9+
public final fun component6 ()Ljava/lang/String;
10+
public final fun copy (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/SignificantVersion;Ljava/lang/String;Ljava/lang/String;)Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords;
11+
public static synthetic fun copy$default (Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/SignificantVersion;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords;
1112
public fun equals (Ljava/lang/Object;)Z
13+
public final fun getComment ()Ljava/lang/String;
1214
public final fun getName ()Ljava/lang/String;
1315
public final fun getOwner ()Ljava/lang/String;
1416
public final fun getPath ()Ljava/lang/String;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public data class ActionCoords(
1515
*/
1616
val significantVersion: SignificantVersion = FULL,
1717
val path: String? = null,
18+
val comment: String? = null,
1819
)
1920

2021
/**

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,11 @@ private fun TypeSpec.Builder.inheritsFromRegularAction(
467467
MINOR -> coords.version.minorVersion
468468
FULL -> coords.version
469469
},
470-
)
470+
).also {
471+
if (coords.comment != null) {
472+
addSuperclassConstructorParameter("%S", coords.comment)
473+
}
474+
}
471475
}
472476

473477
private val String.majorVersion get() = substringBefore('.')
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.ExposedCopyVisibility
15+
import kotlin.String
16+
import kotlin.Suppress
17+
import kotlin.Unit
18+
import kotlin.collections.Map
19+
import kotlin.collections.toList
20+
import kotlin.collections.toTypedArray
21+
22+
/**
23+
* Action: Action with comment
24+
*
25+
* Do something cool
26+
*
27+
* [Action on GitHub](https://github.com/john-smith/action-with-comment)
28+
*
29+
* @param foo &lt;required&gt; Short description
30+
* @param foo_Untyped &lt;required&gt; Short description
31+
* @param _customInputs Type-unsafe map where you can put any inputs that are not yet supported by the binding
32+
* @param _customVersion Allows overriding action's version, for example to use a specific minor version, or a newer version that the binding doesn't yet know about
33+
*/
34+
@ExposedCopyVisibility
35+
public data class ActionWithComment private constructor(
36+
/**
37+
* &lt;required&gt; Short description
38+
*/
39+
public val foo: String? = null,
40+
/**
41+
* &lt;required&gt; Short description
42+
*/
43+
public val foo_Untyped: String? = null,
44+
/**
45+
* Type-unsafe map where you can put any inputs that are not yet supported by the binding
46+
*/
47+
public val _customInputs: Map<String, String> = mapOf(),
48+
/**
49+
* Allows overriding action's version, for example to use a specific minor version, or a newer version that the binding doesn't yet know about
50+
*/
51+
public val _customVersion: String? = null,
52+
) : RegularAction<Action.Outputs>("john-smith", "action-with-comment", _customVersion ?: "v3", "some-comment") {
53+
init {
54+
require(!((foo != null) && (foo_Untyped != null))) {
55+
"Only foo or foo_Untyped must be set, but not both"
56+
}
57+
require((foo != null) || (foo_Untyped != null)) {
58+
"Either foo or foo_Untyped must be set, one of them is required"
59+
}
60+
}
61+
62+
public constructor(
63+
vararg pleaseUseNamedArguments: Unit,
64+
foo: String? = null,
65+
foo_Untyped: String? = null,
66+
_customInputs: Map<String, String> = mapOf(),
67+
_customVersion: String? = null,
68+
) : this(foo = foo, foo_Untyped = foo_Untyped, _customInputs = _customInputs, _customVersion = _customVersion)
69+
70+
@Suppress("SpreadOperator")
71+
override fun toYamlArguments(): LinkedHashMap<String, String> = linkedMapOf(
72+
*listOfNotNull(
73+
foo?.let { "foo" to it },
74+
foo_Untyped?.let { "foo" to it },
75+
*_customInputs.toList().toTypedArray(),
76+
).toTypedArray()
77+
)
78+
79+
override fun buildOutputObject(stepId: String): Action.Outputs = Outputs(stepId)
80+
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,36 @@ class GenerationTest :
582582
binding.shouldContainAndMatchFile("ActionWithNoInputsWithMinorVersion_Untyped.kt")
583583
}
584584
}
585+
586+
test("action with comment") {
587+
// given
588+
val actionManifest =
589+
Metadata(
590+
name = "Action with comment",
591+
description = "Do something cool",
592+
inputs =
593+
mapOf(
594+
"foo" to
595+
Input(
596+
description = "Short description",
597+
required = true,
598+
default = null,
599+
),
600+
),
601+
)
602+
val coords = ActionCoords("john-smith", "action-with-comment", "v3", comment = "some-comment")
603+
604+
// when
605+
val binding =
606+
coords.generateBinding(
607+
metadataRevision = NewestForVersion,
608+
metadata = actionManifest,
609+
inputTypings = ActionTypings(inputTypings = actionManifest.allInputsAsStrings(), source = ACTION),
610+
)
611+
612+
// then
613+
binding.shouldContainAndMatchFile("ActionWithComment.kt")
614+
}
585615
})
586616

587617
private fun Metadata.allInputsAsStrings(): Map<String, Typing> = this.inputs.mapValues { StringTyping }

0 commit comments

Comments
 (0)