diff --git a/action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/typing/TypesProviding.kt b/action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/typing/TypesProviding.kt index 60c77cd66a..de8581ad8c 100644 --- a/action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/typing/TypesProviding.kt +++ b/action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/typing/TypesProviding.kt @@ -68,12 +68,18 @@ private suspend fun ActionCoords.fetchTypingMetadata( logger.info { " ... types from action $url" } val response = httpClient.get(url) when (response.status) { - HttpStatusCode.OK -> response.bodyAsText() + HttpStatusCode.OK -> { + response.bodyAsText() + } + HttpStatusCode.NotFound -> { logger.info { " ... types from action were not found: $url" } null } - else -> throw IOException("Failed fetching from $url") + + else -> { + throw IOException("Failed fetching from $url") + } } } ?: return null @@ -124,12 +130,18 @@ private suspend fun fetchTypingsFromUrl( val response = httpClient.get(url) val typesMetadataYml = when (response.status) { - HttpStatusCode.OK -> response.bodyAsText() + HttpStatusCode.OK -> { + response.bodyAsText() + } + HttpStatusCode.NotFound -> { logger.info { " ... types from catalog were not found: $url" } return null } - else -> throw IOException("Failed fetching from $url") + + else -> { + throw IOException("Failed fetching from $url") + } } return yaml.decodeFromStringOrDefaultIfEmpty(typesMetadataYml, ActionTypes()) } @@ -143,8 +155,14 @@ private fun ActionCoords.toMajorVersion(): ActionCoords = this.copy(version = th private fun ActionType.toTyping(fieldName: String): Typing = when (this.type) { - ActionTypeEnum.String -> StringTyping - ActionTypeEnum.Boolean -> BooleanTyping + ActionTypeEnum.String -> { + StringTyping + } + + ActionTypeEnum.Boolean -> { + BooleanTyping + } + ActionTypeEnum.Integer -> { if (this.namedValues.isEmpty()) { IntegerTyping @@ -155,17 +173,24 @@ private fun ActionType.toTyping(fieldName: String): Typing = ) } } - ActionTypeEnum.Float -> FloatTyping - ActionTypeEnum.List -> + + ActionTypeEnum.Float -> { + FloatTyping + } + + ActionTypeEnum.List -> { ListOfTypings( delimiter = separator, typing = listItem?.toTyping(fieldName) ?: error("Lists should have list-item set!"), ) - ActionTypeEnum.Enum -> + } + + ActionTypeEnum.Enum -> { EnumTyping( items = allowedValues, typeName = name?.toPascalCase() ?: fieldName.toPascalCase(), ) + } } private inline fun Yaml.decodeFromStringOrDefaultIfEmpty( diff --git a/action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/typing/TypingGeneration.kt b/action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/typing/TypingGeneration.kt index 8da21be523..1e19d27362 100644 --- a/action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/typing/TypingGeneration.kt +++ b/action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/typing/TypingGeneration.kt @@ -20,31 +20,61 @@ internal fun Typing.getClassName( fieldName: String, ): TypeName = when (this) { - BooleanTyping -> Boolean::class.asTypeName() + BooleanTyping -> { + Boolean::class.asTypeName() + } + is EnumTyping -> { val typeName = this.typeName?.toPascalCase() ?: fieldName.toPascalCase() ClassName("io.github.typesafegithub.workflows.actions.$actionPackageName", "$actionClassName.$typeName") } - FloatTyping -> Float::class.asTypeName() - IntegerTyping -> Integer::class.asTypeName() + + FloatTyping -> { + Float::class.asTypeName() + } + + IntegerTyping -> { + Integer::class.asTypeName() + } + is IntegerWithSpecialValueTyping -> { val typeName = this.typeName?.toPascalCase() ?: fieldName.toPascalCase() ClassName("io.github.typesafegithub.workflows.actions.$actionPackageName", "$actionClassName.$typeName") } - is ListOfTypings -> + + is ListOfTypings -> { List::class .asClassName() .parameterizedBy(typing.getClassName(actionPackageName, actionClassName, fieldName)) - StringTyping -> String::class.asTypeName() + } + + StringTyping -> { + String::class.asTypeName() + } } internal fun Typing.asString(): String = when (this) { - BooleanTyping -> ".toString()" - is EnumTyping -> ".stringValue" - FloatTyping -> ".toString()" - IntegerTyping -> ".toString()" - is IntegerWithSpecialValueTyping -> ".integerValue.toString()" + BooleanTyping -> { + ".toString()" + } + + is EnumTyping -> { + ".stringValue" + } + + FloatTyping -> { + ".toString()" + } + + IntegerTyping -> { + ".toString()" + } + + is IntegerWithSpecialValueTyping -> { + ".integerValue.toString()" + } + is ListOfTypings -> { val mapValue: String = when (typing) { @@ -56,7 +86,10 @@ internal fun Typing.asString(): String = } ".joinToString(\"${if (delimiter == "\n") "\\n" else delimiter}\")$mapValue" } - else -> "" + + else -> { + "" + } } internal fun Typing.buildCustomType( diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 1799911a0d..93c7439363 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -13,7 +13,7 @@ dependencies { implementation("io.kotest:kotest-framework-plugin-gradle:6.0.7") implementation("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.23.8") - implementation("org.jmailen.gradle:kotlinter-gradle:5.2.0") + implementation("org.jmailen.gradle:kotlinter-gradle:5.3.0") implementation(platform("org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.10.2")) implementation(("org.jetbrains.kotlinx:kotlinx-coroutines-core")) diff --git a/code-generator/src/main/kotlin/io/github/typesafegithub/workflows/dsl/expressions/GenerateEventPayloads.kt b/code-generator/src/main/kotlin/io/github/typesafegithub/workflows/dsl/expressions/GenerateEventPayloads.kt index baf2fb0058..558be911d5 100644 --- a/code-generator/src/main/kotlin/io/github/typesafegithub/workflows/dsl/expressions/GenerateEventPayloads.kt +++ b/code-generator/src/main/kotlin/io/github/typesafegithub/workflows/dsl/expressions/GenerateEventPayloads.kt @@ -88,13 +88,19 @@ fun PayloadEventParams.findAllObjects( element .flatMap { (subpath, entry) -> when (entry) { - is JsonObject -> findAllObjects(entry, "$path.$subpath") + is JsonObject -> { + findAllObjects(entry, "$path.$subpath") + } + is JsonArray -> { (entry.firstOrNull() as? JsonObject) ?.let { firtSchild -> findAllObjects(firtSchild, "$path/$subpath") } ?: nothing } - else -> nothing + + else -> { + nothing + } }.toList() }.toMap() return result + Pair(path, element) @@ -149,11 +155,14 @@ fun Map.Entry.generatePropertySpec( .initializer("%S", "github.$key.$child") .build() } - is JsonObject -> + + is JsonObject -> { PropertySpec .builder(child, ClassName(PACKAGE, payloadClassName("$key.$child", filename))) .initializer("%L", payloadClassName("$key.$child", filename)) .build() + } + is JsonArray -> { PropertySpec .builder(child, listOfStrings) diff --git a/github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/ConsistencyCheckJob.kt b/github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/ConsistencyCheckJob.kt index f82a222c54..c70ac2f9c4 100644 --- a/github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/ConsistencyCheckJob.kt +++ b/github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/ConsistencyCheckJob.kt @@ -43,12 +43,19 @@ internal fun WorkflowBuilder.consistencyCheckJob( ) { val checkoutActionVersion = when (consistencyCheckJobConfig.checkoutActionVersion) { - CheckoutActionVersionSource.BundledWithLibrary -> "v4" - is CheckoutActionVersionSource.Given -> consistencyCheckJobConfig.checkoutActionVersion.version - is CheckoutActionVersionSource.InferFromClasspath -> + CheckoutActionVersionSource.BundledWithLibrary -> { + "v4" + } + + is CheckoutActionVersionSource.Given -> { + consistencyCheckJobConfig.checkoutActionVersion.version + } + + is CheckoutActionVersionSource.InferFromClasspath -> { inferCheckoutActionVersionFromClasspath( consistencyCheckJobConfig.checkoutActionVersion.checkoutActionClassFQN, ) + } } uses( diff --git a/github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/JobsToYaml.kt b/github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/JobsToYaml.kt index 172d0078ec..48c96540ef 100644 --- a/github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/JobsToYaml.kt +++ b/github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/JobsToYaml.kt @@ -63,22 +63,62 @@ private fun Job<*>.toYaml(): Map = @Suppress("CyclomaticComplexMethod") public fun RunnerType.toYaml(): Any = when (this) { - is Custom -> runsOn - is Labelled -> labels.toList() - is RunnerType.Group -> + is Custom -> { + runsOn + } + + is Labelled -> { + labels.toList() + } + + is RunnerType.Group -> { mapOfNotNullValues( "group" to name, "labels" to labels?.toList(), ) - UbuntuLatest -> "ubuntu-latest" - WindowsLatest -> "windows-latest" - MacOSLatest -> "macos-latest" - Windows2025 -> "windows-2025" - Windows2022 -> "windows-2022" - Windows2019 -> "windows-2019" - Windows2016 -> "windows-2016" - Ubuntu2004 -> "ubuntu-20.04" - Ubuntu1804 -> "ubuntu-18.04" - MacOS11 -> "macos-11" - MacOS1015 -> "macos-10.15" + } + + UbuntuLatest -> { + "ubuntu-latest" + } + + WindowsLatest -> { + "windows-latest" + } + + MacOSLatest -> { + "macos-latest" + } + + Windows2025 -> { + "windows-2025" + } + + Windows2022 -> { + "windows-2022" + } + + Windows2019 -> { + "windows-2019" + } + + Windows2016 -> { + "windows-2016" + } + + Ubuntu2004 -> { + "ubuntu-20.04" + } + + Ubuntu1804 -> { + "ubuntu-18.04" + } + + MacOS11 -> { + "macos-11" + } + + MacOS1015 -> { + "macos-10.15" + } } diff --git a/github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/ObjectToYaml.kt b/github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/ObjectToYaml.kt index 319ec43498..9cb35c9c38 100644 --- a/github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/ObjectToYaml.kt +++ b/github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/ObjectToYaml.kt @@ -47,14 +47,26 @@ internal fun Any.toYaml(): String { private fun Any?.elementToYaml(emitter: Emitter) { when (this) { - is Map<*, *> -> this.mapToYaml(emitter) - is List<*> -> this.listToYaml(emitter) - is String, is Int, is Float, is Double, is Boolean, null -> this.scalarToYaml(emitter) + is Map<*, *> -> { + this.mapToYaml(emitter) + } + + is List<*> -> { + this.listToYaml(emitter) + } + + is String, is Int, is Float, is Double, is Boolean, null -> { + this.scalarToYaml(emitter) + } + is StringWithComment -> { this.value.scalarToYaml(emitter) (" " + this.comment).commentToYaml(emitter) } - else -> error("Serializing $this is not supported!") + + else -> { + error("Serializing $this is not supported!") + } } } diff --git a/github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/TriggersToYaml.kt b/github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/TriggersToYaml.kt index 82bf805f9e..fd2e087cce 100644 --- a/github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/TriggersToYaml.kt +++ b/github-workflows-kt/src/main/kotlin/io/github/typesafegithub/workflows/yaml/TriggersToYaml.kt @@ -152,11 +152,15 @@ private fun Schedule.toAdditionalYaml(): List> = triggers.ma private fun WorkflowDispatch.toAdditionalYaml(): Map = when { - inputs.isEmpty() -> emptyMap() - else -> + inputs.isEmpty() -> { + emptyMap() + } + + else -> { mapOf( "inputs" to inputs.mapValues { (_, value) -> value.toYaml() }, ) + } } private fun WorkflowDispatch.Input.toYaml(): Map = @@ -170,13 +174,17 @@ private fun WorkflowDispatch.Input.toYaml(): Map = private fun WorkflowCall.toAdditionalYaml(): Map = when { - inputs.isEmpty() -> emptyMap() - else -> + inputs.isEmpty() -> { + emptyMap() + } + + else -> { mapOfNotNullValues( "inputs" to inputs.mapValues { (_, value) -> value.toYaml() }, "outputs" to outputs?.mapValues { (_, value) -> value.toYaml() }, "secrets" to secrets?.mapValues { (_, value) -> value.toYaml() }, ) + } } private fun WorkflowCall.Input.toYaml(): Map =