Skip to content

GitHub Actions Validator — C++ 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 C++ implementation — the same logic the interactive tool runs, in a shareable, citable form.

// gh-actions-validator — GitHub Actions workflow structural validator. C++17
// 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. Standard library only; never throws on bad input — a hostile
// document yields structural issues or the pinned error strings, not
// exceptions. Node values are value-semantic; NIL doubles as YAML null.
#include <map>
#include <string>
#include <utility>
#include <vector>

struct Node {
    enum Kind { NIL, STR, SEQ, MAP } kind = NIL;
    std::string str;
    std::vector<Node> items;                           // SEQ
    std::vector<std::pair<std::string, Node>> entries; // MAP, insertion order
    bool is_map() const { return kind == MAP; }
    bool is_seq() const { return kind == SEQ; }
    bool is_str() const { return kind == STR; }
    const Node *get(const std::string &k) const { for (const auto &e : entries) if (e.first == k) return &e.second; return nullptr; }
    bool has(const std::string &k) const { return get(k) != nullptr; }
};
struct Issue { std::string path, message; };
struct GhResult { bool valid = false; std::vector<Issue> issues; std::string error; }; // empty error == null

static bool seqmark(const std::string &t) { return t == "-" || t.rfind("- ", 0) == 0; }
// Strip matching quotes; single-quote style un-doubles ''.
static std::string unquote(const std::string &s) {
    size_t n = s.size();
    if (n < 2 || s[0] != s[n-1] || (s[0] != '\'' && s[0] != '"')) return s;
    std::string out;
    for (size_t i = 1; i + 1 < n; i++) { out += s[i]; if (s[0] == '\'' && s[i] == '\'' && i + 2 < n && s[i+1] == '\'') i++; }
    return out;
}
// Cut a line at an unquoted # that starts the line or follows whitespace.
static void strip_hash(std::string &s) { char q = 0;
    for (size_t i = 0; i < s.size(); i++) { if (q) { if (s[i] == q) q = 0; } else if (s[i] == '\'' || s[i] == '"') q = s[i];
        else if (s[i] == '#' && (i == 0 || s[i-1] == ' ' || s[i-1] == '\t')) { s.resize(i); return; } } }
// Split "key: rest" at the first unquoted colon ending the line or followed by space/tab.
static std::pair<std::string, std::string> split_key(const std::string &t) {
    char q = 0;
    for (size_t i = 0; i < t.size(); i++) { if (q) { if (t[i] == q) q = 0; } else if (t[i] == '\'' || t[i] == '"') q = t[i];
        else if (t[i] == ':' && (i + 1 == t.size() || t[i+1] == ' ' || t[i+1] == '\t')) {
            size_t a = 0, b = i, j = i + 1;
            while (a < b && (t[a] == ' ' || t[a] == '\t')) a++;
            while (b > a && (t[b-1] == ' ' || t[b-1] == '\t')) b--;
            while (j < t.size() && (t[j] == ' ' || t[j] == '\t')) j++;
            return {unquote(t.substr(a, b - a)), t.substr(j)}; } }
    return {unquote(t), ""};
}
struct Line { size_t ind; std::string text; };
static std::vector<Line> build_lines(const std::string &raw) {
    std::vector<Line> out;
    for (size_t pos = 0; pos <= raw.size(); ) {
        size_t e = raw.find('\n', pos);
        std::string t = raw.substr(pos, e == std::string::npos ? std::string::npos : e - pos);
        pos = e == std::string::npos ? raw.size() + 1 : e + 1;
        if (!t.empty() && t.back() == '\r') t.pop_back();
        strip_hash(t);
        size_t a = 0, b = t.size();
        while (a < b && (t[a] == ' ' || t[a] == '\t')) a++;
        while (b > a && (t[b-1] == ' ' || t[b-1] == '\t')) b--;
        t = t.substr(a, b - a);
        if (t.empty() || t == "---" || t == "...") continue;
        out.push_back({a, t});
    }
    return out;
}
struct P { const std::vector<Line> *L; size_t i = 0; };
static Node parse_block(P &p);
static Node parse_seq(P &p, size_t ind);
// Value for a key (or "- key") consumed at the cursor: inline scalar, child block, child seq or null.
static Node childval(P &p, size_t ind, const std::string &rest) {
    Node v;
    if (!rest.empty()) { v.kind = Node::STR; v.str = unquote(rest); p.i++; return v; }
    if (p.i + 1 < p.L->size() && (*p.L)[p.i+1].ind > ind) { p.i++; return parse_block(p); }
    if (p.i + 1 < p.L->size() && (*p.L)[p.i+1].ind == ind && seqmark((*p.L)[p.i+1].text)) { p.i++; return parse_seq(p, ind); }
    return p.i++, v;
}
static void pair(P &p, size_t ind, Node &m) { // one "key: value" line of a mapping at ind
    auto kv = split_key(p.L->at(p.i).text);
    m.entries.push_back({kv.first, childval(p, ind, kv.second)});
}
static Node parse_map(P &p, size_t ind) {
    Node m; m.kind = Node::MAP;
    while (p.i < p.L->size() && (*p.L)[p.i].ind == ind && !seqmark((*p.L)[p.i].text)) pair(p, ind, m);
    return m;
}
static Node parse_seq(P &p, size_t ind) {
    Node s; s.kind = Node::SEQ;
    while (p.i < p.L->size() && (*p.L)[p.i].ind == ind && seqmark((*p.L)[p.i].text)) {
        const std::string &t = (*p.L)[p.i].text;
        size_t ro = (t[1] == ' ' || t[1] == '\t') ? 2 : 1;
        while (ro < t.size() && (t[ro] == ' ' || t[ro] == '\t')) ro++;
        size_t rc = ind + ro; // column the item text starts at
        Node v;
        if (ro >= t.size()) v = childval(p, ind, "");
        else {
            auto kv = split_key(t.substr(ro));
            if (!kv.second.empty() || t.back() == ':') { // inline map item: "- key: value" (+ pairs at rc)
                v.kind = Node::MAP;
                v.entries.push_back({kv.first, childval(p, rc, kv.second)});
                while (p.i < p.L->size() && (*p.L)[p.i].ind == rc && !seqmark((*p.L)[p.i].text)) pair(p, rc, v);
            } else { v.kind = Node::STR; v.str = unquote(t.substr(ro)); p.i++; }
        }
        s.items.push_back(v);
    }
    return s;
}
static Node parse_block(P &p) { const Line &l = (*p.L)[p.i]; return seqmark(l.text) ? parse_seq(p, l.ind) : parse_map(p, l.ind); }
static void issue(std::vector<Issue> &out, std::string path, std::string msg) { out.push_back({std::move(path), std::move(msg)}); }

// needs-graph cycle detection: DFS from every job over declared, known edges.
static void visit(const Node &jobs, const std::string &id, std::vector<std::string> &stack,
                  std::map<std::string, int> &color, std::vector<Issue> &out) {
    auto ci = color.find(id);
    if (ci != color.end()) {
        if (ci->second == 2) return;
        size_t from = 0; // ci->second == 1: id already sits on the current path — cycle
        while (from < stack.size() && stack[from] != id) from++;
        std::string msg = "needs cycle: ";
        for (size_t k = from; k < stack.size(); k++) msg += stack[k] + " -> ";
        issue(out, "$.jobs", msg + id);
        return;
    }
    color[id] = 1; stack.push_back(id);
    const Node *needs = nullptr;
    const Node *job = jobs.get(id);
    if (job && job->is_map()) needs = job->get("needs");
    if (needs) {
        auto walk = [&](const Node &n) { if (n.is_str() && jobs.has(n.str)) visit(jobs, n.str, stack, color, out); };
        if (needs->is_str()) walk(*needs);
        else if (needs->is_seq()) for (const auto &x : needs->items) walk(x);
    }
    stack.pop_back(); color[id] = 2;
}
static void check(const Node *root, std::vector<Issue> &out) {
    if (!root || !root->is_map()) { issue(out, "$", "workflow root must be a mapping"); return; }
    if (!root->has("jobs")) issue(out, "$", "missing required key \"jobs\"");
    if (!root->has("on")) issue(out, "$", "missing required key \"on\"");
    const Node *jobs = root->get("jobs");
    if (!jobs || !jobs->is_map()) { if (root->has("jobs")) issue(out, "$.jobs", "jobs must be a mapping"); return; }
    if (jobs->entries.empty()) issue(out, "$.jobs", "jobs must not be empty");
    std::map<std::string, int> color; std::vector<std::string> stack;
    for (const auto &je : jobs->entries) {
        const std::string &id = je.first, at = "$.jobs." + id;
        const Node &job = je.second;
        if (!job.is_map()) { issue(out, at, "job must be a mapping"); continue; }
        bool reusable = job.get("uses") && job.get("uses")->is_str();
        const Node *ro = job.get("runs-on"), *steps = job.get("steps"), *needs = job.get("needs");
        if (!reusable && !(ro && (ro->is_str() || ro->is_seq()))) issue(out, at + ".runs-on", "job needs runs-on (or a reusable-workflow uses)");
        if (!reusable && !job.has("steps")) issue(out, at + ".steps", "job needs steps");
        if (steps && steps->is_seq()) {
            if (steps->items.empty()) issue(out, at + ".steps", "steps must not be empty");
            for (size_t k = 0; k < steps->items.size(); k++) {
                const Node &st = steps->items[k];
                std::string sat = at + ".steps[" + std::to_string(k) + "]";
                if (!st.is_map()) { issue(out, sat, "step must be a mapping"); continue; }
                bool hr = st.get("run") && st.get("run")->is_str(), hu = st.get("uses") && st.get("uses")->is_str();
                if (!hr && !hu) issue(out, sat, "step needs run or uses");
                if (hr && hu) issue(out, sat, "step has both run and uses - pick one");
            }
        } else if (job.has("steps")) issue(out, at + ".steps", "steps must be a list");
        if (job.has("needs")) {
            if (needs && needs->is_str()) {
                if (!jobs->has(needs->str)) issue(out, at + ".needs[0]", "unknown job \"" + needs->str + "\"");
            } else if (needs && needs->is_seq()) {
                for (size_t k = 0; k < needs->items.size(); k++)
                    if (needs->items[k].is_str() && !jobs->has(needs->items[k].str))
                        issue(out, at + ".needs[" + std::to_string(k) + "]", "unknown job \"" + needs->items[k].str + "\"");
            } else issue(out, at + ".needs", "needs must be a string or a list");
        }
    }
    for (const auto &je : jobs->entries) visit(*jobs, je.first, stack, color, out);
}
GhResult validate_gh_actions(const std::string &raw) {
    GhResult r;
    if (raw.find_first_not_of(" \t\r\n") == std::string::npos) { r.error = "A workflow document is required"; return r; }
    std::vector<Line> lines = build_lines(raw);
    P p{&lines};
    if (lines.empty()) check(nullptr, r.issues);
    else { Node root = parse_block(p); check(&root, r.issues); }
    r.valid = r.issues.empty();
    return r;
}
// Example: validate_gh_actions("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[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 →