Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend reporting part to handle more than one action per repo #229

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
6 changes: 3 additions & 3 deletions src/main/kotlin/it/krzeminski/githubactionstyping/Logic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import kotlin.io.path.exists
*/
fun validateTypings(repoRoot: Path = Path.of(".")): Pair<Boolean, String> {
require(repoRoot.exists()) { "The given repo root leads to non-existent dir: $repoRoot" }
val manifest = repoRoot.readYamlFile("action") ?:
val (manifest, manifestPath) = repoRoot.readYamlFile("action") ?:
return Pair(false, "No action manifest (action.yml or action.yaml) found!")

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

return manifestsToReport(manifest, typesManifest)
return manifestsToReport(repoRoot.relativize(manifestPath), manifest, typesManifest)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import it.krzeminski.githubactionstyping.reporting.toPlaintextReport
import it.krzeminski.githubactionstyping.validation.ItemValidationResult
import it.krzeminski.githubactionstyping.validation.buildInputOutputMismatchValidationResult
import it.krzeminski.githubactionstyping.validation.validate
import java.nio.file.Path

fun manifestsToReport(manifest: String, typesManifest: String): Pair<Boolean, String> {
fun manifestsToReport(manifestPath: Path, manifest: String, typesManifest: String): Pair<Boolean, String> {
val parsedTypesManifest = if (typesManifest.isNotBlank()) {
parseTypesManifest(typesManifest)
} else {
Expand All @@ -24,6 +25,7 @@ fun manifestsToReport(manifest: String, typesManifest: String): Pair<Boolean, St

if (inputsInManifest != inputsInTypesManifest || outputsInManifest != outputsInTypesManifest) {
val inputOutputMismatchValidationResult = buildInputOutputMismatchValidationResult(
manifestPath = manifestPath,
inputsInManifest = inputsInManifest,
inputsInTypesManifest = inputsInTypesManifest,
outputsInManifest = outputsInManifest,
Expand All @@ -48,7 +50,7 @@ fun manifestsToReport(manifest: String, typesManifest: String): Pair<Boolean, St
printlnDebug("==============================================")
printlnDebug()

val validationResult = parsedTypesManifest.validate()
val validationResult = parsedTypesManifest.validate(manifestPath)
val isValid = validationResult.overallResult is ItemValidationResult.Valid
val report = validationResult.toPlaintextReport()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import java.nio.file.Path
import kotlin.io.path.exists
import kotlin.io.path.readText

fun Path.readYamlFile(nameWithoutExtension: String): String? =
fun Path.readYamlFile(nameWithoutExtension: String): Pair<String, Path>? =
listOf("yaml", "yml")
.map { "$nameWithoutExtension.$it" }
.firstOrNull { this.resolve(it).exists() }
?.let { this.resolve(it).readText() }
?.let { this.resolve(it).let { Pair(it.readText(), it) } }
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
package it.krzeminski.githubactionstyping.reporting

import it.krzeminski.githubactionstyping.validation.ActionValidationResult
import it.krzeminski.githubactionstyping.validation.ItemValidationResult
import it.krzeminski.githubactionstyping.validation.RepoValidationResult

fun ActionValidationResult.toPlaintextReport(): String = buildString {
appendLine("Overall result: ")
[email protected](this)
appendLine()
fun RepoValidationResult.toPlaintextReport(): String = buildString {
[email protected] { (path, resultForAction) ->
appendLine("For action with manifest at '$path':")
appendLine("Result:")
resultForAction.overallResult.appendStatus(this)
appendLine()

appendLine("Inputs:")
this@toPlaintextReport.inputs.forEach { (key, value) ->
appendLine("• $key:")
append(" ")
value.appendStatus(this)
}
if (this@toPlaintextReport.inputs.isEmpty()) {
appendLine("None.")
}
appendLine()
appendLine("Inputs:")
resultForAction.inputs.forEach { (key, value) ->
appendLine("• $key:")
append(" ")
value.appendStatus(this)
}
if (resultForAction.inputs.isEmpty()) {
appendLine("None.")
}
appendLine()

appendLine("Outputs:")
[email protected] { (key, value) ->
appendLine("• $key:")
append(" ")
value.appendStatus(this)
}
if ([email protected]()) {
appendLine("None.")
appendLine("Outputs:")
resultForAction.outputs.forEach { (key, value) ->
appendLine("• $key:")
append(" ")
value.appendStatus(this)
}
if (resultForAction.outputs.isEmpty()) {
appendLine("None.")
}
appendLine()
}
appendLine()
}

private fun ItemValidationResult.appendStatus(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,64 @@ import it.krzeminski.githubactionstyping.validation.types.validateFloat
import it.krzeminski.githubactionstyping.validation.types.validateInteger
import it.krzeminski.githubactionstyping.validation.types.validateList
import it.krzeminski.githubactionstyping.validation.types.validateString
import java.nio.file.Path

fun TypesManifest.validate(): ActionValidationResult {
fun TypesManifest.validate(manifestPath: Path): RepoValidationResult {
val inputValidationResults = this.inputs.mapValues { (_, value) -> value.validate() }
val outputValidationResults = this.outputs.mapValues { (_, value) -> value.validate() }
val isSomethingInvalid = (inputValidationResults.values + outputValidationResults.values)
.any { it != ItemValidationResult.Valid }

return ActionValidationResult(
return RepoValidationResult(
overallResult = if (isSomethingInvalid) ItemValidationResult.Invalid("Some typing is invalid.") else ItemValidationResult.Valid,
inputs = inputValidationResults,
outputs = outputValidationResults,
pathToActionValidationResult = mapOf(manifestPath to ActionValidationResult(
overallResult = if (isSomethingInvalid) ItemValidationResult.Invalid("Some typing is invalid.") else ItemValidationResult.Valid,
inputs = inputValidationResults,
outputs = outputValidationResults,
)
)
)
}

fun buildInputOutputMismatchValidationResult(
manifestPath: Path,
inputsInManifest: Set<String>,
inputsInTypesManifest: Set<String>,
outputsInManifest: Set<String>,
outputsInTypesManifest: Set<String>,
): ActionValidationResult {
return ActionValidationResult(
overallResult = ItemValidationResult.Invalid(
"Input/output mismatch detected. Please fix it first, then rerun to see other possible violations.",
),
inputs = (inputsInManifest + inputsInTypesManifest)
.associateWith {
if (it in inputsInManifest && it in inputsInTypesManifest) {
ItemValidationResult.Valid
} else {
if (it !in inputsInManifest) {
ItemValidationResult.Invalid("This input doesn't exist in the action manifest.")
): RepoValidationResult {
return RepoValidationResult(
overallResult = ItemValidationResult.Invalid("There was input/output mismatch for one of the actions."),
pathToActionValidationResult = mapOf(manifestPath to ActionValidationResult(
overallResult = ItemValidationResult.Invalid(
"Input/output mismatch detected. Please fix it first, then rerun to see other possible violations.",
),
inputs = (inputsInManifest + inputsInTypesManifest)
.associateWith {
if (it in inputsInManifest && it in inputsInTypesManifest) {
ItemValidationResult.Valid
} else {
ItemValidationResult.Invalid("This input doesn't exist in the types manifest.")
if (it !in inputsInManifest) {
ItemValidationResult.Invalid("This input doesn't exist in the action manifest.")
} else {
ItemValidationResult.Invalid("This input doesn't exist in the types manifest.")
}
}
}
},
outputs = (outputsInManifest + outputsInTypesManifest)
.associateWith {
if (it in outputsInManifest && it in outputsInTypesManifest) {
ItemValidationResult.Valid
} else {
if (it !in outputsInManifest) {
ItemValidationResult.Invalid("This output doesn't exist in the action manifest.")
},
outputs = (outputsInManifest + outputsInTypesManifest)
.associateWith {
if (it in outputsInManifest && it in outputsInTypesManifest) {
ItemValidationResult.Valid
} else {
ItemValidationResult.Invalid("This output doesn't exist in the types manifest.")
if (it !in outputsInManifest) {
ItemValidationResult.Invalid("This output doesn't exist in the action manifest.")
} else {
ItemValidationResult.Invalid("This output doesn't exist in the types manifest.")
}
}
}
},
},
)
)
)
}

Expand All @@ -74,14 +84,3 @@ private fun ApiItem.validate(): ItemValidationResult {
else -> ItemValidationResult.Invalid("Unknown type: '${this.type}'.")
}
}

data class ActionValidationResult(
val overallResult: ItemValidationResult,
val inputs: Map<String, ItemValidationResult> = emptyMap(),
val outputs: Map<String, ItemValidationResult> = emptyMap(),
)

sealed interface ItemValidationResult {
object Valid : ItemValidationResult
data class Invalid(val message: String) : ItemValidationResult
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package it.krzeminski.githubactionstyping.validation

import java.nio.file.Path

data class RepoValidationResult(
val overallResult: ItemValidationResult,
val pathToActionValidationResult: Map<Path, ActionValidationResult>,
)

data class ActionValidationResult(
val overallResult: ItemValidationResult,
val inputs: Map<String, ItemValidationResult> = emptyMap(),
val outputs: Map<String, ItemValidationResult> = emptyMap(),
)

sealed interface ItemValidationResult {
data object Valid : ItemValidationResult
data class Invalid(val message: String) : ItemValidationResult
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class LogicTest : FunSpec({
assertSoftly {
isValid shouldBe true
report shouldBe """
Overall result:
For action with manifest at 'action.yml':
Result:
${'\u001b'}[32m✔ VALID${'\u001b'}[0m

Inputs:
Expand All @@ -48,7 +49,8 @@ class LogicTest : FunSpec({
assertSoftly {
isValid shouldBe false
report shouldBe """
Overall result:
For action with manifest at 'action.yml':
Result:
${'\u001b'}[31m❌ INVALID: Some typing is invalid.${'\u001b'}[0m

Inputs:
Expand Down
Loading
Loading