From 6cfea42772118fce3872ba549bf19ba06d95c658 Mon Sep 17 00:00:00 2001 From: Jean-Paul van Ravensberg <14926452+DevSecNinja@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:15:26 +0100 Subject: [PATCH 1/4] style(python): format and lint this repository's Python with ruff Pin ruff in .mise.toml, add a ruff.toml (120 columns, Python 3.8 target for the ai-commits-badge action) and apply ruff format plus the ruff check fixes, so the ruff linter added to lint.yml next passes here. Refs #539 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .mise.toml | 1 + actions/ai-commits-badge/ai_commits_badge.py | 81 +++++++--- ruff.toml | 8 + .../config-sync/renovate-extends-preset.py | 9 +- scripts/lint-inline-scripts.py | 31 ++-- tests/ai-commits-badge.py | 74 ++++++--- tests/app-key-confinement.py | 50 ++++-- tests/issue-labeler-config.py | 10 +- tests/lint-include.py | 32 ++-- tests/lint-inline-scripts.py | 6 +- tests/pr-labeler-config.py | 8 +- tests/workflow-job-conditions.py | 151 +++++++++++++----- tests/workflow-script-fetch.py | 14 +- 13 files changed, 324 insertions(+), 151 deletions(-) create mode 100644 ruff.toml diff --git a/.mise.toml b/.mise.toml index 409e423e..98df7a9c 100644 --- a/.mise.toml +++ b/.mise.toml @@ -10,6 +10,7 @@ git-cliff = "2.14.2" "pipx:checkov" = "3.3.30" "pipx:json5" = "0.17.3" "pipx:yamllint" = "1.38.0" +ruff = "0.17.5" shellcheck = "0.11.0" shfmt = "3.14.1" trivy = "0.76.1" diff --git a/actions/ai-commits-badge/ai_commits_badge.py b/actions/ai-commits-badge/ai_commits_badge.py index 451e616b..ac094de3 100644 --- a/actions/ai-commits-badge/ai_commits_badge.py +++ b/actions/ai-commits-badge/ai_commits_badge.py @@ -5,6 +5,7 @@ Outputs in --out: ai-commits.svg, ai-commits.json (Shields endpoint), ai-commits-stats.json and ai-commits-card.svg (auto light/dark). """ + import argparse import json import re @@ -34,8 +35,21 @@ ] PURPLE, GREY = "#8250df", "#8c959f" SPARKLE = "M5 0 6.2 3.8 10 5 6.2 6.2 5 10 3.8 6.2 0 5 3.8 3.8Z" -VERDANA = {" ": 3.9, "%": 11.9, "/": 4.7, "I": 4.6, "A": 7.5, "i": 3.1, "m": 10.7, "t": 4.3, - "s": 5.7, "c": 5.7, "o": 6.7, "n": 7.0, "a": 6.6} +VERDANA = { + " ": 3.9, + "%": 11.9, + "/": 4.7, + "I": 4.6, + "A": 7.5, + "i": 3.1, + "m": 10.7, + "t": 4.3, + "s": 5.7, + "c": 5.7, + "o": 6.7, + "n": 7.0, + "a": 6.6, +} def text_width(s): @@ -57,8 +71,11 @@ def ai_tool(identity): def git(repo_dir, *args): return subprocess.run( - ["git", "-C", str(repo_dir), *args], check=True, - capture_output=True, text=True, encoding="utf-8", + ["git", "-C", str(repo_dir), *args], + check=True, + capture_output=True, + text=True, + encoding="utf-8", ).stdout @@ -114,10 +131,12 @@ def tooltip(st, name): return f"{name}: no commits to measure" window = f" since {st['since']}" if st["since"] else "" by_tool = ", ".join(f"{t} {n}" for t, n in st["tools"].items()) or "none" - return (f"{name}: {st['ai_commits']:,} of {st['commits']:,} commits{window} ({message(st)}) involved AI: " - f"{st['agent']:,} by an AI agent, {st['coauthored']:,} co-authored with AI, " - f"{st['unattributed']:,} {NO_ATTRIBUTION}. " - f"By tool: {by_tool}. Excludes merges and {st['excluded_automation']:,} bot-automation commits.") + return ( + f"{name}: {st['ai_commits']:,} of {st['commits']:,} commits{window} ({message(st)}) involved AI: " + f"{st['agent']:,} by an AI agent, {st['coauthored']:,} co-authored with AI, " + f"{st['unattributed']:,} {NO_ATTRIBUTION}. " + f"By tool: {by_tool}. Excludes merges and {st['excluded_automation']:,} bot-automation commits." + ) def badge_svg(st, name, label="AI commits"): @@ -135,10 +154,12 @@ def badge_svg(st, name, label="AI commits"): def txt(cx, t): tl = text_width(t) * 10 - return (f'{escape(t)}' - f'{escape(t)}') + return ( + f'{escape(t)}' + f'{escape(t)}' + ) return ( f'' @@ -202,9 +223,11 @@ def card_svg(st, name, theme="auto"): x += w share = f"{100 * st[key] / n:.0f}%" if n else "0%" lx = 24 + i * 144 - legend.append(f'' - f'{legend_label(key, share)} ' - f'{share}') + legend.append( + f'' + f'{legend_label(key, share)} ' + f'{share}' + ) title = escape(tooltip(st, name)) return ( f'Who wrote the commits?' '' f'' - f'{escape(shown_name)}' + f"{escape(shown_name)}" f'' f'{escape(message(st))}{escape(sub)}' '' f'' - f'{"".join(bar)}{"".join(legend)}' + f"{''.join(bar)}{''.join(legend)}" f'{escape(tools_line(st["tools"]))}' 'Commit authors + Co-authored-by trailers · bots excluded' f'{st["generated_at"][:10]}' @@ -229,8 +252,13 @@ def card_svg(st, name, theme="auto"): def endpoint_json(st, label="AI commits"): logo = f'' - return {"schemaVersion": 1, "label": label, "message": message(st), - "color": PURPLE.lstrip("#") if st["commits"] else "lightgrey", "logoSvg": logo} + return { + "schemaVersion": 1, + "label": label, + "message": message(st), + "color": PURPLE.lstrip("#") if st["commits"] else "lightgrey", + "logoSvg": logo, + } def main(): @@ -241,8 +269,9 @@ def main(): ap.add_argument("--name", default="this repository", help="owner/repo shown in the tooltip") ap.add_argument("--label", default="AI commits") ap.add_argument("--out", default="badges") - ap.add_argument("--themes", action="store_true", - help="also write ai-commits-card-light.svg / -dark.svg for tags") + ap.add_argument( + "--themes", action="store_true", help="also write ai-commits-card-light.svg / -dark.svg for tags" + ) ap.add_argument("--github-output", help="append numeric step outputs to this file") args = ap.parse_args() try: @@ -265,7 +294,15 @@ def main(): (out / filename).write_bytes((content + "\n").encode("utf-8")) if args.github_output: with Path(args.github_output).open("a", encoding="utf-8", newline="\n") as output: - for key in ("ai_percent", "commits", "ai_commits", "agent", "coauthored", "unattributed", "excluded_automation"): + for key in ( + "ai_percent", + "commits", + "ai_commits", + "agent", + "coauthored", + "unattributed", + "excluded_automation", + ): value = "n/a" if st[key] is None else st[key] output.write(f"{key.replace('_', '-')}={value}\n") print(json.dumps(st, indent=2)) diff --git a/ruff.toml b/ruff.toml new file mode 100644 index 00000000..6328e23f --- /dev/null +++ b/ruff.toml @@ -0,0 +1,8 @@ +# ruff config for this repository's Python scripts and tests, which the Lint +# workflow (lint-ruff) checks. config-sync/templates/ruff.toml is the starting +# point for other repositories. +line-length = 120 +# The ai-commits-badge action supports Python 3.8. +target-version = "py38" + +extend-exclude = [".agents"] diff --git a/scripts/config-sync/renovate-extends-preset.py b/scripts/config-sync/renovate-extends-preset.py index 19a9390d..103514e5 100644 --- a/scripts/config-sync/renovate-extends-preset.py +++ b/scripts/config-sync/renovate-extends-preset.py @@ -9,8 +9,9 @@ """ import sys +from pathlib import Path -text = open("renovate.json5", encoding="utf-8").read() +text = Path("renovate.json5").read_text(encoding="utf-8") tokens, i, n = [], 0, len(text) while i < n: c = text[i] @@ -27,7 +28,7 @@ while j < n and text[j] != c: if text[j] == "\\": j += 1 - value.append(text[j:j + 1]) + value.append(text[j : j + 1]) j += 1 tokens.append(("str", "".join(value))) i = j + 1 @@ -49,9 +50,9 @@ depth += 1 elif kind in ("}", "]"): depth -= 1 - elif depth == 1 and value == "extends" and [t[0] for t in tokens[k + 1:k + 3]] == [":", "["]: + elif depth == 1 and value == "extends" and [t[0] for t in tokens[k + 1 : k + 3]] == [":", "["]: extends, nested = [], 0 - for kind2, value2 in tokens[k + 3:]: + for kind2, value2 in tokens[k + 3 :]: if kind2 in ("{", "["): nested += 1 elif kind2 in ("}", "]"): diff --git a/scripts/lint-inline-scripts.py b/scripts/lint-inline-scripts.py index bd7bceed..864b1e9a 100755 --- a/scripts/lint-inline-scripts.py +++ b/scripts/lint-inline-scripts.py @@ -92,9 +92,8 @@ def scan_script(script: str) -> tuple[set[str], list[str]]: body.pop(0) continue words = line.split() - if quote is None and not braces and words: - if words[0] in CONTROL_WORDS or words[0][0] in "({": - found.add(CONTROL) + if quote is None and not braces and words and (words[0] in CONTROL_WORDS or words[0][0] in "({"): + found.add(CONTROL) i, n = 0, len(line) while i < n: c = line[i] @@ -172,9 +171,8 @@ def run_problems(script: str, shell: str | None, found: set[str]) -> list[str]: ordered = [reason for reason in REASON_ORDER if reason in problems] words = (shell or "").split() # A shell given by an expression can't be resolved statically. - if words and "${{" not in shell: - if words[0].rsplit("/", 1)[-1] not in BASH_SHELLS: - ordered.append(f"inline code for shell '{words[0]}'") + if words and "${{" not in shell and words[0].rsplit("/", 1)[-1] not in BASH_SHELLS: + ordered.append(f"inline code for shell '{words[0]}'") return ordered @@ -235,10 +233,13 @@ def step_comments(lines: list[str], step: yaml.MappingNode, shell_comments: list while k < len(lines) and (k == start or not lines[k].strip() or indent(lines[k]) > base): line = lines[k] for col, c in enumerate(line): - if c == "#" and (col == 0 or line[col - 1] in " \t"): - if not any(begin <= (k, col) < end for begin, end in spans): - comments.append(line[col:]) - break + if ( + c == "#" + and (col == 0 or line[col - 1] in " \t") + and not any(begin <= (k, col) < end for begin, end in spans) + ): + comments.append(line[col:]) + break k += 1 return comments + shell_comments @@ -369,17 +370,11 @@ def discover(root: Path) -> list[tuple[Path, bool]]: workflows = root / ".github" / "workflows" if workflows.is_dir(): found += [ - (path, False) - for path in sorted(workflows.iterdir()) - if path.is_file() and path.suffix in (".yml", ".yaml") + (path, False) for path in sorted(workflows.iterdir()) if path.is_file() and path.suffix in (".yml", ".yaml") ] for directory, dirnames, filenames in os.walk(root): dirnames[:] = sorted(name for name in dirnames if name not in SKIP_DIRS) - found += [ - (Path(directory) / name, True) - for name in sorted(filenames) - if name in ("action.yml", "action.yaml") - ] + found += [(Path(directory) / name, True) for name in sorted(filenames) if name in ("action.yml", "action.yaml")] return found diff --git a/tests/ai-commits-badge.py b/tests/ai-commits-badge.py index 2520ec21..88b6dee3 100644 --- a/tests/ai-commits-badge.py +++ b/tests/ai-commits-badge.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 """Offline contract tests using synthetic history and the actual composite step.""" + import importlib.util import json import os @@ -39,12 +40,14 @@ def setUp(self): self.commit("Human", "human@example.org", "Initial") self.commit("copilot-swe-agent[bot]", "agent@example.org", "Agent") self.commit( - "Human", "human@example.org", + "Human", + "human@example.org", "Pair\n\nCo-authored-by: Someone \n" "Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>", ) self.commit( - "renovate[bot]", "bot@example.org", + "renovate[bot]", + "bot@example.org", "Automation\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>", ) side = self.commit("Human", "human@example.org", "Side") @@ -52,14 +55,23 @@ def setUp(self): def git(self, *args, cwd=None, input=None, env=None): return subprocess.run( - ["git", *args], cwd=cwd or self.repo, env=env or self.env, - input=input, capture_output=True, text=True, encoding="utf-8", check=True, + ["git", *args], + cwd=cwd or self.repo, + env=env or self.env, + input=input, + capture_output=True, + text=True, + encoding="utf-8", + check=True, ) def commit(self, name, email, message, parents=None): env = dict( - self.env, GIT_AUTHOR_NAME=name, GIT_AUTHOR_EMAIL=email, - GIT_COMMITTER_NAME=name, GIT_COMMITTER_EMAIL=email, + self.env, + GIT_AUTHOR_NAME=name, + GIT_AUTHOR_EMAIL=email, + GIT_COMMITTER_NAME=name, + GIT_COMMITTER_EMAIL=email, GIT_AUTHOR_DATE="2020-01-01T12:00:00+00:00", GIT_COMMITTER_DATE="2020-01-01T12:00:00+00:00", ) @@ -76,12 +88,17 @@ def commit(self, name, email, message, parents=None): def run_action(self, **overrides): text = (ACTION / "action.yml").read_text(encoding="utf-8") inputs = { - "ref": "HEAD", "since": "", "name": "owner/repo", "label": "AI commits", - "out": str(self.work / "out"), "themes": "false", "repo-dir": str(self.repo), + "ref": "HEAD", + "since": "", + "name": "owner/repo", + "label": "AI commits", + "out": str(self.work / "out"), + "themes": "false", + "repo-dir": str(self.repo), } inputs.update(overrides) env = dict(self.env, GITHUB_OUTPUT=str(self.work / "outputs")) - mappings = re.findall(r"^ ([A-Z_]+): \$\{\{ inputs\.([\w-]+) \}\}$", text, re.M) + mappings = re.findall(r"^ ([A-Z_]+): \$\{\{ inputs\.([\w-]+) \}\}$", text, re.MULTILINE) self.assertEqual({name for _, name in mappings}, set(inputs)) for variable, name in mappings: env[variable] = inputs[name] @@ -89,10 +106,15 @@ def run_action(self, **overrides): generate = shlex.quote(str(ACTION / "generate.sh").replace("\\", "/")) # Windows's python3 app alias is not necessarily the interpreter running this test. interpreter = shlex.quote(sys.executable.replace("\\", "/")) - script = f"python3() {{ {interpreter} \"$@\"; }}\nexport -f python3\nbash {generate}\n" + script = f'python3() {{ {interpreter} "$@"; }}\nexport -f python3\nbash {generate}\n' return subprocess.run( [shutil.which("bash"), "--noprofile", "--norc", "-c", script], - env=env, cwd=self.work, text=True, capture_output=True, encoding="utf-8", + check=False, + env=env, + cwd=self.work, + text=True, + capture_output=True, + encoding="utf-8", ) def stats(self): @@ -103,17 +125,29 @@ def test_classification_files_and_step_outputs(self): self.assertEqual(result.returncode, 0, result.stderr) st = self.stats() expected = { - "commits": 4, "ai_commits": 2, "ai_percent": 50.0, - "agent": 1, "coauthored": 1, "unattributed": 2, "excluded_automation": 1, + "commits": 4, + "ai_commits": 2, + "ai_percent": 50.0, + "agent": 1, + "coauthored": 1, + "unattributed": 2, + "excluded_automation": 1, } for key, value in expected.items(): self.assertEqual(st[key], value, key) self.assertEqual(st["tools"], {"Copilot coding agent": 1, "Copilot CLI": 1}) out = self.work / "out" - self.assertEqual({p.name for p in out.iterdir()}, { - "ai-commits.svg", "ai-commits.json", "ai-commits-stats.json", - "ai-commits-card.svg", "ai-commits-card-light.svg", "ai-commits-card-dark.svg", - }) + self.assertEqual( + {p.name for p in out.iterdir()}, + { + "ai-commits.svg", + "ai-commits.json", + "ai-commits-stats.json", + "ai-commits-card.svg", + "ai-commits-card-light.svg", + "ai-commits-card-dark.svg", + }, + ) endpoint = json.loads((out / "ai-commits.json").read_text(encoding="utf-8")) self.assertEqual(endpoint["schemaVersion"], 1) self.assertEqual(endpoint["message"], "50%") @@ -167,7 +201,8 @@ def test_central_identities_recognize_agent_bots_and_unknown_authors(self): def test_invalid_inputs_fail_before_writing(self): for overrides in ( {"themes": "yes"}, - {"ref": "--all"}, {"ref": "missing"}, + {"ref": "--all"}, + {"ref": "missing"}, ): with self.subTest(overrides=overrides): result = self.run_action(**overrides) @@ -187,7 +222,8 @@ def test_partial_identities_do_not_count_as_ai(self): self.commit("Human", "alice+223556219+Copilot@example.org", "Not Copilot") self.commit("not-copilot-swe-agent", "human@example.org", "Not the agent") self.commit( - "Human", "human@example.org", + "Human", + "human@example.org", "Unrecognized trailers\n\n" "Co-authored-by: Human \n" "Co-authored-by: Human ", diff --git a/tests/app-key-confinement.py b/tests/app-key-confinement.py index b82cd6ed..8ffee803 100644 --- a/tests/app-key-confinement.py +++ b/tests/app-key-confinement.py @@ -46,7 +46,7 @@ # Contexts and secret names are case-insensitive in GitHub expressions. KEY_NAMES = {"automation_app_private_key", "app-private-key", "release_please_app_private_key"} # A string literal, or the `secrets` context outside one. -SECRETS_TOKEN = re.compile(r"'(?:[^']|'')*'|(? list[str]: elif not quoted and text.startswith("}}", pos): break pos += 1 - expressions.append(text[start + 3:pos]) + expressions.append(text[start + 3 : pos]) start = text.find("${{", pos + 2) return expressions @@ -133,7 +145,7 @@ def secret_reads(path: tuple, text: str) -> tuple[bool, bool]: for match in SECRETS_TOKEN.finditer(expression): if not match.group(1): continue - rest = expression[match.end():] + rest = expression[match.end() :] access = SECRET_DOT.match(rest) or SECRET_INDEX.match(rest) if access is None: dump = True @@ -190,16 +202,20 @@ def emptiness_problem(doc: dict, job: dict, step: dict, index: int, var: str, di return "the allowlisted emptiness check inherits an env variable with a computed name" for earlier in job["steps"][:index]: if not SAFE_PRECEDING_STEP.match(str(earlier.get("uses") or "")) or set(earlier) - {"name", "uses", "with"}: - return (f"step {earlier.get('name') or earlier.get('uses') or 'run'!r} runs before the allowlisted " - f"emptiness check and could change its shell through $GITHUB_ENV") + return ( + f"step {earlier.get('name') or earlier.get('uses') or 'run'!r} runs before the allowlisted " + f"emptiness check and could change its shell through $GITHUB_ENV" + ) run = step.get("run") or "" for line in run.splitlines(): if re.search(rf"(? int: for failure in failures: print(f" - {failure}", file=sys.stderr) return 1 - print(f"✓ App key confinement: {len(files)} workflows keep the key in the token step " - f"({len(token_workflows)} token steps, {len(used_entries)} emptiness checks); " - f"{len(cases)} fixture cases behave.") + print( + f"✓ App key confinement: {len(files)} workflows keep the key in the token step " + f"({len(token_workflows)} token steps, {len(used_entries)} emptiness checks); " + f"{len(cases)} fixture cases behave." + ) return 0 diff --git a/tests/issue-labeler-config.py b/tests/issue-labeler-config.py index 01dbfb34..17b24343 100644 --- a/tests/issue-labeler-config.py +++ b/tests/issue-labeler-config.py @@ -93,10 +93,7 @@ def load_rules() -> dict[str, tuple[str, str]]: if isinstance(entries, str): entries = [entries] if not isinstance(entries, list) or len(entries) != 1: - fail( - f"label {label!r} must have exactly one pattern: " - "github/issue-labeler ANDs the entries under a label" - ) + fail(f"label {label!r} must have exactly one pattern: github/issue-labeler ANDs the entries under a label") entry = entries[0] if not isinstance(entry, str): fail(f"label {label!r}: pattern must be a string") @@ -105,10 +102,7 @@ def load_rules() -> dict[str, tuple[str, str]]: if not set(flags) <= JS_FLAGS: fail(f"label {label!r}: unsupported RegExp flags {flags!r}") if INLINE_FLAG_GROUP.search(pattern): - fail( - f"label {label!r}: inline flags are invalid in JavaScript; " - f"use '/pattern/i' instead of {entry!r}" - ) + fail(f"label {label!r}: inline flags are invalid in JavaScript; use '/pattern/i' instead of {entry!r}") rules[label] = (pattern, flags) return rules diff --git a/tests/lint-include.py b/tests/lint-include.py index 735c70a8..287efc3e 100644 --- a/tests/lint-include.py +++ b/tests/lint-include.py @@ -113,10 +113,16 @@ def mutate(description, change, target="include"): def job(workflow): return next(iter(workflow["jobs"].values())) - yield mutate("a new lint.yml linter", - lambda w: call_inputs(w).update({"lint-newtool": {"type": "boolean", "default": True}}), "lint") - yield mutate("a new lint.yml option", - lambda w: call_inputs(w).update({"lint-newtool-args": {"type": "string", "default": ""}}), "lint") + yield mutate( + "a new lint.yml linter", + lambda w: call_inputs(w).update({"lint-newtool": {"type": "boolean", "default": True}}), + "lint", + ) + yield mutate( + "a new lint.yml option", + lambda w: call_inputs(w).update({"lint-newtool-args": {"type": "string", "default": ""}}), + "lint", + ) yield mutate("a linter defaulting to true", lambda w: call_inputs(w)["lint-trivy"].update({"default": True})) yield mutate("a linter without a default", lambda w: call_inputs(w)["lint-trivy"].pop("default")) yield mutate("an option default that drifted", lambda w: call_inputs(w)["upload-sarif"].update({"default": False})) @@ -124,13 +130,17 @@ def job(workflow): yield mutate("a retyped input", lambda w: call_inputs(w)["lint-job-mode"].update({"type": "boolean"})) yield mutate("an input not passed through", lambda w: job(w)["with"].pop("lint-shfmt-exclude")) yield mutate("a hard-coded value", lambda w: job(w)["with"].update({"lint-zizmor": False})) - yield mutate("a value from another input", - lambda w: job(w)["with"].update({"lint-trivy": "${{ inputs.lint-checkov }}"})) + yield mutate( + "a value from another input", lambda w: job(w)["with"].update({"lint-trivy": "${{ inputs.lint-checkov }}"}) + ) yield mutate("a workspace-relative call", lambda w: job(w).update({"uses": "./.github/workflows/lint.yml"})) yield mutate("a missing permission", lambda w: job(w)["permissions"].pop("security-events")) yield mutate("a downgraded permission", lambda w: job(w)["permissions"].update({"security-events": "read"})) - yield mutate("a permission lint.yml starts to need", - lambda w: w["jobs"]["trivy"]["permissions"].update({"actions": "read"}), "lint") + yield mutate( + "a permission lint.yml starts to need", + lambda w: w["jobs"]["trivy"]["permissions"].update({"actions": "read"}), + "lint", + ) def main() -> int: @@ -148,8 +158,10 @@ def main() -> int: print(f" - {failure}", file=sys.stderr) return 1 linters = sum(is_linter(name, spec) for name, spec in call_inputs(lint).items()) - print(f"✓ lint-include.yml forwards all {len(call_inputs(lint))} lint.yml inputs ({linters} linters off by " - f"default); {len(cases)} broken variants are caught.") + print( + f"✓ lint-include.yml forwards all {len(call_inputs(lint))} lint.yml inputs ({linters} linters off by " + f"default); {len(cases)} broken variants are caught." + ) return 0 diff --git a/tests/lint-inline-scripts.py b/tests/lint-inline-scripts.py index 82540b98..9657bf53 100755 --- a/tests/lint-inline-scripts.py +++ b/tests/lint-inline-scripts.py @@ -116,8 +116,10 @@ def main() -> int: print(f"✗ {counts.group(2)} opt-out(s) reported, but {len(rows)} parsed from the step summary") if opt_outs != preflights: failed += 1 - print(f"✗ only the App-key preflights may opt out: unexpected {sorted(opt_outs - preflights)}, " - f"missing {sorted(preflights - opt_outs)}") + print( + f"✗ only the App-key preflights may opt out: unexpected {sorted(opt_outs - preflights)}, " + f"missing {sorted(preflights - opt_outs)}" + ) if failed: print(f"{failed} lint-inline-scripts case(s) failed.") return 1 diff --git a/tests/pr-labeler-config.py b/tests/pr-labeler-config.py index a02fbf23..42b8c928 100644 --- a/tests/pr-labeler-config.py +++ b/tests/pr-labeler-config.py @@ -93,11 +93,7 @@ def expand_braces(glob: str) -> list[str]: if not match: return [glob] head, tail = glob[: match.start()], glob[match.end() :] - return [ - expanded - for option in match.group(1).split(",") - for expanded in expand_braces(head + option + tail) - ] + return [expanded for option in match.group(1).split(",") for expanded in expand_braces(head + option + tail)] def segment_regex(segment: str) -> str: @@ -154,7 +150,7 @@ def load_rules() -> dict[str, list[tuple[str, list[str]]]]: for item in entry["changed-files"]: if not isinstance(item, dict) or len(item) != 1: fail(f"label {label!r}: each `changed-files` item needs one operator") - (operator, globs), = item.items() + ((operator, globs),) = item.items() if operator not in OPERATORS: fail(f"label {label!r}: unknown operator {operator!r}") globs = globs if isinstance(globs, list) else [globs] diff --git a/tests/workflow-job-conditions.py b/tests/workflow-job-conditions.py index 2274ad58..a604b4fa 100755 --- a/tests/workflow-job-conditions.py +++ b/tests/workflow-job-conditions.py @@ -96,7 +96,7 @@ def parse_jobs(path: Path) -> dict[str, dict]: | (?P==|!=|&&|\|\||!|\(|\)|,) | (?P[A-Za-z_][A-Za-z0-9_.\-]*) )""", - re.X, + re.VERBOSE, ) STATUS_FUNCTIONS = re.compile(r"\b(success|always|cancelled|failure)\(") @@ -108,7 +108,7 @@ def tokenize(text: str) -> list[tuple[str, str]]: while pos < len(text): match = TOKEN.match(text, pos) if not match or match.end() == pos: - raise ValueError(f"cannot tokenize at {text[pos:pos + 30]!r}") + raise ValueError(f"cannot tokenize at {text[pos : pos + 30]!r}") kind = match.lastgroup tokens.append((kind, match.group(kind))) pos = match.end() @@ -309,13 +309,20 @@ def running_jobs(jobs: dict[str, dict], context: dict, outputs: dict | None = No def check(workflow: str, name: str, actual: set[str], expected: set[str]) -> None: if actual != expected: - failures.append( - f"{workflow}: {name}\n expected: {sorted(expected)}\n actual: {sorted(actual)}" - ) - - -def pages_context(event_name, *, action=None, actor="DevSecNinja", author=None, ref="refs/heads/main", - changes=None, fork=False, preview=True): + failures.append(f"{workflow}: {name}\n expected: {sorted(expected)}\n actual: {sorted(actual)}") + + +def pages_context( + event_name, + *, + action=None, + actor="DevSecNinja", + author=None, + ref="refs/heads/main", + changes=None, + fork=False, + preview=True, +): event: dict = {"repository": {"name": "site", "private": True}} if event_name == "pull_request": event.update( @@ -328,15 +335,25 @@ def pages_context(event_name, *, action=None, actor="DevSecNinja", author=None, if changes is not None: event["changes"] = changes return { - "github": {"event_name": event_name, "event": event, "actor": actor, "ref": ref, - "repository": "DevSecNinja/site"}, - "inputs": {"cloudflare-preview": preview, "github-pages": True, "production-branch": "main", - "cloudflare-custom-domain": ""}, + "github": { + "event_name": event_name, + "event": event, + "actor": actor, + "ref": ref, + "repository": "DevSecNinja/site", + }, + "inputs": { + "cloudflare-preview": preview, + "github-pages": True, + "production-branch": "main", + "cloudflare-custom-domain": "", + }, } -PAGES_OUTPUTS = {"detect-cloudflare": {"enabled": "true", "targets": "[]", "deploy-production": "false", - "project-name": "site"}} +PAGES_OUTPUTS = { + "detect-cloudflare": {"enabled": "true", "targets": "[]", "deploy-production": "false", "project-name": "site"} +} PRODUCTION = {"validate-inputs", "detect-cloudflare", "test", "deploy"} PREVIEW = {"validate-inputs", "detect-cloudflare", "test", "deploy-preview"} TEST_ONLY = {"validate-inputs", "test"} @@ -350,34 +367,63 @@ def pages_context(event_name, *, action=None, actor="DevSecNinja", author=None, ("workflow_dispatch on main", pages_context("workflow_dispatch"), PRODUCTION), ("human PR opened", pages_context("pull_request", action="opened"), PREVIEW), ("human PR synchronize", pages_context("pull_request", action="synchronize"), PREVIEW), - ("human PR, previews disabled", pages_context("pull_request", action="opened", preview=False), - {"validate-inputs", "detect-cloudflare", "test"}), - ("fork PR opened", pages_context("pull_request", action="opened", fork=True), - {"validate-inputs", "detect-cloudflare", "test"}), + ( + "human PR, previews disabled", + pages_context("pull_request", action="opened", preview=False), + {"validate-inputs", "detect-cloudflare", "test"}, + ), + ( + "fork PR opened", + pages_context("pull_request", action="opened", fork=True), + {"validate-inputs", "detect-cloudflare", "test"}, + ), ("Renovate PR synchronize", pages_context("pull_request", action="synchronize", actor="renovate[bot]"), TEST_ONLY), - ("Renovate PR updated by a human", pages_context("pull_request", action="synchronize", author="renovate[bot]"), - TEST_ONLY), + ( + "Renovate PR updated by a human", + pages_context("pull_request", action="synchronize", author="renovate[bot]"), + TEST_ONLY, + ), ("Dependabot PR opened", pages_context("pull_request", action="opened", actor="dependabot[bot]"), TEST_ONLY), - ("Renovate edits its PR body", pages_context("pull_request", action="edited", actor="renovate[bot]", - changes={"body": {"from": "old"}}), NOTHING), - ("human edits PR title", pages_context("pull_request", action="edited", changes={"title": {"from": "x"}}), - NOTHING), - ("human changes PR base branch", pages_context("pull_request", action="edited", - changes={"base": {"ref": {"from": "develop"}}}), PREVIEW), + ( + "Renovate edits its PR body", + pages_context("pull_request", action="edited", actor="renovate[bot]", changes={"body": {"from": "old"}}), + NOTHING, + ), + ("human edits PR title", pages_context("pull_request", action="edited", changes={"title": {"from": "x"}}), NOTHING), + ( + "human changes PR base branch", + pages_context("pull_request", action="edited", changes={"base": {"ref": {"from": "develop"}}}), + PREVIEW, + ), ("human PR closed", pages_context("pull_request", action="closed"), CLOSE), ("human PR closed, previews disabled", pages_context("pull_request", action="closed", preview=False), NOTHING), ("fork PR closed", pages_context("pull_request", action="closed", fork=True), NOTHING), ("Renovate PR automerged", pages_context("pull_request", action="closed", actor="renovate[bot]"), NOTHING), - ("Renovate PR merged by a human", pages_context("pull_request", action="closed", author="renovate[bot]"), - NOTHING), + ("Renovate PR merged by a human", pages_context("pull_request", action="closed", author="renovate[bot]"), NOTHING), ] for name, context, expected in pages_scenarios: check("pages.yml", name, running_jobs(pages, context, PAGES_OUTPUTS), expected) def lint_context(*, mode="auto", private=False, has_repository=True, event_name="pull_request", **toggles): - inputs = {f"lint-{name}": True for name in ("dprint", "yamlfmt", "yamllint", "actionlint", "gitleaks", "go", - "shellcheck", "shfmt", "checkov", "trivy", "zizmor", "inline-scripts")} + inputs = { + f"lint-{name}": True + for name in ( + "dprint", + "yamlfmt", + "yamllint", + "actionlint", + "gitleaks", + "go", + "shellcheck", + "shfmt", + "checkov", + "trivy", + "zizmor", + "ruff", + "inline-scripts", + ) + } inputs["lint-config-drift"] = False inputs["lint-job-mode"] = mode inputs.update({f"lint-{k.replace('_', '-')}": v for k, v in toggles.items()}) @@ -386,18 +432,37 @@ def lint_context(*, mode="auto", private=False, has_repository=True, event_name= lint = parse_jobs(WORKFLOWS / "lint.yml") -PER_LINTER = {"dprint", "yamlfmt", "yamllint", "actionlint", "gitleaks", "go", "shellcheck", "shfmt", "checkov", - "trivy", "zizmor", "inline-scripts"} +PER_LINTER = { + "dprint", + "yamlfmt", + "yamllint", + "actionlint", + "gitleaks", + "go", + "shellcheck", + "shfmt", + "checkov", + "trivy", + "zizmor", + "ruff", + "inline-scripts", +} SINGLE = {"all-linters"} lint_scenarios = [ ("public repo, auto", lint_context(), PER_LINTER), ("private repo, auto", lint_context(private=True), SINGLE), - ("schedule event without repository payload, auto", lint_context(has_repository=False, event_name="schedule"), - PER_LINTER), + ( + "schedule event without repository payload, auto", + lint_context(has_repository=False, event_name="schedule"), + PER_LINTER, + ), ("public repo, single", lint_context(mode="single"), SINGLE), ("private repo, parallel", lint_context(mode="parallel", private=True), PER_LINTER), - ("private repo, parallel, go and trivy off", lint_context(mode="parallel", private=True, go=False, trivy=False), - PER_LINTER - {"go", "trivy"}), + ( + "private repo, parallel, go and trivy off", + lint_context(mode="parallel", private=True, go=False, trivy=False), + PER_LINTER - {"go", "trivy"}, + ), ("public repo, config drift on", lint_context(config_drift=True), PER_LINTER | {"config-drift"}), ("public repo, inline scripts off", lint_context(inline_scripts=False), PER_LINTER - {"inline-scripts"}), ("invalid mode fails loudly in the single job", lint_context(mode="bogus"), SINGLE), @@ -411,8 +476,10 @@ def labeler_context(event_name, *, author="DevSecNinja", fork=False): if event_name == "issues": event["issue"] = {"user": {"login": author}} else: - event["pull_request"] = {"user": {"login": author}, - "head": {"repo": {"full_name": "someone/repo" if fork else "DevSecNinja/repo"}}} + event["pull_request"] = { + "user": {"login": author}, + "head": {"repo": {"full_name": "someone/repo" if fork else "DevSecNinja/repo"}}, + } return {"github": {"event_name": event_name, "event": event, "repository": "DevSecNinja/repo"}} @@ -432,5 +499,7 @@ def labeler_context(event_name, *, author="DevSecNinja", fork=False): print(f" - {failure}", file=sys.stderr) sys.exit(1) -print(f"✓ Job conditions: {len(pages_scenarios)} pages.yml, {len(lint_scenarios)} lint.yml and " - f"{len(labeler_scenarios)} labeler.yml scenarios run exactly the expected jobs.") +print( + f"✓ Job conditions: {len(pages_scenarios)} pages.yml, {len(lint_scenarios)} lint.yml and " + f"{len(labeler_scenarios)} labeler.yml scenarios run exactly the expected jobs." +) diff --git a/tests/workflow-script-fetch.py b/tests/workflow-script-fetch.py index bc278e75..f7b703f3 100644 --- a/tests/workflow-script-fetch.py +++ b/tests/workflow-script-fetch.py @@ -81,7 +81,7 @@ def check_job(file: str, job_id: str, job: dict) -> tuple[list[str], int]: return [], 0 if len(uses) > 1: return [f"{name}: runs scripts of several kinds {sorted(uses)}; fetch one kind per job"], 0 - (kind, used), = uses.items() + ((kind, used),) = uses.items() start = 0 while start < len(steps) and preamble(file, job_id, steps[start]): start += 1 @@ -97,8 +97,10 @@ def check_job(file: str, job_id: str, job: dict) -> tuple[list[str], int]: "persist-credentials": False, } if not CHECKOUT.match(str(fetch.get("uses") or "")): - errors.append(f"{name}: the first step (after harden-runner and App-key preflights) must be " - f"actions/checkout pinned to a full SHA") + errors.append( + f"{name}: the first step (after harden-runner and App-key preflights) must be " + f"actions/checkout pinned to a full SHA" + ) with_ = fetch.get("with") or {} for key, value in expected_with.items(): if with_.get(key) != value: @@ -137,8 +139,10 @@ def main() -> int: for step in job.get("steps") or []: local = LOCAL_SCRIPT_REF.match(str(step.get("run") or "").strip()) if local and not (ROOT / local.group(1)).is_file(): - errors.append(f"{file.name} job {job_id}: step {step.get('name')!r} runs {local.group(1)}, " - f"which does not exist") + errors.append( + f"{file.name} job {job_id}: step {step.get('name')!r} runs {local.group(1)}, " + f"which does not exist" + ) for name in sorted(EXPECTED - set(fetching)): errors.append(f"{name}: expected jobs that run fetched scripts, found none") if errors: From cd79289f86c44249e94cbf98d80915eb6ac462b0 Mon Sep 17 00:00:00 2001 From: Jean-Paul van Ravensberg <14926452+DevSecNinja@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:15:34 +0100 Subject: [PATCH 2/4] feat(lint): run ruff when the repository has Python files Add a lint-ruff linter (default on, off in lint-include.yml) to both job modes. A first step lists the tracked Python files, minus the shared dprint excludes, and skips the mise install and ruff when there are none. ruff runs ruff check (annotations, plus SARIF for code scanning with upload-sarif) and ruff format --diff. Add a create-only ruff.toml template and pin ruff in the .mise.toml template. Refs #539 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .github/workflows/lint-include.yml | 10 +++- .github/workflows/lint.yml | 95 +++++++++++++++++++++++++++++- config-sync/templates/.mise.toml | 1 + config-sync/templates/README.md | 7 +++ config-sync/templates/ruff.toml | 25 ++++++++ docs/architecture.md | 20 ++++++- scripts/lint/detect-language.sh | 15 +++++ scripts/lint/language-files.sh | 49 +++++++++++++++ scripts/lint/ruff.sh | 30 ++++++++++ tests/lint-job-modes-contract.sh | 2 +- tests/lint-scripts.sh | 54 ++++++++++++++++- workflow-templates/lint.yml | 3 + 12 files changed, 302 insertions(+), 9 deletions(-) create mode 100644 config-sync/templates/ruff.toml create mode 100755 scripts/lint/detect-language.sh create mode 100755 scripts/lint/language-files.sh create mode 100755 scripts/lint/ruff.sh diff --git a/.github/workflows/lint-include.yml b/.github/workflows/lint-include.yml index 09233b67..55ed377d 100644 --- a/.github/workflows/lint-include.yml +++ b/.github/workflows/lint-include.yml @@ -101,6 +101,11 @@ on: required: false type: boolean default: false + lint-ruff: + description: "Run ruff (Python linting and formatting) when the repository has Python files — uploads SARIF" + required: false + type: boolean + default: false lint-config-drift: description: "Check local config files match central DevSecNinja/.github repo" required: false @@ -117,8 +122,8 @@ on: type: boolean default: false upload-sarif: - description: "Upload checkov/trivy/zizmor results to GitHub code scanning (requires GitHub Advanced Security). Set - false for private repos without GHAS to gate on exit codes with console output instead." + description: "Upload checkov/trivy/zizmor/ruff results to GitHub code scanning (requires GitHub Advanced Security). + Set false for private repos without GHAS to gate on exit codes with console output instead." required: false type: boolean default: true @@ -165,6 +170,7 @@ jobs: lint-checkov: ${{ inputs.lint-checkov }} lint-trivy: ${{ inputs.lint-trivy }} lint-zizmor: ${{ inputs.lint-zizmor }} + lint-ruff: ${{ inputs.lint-ruff }} lint-config-drift: ${{ inputs.lint-config-drift }} lint-inline-scripts: ${{ inputs.lint-inline-scripts }} lint-inline-scripts-blocking: ${{ inputs.lint-inline-scripts-blocking }} diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 4ee34452..69012017 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -93,6 +93,11 @@ on: required: false type: boolean default: true + lint-ruff: + description: "Run ruff (Python linting and formatting) when the repository has Python files — uploads SARIF" + required: false + type: boolean + default: true lint-config-drift: description: "Check local config files match central DevSecNinja/.github repo" required: false @@ -112,8 +117,8 @@ on: type: boolean default: false upload-sarif: - description: "Upload checkov/trivy/zizmor results to GitHub code scanning (requires GitHub Advanced Security). Set - false for private repos without GHAS to gate on exit codes with console output instead." + description: "Upload checkov/trivy/zizmor/ruff results to GitHub code scanning (requires GitHub Advanced Security). + Set false for private repos without GHAS to gate on exit codes with console output instead." required: false type: boolean default: true @@ -674,6 +679,67 @@ jobs: sarif_file: ${{ runner.temp }}/zizmor.sarif category: zizmor + ruff: + name: ruff + if: ${{ inputs.lint-ruff && (inputs.lint-job-mode == 'parallel' || (inputs.lint-job-mode == 'auto' && !github.event.repository.private)) + }} + continue-on-error: ${{ ! inputs.lint-fail-on-error }} + runs-on: ubuntu-24.04 + timeout-minutes: 10 + permissions: + contents: read + security-events: write + steps: + - name: Checkout lint scripts + # The linter scripts ship with this reusable workflow: fetch them from + # the same commit of DevSecNinja/.github that the caller pinned. + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: ${{ job.workflow_repository }} + ref: ${{ job.workflow_sha }} + path: .devsecninja-lint + sparse-checkout: |- + scripts/lint/ + scripts/lint-inline-scripts.py + sparse-checkout-cone-mode: false + persist-credentials: false + + - name: Move lint scripts out of the workspace + # Outside the workspace the caller's linters don't scan them, and the + # code checkout doesn't delete them. + run: mv .devsecninja-lint "${RUNNER_TEMP}/devsecninja-lint" + + - name: Checkout code + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Find Python files + # Repositories without Python skip the mise install and ruff. + id: python-files + run: bash "${RUNNER_TEMP}/devsecninja-lint/scripts/lint/detect-language.sh" python + + - name: Install mise + if: ${{ steps.python-files.outputs.found == 'true' }} + uses: jdx/mise-action@c2a87611a18de5b3828c5652fe268e992400cb5c # v4.3.0 + with: + install: true + version: ${{ inputs.mise-version }} + + - name: Run ruff + id: ruff + if: ${{ steps.python-files.outputs.found == 'true' }} + env: + UPLOAD_SARIF: ${{ inputs.upload-sarif }} + run: bash "${RUNNER_TEMP}/devsecninja-lint/scripts/lint/ruff.sh" + + - name: Upload SARIF report + if: ${{ !cancelled() && steps.ruff.outputs.sarif == 'true' }} + uses: github/codeql-action/upload-sarif@9b3254085494b55a0f7c4c2b3d2d52cc712ce370 # v4.39.1 + with: + sarif_file: ${{ runner.temp }}/ruff.sarif + category: ruff + config-drift: name: config drift if: ${{ inputs.lint-config-drift && (inputs.lint-job-mode == 'parallel' || (inputs.lint-job-mode == 'auto' && !github.event.repository.private)) @@ -798,11 +864,17 @@ jobs: fetch-depth: ${{ !inputs.lint-gitleaks && 1 || 0 }} persist-credentials: false + - name: Find Python files + # Repositories without Python skip ruff (and its mise install). + id: python-files + if: ${{ inputs.lint-ruff }} + run: bash "${RUNNER_TEMP}/devsecninja-lint/scripts/lint/detect-language.sh" python + - name: Install mise if: >- ${{ inputs.lint-dprint || inputs.lint-yamlfmt || inputs.lint-yamllint || inputs.lint-actionlint || inputs.lint-gitleaks || inputs.lint-shellcheck || inputs.lint-shfmt || inputs.lint-checkov || inputs.lint-trivy || inputs.lint-zizmor - }} + || steps.python-files.outputs.found == 'true' }} uses: jdx/mise-action@c2a87611a18de5b3828c5652fe268e992400cb5c # v4.3.0 with: install: true @@ -986,6 +1058,23 @@ jobs: sarif_file: ${{ runner.temp }}/zizmor.sarif category: zizmor + - name: Run ruff + id: ruff + if: ${{ steps.python-files.outputs.found == 'true' }} + continue-on-error: true + env: + UPLOAD_SARIF: ${{ inputs.upload-sarif }} + run: bash "${RUNNER_TEMP}/devsecninja-lint/scripts/lint/ruff.sh" + + - name: Upload ruff SARIF report + id: ruff-sarif + if: ${{ !cancelled() && steps.ruff.outputs.sarif == 'true' }} + continue-on-error: true + uses: github/codeql-action/upload-sarif@9b3254085494b55a0f7c4c2b3d2d52cc712ce370 # v4.39.1 + with: + sarif_file: ${{ runner.temp }}/ruff.sarif + category: ruff + - name: Check config drift against central repo id: config-drift if: ${{ inputs.lint-config-drift }} diff --git a/config-sync/templates/.mise.toml b/config-sync/templates/.mise.toml index 6907e7f7..3c179e30 100644 --- a/config-sync/templates/.mise.toml +++ b/config-sync/templates/.mise.toml @@ -7,6 +7,7 @@ actionlint = "1.7.12" dprint = "0.64.3" gitleaks = "8.30.1" lefthook = "2.2.2" +ruff = "0.17.5" shellcheck = "0.11.0" shfmt = "3.14.1" trivy = "0.76.1" diff --git a/config-sync/templates/README.md b/config-sync/templates/README.md index 54b86bbe..a3692793 100644 --- a/config-sync/templates/README.md +++ b/config-sync/templates/README.md @@ -95,3 +95,10 @@ repo’s tech stack. ## trivy.yaml Base Trivy config. Add repo-specific `skip-files` entries as needed. + +## ruff.toml + +Config for ruff, which the shared Lint workflow runs when the repo has +Python files (`lint-ruff`). It sets a 120-character line length and skips the +same generated and vendored paths as the shared `dprint.json`. Repos that +configure ruff in `pyproject.toml` don't need it. diff --git a/config-sync/templates/ruff.toml b/config-sync/templates/ruff.toml new file mode 100644 index 00000000..a0f870c3 --- /dev/null +++ b/config-sync/templates/ruff.toml @@ -0,0 +1,25 @@ +# Template — adapt per repo. Config for ruff, which the shared Lint workflow +# (lint-ruff) runs on the repository's Python files: `ruff check` and +# `ruff format --diff`. Projects that configure ruff in pyproject.toml +# ([tool.ruff]) don't need this file. +line-length = 120 + +# Generated and vendored paths, as in the shared dprint.json. The Lint workflow +# skips them already; this keeps local `ruff check .` runs in line. +extend-exclude = [ + "apm_modules", + ".agents", + ".claude", + ".cursor", + ".gemini", + ".github/agents", + ".github/chatmodes", + ".github/instructions", + ".github/prompts", + ".github/skills", + ".kiro", + ".opencode", + ".specify", + ".venv*", + ".windsurf", +] diff --git a/docs/architecture.md b/docs/architecture.md index 74d1595a..ac5f0245 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -74,7 +74,7 @@ workflow, so it calls `scripts/apm-materialize/` from its own checkout. ### Lint (`lint.yml`) -Runs up to twelve linters. By default, linter failures are reported without +Runs up to fourteen linters. By default, linter failures are reported without failing the workflow; set `lint-fail-on-error` to `true` to make lint failures fail the workflow. Toggle individual linters on/off with boolean inputs; all linter toggles default to `true`. @@ -173,6 +173,21 @@ jobs: lint-gitleaks: true ``` +**Language linters.** Linters for a programming language run only when the +repository has files of that language: a first step lists them with +`git ls-files` (`scripts/lint/language-files.sh`), and without any it skips the +mise install and the linter, so the job costs seconds. Generated and vendored +paths don't count: the excludes of the shared `dprint.json` (Spec Kit output, +APM-deployed agent files, `node_modules`, `.venv*`). + +- **ruff** (`lint-ruff`, Python, after v4.5.0) runs `ruff check` and + `ruff format --diff` on the `*.py` and `*.pyi` files. Pin `ruff` in the + repository's mise config. ruff reads the repository's own `ruff.toml`, + `.ruff.toml` or `[tool.ruff]` in `pyproject.toml`; config-sync's + [`ruff.toml` template](../config-sync/templates/ruff.toml) is a starting + point. Findings are annotated on the PR, and with `upload-sarif` they also go + to code scanning (category `ruff`). + **Inline scripts.** Code in a `run:` block is out of reach of linters, formatters and tests, so a `run:` step may hold exactly one line with one simple command, such as `./scripts/build.sh`, `bash .github/scripts/x.sh`, @@ -214,7 +229,7 @@ runner's system Python, which ships PyYAML (`ubuntu-24.04`). | `lint-config-dir` | Optional linter config directory. Default: `""`. | | `lint-fail-on-error` | Fail when a linter fails. Default: `false`. | | `lint-job-mode` | `auto`, `single`, or `parallel` (see above). Default: `auto`. v3.3.0+. | -| `upload-sarif` | Upload checkov/trivy/zizmor SARIF to code scanning. Default: `true`. | +| `upload-sarif` | Upload SARIF (checkov, trivy, zizmor, ruff). Default: `true`. | | `lint-dprint` | Markdown formatting (dprint). Default: `true`. | | `lint-yamlfmt` | YAML formatting (yamlfmt). Default: `true`. | | `lint-yamllint` | YAML linting (yamllint). Default: `true`. | @@ -226,6 +241,7 @@ runner's system Python, which ships PyYAML (`ubuntu-24.04`). | `lint-checkov` | IaC security scan (checkov). Default: `true`. | | `lint-trivy` | Filesystem scan (trivy). Default: `true`. | | `lint-zizmor` | Actions security scan (zizmor). Default: `true`. | +| `lint-ruff` | Python linting (ruff, see above). Default: `true`. After v4.5.0. | | `lint-config-drift` | Config-drift check. Default: `false`. | | `lint-inline-scripts` | Inline-script check (see above). Default: `true`. After v4.4.0. | | `lint-inline-scripts-blocking` | Report inline scripts as errors. Default: `false`. After v4.4.0. | diff --git a/scripts/lint/detect-language.sh b/scripts/lint/detect-language.sh new file mode 100755 index 00000000..c7a048eb --- /dev/null +++ b/scripts/lint/detect-language.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# +# Checks whether the repository has files of language $1, so its linter (and +# the mise install it needs) runs only for repositories that have them. +# A step of .github/workflows/lint.yml, run the same way in both lint modes. +# Writes found= to GITHUB_OUTPUT. + +set -eo pipefail + +if [ -n "$(bash "$(dirname "${BASH_SOURCE[0]}")/language-files.sh" "$1" | head -c 1)" ]; then + echo "found=true" >>"${GITHUB_OUTPUT}" +else + echo "::notice::No $1 files found; skipping its linter" + echo "found=false" >>"${GITHUB_OUTPUT}" +fi diff --git a/scripts/lint/language-files.sh b/scripts/lint/language-files.sh new file mode 100755 index 00000000..ef7502f6 --- /dev/null +++ b/scripts/lint/language-files.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +# +# Prints the repository's tracked files of language $1, NUL-separated. +# Used by the language linters of .github/workflows/lint.yml, which run only +# when this lists a file. +# +# Generated and vendored paths are skipped: the excludes of the shared +# config-sync/files/dprint.json (Spec Kit output, APM-deployed agent files, +# node_modules and Python virtualenvs). tests/lint-scripts.sh fails when a +# dprint.json exclude is missing here. + +set -eo pipefail + +case "$1" in +python) patterns=('**/*.py' '**/*.pyi') ;; +*) + echo "::error::language-files.sh: unknown language '$1'" >&2 + exit 2 + ;; +esac + +excludes=( + '**/node_modules/**' + 'apm_modules/**' + '.github/prompts/**' + '.github/instructions/**' + '.github/agents/**' + '.github/chatmodes/**' + '.claude/**' + '.cursor/**' + '.opencode/**' + '.gemini/**' + '.windsurf/**' + '.kiro/**' + '.agents/**' + '.github/skills/**' + '.specify/**' + '.venv*/**' +) + +pathspecs=() +for pattern in "${patterns[@]}"; do + pathspecs+=(":(glob)${pattern}") +done +for pattern in "${excludes[@]}"; do + pathspecs+=(":(glob,exclude)${pattern}") +done + +git ls-files -z -- "${pathspecs[@]}" diff --git a/scripts/lint/ruff.sh b/scripts/lint/ruff.sh new file mode 100755 index 00000000..9c96f2c2 --- /dev/null +++ b/scripts/lint/ruff.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# +# Lints the repository's Python files with ruff and checks their formatting. +# A step of .github/workflows/lint.yml, run the same way in both lint modes. +# ruff reads the repository's own config (ruff.toml, .ruff.toml or pyproject.toml). +# Env: UPLOAD_SARIF: also write the lint findings as SARIF for code scanning, +# and sarif=true to GITHUB_OUTPUT. + +set -eo pipefail + +lint_dir="$(dirname "${BASH_SOURCE[0]}")" +bash "${lint_dir}/require-mise-tool.sh" ruff +mapfile -d '' files < <(bash "${lint_dir}/language-files.sh" python) +# Without file arguments ruff would check the whole directory. +if [ "${#files[@]}" -eq 0 ]; then + echo "::notice::No Python files found; skipping ruff" + exit 0 +fi + +# --force-exclude keeps the config's excludes for the files passed explicitly. +if [ "${UPLOAD_SARIF}" = "true" ]; then + mise exec -- ruff check --force-exclude --exit-zero \ + --output-format sarif --output-file "${RUNNER_TEMP}/ruff.sarif" -- "${files[@]}" + echo "sarif=true" >>"${GITHUB_OUTPUT}" +fi + +status=0 +mise exec -- ruff check --force-exclude --output-format github -- "${files[@]}" || status=1 +mise exec -- ruff format --force-exclude --diff -- "${files[@]}" || status=1 +exit "${status}" diff --git a/tests/lint-job-modes-contract.sh b/tests/lint-job-modes-contract.sh index b0bec898..8093677a 100755 --- a/tests/lint-job-modes-contract.sh +++ b/tests/lint-job-modes-contract.sh @@ -14,7 +14,7 @@ set -euo pipefail repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" workflow="${repo_root}/.github/workflows/lint.yml" combined_job="all-linters" -linter_jobs=(dprint yamlfmt yamllint actionlint gitleaks go shellcheck shfmt checkov trivy zizmor config-drift inline-scripts) +linter_jobs=(dprint yamlfmt yamllint actionlint gitleaks go shellcheck shfmt checkov trivy zizmor ruff config-drift inline-scripts) parallel_guard="(inputs.lint-job-mode == 'parallel' || (inputs.lint-job-mode == 'auto' && !github.event.repository.private))" fail() { diff --git a/tests/lint-scripts.sh b/tests/lint-scripts.sh index 33f80d36..6c385973 100755 --- a/tests/lint-scripts.sh +++ b/tests/lint-scripts.sh @@ -120,7 +120,7 @@ expect_out "::error::'dprint' is not pinned in this repository's mise config." " [ -z "${log}" ] || fail "an unpinned tool must not run: ${log}" # Every linter that needs a pinned tool checks for it first. -for script in dprint yamlfmt yamllint shellcheck shfmt trivy; do +for script in dprint yamlfmt yamllint shellcheck shfmt trivy ruff; do run "${script}" FAKE_TOOLS="" expect_status 1 "${script} without its tool" expect_out "::error::'${script}' is not pinned" "${script} without its tool" @@ -196,6 +196,58 @@ run zizmor UPLOAD_SARIF=false FAKE_EXIT=1 expect_status 1 "zizmor without SARIF" expect_log "${repo}: zizmor ." "zizmor without SARIF" +# The language linters see the tracked files of their language, minus every +# path the shared dprint.json excludes (vendored and generated files). +code="${tmp_dir}/code" +mkdir -p "${code}/sub" +git -C "${code}" init -q +touch "${code}/mise.toml" "${code}/a.py" "${code}/sub/b.pyi" "${code}/untracked.py" "${code}/c.sh" +excluded=() +while IFS= read -r pattern; do + pattern="${pattern%$'\r'}" # jq on Windows ends lines with CRLF + # **/node_modules -> sub/node_modules/x.py, .venv*/** -> .venvx/x.py + path="${pattern/#\*\*\//sub/}" + path="${path%/\*\*}" + path="${path//\*/x}/x.py" + mkdir -p "${code}/${path%/*}" + touch "${code}/${path}" + excluded+=("${path}") +done < <(jq -r '.excludes[] | select(endswith(".md") | not)' "${repo_root}/config-sync/files/dprint.json") +[ "${#excluded[@]}" -ge 10 ] || fail "expected the dprint.json excludes, got: ${excluded[*]}" +git -C "${code}" add -f -- mise.toml a.py sub/b.pyi c.sh "${excluded[@]}" +listed="$(cd "${code}" && bash "${scripts}/language-files.sh" python | tr '\0' '\n')" +[ "${listed}" = $'a.py\nsub/b.pyi' ] || fail "language-files.sh python must list only a.py and sub/b.pyi; got: ${listed}" +run_in "${code}" "language-files bogus" +expect_status 2 "an unknown language" + +run_in "${code}" "detect-language python" +expect_status 0 "a repository with Python files" +[ "${outputs}" = found=true ] || fail "expected found=true; got: ${outputs}" +run "detect-language python" +expect_status 0 "a repository without Python files" +[ "${outputs}" = found=false ] || fail "expected found=false; got: ${outputs}" +expect_out "::notice::No python files found; skipping its linter" "a repository without Python files" + +# ruff lints and format-checks only those files, and writes SARIF for code +# scanning without letting it gate the step. +export FAKE_TOOLS="${FAKE_TOOLS} ruff" +run_in "${code}" ruff +expect_status 0 "ruff with SARIF" +expect_log "ruff check --force-exclude --exit-zero --output-format sarif --output-file ${RUNNER_TEMP}/ruff.sarif -- a.py sub/b.pyi" \ + "ruff with SARIF" +expect_log "ruff check --force-exclude --output-format github -- a.py sub/b.pyi" "ruff with SARIF" +expect_log "ruff format --force-exclude --diff -- a.py sub/b.pyi" "ruff with SARIF" +[ "${outputs}" = sarif=true ] || fail "ruff must report its SARIF report; got: ${outputs}" +run_in "${code}" ruff UPLOAD_SARIF=false FAKE_EXIT=1 +expect_status 1 "a failing ruff" +grep -Fq sarif <<<"${log}${outputs}" && fail "ruff without SARIF must not write it: ${log} ${outputs}" +expect_log "ruff format --force-exclude --diff" "a failing ruff check must not skip the format check" +git -C "${code}" rm -q --cached a.py sub/b.pyi +run_in "${code}" ruff +expect_status 0 "ruff without Python files" +expect_out "::notice::No Python files found; skipping ruff" "ruff without Python files" +[ -z "${log}" ] || fail "ruff must not run without Python files: ${log}" + # The SARIF filter drops checkov's skipped checks (level none) and tolerates a # missing report. run checkov-sarif-filter diff --git a/workflow-templates/lint.yml b/workflow-templates/lint.yml index f951fc12..60a9fcc3 100644 --- a/workflow-templates/lint.yml +++ b/workflow-templates/lint.yml @@ -62,6 +62,9 @@ jobs: # lint-checkov: true # lint-trivy: true # lint-zizmor: true + # Optional: needs a lint.yml release after v4.5.0; runs only when the + # repository has Python files. Pin ruff in your mise config. + # lint-ruff: true # lint-config-drift: false # Optional: report inline scripts in run: steps. Needs a lint.yml release # after v4.4.0; bump the pin above before uncommenting. From 82ce22b8d1cfad4a52e1feff646cc05add129237 Mon Sep 17 00:00:00 2001 From: Jean-Paul van Ravensberg <14926452+DevSecNinja@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:17:29 +0100 Subject: [PATCH 3/4] style(python): make the Python scripts with a shebang executable ruff (EXE001) flags a shebang on a file without the executable bit. Refs #539 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- actions/ai-commits-badge/ai_commits_badge.py | 0 tests/ai-commits-badge.py | 0 tests/app-key-confinement.py | 0 tests/issue-labeler-config.py | 0 tests/lint-include.py | 0 tests/pr-labeler-config.py | 0 tests/workflow-script-fetch.py | 0 7 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 actions/ai-commits-badge/ai_commits_badge.py mode change 100644 => 100755 tests/ai-commits-badge.py mode change 100644 => 100755 tests/app-key-confinement.py mode change 100644 => 100755 tests/issue-labeler-config.py mode change 100644 => 100755 tests/lint-include.py mode change 100644 => 100755 tests/pr-labeler-config.py mode change 100644 => 100755 tests/workflow-script-fetch.py diff --git a/actions/ai-commits-badge/ai_commits_badge.py b/actions/ai-commits-badge/ai_commits_badge.py old mode 100644 new mode 100755 diff --git a/tests/ai-commits-badge.py b/tests/ai-commits-badge.py old mode 100644 new mode 100755 diff --git a/tests/app-key-confinement.py b/tests/app-key-confinement.py old mode 100644 new mode 100755 diff --git a/tests/issue-labeler-config.py b/tests/issue-labeler-config.py old mode 100644 new mode 100755 diff --git a/tests/lint-include.py b/tests/lint-include.py old mode 100644 new mode 100755 diff --git a/tests/pr-labeler-config.py b/tests/pr-labeler-config.py old mode 100644 new mode 100755 diff --git a/tests/workflow-script-fetch.py b/tests/workflow-script-fetch.py old mode 100644 new mode 100755 From fc540403bbb3977f3fe0776d8ab2497ac580bc6e Mon Sep 17 00:00:00 2001 From: Jean-Paul van Ravensberg <14926452+DevSecNinja@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:24:34 +0100 Subject: [PATCH 4/4] fix(lint): keep ruff checks read-only and the ruff.toml template out of configured repos Pass --no-fix to ruff check so a fix = true in the repository's config can't edit the files being checked, and don't bootstrap the ruff.toml template next to .ruff.toml or [tool.ruff] in pyproject.toml, which it would override. Refs #539 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- config-sync/templates/README.md | 6 +++-- config-sync/templates/ruff.toml | 4 +-- scripts/config-sync/sync-files.sh | 26 ++++++++++++++++++ scripts/lint/ruff.sh | 7 ++--- tests/config-sync-scripts.sh | 44 +++++++++++++++++++++++++++++-- tests/lint-scripts.sh | 4 +-- 6 files changed, 80 insertions(+), 11 deletions(-) diff --git a/config-sync/templates/README.md b/config-sync/templates/README.md index a3692793..9f81df33 100644 --- a/config-sync/templates/README.md +++ b/config-sync/templates/README.md @@ -100,5 +100,7 @@ Base Trivy config. Add repo-specific `skip-files` entries as needed. Config for ruff, which the shared Lint workflow runs when the repo has Python files (`lint-ruff`). It sets a 120-character line length and skips the -same generated and vendored paths as the shared `dprint.json`. Repos that -configure ruff in `pyproject.toml` don't need it. +same generated and vendored paths as the shared `dprint.json`. Config-sync +doesn't add it to a repo that configures ruff in `.ruff.toml` or in +`[tool.ruff]` of `pyproject.toml`, where `ruff.toml` would take precedence and +replace those settings. diff --git a/config-sync/templates/ruff.toml b/config-sync/templates/ruff.toml index a0f870c3..87ace74e 100644 --- a/config-sync/templates/ruff.toml +++ b/config-sync/templates/ruff.toml @@ -1,7 +1,7 @@ # Template — adapt per repo. Config for ruff, which the shared Lint workflow # (lint-ruff) runs on the repository's Python files: `ruff check` and -# `ruff format --diff`. Projects that configure ruff in pyproject.toml -# ([tool.ruff]) don't need this file. +# `ruff format --diff`. Config-sync doesn't add it to a repo that configures +# ruff in .ruff.toml or [tool.ruff] of pyproject.toml. line-length = 120 # Generated and vendored paths, as in the shared dprint.json. The Lint workflow diff --git a/scripts/config-sync/sync-files.sh b/scripts/config-sync/sync-files.sh index 3f8878d4..a63a2ddf 100644 --- a/scripts/config-sync/sync-files.sh +++ b/scripts/config-sync/sync-files.sh @@ -84,6 +84,26 @@ apm_owner() { return 1 } +# A template is not bootstrapped when the repository configures the tool in +# another file already: ruff.toml would take precedence over [tool.ruff] in +# pyproject.toml and silently replace its settings. +configured_elsewhere() { + case "$1" in + ruff.toml) + if [ -e .ruff.toml ]; then + echo ".ruff.toml" + return 0 + fi + if [ -f pyproject.toml ] && grep -Eq '^[[:space:]]*\[tool\.ruff[].]' pyproject.toml; then + echo "[tool.ruff] in pyproject.toml" + return 0 + fi + ;; + *) ;; + esac + return 1 +} + sync_tree() { local kind="$1" # "files" or "templates" local only="${2:-}" # optional: sync just this path @@ -127,6 +147,12 @@ sync_tree() { continue fi + # shellcheck disable=SC2310 # configured_elsewhere only tests; its status is the answer. + if [ "${kind}" = "templates" ] && reason=$(configured_elsewhere "${dest}"); then + echo " ✔ ${dest} skipped: configured in ${reason}" + continue + fi + mkdir -p "$(dirname "${dest}")" if [ -f "${dest}" ] && cmp -s "${src}" "${dest}"; then diff --git a/scripts/lint/ruff.sh b/scripts/lint/ruff.sh index 9c96f2c2..7b8b8d23 100755 --- a/scripts/lint/ruff.sh +++ b/scripts/lint/ruff.sh @@ -17,14 +17,15 @@ if [ "${#files[@]}" -eq 0 ]; then exit 0 fi -# --force-exclude keeps the config's excludes for the files passed explicitly. +# --force-exclude keeps the config's excludes for the files passed explicitly, +# and --no-fix overrides a `fix = true` in it: checks must not edit the files. if [ "${UPLOAD_SARIF}" = "true" ]; then - mise exec -- ruff check --force-exclude --exit-zero \ + mise exec -- ruff check --force-exclude --no-fix --exit-zero \ --output-format sarif --output-file "${RUNNER_TEMP}/ruff.sarif" -- "${files[@]}" echo "sarif=true" >>"${GITHUB_OUTPUT}" fi status=0 -mise exec -- ruff check --force-exclude --output-format github -- "${files[@]}" || status=1 +mise exec -- ruff check --force-exclude --no-fix --output-format github -- "${files[@]}" || status=1 mise exec -- ruff format --force-exclude --diff -- "${files[@]}" || status=1 exit "${status}" diff --git a/tests/config-sync-scripts.sh b/tests/config-sync-scripts.sh index b3c07034..a128c7bb 100644 --- a/tests/config-sync-scripts.sh +++ b/tests/config-sync-scripts.sh @@ -1,8 +1,9 @@ #!/usr/bin/env bash # # Tests scripts/config-sync/ensure-label.sh, which -# .github/workflows/config-sync.yml runs before it opens the sync PR (#537). -# scripts/config-sync/sync-files.sh is covered by +# .github/workflows/config-sync.yml runs before it opens the sync PR (#537), +# and the templates that scripts/config-sync/sync-files.sh skips. The rest of +# sync-files.sh is covered by # tests/config-sync-renovate-stub.sh and tests/config-sync-ai-toolkit-instructions.sh. set -euo pipefail @@ -41,3 +42,42 @@ grep -Fq "::warning::Could not create/update config-sync label" "${tmp_dir}/log" fail "a label that cannot be created must warn" echo "✓ config-sync: ensure-label.sh creates the label and only warns when it can't." + +# sync-files.sh doesn't bootstrap the ruff.toml template when the repository +# configures ruff elsewhere: ruff.toml would take precedence and replace it. +sync_script="${repo_root}/scripts/config-sync/sync-files.sh" +consumer="${tmp_dir}/consumer" + +# Runs the sync with sync-templates in a fresh consumer checkout; $1 is a +# file to create there, $2 its content. +sync_templates() { + rm -rf "${consumer}" + mkdir -p "${consumer}/.config-sync-source/config-sync" + cp -R "${repo_root}/config-sync/templates" "${consumer}/.config-sync-source/config-sync/templates" + git -C "${consumer}/.config-sync-source" init --quiet + git -C "${consumer}/.config-sync-source" -c user.name=t -c user.email=t@t \ + -c commit.gpgsign=false commit --quiet --allow-empty -m init + [ -z "$1" ] || printf '%s\n' "$2" >"${consumer}/$1" + ( + cd "${consumer}" + GITHUB_REPOSITORY=DevSecNinja/example GITHUB_OUTPUT="${tmp_dir}/output" SYNC_TEMPLATES=true \ + IGNORE_FILE=.github/.config-sync-ignore SOURCE_DIR=.config-sync-source/config-sync \ + bash "${sync_script}" >"${tmp_dir}/log" 2>&1 + ) || fail "sync step failed: $(cat "${tmp_dir}/log")" +} + +sync_templates "" "" +cmp -s "${repo_root}/config-sync/templates/ruff.toml" "${consumer}/ruff.toml" || + fail "a repository without ruff config must get the ruff.toml template" +sync_templates pyproject.toml $'[project]\nname = "x"\n\n[tool.ruffle]\nx = 1' +[ -e "${consumer}/ruff.toml" ] || fail "[tool.ruffle] in pyproject.toml is not ruff config" +for config in "[tool.ruff]" "[tool.ruff.lint]" " [tool.ruff.format]"; do + sync_templates pyproject.toml $'[project]\nname = "x"\n\n'"${config}"$'\nline-length = 100' + [ ! -e "${consumer}/ruff.toml" ] || fail "ruff.toml must not be added next to ${config} in pyproject.toml" + grep -Fq "ruff.toml skipped: configured in [tool.ruff] in pyproject.toml" "${tmp_dir}/log" || + fail "the skipped ruff.toml must be logged: $(cat "${tmp_dir}/log")" +done +sync_templates .ruff.toml "line-length = 100" +[ ! -e "${consumer}/ruff.toml" ] || fail "ruff.toml must not be added next to .ruff.toml" + +echo "✓ config-sync: sync-files.sh keeps the ruff.toml template out of repositories that configure ruff elsewhere." diff --git a/tests/lint-scripts.sh b/tests/lint-scripts.sh index 6c385973..56413a7a 100755 --- a/tests/lint-scripts.sh +++ b/tests/lint-scripts.sh @@ -233,9 +233,9 @@ expect_out "::notice::No python files found; skipping its linter" "a repository export FAKE_TOOLS="${FAKE_TOOLS} ruff" run_in "${code}" ruff expect_status 0 "ruff with SARIF" -expect_log "ruff check --force-exclude --exit-zero --output-format sarif --output-file ${RUNNER_TEMP}/ruff.sarif -- a.py sub/b.pyi" \ +expect_log "ruff check --force-exclude --no-fix --exit-zero --output-format sarif --output-file ${RUNNER_TEMP}/ruff.sarif -- a.py sub/b.pyi" \ "ruff with SARIF" -expect_log "ruff check --force-exclude --output-format github -- a.py sub/b.pyi" "ruff with SARIF" +expect_log "ruff check --force-exclude --no-fix --output-format github -- a.py sub/b.pyi" "ruff with SARIF" expect_log "ruff format --force-exclude --diff -- a.py sub/b.pyi" "ruff with SARIF" [ "${outputs}" = sarif=true ] || fail "ruff must report its SARIF report; got: ${outputs}" run_in "${code}" ruff UPLOAD_SARIF=false FAKE_EXIT=1