feat(pipeline): a workflow references its local actions and workflows

A local `uses:` of a GitHub workflow or composite action is a path from
the repository root. A step's names an action by its directory, a job's a
reusable workflow by its file. Each draws REFERENCES_FILE {via: uses, line}
from the file stating it to the file it names: the directory's action.yml
or action.yaml (the one it holds), or the workflow file. Once per target,
at the first use.

An action of another repository, a path with an expression, a directory
with no action file or with both spellings, and a path out of the
repository draw nothing.

The CI walk that reads `run:` steps hands the local uses to the shell pass
through a second callback of cbm_ci_commands, in the same walk: a file is
parsed once, as before.

The incremental repair reads no workflow again when an action file is
deleted. A directory with both spellings has no edge, and deleting one of
the two makes the other the action, so that deletion takes the full route
(local_action_pair_deleted).

Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
This commit is contained in:
Martin Vogel
2026-11-26 23:05:49 +01:00
parent c17a62f3c2
commit 0c015a5a6a
6 changed files with 295 additions and 5 deletions
+28 -2
View File
@@ -14,6 +14,9 @@
* unknown one. In the text, `${{ github.workspace }}` is
* $GITHUB_WORKSPACE, `${{ github.action_path }}` $GITHUB_ACTION_PATH, any
* other `${{ }}` an unknown variable (its line breaks kept).
* A local `uses:` ("./...", a path from the repository root) is handed on
* as well: a step's names an action directory, a job's a reusable
* workflow file; one with `${{` in it is none.
* - Prow: a file with a top-level presubmits / postsubmits / periodics key
* (in-repo job config). A job container's command + args is exec'd, no
* shell: the words are literal, a kubelet `$(VAR)` unknown; `bash -c TEXT`
@@ -53,6 +56,8 @@ typedef struct {
const char *src;
const char *rel;
cbm_ci_cmd_fn fn;
cbm_ci_use_fn use_fn; /* local `uses:`; NULL: not wanted */
void *use_ud; /* use_fn's own userdata */
cbm_build_site_fn site_fn; /* a build-site walk's callback */
cbm_compose_service_fn service_fn; /* a compose walk's callback */
void *ud;
@@ -451,10 +456,29 @@ static const char *ci_run_default(const ci_t *c, TSNode m, const char *key) {
return ci_get_str(c, ci_get(c, ci_get(c, m, "defaults"), "run"), key);
}
static const char *ci_repo_path(CBMArena *a, const char *base, const char *rel);
/* The `uses:` of step or job `owner` when it names something of this
* repository: "./dir", "./.github/workflows/x.yml". One with an expression in
* it, or leaving the repository, is none. */
static void ci_local_use(const ci_t *c, TSNode owner, bool job) {
int row = 0;
const char *uses = c->use_fn ? ci_scalar(c, ci_get(c, owner, "uses"), &row) : NULL;
if (!uses || uses[0] != '.' || uses[SKIP_ONE] != '/' || strstr(uses, CI_EXPR_OPEN)) {
return;
}
const char *path = ci_repo_path(c->a, "", uses);
if (path) {
cbm_ci_use_t use = {.path = path, .job = job, .line = row + SKIP_ONE};
c->use_fn(&use, c->use_ud);
}
}
static void ci_gha_steps(const ci_t *c, TSNode steps, const char *job, const ci_scope_t *sc) {
int n = 0;
TSNode *items = ci_items(c, steps, &n);
for (int i = 0; i < n; i++) {
ci_local_use(c, items[i], false);
int row = 0;
const char *run = ci_scalar(c, ci_get(c, items[i], "run"), &row);
if (!run) {
@@ -500,6 +524,7 @@ static void ci_workflow(const ci_t *c, TSNode root) {
ci_pair_t *jobs = ci_pairs(c, ci_get(c, root, "jobs"), &n);
for (int i = 0; i < n; i++) {
TSNode j = jobs[i].value;
ci_local_use(c, j, true);
const char *wd = ci_run_default(c, j, "working-directory");
const char *sh = ci_run_default(c, j, "shell");
ci_scope_t sc = {.env = ci_env_add(c, &wenv, ci_get(c, j, "env")),
@@ -1392,7 +1417,7 @@ bool cbm_ci_path(const char *rel) {
}
void cbm_ci_commands(CBMArena *a, const char *rel, const char *src, size_t len, cbm_ci_cmd_fn fn,
void *ud) {
void *ud, cbm_ci_use_fn use_fn, void *use_ud) {
if (!ci_yaml_ext(rel)) {
return;
}
@@ -1405,7 +1430,8 @@ void cbm_ci_commands(CBMArena *a, const char *rel, const char *src, size_t len,
tree = ts_parser_parse_string(parser, NULL, src, (uint32_t)len);
}
if (tree) {
ci_t c = {.a = a, .src = src, .rel = rel, .fn = fn, .ud = ud};
ci_t c = {
.a = a, .src = src, .rel = rel, .fn = fn, .ud = ud, .use_fn = use_fn, .use_ud = use_ud};
TSNode stream = ts_tree_root_node(tree);
for (uint32_t i = 0; i < ts_node_named_child_count(stream); i++) {
TSNode doc = ts_node_named_child(stream, i);
+43 -1
View File
@@ -4020,11 +4020,53 @@ static void sh_ci_cmd(const cbm_ci_cmd_t *cmd, void *ud) {
sh_state_free(&s);
}
typedef struct {
sh_pass_t *p;
const char *rel;
CBMArena *a;
CBMHashTable *seen; /* target paths drawn */
} sh_ci_use_t;
/* A local `uses:` of a workflow or composite action: REFERENCES_FILE {via:
* uses, line} from its File to the file it names -- a job's reusable
* workflow, a step's action by its action.yml or action.yaml (the one the
* directory holds; with both, or neither, there is no edge) -- once per
* file, at the first use. */
static void sh_ci_use(const cbm_ci_use_t *use, void *ud) {
static const char *const NAMES[PAIR_LEN] = {"action.yml", "action.yaml"};
sh_ci_use_t *x = ud;
const sh_index_t *ix = x->p->ix;
const char *target = use->job ? use->path : NULL;
for (int k = 0; !use->job && k < PAIR_LEN; k++) {
const char *cand =
cbm_arena_sprintf(x->a, "%s%s%s", use->path, use->path[0] ? "/" : "", NAMES[k]);
if (cand && cbm_ht_get(ix->file_ids, cand)) {
if (target) {
return; /* both spellings: which one GitHub reads is not ours to say */
}
target = cand;
}
}
int64_t src = (int64_t)(intptr_t)cbm_ht_get(ix->file_ids, x->rel);
int64_t tgt = target ? (int64_t)(intptr_t)cbm_ht_get(ix->file_ids, target) : 0;
if (src <= 0 || tgt <= 0 || src == tgt || !x->seen || cbm_ht_get(x->seen, target)) {
return;
}
cbm_ht_set(x->seen, target, (void *)target);
char props[CBM_SZ_64];
snprintf(props, sizeof(props), "{\"via\":\"uses\",\"line\":%d}", use->line);
cbm_gbuf_insert_edge(ix->ctx->gbuf, src, tgt, "REFERENCES_FILE", props);
}
static void sh_ci_file(sh_pass_t *p, const char *rel, const char *src, size_t len) {
CBMArena a;
cbm_arena_init(&a);
sh_ci_t x = {.p = p, .rel = rel, .a = &a};
cbm_ci_commands(&a, rel, src, len, sh_ci_cmd, &x);
sh_ci_use_t u = {.p = p, .rel = rel, .a = &a, .seen = cbm_ht_create(0)};
cbm_ci_commands(&a, rel, src, len, sh_ci_cmd, &x, sh_ci_use, &u);
if (u.seen) {
cbm_ht_free(u.seen);
}
cbm_arena_destroy(&a);
}
+33
View File
@@ -2781,6 +2781,35 @@ static const char *closure_docker_context_decline(const cbm_file_info_t *files,
return decline;
}
/* A workflow names a local action by its directory, and its edge goes to the
* one action file that directory holds (pass_shell.c sh_ci_use); with both
* spellings, action.yml and action.yaml, there is none. Deleting one of the
* two makes the other the action: an edge the full index draws from a
* workflow that did not change, and the repair reads no workflow again. So
* a deleted action file whose other spelling is still indexed declines.
* One lookup per deleted file. Returns the reason, or NULL. */
static const char *closure_local_action_decline(CBMHashTable *files_by_path, char **deleted,
int n_deleted) {
static const char *const names[PAIR_LEN] = {"action.yml", "action.yaml"};
for (int i = 0; i < n_deleted; i++) {
const char *p = deleted[i];
const char *slash = p ? strrchr(p, '/') : NULL;
const char *base = slash ? slash + SKIP_ONE : p;
for (int k = 0; base && k < PAIR_LEN; k++) {
if (strcmp(base, names[k]) != 0) {
continue;
}
char other[CBM_SZ_4K];
int n = snprintf(other, sizeof(other), "%.*s%s", (int)(base - p), p,
names[PAIR_LEN - SKIP_ONE - k]);
if (n > 0 && (size_t)n < sizeof(other) && cbm_ht_get(files_by_path, other)) {
return "local_action_pair_deleted";
}
}
}
return NULL;
}
/* Kubernetes inputs reach past their own file (pass_k8s.c): a kustomization
* decides which manifests deploy together and in which namespace, a
* Chart.yaml links other charts, and a manifest's Services select workloads
@@ -3360,6 +3389,10 @@ static int closure_try_plan(cbm_pipeline_t *p, cbm_store_t *store, const char *p
if (decline) {
goto done;
}
decline = closure_local_action_decline(files_by_path, changed_paths + n_changed, n_deleted);
if (decline) {
goto done;
}
decline = closure_terraform_decline(changed_paths, n_changed + n_deleted);
if (decline) {
goto done;
+15 -2
View File
@@ -1432,11 +1432,24 @@ typedef void (*cbm_ci_cmd_fn)(const cbm_ci_cmd_t *cmd, void *ud);
* (.github/workflows/<name>.yml|yaml) or a composite action (action.yml|yaml). */
bool cbm_ci_path(const char *rel);
/* A local `uses:` of a GitHub workflow or composite action (pass_ci.c): a
* path from the repository root ("./..."), which is how GitHub reads it
* wherever the file lies. A step's names an action's directory (`path`; ""
* is the root), a job's names a reusable workflow file. `line` is 1-based. */
typedef struct {
const char *path;
bool job;
int line;
} cbm_ci_use_t;
typedef void (*cbm_ci_use_fn)(const cbm_ci_use_t *use, void *ud);
/* Hand each command line the CI configuration `rel` (YAML text src) runs to
* fn: GitHub Actions run: steps for a workflow or composite action, else
* Prow job containers. Strings live in arena `a`. */
* Prow job containers. In the same walk, each local `uses:` of a workflow or
* composite action goes to use_fn (NULL: not wanted) with use_ud, in
* document order. Strings live in arena `a`. */
void cbm_ci_commands(CBMArena *a, const char *rel, const char *src, size_t len, cbm_ci_cmd_fn fn,
void *ud);
void *ud, cbm_ci_use_fn use_fn, void *use_ud);
/* An image a build configuration builds from a Dockerfile (pass_ci.c). */
typedef struct {
+95
View File
@@ -3096,6 +3096,100 @@ TEST(probe_kustomize_helm_charts_deploy_their_chart) {
PASS();
}
/* Every REFERENCES_FILE edge a local `uses:` draws, as
* "<file> -> <target file> (uses:line)", sorted, one per line. */
static char *gpf_local_uses(const GpfFile *files, int nfiles) {
GpfProj lp;
cbm_store_t *store = gpf_index_files(&lp, files, nfiles);
char *out = NULL;
if (store) {
sqlite3_stmt *st = NULL;
const char *sql =
"SELECT coalesce(group_concat(x, char(10)), '') FROM (SELECT "
"s.file_path || ' -> ' || t.file_path || ' (' || "
"json_extract(e.properties,'$.via') || ':' || json_extract(e.properties,'$.line') "
"|| ')' AS x "
"FROM edges e JOIN nodes s ON s.id=e.source_id JOIN nodes t ON t.id=e.target_id "
"WHERE e.project=?1 AND e.type='REFERENCES_FILE' "
"AND json_extract(e.properties,'$.via')='uses' ORDER BY x)";
if (sqlite3_prepare_v2(cbm_store_get_db(store), sql, -1, &st, NULL) == SQLITE_OK) {
sqlite3_bind_text(st, 1, lp.project, -1, SQLITE_STATIC);
if (sqlite3_step(st) == SQLITE_ROW) {
out = strdup((const char *)sqlite3_column_text(st, 0));
}
}
sqlite3_finalize(st);
}
gpf_cleanup(&lp, store);
return out;
}
/* A local `uses:` ("./...") is a path from the repository root, wherever the
* workflow or action lies. A step's names an action by its directory: the
* edge goes to that directory's action.yml or action.yaml, the one it holds.
* A job's names a reusable workflow file. Each file once, at the first use.
* No edge for an action of another repository, a path with an expression, a
* directory with no action file, or one holding both spellings. */
TEST(probe_github_actions_local_uses_reference_their_files) {
static const GpfFile files[] = {
{".github/workflows/ci.yml", "name: ci\n"
"on: [push]\n"
"jobs:\n"
" build:\n"
" runs-on: ubuntu-latest\n"
" steps:\n"
" - uses: actions/checkout@v4\n"
" - uses: ./.github/actions/setup\n"
" - uses: ./\n"
" - uses: ./.github/actions/${{ matrix.kind }}\n"
" - uses: ./.github/actions/missing\n"
" - uses: ./.github/actions/both\n"
" - name: again\n"
" uses: ./.github/actions/setup\n"
" - run: echo done\n"
" call:\n"
" uses: ./.github/workflows/reusable.yml\n"
" far:\n"
" uses: org/repo/.github/workflows/x.yml@main\n"},
{".github/workflows/reusable.yml", "on:\n"
" workflow_call: {}\n"
"jobs:\n"
" a:\n"
" runs-on: ubuntu-latest\n"
" steps:\n"
" - run: echo a\n"},
{".github/actions/setup/action.yml", "name: setup\n"
"runs:\n"
" using: composite\n"
" steps:\n"
" - uses: ./.github/actions/inner\n"
" - run: echo setup\n"
" shell: bash\n"},
{".github/actions/inner/action.yaml", "name: inner\n"
"runs:\n"
" using: composite\n"
" steps:\n"
" - run: echo inner\n"
" shell: bash\n"},
{".github/actions/both/action.yml", "name: both\nruns:\n using: composite\n steps: []\n"},
{".github/actions/both/action.yaml", "name: both\nruns:\n using: composite\n steps: []\n"},
{"action.yaml", "name: root\nruns:\n using: composite\n steps: []\n"}};
int n = (int)(sizeof(files) / sizeof(files[0]));
char *got = gpf_local_uses(files, n);
static const char want[] =
".github/actions/setup/action.yml -> .github/actions/inner/action.yaml (uses:5)\n"
".github/workflows/ci.yml -> .github/actions/setup/action.yml (uses:8)\n"
".github/workflows/ci.yml -> .github/workflows/reusable.yml (uses:17)\n"
".github/workflows/ci.yml -> action.yaml (uses:9)";
if (!got || strcmp(got, want) != 0) {
fprintf(stderr, "local uses got:\n%s\nwant:\n%s\n", got ? got : "(null)", want);
free(got);
FAIL("local uses");
}
free(got);
PASS();
}
/* Every Terraform reference edge as
* "<file> <block> -> <file> <block> <type> <ref>:<line>", then a line
* "other tf edges <n>": USAGE / CALLS edges leaving a .tf file that the
@@ -7427,6 +7521,7 @@ SUITE(grammar_probe_f) {
RUN_TEST(probe_shell_programs_by_path_spawn);
RUN_TEST(probe_make_programs_by_path_spawn);
RUN_TEST(probe_ci_and_scripts_run_scripts);
RUN_TEST(probe_github_actions_local_uses_reference_their_files);
RUN_TEST(probe_make_targets_spawn_their_rules);
RUN_TEST(probe_make_rules_have_a_node_per_target);
RUN_TEST(probe_helmrelease_chartref_deploys_its_oci_chart);
+81
View File
@@ -5565,6 +5565,85 @@ TEST(incremental_terraform_file_reference_follows_its_file) {
PASS();
}
/* A workflow's local action whose action file appears after the workflow was
* indexed. */
TEST(incremental_local_uses_reference_follows_its_file) {
int counts[4] = {-1, -1, -1, -1};
int rc = incremental_file_reference_steps(".github/workflows/ci.yml",
"name: ci\n"
"on: [push]\n"
"jobs:\n"
" build:\n"
" runs-on: ubuntu-latest\n"
" steps:\n"
" - uses: ./.github/actions/late\n",
NULL, NULL, ".github/actions/late/action.yml",
"uses", counts);
ASSERT_EQ(rc, 0);
ASSERT_EQ(counts[0], 0);
ASSERT_EQ(counts[1], 1);
ASSERT_EQ(counts[2], 1);
ASSERT_EQ(counts[3], 0);
PASS();
}
/* A local action is named by its directory, and the edge goes to the one
* action file it holds. With both spellings there is none; deleting one of
* the two makes the other the action, although no workflow changed. The
* repair reads no workflow again, so that deletion takes the full route. */
TEST(incremental_local_action_pair_deleted_takes_the_full_route) {
char tmp[256];
snprintf(tmp, sizeof(tmp), "/tmp/cbm_actpair_XXXXXX");
if (!cbm_mkdtemp(tmp)) {
FAIL("tmpdir");
}
write_temp_file(tmp, ".github/workflows/ci.yml", "name: ci\n"
"on: [push]\n"
"jobs:\n"
" build:\n"
" runs-on: ubuntu-latest\n"
" steps:\n"
" - uses: ./.github/actions/both\n");
static const char action[] = "name: both\nruns:\n using: composite\n steps: []\n";
write_temp_file(tmp, ".github/actions/both/action.yml", action);
write_temp_file(tmp, ".github/actions/both/action.yaml", action);
write_temp_file(tmp, "hack/x.sh", "#!/bin/sh\necho x\n");
char db_path[512];
snprintf(db_path, sizeof(db_path), "%s/actpair.db", tmp);
static const char uses[] = "SELECT COUNT(*) FROM edges WHERE type='REFERENCES_FILE' AND "
"json_extract(properties,'$.via')='uses'";
static const char to_yml[] =
"SELECT COUNT(*) FROM edges e JOIN nodes t ON t.id=e.target_id WHERE "
"e.type='REFERENCES_FILE' AND t.file_path='.github/actions/both/action.yml'";
int rc_full = k8s_inc_index(tmp, db_path);
int uses_full = k8s_inc_count(db_path, uses);
write_temp_file(tmp, "hack/x.sh", "#!/bin/sh\necho x\necho edited\n");
int rc_other = k8s_inc_index(tmp, db_path);
cbm_incremental_route_t other_route = cbm_pipeline_incremental_test_last_route();
int uses_other = k8s_inc_count(db_path, uses);
char path[512];
snprintf(path, sizeof(path), "%s/.github/actions/both/action.yaml", tmp);
(void)remove(path);
int rc_pair = k8s_inc_index(tmp, db_path);
cbm_incremental_route_t pair_route = cbm_pipeline_incremental_test_last_route();
int uses_pair = k8s_inc_count(db_path, uses);
int yml_pair = k8s_inc_count(db_path, to_yml);
th_rmtree(tmp);
ASSERT_EQ(rc_full, 0);
ASSERT_EQ(uses_full, 0); /* two action files: which one GitHub reads is not ours to say */
ASSERT_EQ(rc_other, 0);
ASSERT_EQ(uses_other, 0);
ASSERT_EQ(other_route, CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR);
ASSERT_EQ(rc_pair, 0);
ASSERT_EQ(uses_pair, 1); /* action.yml is the action now */
ASSERT_EQ(yml_pair, 1);
ASSERT_EQ(pair_route, CBM_INCREMENTAL_ROUTE_FORCED_FULL);
PASS();
}
/* Deleting the only script requesting a URL leaves no Route behind -- a full
* index never makes one without an edge; the Route a kept script requests
* stays. `legacy`: through the full-load route, else closure repair. Returns
@@ -37106,6 +37185,8 @@ SUITE(pipeline_semantic_manifest_repro) {
RUN_TEST(incremental_docker_copy_reference_follows_its_file);
RUN_TEST(incremental_compose_file_reference_follows_its_file);
RUN_TEST(incremental_terraform_file_reference_follows_its_file);
RUN_TEST(incremental_local_uses_reference_follows_its_file);
RUN_TEST(incremental_local_action_pair_deleted_takes_the_full_route);
RUN_TEST(incremental_script_request_to_compose_service_keeps_host);
RUN_TEST(incremental_deleted_dockerfile_drops_its_image);
RUN_TEST(incremental_dockerfile_edit_keeps_build_edges);