GitHub Actions Validator — Kotlin source
Check a workflow YAML before you push: on-shape, jobs, runs-on, steps with run or uses, and the needs graph including cycle detection — every issue reported at its path. Runs locally in your browser.
This is the Kotlin implementation — the same logic the interactive tool runs, in a shareable, citable form.
// gh-actions-validator — GitHub Actions workflow validator. Kotlin port
// (canonical TS: src/lib/gh-actions-validator.ts; Go twin: cli/gh-actions-validator).
// Minimal YAML-subset parser (block maps, lists, scalars) then the rules:
// on/jobs presence, job shape, runs-on (or reusable uses), steps run-xor-uses,
// needs known + acyclic. Issues print as "path — message".
import kotlin.text.Regex
data class Node(val map: MutableMap<String, Node> = mutableMapOf(), val list: MutableList<Node> = mutableListOf(), var scalar: String? = null) {
val isMap get() = scalar == null && map.isNotEmpty() || (scalar == null && list.isEmpty() && map.isNotEmpty())
val isList get() = list.isNotEmpty()
val isScalar get() = scalar != null
}
class Parser(private val lines: List<String>) {
private var i = 0
fun parse(indent: Int): Node {
val n = Node()
var any = false
while (i < lines.size) {
val raw = lines[i]
val t = raw.trim()
if (t.isEmpty() || t.startsWith("#")) { i++; continue }
val ind = raw.length - raw.trimStart().length
if (ind < indent) break
if (t.startsWith("- ")) {
n.list.add(scalarOrNested(t.drop(2), ind))
any = true
continue
}
val colon = t.indexOf(':') ?: run { i++; continue }
val key = t.substring(0, colon).trim()
val rest = t.substring(colon + 1).trim()
if (rest.isEmpty()) { i++; n.map[key] = parse(indent + 2) } else n.map[key] = scalarOrNested(rest, ind)
any = true
}
if (!any && n.scalar == null) n.scalar = ""
return n
}
private fun scalarOrNested(text: String, ind: Int): Node {
val n = Node()
val v = text.trim().removeSurrounding("\"").removeSurrounding("'")
n.scalar = v
return n
}
}
fun needsCycle(jobs: Map<String, Node>): String? {
val color = mutableMapOf<String, Int>()
fun visit(id: String, stack: List<String>): String? {
if (color[id] == 2) return null
if (color[id] == 1) {
val from = stack.indexOf(id)
return (stack.drop(from) + id).joinToString(" -> ")
}
color[id] = 1
val job = jobs[id] ?: return null
val needsRaw = job.map["needs"]
val needs = when {
needsRaw?.isList == true -> needsRaw.list.mapNotNull { it.scalar }
needsRaw?.scalar != null -> listOf(needsRaw.scalar!!)
else -> emptyList()
}
for (n in needs) if (n in jobs) visit(n, stack + id)?.let { return it }
color[id] = 2
return null
}
for (id in jobs.keys) visit(id, emptyList())?.let { return it }
return null
}
fun main() {
val src = generateSequence(::readLine).toList()
val root = Parser(src).parse(0)
val issues = mutableListOf<Pair<String, String>>()
if (root.map["on"] == null) issues += "$" to "missing required key \"on\""
val jobs = root.map["jobs"]
if (jobs == null) issues += "$" to "missing required key \"jobs\""
else {
if (jobs.map.isEmpty()) issues += "$.jobs" to "jobs must not be empty"
for ((id, job) in jobs.map) {
val at = "$.jobs.$id"
val reusable = job.map["uses"]?.scalar != null
if (!reusable && job.map["runs-on"] == null) issues += "$at.runs-on" to "job needs runs-on (or a reusable-workflow uses)"
val steps = job.map["steps"]
if (!reusable && steps == null) issues += "$at.steps" to "job needs steps"
if (steps?.isList == true) {
if (steps.list.isEmpty()) issues += "$at.steps" to "steps must not be empty"
steps.list.forEachIndexed { k, step ->
val hasRun = step.map["run"]?.scalar != null
val hasUses = step.map["uses"]?.scalar != null
if (!hasRun && !hasUses) issues += "$at.steps[$k]" to "step needs run or uses"
if (hasRun && hasUses) issues += "$at.steps[$k]" to "step has both run and uses - pick one"
}
}
val needsRaw = job.map["needs"]
if (needsRaw != null) {
val needs = when {
needsRaw.isList -> needsRaw.list.mapNotNull { it.scalar }
needsRaw.scalar != null -> listOf(needsRaw.scalar!!)
else -> { issues += "$at.needs" to "needs must be a string or a list"; emptyList() }
}
needs.forEachIndexed { k, n -> if (n !in jobs.map) issues += "$at.needs[$k]" to "unknown job \"$n\"" }
}
}
needsCycle(jobs.map)?.let { issues += "$.jobs" to "needs cycle: $it" }
}
issues.forEach { (p, m) -> println("$p — $m") }
if (issues.isEmpty()) println("valid")
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →