Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/main/kotlin/it/krzeminski/githubactionstyping/Logic.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package it.krzeminski.githubactionstyping

import it.krzeminski.githubactionstyping.parsing.readYamlFile
import java.nio.file.Path

/**
* Runs validation for a given action, with its manifest files present in the current directory.
*
* @param repoRoot: Allows customizing which path should be taken as repo root for action(-types).y(a)ml file discovery.
*
* @return a pair where:
* - the boolean means if the typings are valid
* - the string is a printable report, with details about all inputs and outputs
*/
fun validateTypings(repoRoot: Path = Path.of(".")): Pair<Boolean, String> {
val manifest = repoRoot.readYamlFile("action") ?:
return Pair(false, "No action manifest (action.yml or action.yaml) found!")

val typesManifest = repoRoot.readYamlFile("action-types") ?:
return Pair(false, "No types manifest (action-types.yml or action-types.yaml) found!")

return manifestsToReport(manifest, typesManifest)
}
15 changes: 1 addition & 14 deletions src/main/kotlin/it/krzeminski/githubactionstyping/Main.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
package it.krzeminski.githubactionstyping

import it.krzeminski.githubactionstyping.parsing.readYamlFile
import kotlin.system.exitProcess

fun main() {
val typesManifest = readYamlFile("action-types") ?: run {
println("No types manifest (action-types.yml or action-types.yaml) found!")
exitProcess(1)
throw IllegalStateException()
}

val manifest = readYamlFile("action") ?: run {
println("No action manifest (action.yml or action.yaml) found!")
exitProcess(1)
throw IllegalStateException()
}

val (isValid, report) = manifestsToReport(manifest, typesManifest)
val (isValid, report) = validateTypings()
println(report)

if (!isValid) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package it.krzeminski.githubactionstyping.parsing

import java.io.File
import java.nio.file.Path
import kotlin.io.path.exists
import kotlin.io.path.readText

fun readYamlFile(nameWithoutExtension: String): String? =
fun Path.readYamlFile(nameWithoutExtension: String): String? =
listOf("yaml", "yml")
.map { "$nameWithoutExtension.$it" }
.firstOrNull { File(it).exists() }
?.let { File(it).readText() }
.firstOrNull { this.resolve(it).exists() }
?.let { this.resolve(it).readText() }
93 changes: 93 additions & 0 deletions src/test/kotlin/it/krzeminski/githubactionstyping/LogicTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package it.krzeminski.githubactionstyping

import io.kotest.assertions.assertSoftly
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import java.io.File
import java.nio.file.Path

class LogicTest : FunSpec({
val testRepos: Path = javaClass.classLoader.getResource("test-repos").let {
File(it.toURI()).toPath()
}

test("repo with only top-level action and valid typings") {
// When
val (isValid, report) = validateTypings(
repoRoot = testRepos.resolve("repo-with-only-top-level-action-and-valid-typings"),
)

// Then
assertSoftly {
isValid shouldBe true
report shouldBe """
Overall result:
${'\u001b'}[32m✔ VALID${'\u001b'}[0m

Inputs:
• verbose:
${'\u001b'}[32m✔ VALID${'\u001b'}[0m
• someEnum:
${'\u001b'}[32m✔ VALID${'\u001b'}[0m

Outputs:
None.


""".trimIndent()
}
}

test("repo with only top-level action and invalid typings") {
// When
val (isValid, report) = validateTypings(
repoRoot = testRepos.resolve("repo-with-only-top-level-action-and-invalid-typings"),
)

// Then
assertSoftly {
isValid shouldBe false
report shouldBe """
Overall result:
${'\u001b'}[31m❌ INVALID: Some typing is invalid.${'\u001b'}[0m

Inputs:
• verbose:
${'\u001b'}[32m✔ VALID${'\u001b'}[0m
• someEnum:
${'\u001b'}[31m❌ INVALID: 'allowed-values' is not allowed for this type.${'\u001b'}[0m

Outputs:
None.


""".trimIndent()
}
}

test("repo with only top-level action and no typings") {
// When
val (isValid, report) = validateTypings(
repoRoot = testRepos.resolve("repo-with-only-top-level-action-and-no-typings"),
)

// Then
assertSoftly {
isValid shouldBe false
report shouldBe "No types manifest (action-types.yml or action-types.yaml) found!"
}
}

test("repo with only top-level action and top-level manifest") {
// When
val (isValid, report) = validateTypings(
repoRoot = testRepos.resolve("repo-with-only-top-level-action-and-no-top-level-manifest"),
)

// Then
assertSoftly {
isValid shouldBe false
report shouldBe "No action manifest (action.yml or action.yaml) found!"
}
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I'm an action with just a single top-level "action.yml" and its associated typings that are invalid.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
inputs:
verbose:
type: boolean
someEnum:
type: string
allowed-values:
- foo
- bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: GitHub Actions Typing
description: Bring type-safety to your GitHub actions' API!
author: Piotr Krzemiński
inputs:
verbose:
description: 'Set to true to display debug information helpful when troubleshooting issues with this action.'
required: false
default: 'false'
someEnum:
description: 'Testing enum'
required: false
runs:
using: 'docker'
image: 'Dockerfile'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I'm an action with just a single top-level "action.yml" and no associated typings.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: GitHub Actions Typing
description: Bring type-safety to your GitHub actions' API!
author: Piotr Krzemiński
inputs:
verbose:
description: 'Set to true to display debug information helpful when troubleshooting issues with this action.'
required: false
default: 'false'
someEnum:
description: 'Testing enum'
required: false
runs:
using: 'docker'
image: 'Dockerfile'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I'm an action with just a single top-level "action.yml" and no associated typings.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I'm an action with just a single top-level "action.yml" and its associated typings that are valid.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
inputs:
verbose:
type: boolean
someEnum:
type: enum
allowed-values:
- foo
- bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: GitHub Actions Typing
description: Bring type-safety to your GitHub actions' API!
author: Piotr Krzemiński
inputs:
verbose:
description: 'Set to true to display debug information helpful when troubleshooting issues with this action.'
required: false
default: 'false'
someEnum:
description: 'Testing enum'
required: false
runs:
using: 'docker'
image: 'Dockerfile'