diff --git a/src/research/baseline/workflow-parser.ts b/src/research/baseline/workflow-parser.ts index 5e634dd0..5404114d 100644 --- a/src/research/baseline/workflow-parser.ts +++ b/src/research/baseline/workflow-parser.ts @@ -51,12 +51,13 @@ function parseWorkflow(absolutePath: string, relativePath: string, scripts: Reco const jobs = (doc as Record).jobs as Record | undefined; if (!jobs || typeof jobs !== "object") return { path: relativePath, name, tasks }; + const filters = workflowPathFilters((doc as Record).on); for (const [jobId, jobValue] of Object.entries(jobs)) { if (!jobValue || typeof jobValue !== "object") continue; const job = jobValue as Record; const namePart = typeof job.name === "string" ? job.name : jobId; - const paths = normalizeStringArray(job["paths"]) ?? normalizeStringArray((job.on as Record)?.paths) ?? []; - const pathsIgnore = normalizeStringArray(job["paths-ignore"]) ?? []; + const paths = filters.paths ?? []; + const pathsIgnore = filters.pathsIgnore ?? []; const dependsOn = normalizeStringArray(job.needs) ?? []; const stepNames: string[] = []; const runLines: string[] = []; @@ -83,6 +84,45 @@ function parseWorkflow(absolutePath: string, relativePath: string, scripts: Reco return { path: relativePath, name, tasks }; } +/** The triggers whose runs a change's paths decide. */ +const PATH_FILTERED_EVENTS = ["push", "pull_request", "pull_request_target"]; +/** Characters every glob matcher here reads the way GitHub does; anything else keeps the defaults. */ +const PLAIN_GLOB = /^[A-Za-z0-9_.@/*{},-]+$/; + +/** + * The workflow-level `on..paths` / `paths-ignore` filters (#177). GitHub has no job-level `paths`, + * so the job is gated by the workflow's triggers. A filter is reported only when it bounds every + * push/pull_request trigger the workflow has: a trigger without one runs the workflow for any change. + * Negated or non-plain patterns are left out, so they can never be read more narrowly than GitHub reads them. + */ +function workflowPathFilters(on: unknown): { paths?: string[]; pathsIgnore?: string[] } { + if (!on || typeof on !== "object" || Array.isArray(on)) return {}; + const triggers = PATH_FILTERED_EVENTS.filter((event) => event in on).map((event) => (on as Record)[event]); + if (triggers.length === 0) return {}; + const collect = (key: "paths" | "paths-ignore"): string[] | undefined => { + const lists = triggers.map((trigger) => trigger && typeof trigger === "object" && !Array.isArray(trigger) ? normalizeStringArray((trigger as Record)[key]) : undefined); + if (lists.some((list) => !list || list.length === 0)) return undefined; + const patterns = [...new Set(lists.flat() as string[])]; + return patterns.every((pattern) => PLAIN_GLOB.test(pattern) && !hasPartialGlobstar(pattern)) ? patterns : undefined; + }; + return { paths: collect("paths"), pathsIgnore: collect("paths-ignore") }; +} + +/** + * Whether `pattern` has a `**` the planner's matcher would read more narrowly than GitHub: one that is not a + * whole path segment between slashes (`**.js`, `src**`, `{**,x}`), or a bare `**`. GitHub lets those cross + * directories, but the planner reads them as `*`, so a filter of `'**'` would only match top-level files. + */ +function hasPartialGlobstar(pattern: string): boolean { + if (pattern === "**") return true; + for (const match of pattern.matchAll(/\*{2,}/g)) { + const before = pattern[match.index - 1]; + const after = pattern[match.index + match[0].length]; + if (match[0].length !== 2 || (before !== undefined && before !== "/") || (after !== undefined && after !== "/")) return true; + } + return false; +} + function normalizeStringArray(value: unknown): string[] | undefined { if (Array.isArray(value)) return value.filter((v): v is string => typeof v === "string"); if (typeof value === "string") return [value]; diff --git a/tests/research/baseline/workflow-parser.test.ts b/tests/research/baseline/workflow-parser.test.ts index 5aa3033c..01c8b326 100644 --- a/tests/research/baseline/workflow-parser.test.ts +++ b/tests/research/baseline/workflow-parser.test.ts @@ -5,6 +5,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, beforeEach, describe, it } from "node:test"; import { parseRepositoryWorkflows } from "../../../src/research/baseline/workflow-parser.js"; +import { buildGenericTaskRegistry } from "../../../src/research/baseline/registry.js"; import type { RepositoryProfile } from "../../../src/repo/types.js"; // Regression guard: src/research/baseline/workflow-parser.ts imports the "yaml" package, but it was @@ -119,4 +120,48 @@ describe("parseRepositoryWorkflows (yaml dependency regression)", () => { assert.equal(parsed.length, 1); assert.deepEqual(parsed[0]!.tasks, []); }); + + // #177: path filters live under the workflow's `on:` triggers; GitHub has no job-level `paths`. + const filtered = (on: string): ReturnType[number]["tasks"][number] => { + mkdirSync(join(dir, ".github/workflows"), { recursive: true }); + writeFileSync(join(dir, ".github/workflows/api.yml"), `${on}\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: npm test\n`); + return parseRepositoryWorkflows(repoProfile(".github/workflows/api.yml"), dir)[0]!.tasks[0]!; + }; + + it("reads the workflow's on.pull_request.paths filter", () => { + assert.deepEqual(filtered("on:\n pull_request:\n paths: ['api/**']").pathGlobs, ["api/**"]); + }); + + it("unions the filters when every push and pull_request trigger has one", () => { + const task = filtered("on:\n push:\n paths: ['api/**']\n pull_request:\n paths: ['api/**', 'shared/*.ts']\n workflow_dispatch:"); + assert.deepEqual(task.pathGlobs, ["api/**", "shared/*.ts"]); + }); + + it("reads paths-ignore the same way", () => { + const task = filtered("on:\n pull_request:\n paths-ignore: ['docs/**']"); + assert.equal(task.pathGlobs, undefined); + assert.deepEqual(task.pathIgnoreGlobs, ["docs/**"]); + }); + + it("keeps no filter when a trigger runs for any path, or a pattern is negated or not a plain glob", () => { + assert.equal(filtered("on:\n push:\n pull_request:\n paths: ['api/**']").pathGlobs, undefined); + assert.equal(filtered("on: [push, pull_request]").pathGlobs, undefined); + assert.equal(filtered("on: push").pathGlobs, undefined); + assert.equal(filtered("on:\n pull_request:\n paths: ['api/**', '!api/docs/**']").pathGlobs, undefined); + assert.equal(filtered("on:\n pull_request:\n paths: ['app/(shop)/**']").pathGlobs, undefined); + assert.equal(filtered("on:\n workflow_dispatch:").pathGlobs, undefined); + }); + + it("keeps no filter when a ** is not a whole path segment, which the planner would read as *", () => { + for (const pattern of ["'**'", "'**.js'", "'src**'", "'{**,docs/*}'", "'a/***'"]) { + assert.equal(filtered(`on:\n pull_request:\n paths: [${pattern}]`).pathGlobs, undefined, pattern); + } + assert.deepEqual(filtered("on:\n pull_request:\n paths: ['**/*.ts', 'api/**', 'a/**/b', '{api,web}/**']").pathGlobs, ["**/*.ts", "api/**", "a/**/b", "{api,web}/**"]); + }); + + it("gives the registry task the workflow's filter as its inputs", () => { + filtered("on:\n pull_request:\n paths: ['api/**']"); + const registry = buildGenericTaskRegistry(repoProfile(".github/workflows/api.yml"), "typescript", parseRepositoryWorkflows(repoProfile(".github/workflows/api.yml"), dir)); + assert.deepEqual(registry.all().find((task) => task.id === ".github/workflows/api::test")!.inputPatterns, ["api/**"]); + }); });