GitHub Actions Validator — Java 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 Java implementation — the same logic the interactive tool runs, in a shareable, citable form.
// gh-actions-validator — GitHub Actions workflow structural validator. Java
// port (canonical TS: src/lib/gh-actions-validator.ts; Go twin:
// cli/gh-actions-validator). Trimmed port: a minimal YAML-subset parser
// (block maps/sequences with `- key: value` items, plain/quoted scalars —
// no flow style, anchors or block scalars; inline lists like needs: [a, b]
// stay literal scalars) plus terse rule checks: root shape, on/jobs
// presence, jobs non-empty, runs-on, steps run-xor-uses, needs known +
// acyclic. JDK standard library only; never throws on bad input — errors
// travel in the result (error null when the verdict is real). Values are
// LinkedHashMap / ArrayList / String / null, so insertion order drives
// issue ordering (Object.keys parity with the TS).
// Language: Java 17. (Class is package-private so the showcase filename
// java.java compiles as-is — a public class would demand a matching name.)
package org.cosmolabs.cosmodev.ghactions;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
final class GhActionsValidator {
record Issue(String path, String message) {}
record GhResult(boolean valid, List<Issue> issues, String error) {}
private record Line(int ind, String text) {}
/** Cursor over the preprocessed lines; parsers advance {@code i}. */
private static final class P {
final List<Line> l;
int i = 0;
P(List<Line> l) { this.l = l; }
}
private static boolean seqmark(String t) { return t.equals("-") || t.startsWith("- "); }
private static boolean isSeq(Object v) { return v instanceof List<?>; }
/** Strip matching quotes; single-quote style un-doubles ''. */
private static String unquote(String s) {
int n = s.length();
if (n < 2 || s.charAt(0) != s.charAt(n - 1) || (s.charAt(0) != '\'' && s.charAt(0) != '"')) return s;
StringBuilder b = new StringBuilder();
for (int i = 1; i + 1 < n; i++) {
b.append(s.charAt(i));
if (s.charAt(0) == '\'' && s.charAt(i) == '\'' && i + 2 < n && s.charAt(i + 1) == '\'') i++;
}
return b.toString();
}
/** Cut a line at an unquoted # that starts the line or follows whitespace. */
private static String stripComment(String s) {
char q = 0;
for (int i = 0; i < s.length(); i++) {
if (q != 0) { if (s.charAt(i) == q) q = 0; }
else if (s.charAt(i) == '\'' || s.charAt(i) == '"') q = s.charAt(i);
else if (s.charAt(i) == '#' && (i == 0 || s.charAt(i - 1) == ' ' || s.charAt(i - 1) == '\t')) return s.substring(0, i);
}
return s;
}
private static String lstrip(String s) {
int i = 0;
while (i < s.length() && (s.charAt(i) == ' ' || s.charAt(i) == '\t')) i++;
return s.substring(i);
}
/** Split "key: rest" at the first unquoted colon ending the line or followed by space/tab. */
private static String[] splitKey(String t) {
char q = 0;
for (int i = 0; i < t.length(); i++) {
if (q != 0) { if (t.charAt(i) == q) q = 0; }
else if (t.charAt(i) == '\'' || t.charAt(i) == '"') q = t.charAt(i);
else if (t.charAt(i) == ':' && (i + 1 == t.length() || t.charAt(i + 1) == ' ' || t.charAt(i + 1) == '\t'))
return new String[]{unquote(t.substring(0, i).trim()), lstrip(t.substring(i + 1))};
}
return new String[]{unquote(t.trim()), ""};
}
private static List<Line> buildLines(String raw) {
List<Line> lines = new ArrayList<>();
for (String rawLine : raw.split("\n", -1)) {
String cut = stripComment(rawLine.endsWith("\r") ? rawLine.substring(0, rawLine.length() - 1) : rawLine);
String t = cut.trim();
if (t.isEmpty() || t.equals("---") || t.equals("...")) continue;
lines.add(new Line(cut.length() - lstrip(cut).length(), t));
}
return lines;
}
/** Value for a key (or "- key") consumed at the cursor: inline scalar, child block, child seq or null. */
private static Object childVal(P p, int ind, String rest) {
if (!rest.isEmpty()) { p.i++; return unquote(rest); }
if (p.i + 1 < p.l.size() && p.l.get(p.i + 1).ind() > ind) { p.i++; return parseBlock(p); }
if (p.i + 1 < p.l.size() && p.l.get(p.i + 1).ind() == ind && seqmark(p.l.get(p.i + 1).text())) { p.i++; return parseSeq(p, ind); }
p.i++;
return null;
}
/** Consume one "key: value" line of a mapping at {@code ind} into m. */
private static void pair(P p, int ind, Map<String, Object> m) {
String[] kv = splitKey(p.l.get(p.i).text());
m.put(kv[0], childVal(p, ind, kv[1]));
}
private static Object parseMap(P p, int ind) {
Map<String, Object> m = new LinkedHashMap<>();
while (p.i < p.l.size() && p.l.get(p.i).ind() == ind && !seqmark(p.l.get(p.i).text())) pair(p, ind, m);
return m;
}
private static Object parseSeq(P p, int ind) {
List<Object> s = new ArrayList<>();
while (p.i < p.l.size() && p.l.get(p.i).ind() == ind && seqmark(p.l.get(p.i).text())) {
String t = p.l.get(p.i).text();
int ro = t.length() > 1 && (t.charAt(1) == ' ' || t.charAt(1) == '\t') ? 2 : 1;
String rest = lstrip(t.substring(ro < t.length() ? ro : t.length()));
int rc = ind + (t.length() - rest.length()); // column the item text starts at
Object v;
if (rest.isEmpty()) v = childVal(p, ind, rest);
else {
String[] kv = splitKey(rest);
if (!kv[1].isEmpty() || rest.endsWith(":")) { // inline map item: "- key: value" (+ pairs at rc)
Map<String, Object> m = new LinkedHashMap<>();
m.put(kv[0], childVal(p, rc, kv[1]));
while (p.i < p.l.size() && p.l.get(p.i).ind() == rc && !seqmark(p.l.get(p.i).text())) pair(p, rc, m);
v = m;
} else { v = unquote(rest); p.i++; }
}
s.add(v);
}
return s;
}
private static Object parseBlock(P p) {
Line l = p.l.get(p.i);
return seqmark(l.text()) ? parseSeq(p, l.ind()) : parseMap(p, l.ind());
}
/** needs-graph cycle detection: DFS from every job over declared, known edges. */
private static void visit(Map<String, Object> jobs, String id, List<String> stack, Map<String, Integer> color, List<Issue> out) {
Integer c = color.get(id);
if (c != null) {
if (c == 2) return;
int from = stack.indexOf(id); // c == 1: id already sits on the current path — cycle
List<String> chain = new ArrayList<>(stack.subList(from, stack.size()));
chain.add(id);
out.add(new Issue("$.jobs", "needs cycle: " + String.join(" -> ", chain)));
return;
}
color.put(id, 1);
stack.add(id);
if (jobs.get(id) instanceof Map<?, ?> job && job.get("needs") != null) {
Object needs = job.get("needs");
List<?> ns = needs instanceof String n ? List.of(n) : needs instanceof List<?> l ? l : List.of();
for (Object x : ns) if (x instanceof String xn && jobs.containsKey(xn)) visit(jobs, xn, stack, color, out);
}
stack.remove(stack.size() - 1);
color.put(id, 2);
}
@SuppressWarnings("unchecked")
private static void check(Object root, List<Issue> out) {
if (!(root instanceof Map<?, ?> r0)) { out.add(new Issue("$", "workflow root must be a mapping")); return; }
if (!r0.containsKey("jobs")) out.add(new Issue("$", "missing required key \"jobs\""));
if (!r0.containsKey("on")) out.add(new Issue("$", "missing required key \"on\""));
Object jobsObj = r0.get("jobs");
if (!(jobsObj instanceof Map<?, ?>)) { if (r0.containsKey("jobs")) out.add(new Issue("$.jobs", "jobs must be a mapping")); return; }
Map<String, Object> jobs = (Map<String, Object>) jobsObj;
if (jobs.isEmpty()) out.add(new Issue("$.jobs", "jobs must not be empty"));
List<String> stack = new ArrayList<>();
Map<String, Integer> color = new LinkedHashMap<>();
for (Map.Entry<String, Object> e : jobs.entrySet()) {
String id = e.getKey(), at = "$.jobs." + id;
if (!(e.getValue() instanceof Map<?, ?> job)) { out.add(new Issue(at, "job must be a mapping")); continue; }
boolean reusable = job.get("uses") instanceof String;
Object ro = job.get("runs-on"), steps = job.get("steps"), needs = job.get("needs");
if (!reusable && !(ro instanceof String) && !isSeq(ro)) out.add(new Issue(at + ".runs-on", "job needs runs-on (or a reusable-workflow uses)"));
if (!reusable && !job.containsKey("steps")) out.add(new Issue(at + ".steps", "job needs steps"));
if (isSeq(steps)) {
List<?> sv = (List<?>) steps;
if (sv.isEmpty()) out.add(new Issue(at + ".steps", "steps must not be empty"));
for (int k = 0; k < sv.size(); k++) {
String sat = at + ".steps[" + k + "]";
if (!(sv.get(k) instanceof Map<?, ?> st)) { out.add(new Issue(sat, "step must be a mapping")); continue; }
boolean hr = st.get("run") instanceof String, hu = st.get("uses") instanceof String;
if (!hr && !hu) out.add(new Issue(sat, "step needs run or uses"));
if (hr && hu) out.add(new Issue(sat, "step has both run and uses - pick one"));
}
} else if (job.containsKey("steps")) out.add(new Issue(at + ".steps", "steps must be a list"));
if (job.containsKey("needs")) {
if (needs instanceof String n) { if (!jobs.containsKey(n)) out.add(new Issue(at + ".needs[0]", "unknown job \"" + n + "\"")); }
else if (isSeq(needs)) {
List<?> ns = (List<?>) needs;
for (int k = 0; k < ns.size(); k++)
if (ns.get(k) instanceof String xn && !jobs.containsKey(xn)) out.add(new Issue(at + ".needs[" + k + "]", "unknown job \"" + xn + "\""));
} else out.add(new Issue(at + ".needs", "needs must be a string or a list"));
}
}
for (String id : jobs.keySet()) visit(jobs, id, stack, color, out);
}
/** Validate a GitHub Actions workflow YAML document. */
static GhResult validateGhActions(String raw) {
if (raw.isBlank()) return new GhResult(false, List.of(), "A workflow document is required");
List<Line> lines = buildLines(raw);
P p = new P(lines);
List<Issue> issues = new ArrayList<>();
check(lines.isEmpty() ? null : parseBlock(p), issues);
return new GhResult(issues.isEmpty(), issues, null);
}
}
// Example: validateGhActions("on: push\njobs:\n a:\n needs: b\n runs-on: ubuntu-latest\n steps:\n - run: make\n b:\n needs: a\n runs-on: ubuntu-latest\n steps:\n - run: make").issues().get(0) // { "$.jobs", "needs cycle: a -> b -> a" }
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 →