Skip to content

Commit 77a6d93

Browse files
committed
feat(server): make it possible to POST custom typings for testing during typing creation
1 parent 65ffa66 commit 77a6d93

File tree

9 files changed

+199
-26
lines changed

9 files changed

+199
-26
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ public abstract interface annotation class io/github/typesafegithub/workflows/ac
22
}
33

44
public final class io/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords {
5-
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
5+
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
6+
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
67
public final fun component1 ()Ljava/lang/String;
78
public final fun component2 ()Ljava/lang/String;
89
public final fun component3 ()Ljava/lang/String;
9-
public final fun copy (Ljava/lang/String;Ljava/lang/String;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;ILjava/lang/Object;)Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords;
10+
public final fun component4 ()Ljava/lang/String;
11+
public final fun copy (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords;
12+
public static synthetic fun copy$default (Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords;
1113
public fun equals (Ljava/lang/Object;)Z
1214
public final fun getName ()Ljava/lang/String;
1315
public final fun getOwner ()Ljava/lang/String;
16+
public final fun getTypesUuid ()Ljava/lang/String;
1417
public final fun getVersion ()Ljava/lang/String;
1518
public fun hashCode ()I
1619
public fun toString ()Ljava/lang/String;
@@ -46,6 +49,7 @@ public final class io/github/typesafegithub/workflows/actionbindinggenerator/dom
4649

4750
public final class io/github/typesafegithub/workflows/actionbindinggenerator/domain/TypingActualSource : java/lang/Enum {
4851
public static final field ACTION Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/TypingActualSource;
52+
public static final field CUSTOM Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/TypingActualSource;
4953
public static final field TYPING_CATALOG Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/TypingActualSource;
5054
public static fun getEntries ()Lkotlin/enums/EnumEntries;
5155
public static fun valueOf (Ljava/lang/String;)Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/TypingActualSource;
@@ -72,8 +76,8 @@ public final class io/github/typesafegithub/workflows/actionbindinggenerator/gen
7276
}
7377

7478
public final class io/github/typesafegithub/workflows/actionbindinggenerator/generation/GenerationKt {
75-
public static final fun generateBinding (Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords;Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/MetadataRevision;Lio/github/typesafegithub/workflows/actionbindinggenerator/metadata/Metadata;Lkotlin/Pair;)Ljava/util/List;
76-
public static synthetic fun generateBinding$default (Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords;Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/MetadataRevision;Lio/github/typesafegithub/workflows/actionbindinggenerator/metadata/Metadata;Lkotlin/Pair;ILjava/lang/Object;)Ljava/util/List;
79+
public static final fun generateBinding (Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords;Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/MetadataRevision;Lio/github/typesafegithub/workflows/actionbindinggenerator/metadata/Metadata;Lkotlin/Pair;Ljava/lang/String;)Ljava/util/List;
80+
public static synthetic fun generateBinding$default (Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/ActionCoords;Lio/github/typesafegithub/workflows/actionbindinggenerator/domain/MetadataRevision;Lio/github/typesafegithub/workflows/actionbindinggenerator/metadata/Metadata;Lkotlin/Pair;Ljava/lang/String;ILjava/lang/Object;)Ljava/util/List;
7781
}
7882

7983
public final class io/github/typesafegithub/workflows/actionbindinggenerator/metadata/Input {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public data class ActionCoords(
44
val owner: String,
55
val name: String,
66
val version: String,
7+
val typesUuid: String? = null,
78
)
89

910
/**
@@ -12,7 +13,7 @@ public data class ActionCoords(
1213
*/
1314
public val ActionCoords.isTopLevel: Boolean get() = "/" !in name
1415

15-
public val ActionCoords.prettyPrint: String get() = "$owner/$name@$version"
16+
public val ActionCoords.prettyPrint: String get() = "$owner/$name@$version${typesUuid?.let { " (types: $it)" } ?: ""}"
1617

1718
/**
1819
* For most actions, it's the same as [ActionCoords.name].

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ package io.github.typesafegithub.workflows.actionbindinggenerator.domain
33
public enum class TypingActualSource {
44
ACTION,
55
TYPING_CATALOG,
6+
CUSTOM,
67
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,26 @@ public fun ActionCoords.generateBinding(
5757
metadataRevision: MetadataRevision,
5858
metadata: Metadata? = null,
5959
inputTypings: Pair<Map<String, Typing>, TypingActualSource?>? = null,
60+
types: String? = null,
6061
): List<ActionBinding> {
6162
val metadataResolved = metadata ?: this.fetchMetadata(metadataRevision) ?: return emptyList()
6263
val metadataProcessed = metadataResolved.removeDeprecatedInputsIfNameClash()
6364

64-
val inputTypingsResolved = inputTypings ?: this.provideTypes(metadataRevision)
65+
val (inputTypingsResolved, typingActualSource) =
66+
inputTypings ?: this.provideTypes(metadataRevision, types = types)
6567

6668
val classNameUntyped = this.buildActionClassName() + "_Untyped"
6769
val actionBindingSourceCodeUntyped =
6870
generateActionBindingSourceCode(metadataProcessed, this, emptyMap(), classNameUntyped, untyped = true)
6971

7072
val classNameAndSourceCodeTyped =
71-
if (inputTypingsResolved.second != null) {
73+
if (typingActualSource != null) {
7274
val className = this.buildActionClassName()
7375
val actionBindingSourceCode =
7476
generateActionBindingSourceCode(
7577
metadataProcessed,
7678
this,
77-
inputTypingsResolved.first,
79+
inputTypingsResolved,
7880
className,
7981
untyped = false,
8082
)
@@ -99,7 +101,7 @@ public fun ActionCoords.generateBinding(
99101
filePath = "io/github/typesafegithub/workflows/actions/$packageName/$className.kt",
100102
className = className,
101103
packageName = packageName,
102-
typingActualSource = inputTypingsResolved.second,
104+
typingActualSource = typingActualSource,
103105
)
104106
},
105107
)

action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/typing/TypesProviding.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.domain.Metadata
77
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.NewestForVersion
88
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.TypingActualSource
99
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.TypingActualSource.ACTION
10+
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.TypingActualSource.CUSTOM
1011
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.TypingActualSource.TYPING_CATALOG
1112
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.repoName
1213
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.subName
@@ -21,10 +22,12 @@ import java.net.URI
2122
internal fun ActionCoords.provideTypes(
2223
metadataRevision: MetadataRevision,
2324
fetchUri: (URI) -> String = ::fetchUri,
25+
types: String? = null,
2426
): Pair<Map<String, Typing>, TypingActualSource?> =
2527
(
26-
this.fetchTypingMetadata(metadataRevision, fetchUri)
27-
?: this.toMajorVersion().fetchFromTypingsFromCatalog(fetchUri)
28+
customTypingMetadata(types)
29+
?: this.fetchTypingMetadata(metadataRevision, fetchUri)
30+
?: this.toMajorVersion().fetchTypingsFromCatalog(fetchUri)
2831
)?.let { Pair(it.first.toTypesMap(), it.second) }
2932
?: Pair(emptyMap(), null)
3033

@@ -42,6 +45,9 @@ private fun ActionCoords.catalogMetadata() =
4245
private fun ActionCoords.actionTypesYamlUrl(gitRef: String) =
4346
"https://raw.githubusercontent.com/$owner/$repoName/$gitRef$subName/action-types.yaml"
4447

48+
private fun customTypingMetadata(types: String? = null) =
49+
types?.let { Pair(myYaml.decodeFromStringOrDefaultIfEmpty(it, ActionTypes()), CUSTOM) }
50+
4551
private fun ActionCoords.fetchTypingMetadata(
4652
metadataRevision: MetadataRevision,
4753
fetchUri: (URI) -> String = ::fetchUri,
@@ -65,7 +71,7 @@ private fun ActionCoords.fetchTypingMetadata(
6571
return Pair(myYaml.decodeFromStringOrDefaultIfEmpty(typesMetadataYaml, ActionTypes()), ACTION)
6672
}
6773

68-
private fun ActionCoords.fetchFromTypingsFromCatalog(fetchUri: (URI) -> String = ::fetchUri): Pair<ActionTypes, TypingActualSource>? =
74+
private fun ActionCoords.fetchTypingsFromCatalog(fetchUri: (URI) -> String = ::fetchUri): Pair<ActionTypes, TypingActualSource>? =
6975
(
7076
fetchTypingsFromUrl(url = actionTypesFromCatalog(), fetchUri = fetchUri)
7177
?: fetchTypingsForOlderVersionFromCatalog(fetchUri = fetchUri)

action-binding-generator/src/test/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/typing/TypesProvidingTest.kt

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ class TypesProvidingTest :
8585
stored-in-typing-catalog:
8686
type: string
8787
""".trimIndent()
88+
val custom =
89+
"""
90+
inputs:
91+
custom:
92+
type: string
93+
""".trimIndent()
8894
val metadata =
8995
"""
9096
"versionsWithTypings":
@@ -309,6 +315,134 @@ class TypesProvidingTest :
309315
types shouldBe Pair(mapOf("hosted-by-action-yml" to StringTyping), TypingActualSource.ACTION)
310316
}
311317

318+
test("only custom") {
319+
// Given
320+
val fetchUri: (URI) -> String = { throw IOException() }
321+
val actionCoord = ActionCoords("some-owner", "some-name", "v3")
322+
323+
// When
324+
val types = actionCoord.provideTypes(metadataRevision = CommitHash("some-hash"), fetchUri = fetchUri, types = custom)
325+
326+
// Then
327+
types shouldBe Pair(mapOf("custom" to StringTyping), TypingActualSource.CUSTOM)
328+
}
329+
330+
test("only custom for subaction") {
331+
// Given
332+
val fetchUri: (URI) -> String = { throw IOException() }
333+
val actionCoord = ActionCoords("some-owner", "some-name/some-sub", "v3")
334+
335+
// When
336+
val types = actionCoord.provideTypes(metadataRevision = CommitHash("some-hash"), fetchUri = fetchUri, types = custom)
337+
338+
// Then
339+
types shouldBe Pair(mapOf("custom" to StringTyping), TypingActualSource.CUSTOM)
340+
}
341+
342+
test("hosted by action, stored in typing catalog, and custom") {
343+
// Given
344+
val fetchUri: (URI) -> String = {
345+
when (it) {
346+
URI(
347+
"https://raw.githubusercontent.com/some-owner/some-name/" +
348+
"some-hash/action-types.yml",
349+
),
350+
-> hostedByActionYml
351+
URI(
352+
"https://raw.githubusercontent.com/typesafegithub/github-actions-typing-catalog/" +
353+
"main/typings/some-owner/some-name/v3/action-types.yml",
354+
),
355+
-> storedInTypingCatalog
356+
else -> throw IOException()
357+
}
358+
}
359+
val actionCoord = ActionCoords("some-owner", "some-name", "v3")
360+
361+
// When
362+
val types = actionCoord.provideTypes(metadataRevision = CommitHash("some-hash"), fetchUri = fetchUri, types = custom)
363+
364+
// Then
365+
types shouldBe Pair(mapOf("custom" to StringTyping), TypingActualSource.CUSTOM)
366+
}
367+
368+
test("hosted by subaction, stored in typing catalog, and custom") {
369+
// Given
370+
val fetchUri: (URI) -> String = {
371+
when (it) {
372+
URI(
373+
"https://raw.githubusercontent.com/some-owner/some-name/" +
374+
"some-hash/some-sub/action-types.yml",
375+
),
376+
-> hostedByActionYml
377+
URI(
378+
"https://raw.githubusercontent.com/typesafegithub/github-actions-typing-catalog/" +
379+
"main/typings/some-owner/some-name/v3/some-sub/action-types.yml",
380+
),
381+
-> storedInTypingCatalog
382+
else -> throw IOException()
383+
}
384+
}
385+
val actionCoord = ActionCoords("some-owner", "some-name/some-sub", "v3")
386+
387+
// When
388+
val types = actionCoord.provideTypes(metadataRevision = CommitHash("some-hash"), fetchUri = fetchUri, types = custom)
389+
390+
// Then
391+
types shouldBe Pair(mapOf("custom" to StringTyping), TypingActualSource.CUSTOM)
392+
}
393+
394+
test("hosted by action, stored in typing catalog, and empty custom") {
395+
// Given
396+
val fetchUri: (URI) -> String = {
397+
when (it) {
398+
URI(
399+
"https://raw.githubusercontent.com/some-owner/some-name/" +
400+
"some-hash/action-types.yml",
401+
),
402+
-> hostedByActionYml
403+
URI(
404+
"https://raw.githubusercontent.com/typesafegithub/github-actions-typing-catalog/" +
405+
"main/typings/some-owner/some-name/v3/action-types.yml",
406+
),
407+
-> storedInTypingCatalog
408+
else -> throw IOException()
409+
}
410+
}
411+
val actionCoord = ActionCoords("some-owner", "some-name", "v3")
412+
413+
// When
414+
val types = actionCoord.provideTypes(metadataRevision = CommitHash("some-hash"), fetchUri = fetchUri, types = "")
415+
416+
// Then
417+
types shouldBe Pair(emptyMap(), TypingActualSource.CUSTOM)
418+
}
419+
420+
test("hosted by subaction, stored in typing catalog, and empty custom") {
421+
// Given
422+
val fetchUri: (URI) -> String = {
423+
when (it) {
424+
URI(
425+
"https://raw.githubusercontent.com/some-owner/some-name/" +
426+
"some-hash/some-sub/action-types.yml",
427+
),
428+
-> hostedByActionYml
429+
URI(
430+
"https://raw.githubusercontent.com/typesafegithub/github-actions-typing-catalog/" +
431+
"main/typings/some-owner/some-name/v3/some-sub/action-types.yml",
432+
),
433+
-> storedInTypingCatalog
434+
else -> throw IOException()
435+
}
436+
}
437+
val actionCoord = ActionCoords("some-owner", "some-name/some-sub", "v3")
438+
439+
// When
440+
val types = actionCoord.provideTypes(metadataRevision = CommitHash("some-hash"), fetchUri = fetchUri, types = "")
441+
442+
// Then
443+
types shouldBe Pair(emptyMap(), TypingActualSource.CUSTOM)
444+
}
445+
312446
test("only stored in typing catalog for older version") {
313447
// Given
314448
val fetchUri: (URI) -> String = {

0 commit comments

Comments
 (0)