diff --git a/.github/workflows/container.yml b/.github/workflows/container.yml index ef65e7d..da9c9d5 100644 --- a/.github/workflows/container.yml +++ b/.github/workflows/container.yml @@ -318,43 +318,3 @@ jobs: echo "promoted $version" ;; esac - - # Step 3 of a release (update-tools.yml describes all three): once a version - # tag's image is promoted -- so the tag a consumer will pull exists -- open a - # bump PR in every repo that pins python-sdlc. Each repo's own CI decides - # whether its bump lands. - bump-consumers: - name: Bump every repo that pins python-sdlc - needs: promote - if: startsWith(github.ref, 'refs/tags/v') - runs-on: ubuntu-latest - permissions: - contents: read - # A FIXED group, like promote's, and for the same kind of reason. Each repo - # gets ONE bump branch which this force-pushes, so two releases running at - # once would fight over it and the loser's version would be the one left on - # the branch. Serialized, the later release simply supersedes the earlier -- - # which is correct, since its image contains the earlier one's code. - # Not cancel-in-progress: a half-finished fleet bump is worse than a slow one. - concurrency: - group: bump-consumers-python-sdlc - cancel-in-progress: false - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - persist-credentials: false - - - name: Mint CI token - id: ci-token - uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 - with: - client-id: ${{ vars.CRUNCHTOOLS_CI_CLIENT_ID }} - private-key: ${{ secrets.CRUNCHTOOLS_CI_APP_KEY }} - owner: crunchtools - - - name: Open the bump pull requests - env: - GH_TOKEN: ${{ steps.ci-token.outputs.token }} - run: | - gh auth setup-git - python3 scripts/bump_consumers.py "$GITHUB_REF_NAME" diff --git a/.github/workflows/update-tools.yml b/.github/workflows/update-tools.yml index 56b136f..b5abf95 100644 --- a/.github/workflows/update-tools.yml +++ b/.github/workflows/update-tools.yml @@ -1,11 +1,11 @@ -# A Gourmand or Gatehouse release reaches every Python repo in three steps, and -# this is the first: move the pin and bump this image's patch version, as a pull +# A Gourmand or Gatehouse release reaches every repo in three steps, and this +# is the first: move the pin and bump this image's patch version, as a pull # request that merges itself when CI is green. # # 1. this workflow new tool release -> PR moving the pin here # 2. release.yml that PR merges -> python-sdlc vX.Y.Z is released -# 3. container.yml the release is promoted -> a bump PR in every repo -# that pins python-sdlc (scripts/bump_consumers.py) +# 3. container.yml the release is promoted to sdlc:latest, which every +# converted repo floats on: no pull request per repo # # Gourmand and Gatehouse send `parent-image-updated` when they release. The # weekly run catches a dispatch that was lost. diff --git a/CHANGELOG.md b/CHANGELOG.md index 2922206..211af7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ All notable changes to this project are documented here, following ## [Unreleased] +## [0.9.0] - 2026-11-17 + ### Added - **No `rev:` to pin.** The image now carries its own launcher @@ -50,15 +52,41 @@ All notable changes to this project are documented here, following check's record carries exit 4. Only the smoke run can skip: a 4 from a tool in the image is still a finding. +- `scripts/convert_repo.py` writes the config a converted repo needs to run + on its first try: `skip-stacks` for node, bootc or docs, which this image + cannot check yet, and `[tool.sdlc.ci] skip` for `container-build`, + `container-smoke` and `container-scan` when the Containerfile needs build + secrets (`--mount=type=secret`, `subscription-manager`, RHSM). A Python repo + naming no stacks is untouched; an existing key that disagrees is refused. + ### Changed +- **`container-smoke` is advisory by default.** It runs and prints what it + found as `FOUND SOMETHING, ADVISORY: not gating` (record exit 5), and the + command does not fail. `gate = true` in `[tool.sdlc.ci.container-smoke]` + makes a finding gate; a smoke run that could not run is still a 2. +- `containerfile-lint` also ignores SC1072, which hadolint raises on an + exec-form `RUN ["python3", "-c", …]`, and a Containerfile hadolint cannot + parse (a `RUN` heredoc) is printed as ADVISORY and SKIPPED, not failed. +- `examples/gatehouse.yml` is gatehouse at 4ae89d0 (gatehouse#150): the guard + also refuses a fork's change to `.sdlc/`. The converter brings an older guard + up to date. - `containerfile-lint` without a `.hadolint.yaml` now fails at `error`, not `warning`, and ignores DL3007 (floating base tags) and DL3041 (unpinned dnf packages), which the fleet does on purpose. Warnings such as DL3002 are printed and do not fail. A repo's `.hadolint.yaml` replaces these defaults. +### Removed + +- `bump-consumers` (and `scripts/bump_consumers.py`): repos float on + `sdlc:latest`, so a release opens no bump pull requests. A repo still pinned + by `rev:` moves when it converts. + ### Fixed +- `container-smoke` passes an exit 0 whatever podman says of OOMKilled: nested + rootless podman reports it true even for `true`. OOMKilled counts only + beside a non-zero exit. - `container-smoke` removes the smoke container with `-v`, so an image that declares a `VOLUME` no longer leaks an anonymous volume on every run. A fleet measurement filled a seat's disk with 13 GB of them. diff --git a/Containerfile b/Containerfile index 39d046a..d31971d 100644 --- a/Containerfile +++ b/Containerfile @@ -53,7 +53,7 @@ LABEL description="Every CrunchTools SDLC check in one image: ruff, mypy, pytest LABEL org.opencontainers.image.source="https://github.com/crunchtools/python-sdlc" LABEL org.opencontainers.image.description="Every CrunchTools SDLC check in one image, for Python and container repos." LABEL org.opencontainers.image.licenses="AGPL-3.0-or-later" -LABEL version="0.8.0" +LABEL version="0.9.0" # What the image carries, readable without starting anything. `sdlc versions` # prints these and a consuming repo's CI pins them. diff --git a/IMAGE_TAG b/IMAGE_TAG index a3df0a6..ac39a10 100644 --- a/IMAGE_TAG +++ b/IMAGE_TAG @@ -1 +1 @@ -0.8.0 +0.9.0 diff --git a/README.md b/README.md index d5d5b94..74c24f8 100644 --- a/README.md +++ b/README.md @@ -74,8 +74,16 @@ rev-pinned hooks below, or the standalone Gourmand and Gatehouse hooks -- and prints the diff; `--apply` writes it. It replaces a workflow only when the CI stub covers every job in it (`sdlc ci`, Gourmand), refreshes `gatehouse.yml` from [`examples/gatehouse.yml`](examples/gatehouse.yml) when it differs only in -comments, keeps everything else (publish, release, container builds), and -refuses with exit 2 a shape it would have to guess at. +comments or in the guard's script (an older guard that does not cover +`.sdlc/`), keeps everything else (publish, release, container builds), and +refuses with exit 2 a shape it would have to guess at. So that a converted repo +never meets GATE CANNOT RUN on its first run, it also writes `skip-stacks` for +a stack this image cannot check yet (node, bootc, docs), and `[tool.sdlc.ci] +skip` for the three container checks when the Containerfile needs build +secrets (`--mount=type=secret`, `subscription-manager`, RHSM) that `sdlc ci` +is not given; the publish workflow keeps building those with its secrets. A +Python repo that names no stacks is left alone, and an existing key that does +not already say this is refused rather than rewritten. `scripts/fleet_wave.py` runs it across the org: a plan by default, one auto-merging pull request per repo with `--apply`, a TSV ledger either way. It reads `GH_TOKEN` from the environment for the GitHub API (required with @@ -166,9 +174,9 @@ refused. | Check | Command | What it does | |---|---|---| -| `containerfile-lint` | push, ci | hadolint over the Containerfile. Without a `.hadolint.yaml` the threshold is `error` and DL3007 (floating base tags) and DL3041 (unpinned dnf) are ignored, as fleet conventions; warnings such as DL3002 are printed and do not fail. A repo's `.hadolint.yaml` replaces all of that | +| `containerfile-lint` | push, ci | hadolint over the Containerfile. Without a `.hadolint.yaml` the threshold is `error` and DL3007 (floating base tags) DL3041 (unpinned dnf) and SC1072 (raised on an exec-form `RUN ["python3", "-c", …]`) are ignored, as fleet conventions; warnings such as DL3002 are printed and do not fail. A repo's `.hadolint.yaml` replaces all of that. A Containerfile hadolint cannot parse (a `RUN` heredoc) is printed as ADVISORY and SKIPPED, not failed; `container-build` still builds it | | `container-build` | ci | `podman build`, at the tag the next two read | -| `container-smoke` | ci | starts the built image detached, with no network and no capabilities, and reads its state from podman: still running after `seconds`, or exited 0, passes; any other exit, the OOM killer, or an entrypoint that cannot start (126/127) fails. An image whose PID 1 is systemd (`/sbin/init`, `systemd`) is SKIPPED with its reason: systemd needs privileges the run withholds | +| `container-smoke` | ci | starts the built image detached, with no network and no capabilities, and reads its state from podman: still running after `seconds`, or exited 0, passes; any other exit, the OOM killer, or an entrypoint that cannot start (126/127) is a finding. **Advisory by default**: a finding is printed as `FOUND SOMETHING, ADVISORY` and does not fail the command; `gate = true` makes it gate. Exit 0 passes even when podman reports OOMKilled, which nested rootless podman does for any container; OOMKilled counts only beside a non-zero exit. Podman failing to create the container is still a 2. An image whose PID 1 is systemd (`/sbin/init`, `systemd`) is SKIPPED with its reason: systemd needs privileges the run withholds | | `container-scan` | ci | grype over the built image's filesystem, mounted read-only by podman; fails on a fixable Critical by default, and the repo's `.grype.yaml` applies | Build, smoke and scan are one lane, beside everything else. @@ -255,8 +263,10 @@ $ jq -r '.checks[] | "\(.seconds)s \(.check) exit \(.exit)"' .git/sdlc/last-push ``` A check's `exit` is its status: 0 passed, 1 found something, 2 could not run, -3 deferred to CI, and 4 SKIPPED, which only `container-smoke` reports, for a -systemd PID 1 it cannot boot. A 4 never decides the command's own exit. +3 deferred to CI, 4 SKIPPED -- reported only by `container-smoke`, for a +systemd PID 1 it cannot boot, and `containerfile-lint`, for a Containerfile +hadolint cannot parse -- and 5 an ADVISORY finding from an ungated +`container-smoke`. A 4 or a 5 never decides the command's own exit. The checks appear in the order they **finished**, which with lanes is not the order they were listed in. A check that streamed to your terminal has no log @@ -327,7 +337,7 @@ tests = ["tests/image/"] | `ruff-check`, `ruff-format`, `lint`, `mypy`, `gourmand`, `gatehouse`, `pip-audit`, `hooks-installed` | none | | `containerfile-lint` | `containerfiles` (default: the root Containerfile or Dockerfile), `threshold` | | `container-build` | the `image-build` kind's settings; `containerfile` defaults to the root one | -| `container-smoke` | `seconds` (default 10), `args`, `env`, `network` (default `false`), `image` | +| `container-smoke` | `gate` (default `false`: findings are advisory), `seconds` (default 10), `args`, `env`, `network` (default `false`), `image` | | `container-scan` | `fail_on` (default `critical`), `only_fixed` (default `true`), `image` | ### Kinds, for a repo's own checks @@ -461,8 +471,9 @@ sees it. `sdlc codeql-baseline` writes it from the current findings. See ## How a new Gourmand or Gatehouse arrives Their releases trigger a rebuild here, which is released as a new python-sdlc -version, which opens a version-bump pull request in each Python repo. The laptop -then runs the reviewer CI runs. +version and promoted to `sdlc:latest`. Every converted repo floats on that tag, +so the laptop then runs the reviewer CI runs, with no pull request per repo. A +repo still pinned by `rev:` moves only when it converts or bumps by hand. ## Host contract diff --git a/bin/sdlc b/bin/sdlc index 2b77081..50f57c1 100755 --- a/bin/sdlc +++ b/bin/sdlc @@ -650,10 +650,12 @@ run_up() { # seconds run-args -> the smoke verdict "$_cname" 2>/dev/null) || _upstate= podman logs "$_cname" 2>&1 || : podman rm -f -v "$_cname" >/dev/null 2>&1 || : + # Exit 0 is a pass whatever OOMKilled says: nested rootless podman reports + # it true even for `true`. It counts only beside a non-zero exit. case "$_upstate" in 'true '*) printf 'still running after %ss: the image starts and stays up\n' "$_upsecs"; return 0 ;; - *' true') printf '%s: the container was killed for memory\n' "$ME" >&2; return 1 ;; 'false 0 '*) return 0 ;; + *' true') printf '%s: the container was killed for memory\n' "$ME" >&2; return 1 ;; 'false '*) printf '%s: the container exited %s within %ss of starting\n' "$ME" \ "$(printf '%s' "$_upstate" | cut -d' ' -f2)" "$_upsecs" >&2; return 1 ;; *) printf '%s: podman could not report the container state; nothing was judged\n' "$ME" >&2 @@ -716,12 +718,25 @@ run_step() { # mode payload -> the step's exit status _run_step "$@" || _rs=$? # Only a push may defer to CI. Anything else that reports 3 could not run. if [ "$_rs" -eq 3 ] && [ "${SDLC_TIER:-}" != push ]; then return 2; fi - # Only run_up may skip, and only by its own decision. A tool inside a check - # exiting 4 found something, as it always has. - if [ "$_rs" -eq 4 ] && [ "$1" != host-up ]; then return 1; fi + # Two statuses mean "not gating", and each comes from one place only: + # 4 SKIPPED, not judged: run_up for a systemd PID 1, and + # containerfile-lint when hadolint cannot parse the Containerfile + # 5 found something, ADVISORY: an advisory smoke run's finding + # Anything else exiting 4 or 5 -- any tool in any other check -- found + # something, as it always has. + case "$_rs:$1:$2" in + 4:host-up:*|4:host-up-advisory:*|4:image-ro:containerfile-lint) ;; + 1:host-up-advisory:*) return 5 ;; + 4:*|5:*) return 1 ;; + esac return "$_rs" } +advisory() { # check seconds -> the footer of an advisory finding + printf '\n== %s FOUND SOMETHING, ADVISORY: not gating, after %ss\n' "$1" "$2" + printf '%s: To make it gate: gate = true in [tool.sdlc.ci.%s]\n' "$ME" "$1" +} + deferred() { # check -> the warning a deferral prints, every time printf '\n%s: %s COULD NOT RUN -- DEFERRED TO CI.\n' "$ME" "$1" >&2 printf '%s: This push is not blocked, and nothing was judged here.\n' "$ME" >&2 @@ -739,7 +754,7 @@ _run_step() { *) die "the image asked for an unknown local check: $2" ;; esac ;; host) run_host "$2" ;; - host-up) + host-up|host-up-advisory) _secs=${2%%"$US"*} case "$_secs" in ''|*[!0-9]*) die "a host-up step must start with a number of seconds" ;; esac run_host "${2#*"$US"}" "$_secs" ;; @@ -753,6 +768,7 @@ _close_serial() { # the open check is finished: footer and record # happened, rather than when the next check starts. [ "$_scur_rc" -eq 0 ] && printf '\n== %s passed in %ss\n' "$_scur" "$_scur_took" [ "$_scur_rc" -eq 4 ] && printf '\n== %s SKIPPED, not judged, after %ss\n' "$_scur" "$_scur_took" + [ "$_scur_rc" -eq 5 ] && advisory "$_scur" "$_scur_took" record_check "$_scur" "$_scur_rc" "$_scur_took" "" _scur= } @@ -795,8 +811,8 @@ run_serial() { # the plan -> its worst step status _failed="$_failed $_check" _scur_rc=3 deferred "$_check" - elif [ "$_rc" -eq 4 ]; then - _scur_rc=4 + elif [ "$_rc" -eq 4 ] || [ "$_rc" -eq 5 ]; then + _scur_rc=$_rc elif [ "$_rc" -ne 0 ]; then _failed="$_failed $_check" _scur_rc=$_rc @@ -864,6 +880,8 @@ _close_check() { # the open check's block is complete printf '\n== %s passed in %ss\n' "$_cur_name" "$_cur_took" >> "$OUT/out.$_cur" elif [ "$_cur_rc" -eq 4 ]; then printf '\n== %s SKIPPED, not judged, after %ss\n' "$_cur_name" "$_cur_took" >> "$OUT/out.$_cur" + elif [ "$_cur_rc" -eq 5 ]; then + advisory "$_cur_name" "$_cur_took" >> "$OUT/out.$_cur" elif [ "$_cur_rc" -eq 3 ]; then deferred "$_cur_name" 2>> "$OUT/out.$_cur" else @@ -924,8 +942,8 @@ run_lane() { # lane-index steps-file lane-cpus -> the lane's worst if [ "$_rc" -eq 3 ]; then _failed="$_failed $_check" _cur_rc=3 - elif [ "$_rc" -eq 4 ]; then - _cur_rc=4 + elif [ "$_rc" -eq 4 ] || [ "$_rc" -eq 5 ]; then + _cur_rc=$_rc elif [ "$_rc" -ne 0 ]; then _failed="$_failed $_check" _cur_rc=$_rc diff --git a/examples/gatehouse.yml b/examples/gatehouse.yml index f0a31d7..a4377cf 100644 --- a/examples/gatehouse.yml +++ b/examples/gatehouse.yml @@ -42,7 +42,8 @@ permissions: {} # default-deny; each job grants only what it needs jobs: # ── 1) Protect the workflow files themselves ──────────────────────────────── - # Rejects PRs from forks that touch .github/workflows/. Safe under + # Rejects PRs from forks that touch .github/workflows/ or .sdlc/ (the + # launcher the required `sdlc ci` check runs). Safe under # pull_request_target because it only reads the changed-file LIST via the # API — it never checks out or runs your code. And because pull_request_target # runs the BASE version of this file, a PR can't edit this job to disable it. @@ -71,11 +72,11 @@ jobs: echo "Head branch is in $GITHUB_REPOSITORY; allowing."; exit 0 fi # Both sides of a rename: `gh pr diff --name-only` lists only the new - # path, so a fork could move a workflow out of .github/workflows/ unseen. + # path, so a fork could move a file out of .github/workflows/ or .sdlc/ unseen. changed="$(gh api --paginate "repos/$GITHUB_REPOSITORY/pulls/$PR/files" \ --jq '.[] | .filename, (.previous_filename // empty)')" - if echo "$changed" | grep -qE '^\.github/workflows/'; then - echo "::error::PR modifies .github/workflows/ — not accepted from external contributors." + if echo "$changed" | grep -qE '^(\.github/workflows|\.sdlc)/'; then + echo "::error::PR modifies .github/workflows/ or .sdlc/ — not accepted from external contributors." # Explain once, on the PR event; replies re-run the check, not the comment. if [ "$EVENT" = "pull_request_target" ]; then gh pr comment "$PR" --repo "$GITHUB_REPOSITORY" --body \ diff --git a/pyproject.toml b/pyproject.toml index fc7f69b..c0580a2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ [project] name = "python-sdlc" -version = "0.8.0" +version = "0.9.0" description = "One container image and one script for every CrunchTools Python SDLC check" requires-python = ">=3.14" license = "AGPL-3.0-or-later" diff --git a/scripts/bump_consumers.py b/scripts/bump_consumers.py deleted file mode 100755 index e89ac24..0000000 --- a/scripts/bump_consumers.py +++ /dev/null @@ -1,322 +0,0 @@ -#!/usr/bin/env python3 -"""Open a version-bump pull request in every repo that pins python-sdlc. - -Run after a release is promoted, so the image a bump points at exists. Each -consuming repo gets one PR moving its `rev:`, queued for auto-merge, and its own -CI decides whether it lands -- a bad release fails per repo, not fleet-wide. -The same shape as crunchtools/constitution's scripts/fleet-bump.py: clone, -rewrite, push, open, queue, and name the repos that failed so they can be rerun. - -ONE BRANCH PER REPO, not one per version, and reused in place. Per-version -branches look tidier and are a trap: two releases close together open two PRs -against the same `rev:` line, and when the OLDER one merges it moves that line -out from under the newer branch, which goes CONFLICTING. v0.4.0 and v0.5.0 were -two minutes apart and left 10 repos exactly there. - -What makes that bad rather than merely untidy is the shape of the failure: a -conflicting pull request runs no `pull_request` workflows AT ALL, so its -required check is ABSENT rather than red. Auto-merge never fires, nothing -reports a failure, and the PR sits looking green forever. So this also asks -GitHub whether each pull request can actually merge, and a run that leaves one -unmergeable FAILS -- the same reason this script already refuses to finish -quietly (RT#1553, below). - -Usage: scripts/bump_consumers.py vX.Y.Z [--only repo,repo] [--dry-run] -Needs GH_TOKEN able to push branches and open PRs across the org. -""" - -from __future__ import annotations - -import argparse -import base64 -import json -import re -import subprocess -import sys -import tempfile -import time -from pathlib import Path - -ORG = "crunchtools" -SELF = "python-sdlc" -# One per repo, reused and force-updated each release. See the module docstring. -BRANCH = f"chore/{SELF}" -PIN = re.compile( - r"(-\s*repo:\s*https://github\.com/crunchtools/python-sdlc(?:\.git)?\s*\n\s*rev:\s*)(v\d+\.\d+\.\d+)" -) - - -def gh(*args: str) -> str: - return subprocess.run(["gh", *args], capture_output=True, text=True, check=True).stdout - - -def org_repos() -> list[str]: - out = gh( - "api", - "--paginate", - f"orgs/{ORG}/repos?per_page=100", - "--jq", - ".[] | select(.archived | not) | .name", - ) - return sorted(name for name in out.split() if name != SELF) - - -def pinned_rev(repo: str) -> str | None: - """The python-sdlc rev a repo pins, or None if it does not pin one.""" - proc = subprocess.run( - ["gh", "api", f"repos/{ORG}/{repo}/contents/.pre-commit-config.yaml"], - capture_output=True, - text=True, - check=False, - ) - # Only a missing file means "does not pin". A rate limit, an auth failure - # or a timeout must fail the run, or the repo is silently skipped and the - # run reports success without having bumped it. - if proc.returncode != 0: - if "HTTP 404" in proc.stderr: - return None - raise RuntimeError(f"cannot read {repo}'s .pre-commit-config.yaml: {proc.stderr.strip()}") - raw = proc.stdout - text = base64.b64decode(json.loads(raw)["content"]).decode("utf-8", "replace") - match = PIN.search(text) - return match.group(2) if match else None - - -def open_bump(repo: str) -> str | None: - """The bump pull request already open on this repo's branch, or None.""" - return ( - gh( - "pr", - "list", - "-R", - f"{ORG}/{repo}", - "--head", - BRANCH, - "--state", - "open", - "--json", - "url", - "--jq", - ".[0].url // empty", - ).strip() - or None - ) - - -# How many times, and how long apart, the mergeability pass asks again. Asked -# once per ROUND for the repos still unanswered, not once per repo: see -# conflicting(). -ROUNDS = 3 -PAUSE = 5 - - -def conflicting(opened: dict[str, str]) -> tuple[list[str], list[str]]: - """Which of these pull requests cannot merge, and which GitHub would not say. - - Asked AFTER every repo has been bumped rather than inside bump(), and this - is the difference between a question and a wait. GitHub computes - mergeability asynchronously, so asked immediately after a push the answer is - always UNKNOWN; polling per repo would spend PAUSE seconds of a release run - per unanswered repo, serially, for an answer that arrives for free once the - rest of the fleet has been bumped. Across 33 repos that is minutes of a - release doing nothing. Here each round asks only the repos still unanswered. - - Returns (cannot merge, no answer). UNKNOWN is deliberately not a failure, - and neither is a query the API refuses: a release must not go red because - GitHub was slow or momentarily unavailable. Both are reported by name, so a - guard that quietly answered for nobody is visible rather than assumed. - """ - pending = dict(opened) - cannot: list[str] = [] - for attempt in range(ROUNDS): - if attempt: - time.sleep(PAUSE) - for repo, url in list(pending.items()): - try: - state = gh( - "pr", - "view", - url, - "-R", - f"{ORG}/{repo}", - "--json", - "mergeable", - "--jq", - ".mergeable", - ).strip() - except subprocess.CalledProcessError as exc: - # Named, and survived. Outside the main loop's per-repo handler, - # an exception here would end the whole run with a traceback and - # no summary -- and the summary is the thing this script exists - # to print (RT#1553, below). One repo the API will not answer - # for is reported as unanswered, like UNKNOWN: a release must not - # go red because a query failed. - print( - f" warning: {repo}: cannot read whether it can merge: {exc.stderr.strip()}", - file=sys.stderr, - ) - del pending[repo] - continue - if not state or state == "UNKNOWN": - continue - del pending[repo] - if state == "CONFLICTING": - cannot.append(repo) - if not pending: - break - return cannot, sorted(pending) - - -def bump(repo: str, version: str) -> str: - branch = BRANCH - with tempfile.TemporaryDirectory() as workdir: - root = Path(workdir) / repo - gh("repo", "clone", f"{ORG}/{repo}", str(root), "--", "--depth", "1", "-q") - config = root / ".pre-commit-config.yaml" - text, n = PIN.subn(rf"\g<1>{version}", config.read_text(encoding="utf-8")) - if n != 1: - raise RuntimeError(f"expected one python-sdlc pin, found {n}") - config.write_text(text, encoding="utf-8") - - def git(*a: str) -> None: - subprocess.run(["git", "-C", str(root), *a], check=True, capture_output=True) - - # -B, not -b: the branch is this script's own and may already exist from - # an earlier release. The clone is the CURRENT default branch, so the - # rewrite is always against what the repo pins right now. - git("checkout", "-q", "-B", branch) - git( - "-c", - "user.name=crunchtools-ci", - "-c", - "user.email=ci@crunchtools.com", - "commit", - "-qam", - f"chore: python-sdlc {version}", - ) - # Forced, because this branch carries one commit that is rewritten each - # release rather than a history worth keeping. - git("push", "-q", "-u", "--force", "origin", branch) - - title = f"chore: python-sdlc {version}" - body = ( - f"Moves the python-sdlc pin to {version}. Its own CI decides whether " - f"it lands; it merges itself when green.\n\n" - f"One branch per repo, reused each release, so two releases close " - f"together cannot leave two pull requests fighting over the same line." - ) - url = open_bump(repo) - if url: - # Reused rather than opened again. The force-push above has already - # moved it to this version; this only makes it say so. - gh("pr", "edit", url, "-R", f"{ORG}/{repo}", "--title", title, "--body", body) - print(f" updated in place: {url}") - else: - url = gh( - "pr", - "create", - "-R", - f"{ORG}/{repo}", - "--head", - branch, - "--title", - title, - "--body", - body, - ).strip() - try: - gh("pr", "merge", url, "--auto", "--merge") - except subprocess.CalledProcessError as exc: - reason = exc.stderr.strip() - print(f" warning: {repo}: auto-merge not queued: {reason}", file=sys.stderr) - # Whether it can actually merge is asked once, for the whole fleet, - # after this loop has finished. See conflicting(). - return url - - -def survey(repos: list[str], version: str) -> tuple[list[str], list[str], list[str]]: - """Split repos into those pinning `version`, those behind it, and unreadable ones.""" - current, stale, failed = [], [], [] - for repo in repos: - try: - rev = pinned_rev(repo) - except RuntimeError as read_error: - print(f"{repo}: {read_error}", file=sys.stderr) - failed.append(repo) - continue - if rev is None: - continue - if rev == version: - current.append(repo) - continue - print(f"{repo}: {rev} -> {version}") - stale.append(repo) - return current, stale, failed - - -def main() -> int: - ap = argparse.ArgumentParser() - ap.add_argument("version") - ap.add_argument("--only", help="comma-separated repo names") - ap.add_argument("--dry-run", action="store_true") - args = ap.parse_args() - if not re.fullmatch(r"v\d+\.\d+\.\d+", args.version): - print(f"not a release tag: {args.version}", file=sys.stderr) - return 2 - - repos = args.only.split(",") if args.only else org_repos() - current, stale, failed = survey(repos, args.version) - opened: dict[str, str] = {} - if not args.dry_run: - for repo in stale: - try: - url = bump(repo, args.version) - print(f" {url}") - opened[repo] = url - except (subprocess.CalledProcessError, RuntimeError) as bump_error: - detail = getattr(bump_error, "stderr", "") or "" - print(f" FAILED: {bump_error}: {detail}".rstrip(": "), file=sys.stderr) - failed.append(repo) - - # One pass over everything just opened. A pull request that cannot merge is - # the one failure here that NOTHING else reports: a conflicting pull request - # runs no `pull_request` workflows, so its required check is absent rather - # than red, auto-merge never fires, and it sits looking green indefinitely. - # v0.4.0 and v0.5.0 two minutes apart left 10 repos exactly there. - cannot: list[str] = [] - if opened: - cannot, unanswered = conflicting(opened) - for repo in cannot: - print( - f" CANNOT MERGE: {opened[repo]} conflicts with its base. " - f"Nothing else will report this.", - file=sys.stderr, - ) - failed.append(repo) - if unanswered: - print( - f" note: GitHub would not say whether these can merge: {', '.join(unanswered)}", - file=sys.stderr, - ) - - # Always say what happened. A run with nothing to do and a run that could - # not open a pull request both used to print nothing at all, which is how - # RT#1553 -- the App had no `pull_requests: write` -- survived two releases - # looking like a job that had half worked. - # `failed` also carries repos whose config could not be READ, which were - # never opened, so it is not what to subtract here. - did = f"{len(stale)} would be bumped" if args.dry_run else f"{len(opened) - len(cannot)} bumped" - print( - f"{len(current) + len(stale)} repo(s) pin python-sdlc: " - f"{did} to {args.version}, {len(current)} already there, {len(failed)} failed" - ) - if not current and not stale and not failed: - print("no repo pins python-sdlc; nothing was bumped", file=sys.stderr) - if failed: - print(f"rerun with --only {','.join(failed)}", file=sys.stderr) - return 1 - return 0 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/scripts/convert_repo.py b/scripts/convert_repo.py index 17712ca..051915f 100755 --- a/scripts/convert_repo.py +++ b/scripts/convert_repo.py @@ -37,8 +37,10 @@ import argparse import difflib +import json import re import sys +import tomllib from pathlib import Path try: @@ -377,6 +379,55 @@ def float_sdlc_step(name: str, text: str) -> str: return PINNED_STEP.sub(lambda m: f"{m.group('i')}{FLOATING_CI_LINE}\n", text, count=1) +# gatehouse#150's change to the guard's script, from new back to old: the one +# earlier script a fleet copy may carry and still be brought up to date. +GUARD_BEFORE_150 = ( + ( + "a file out of .github/workflows/ or .sdlc/ unseen", + "a workflow out of .github/workflows/ unseen", + ), + ("'^(\\.github/workflows|\\.sdlc)/'", "'^\\.github/workflows/'"), + ("modifies .github/workflows/ or .sdlc/ —", "modifies .github/workflows/ —"), +) + + +def prior_guard(run: str) -> str: + """The guard script as it was before gatehouse#150.""" + for after, prior in GUARD_BEFORE_150: + run = run.replace(after, prior) + return run + + +def same_gatehouse(current: dict, canonical: dict) -> bool: + """`current` is examples/gatehouse.yml in all but comments, or its guard is the one before. + + gatehouse#150 changed the guard's script (it now refuses a fork's change to + .sdlc/ too). A copy whose guard script is exactly the one before that is + brought up to date; any other difference in any step, the guard's + included, keeps the file as it is. + """ + if current == canonical: + return True + guard = (current.get("jobs") or {}).get("guard") + new = (canonical.get("jobs") or {}).get("guard") or {} + steps = guard.get("steps") if isinstance(guard, dict) else None + if not isinstance(steps, list) or len(steps) != len(new.get("steps") or []): + return False + # Only a `run` that is exactly the pre-#150 script becomes the new one; + # every other field of every step is then compared as it is. + updated = [ + {**step, "run": want["run"]} + if isinstance(step, dict) + and isinstance(want, dict) + and isinstance(want.get("run"), str) + and step.get("run") == prior_guard(want["run"]) + else step + for step, want in zip(steps, new["steps"], strict=True) + ] + jobs = {**current["jobs"], "guard": {**guard, "steps": updated}} + return {**current, "jobs": jobs} == canonical + + def classify(files: list[Path]) -> tuple[list[Path], list[Path]]: """Workflows the CI stub covers whole, and ones that share an `sdlc ci` job.""" covered: list[Path] = [] @@ -426,7 +477,7 @@ def convert_workflows(repo: Path, planned: dict[Path, str | None], notes: list[s gatehouse = wf_dir / "gatehouse.yml" canonical = GATEHOUSE_TEMPLATE.read_text() current = gatehouse.read_text() if gatehouse.exists() else canonical - if parse(gatehouse.name, current) == parse(GATEHOUSE_TEMPLATE.name, canonical): + if same_gatehouse(parse(gatehouse.name, current), parse(GATEHOUSE_TEMPLATE.name, canonical)): planned[gatehouse] = canonical else: notes.append(f"kept: {rel(repo, gatehouse)} (differs from the stub in more than comments)") @@ -460,6 +511,116 @@ def refuse_links(repo: Path, paths: list[Path]) -> None: raise RefusedError(f"{rel(repo, part)} is a symlink; this script writes no links") +# ---------------------------------------------------------------- the config +# +# A converted repo must never meet "GATE CANNOT RUN" on its first run. Two +# shapes would: a stack this image names but has no checks for yet, and a +# Containerfile that CI cannot build without secrets. The planner's own rules, +# mirrored here, decide which repos those are. + +PLANNED_STACKS = ("node", "bootc", "docs") +CONTAINERFILES = ("Containerfile", "Dockerfile") +CONTAINER_CI = ["container-build", "container-smoke", "container-scan"] +# A build that needs a secret CI does not hand `sdlc ci`: a BuildKit secret +# mount, or Red Hat subscription content. The publish workflow keeps building +# these with the secrets it has. +BUILD_SECRETS = re.compile(r"--mount=type=secret|subscription-manager|\brhsm\b", re.I) +SKIP_STACKS_NOTE = ( + "# Written by python-sdlc's scripts/convert_repo.py: the image has no checks\n" + "# for these stacks yet, and a stack it cannot check makes every run exit 2.\n" +) +SKIP_BUILD_NOTE = ( + "# Written by python-sdlc's scripts/convert_repo.py: the Containerfile needs\n" + "# build secrets (a secret mount, or RHSM) that `sdlc ci` is not given. The\n" + "# publish workflow still builds it, with them.\n" +) + + +def needs_build_secrets(text: str) -> bool: + """A secret mount or RHSM in an instruction; a comment saying "no RHSM" is not one.""" + code = "\n".join(line for line in text.splitlines() if not line.lstrip().startswith("#")) + return BUILD_SECRETS.search(code) is not None + + +def detected_stacks(repo: Path) -> list[str]: + """The planner's detect_stacks(), over a checkout.""" + found = ["python"] if (repo / "pyproject.toml").is_file() else [] + cf = next((repo / f for f in CONTAINERFILES if (repo / f).is_file()), None) + bootc = cf is not None and any( + line.split()[0].upper() == "FROM" and "bootc" in line.lower() + for line in cf.read_text(errors="replace").splitlines() + if line.strip() + ) + if cf is not None and not bootc: + found.append("container") + if (repo / "package.json").is_file(): + found.append("node") + if bootc: + found.append("bootc") + return found or ["docs"] + + +def with_key(text: str, table: str, key: str, value: list[str], note: str) -> str: + """`text` with `key = value` set in `[table]`, written by line, not by a TOML writer.""" + line = note + f"{key} = {json.dumps(value)}\n" + header = re.search(rf"^\[{re.escape(table)}\][ \t]*(#.*)?$\n?", text, re.M) + if header is None: + return text.rstrip("\n") + ("\n\n" if text.strip() else "") + f"[{table}]\n{line}" + return text[: header.end()] + line + text[header.end() :] + + +def convert_config(repo: Path, planned: dict[Path, str | None], notes: list[str]) -> None: + pyproject = repo / "pyproject.toml" + path = pyproject if pyproject.is_file() else repo / "sdlc.toml" + text = planned.get(path) or (path.read_text() if path.is_file() else "") + try: + cfg = tomllib.loads(text).get("tool", {}).get("sdlc", {}) + except tomllib.TOMLDecodeError as exc: + raise RefusedError(f"{rel(repo, path)} is not TOML: {exc}") from exc + if pyproject.is_file() and "stacks" not in cfg and "skip-stacks" not in cfg: + return # a Python repo naming no stacks runs the python stack alone + drop = set(cfg.get("skip-stacks") or []) + active = [s for s in [*detected_stacks(repo), *(cfg.get("stacks") or [])] if s not in drop] + unbuilt = sorted({s for s in active if s in PLANNED_STACKS}) + ci = cfg.get("ci") or {} + cf = next((repo / f for f in CONTAINERFILES if (repo / f).is_file()), None) + secrets = ( + "container" in active + and cf is not None + and needs_build_secrets(cf.read_text(errors="replace")) + and "container-build" not in (ci.get("skip") or []) + ) + if unbuilt: + if "skip-stacks" in cfg: + raise RefusedError( + f"{rel(repo, path)} skip-stacks does not name {', '.join(unbuilt)}, " + f"which this image cannot check yet; add them by hand" + ) + text = with_key(text, "tool.sdlc", "skip-stacks", unbuilt, SKIP_STACKS_NOTE) + notes.append(f"skip-stacks: {', '.join(unbuilt)} in {rel(repo, path)} (not checked yet)") + if secrets: + if "skip" in ci: + raise RefusedError( + f"{rel(repo, path)} [tool.sdlc.ci] skip exists; the Containerfile needs build " + f"secrets, so add {', '.join(CONTAINER_CI)} to it by hand" + ) + text = with_key(text, "tool.sdlc.ci", "skip", CONTAINER_CI, SKIP_BUILD_NOTE) + notes.append(f"skip in ci: the container checks, in {rel(repo, path)} (build secrets)") + if not (unbuilt or secrets): + return + # Read back what was written: the stacks and the skips must be the ones + # meant, in a file that is still TOML, or nothing is written at all. + try: + after = tomllib.loads(text).get("tool", {}).get("sdlc", {}) + except tomllib.TOMLDecodeError as exc: + raise RefusedError(f"{rel(repo, path)} would not be TOML: {exc}") from exc + if not set(unbuilt) <= set(after.get("skip-stacks") or []) or ( + secrets and not set(CONTAINER_CI) <= set((after.get("ci") or {}).get("skip") or []) + ): + raise RefusedError(f"{rel(repo, path)}: the keys written did not read back as meant") + planned[path] = text + + def plan(repo: Path) -> tuple[dict[Path, str | None], list[str]]: """Every file the conversion writes (None: deletes), and what it kept.""" planned: dict[Path, str | None] = {repo / BOOTSTRAP_AT: BOOTSTRAP.read_text()} @@ -469,6 +630,7 @@ def plan(repo: Path) -> tuple[dict[Path, str | None], list[str]]: refuse_links(repo, [hooks, *planned, wf_dir, repo / "pyproject.toml", repo / "sdlc.toml"]) planned[hooks] = convert_hooks(hooks.read_text() if hooks.exists() else "") convert_workflows(repo, planned, notes) + convert_config(repo, planned, notes) notes.append(f"writes: {BOOTSTRAP_AT}, executable (python-sdlc's bin/sdlc-bootstrap)") # An image reference in anything that survives, and in the repo's config. candidates = [repo / "pyproject.toml", repo / "sdlc.toml"] @@ -486,22 +648,37 @@ def plan(repo: Path) -> tuple[dict[Path, str | None], list[str]]: return planned, notes -def verify(repo: Path, planned: dict[Path, str | None]) -> None: - """Refuse a plan whose files do not load, or do not wire what they should. - - Every YAML file this writes is parsed back, and the result is held to the - wiring itself -- the floating hooks, both hook types, a job named `sdlc ci` - running the bootstrap -- rather than to how the text was produced. - """ +def parse_back(repo: Path, planned: dict[Path, str | None]) -> None: + """Refuse a plan with a YAML or TOML file that would not load.""" for path, text in planned.items(): if text is not None and path.suffix in (".yml", ".yaml"): parse(rel(repo, path), text) - hooks = parse(".pre-commit-config.yaml", planned[repo / ".pre-commit-config.yaml"] or "") + if text is not None and path.suffix == ".toml": + try: + tomllib.loads(text) + except tomllib.TOMLDecodeError as exc: + raise RefusedError(f"{rel(repo, path)} would not be TOML: {exc}") from exc + + +def verify_hooks(text: str) -> None: + """Refuse a hooks file that does not install both types and float.""" + hooks = parse(".pre-commit-config.yaml", text) types = hooks.get("default_install_hook_types") or [] if not (isinstance(types, list) and {"pre-commit", "pre-push"} <= set(types)): raise RefusedError("the converted hooks file does not install both hook types") if not is_floating(hooks.get("repos") or []): raise RefusedError("the converted hooks file does not run .sdlc/bin/sdlc as its only sdlc") + + +def verify(repo: Path, planned: dict[Path, str | None]) -> None: + """Refuse a plan whose files do not load, or do not wire what they should. + + Every YAML file this writes is parsed back, and the result is held to the + wiring itself -- the floating hooks, both hook types, a job named `sdlc ci` + running the bootstrap -- rather than to how the text was produced. + """ + parse_back(repo, planned) + verify_hooks(planned[repo / ".pre-commit-config.yaml"] or "") if planned[repo / BOOTSTRAP_AT] != BOOTSTRAP.read_text(): raise RefusedError(f"{BOOTSTRAP_AT} would not be bin/sdlc-bootstrap") wf_dir = repo / ".github" / "workflows" diff --git a/scripts/update_tools.py b/scripts/update_tools.py index 476d590..9588a4d 100755 --- a/scripts/update_tools.py +++ b/scripts/update_tools.py @@ -5,7 +5,7 @@ changes nothing: the build pulls the same version it pulled before. What a release has to do is MOVE THE PIN. This script does that, and when anything moved it also bumps this image's own PATCH version, so the change ships as a -python-sdlc release that every Python repo then receives as a bump PR. +python-sdlc release, which every converted repo receives through sdlc:latest. Run by .github/workflows/update-tools.yml. Writes `changed=true|false` and a one-line `summary` to $GITHUB_OUTPUT when that is set. diff --git a/sdlc b/sdlc index 3bad699..233c8cc 100755 --- a/sdlc +++ b/sdlc @@ -111,6 +111,9 @@ CLEAN = 0 FOUND = 1 CANNOT_RUN = 2 DEFERRED = 3 +# Not judged, printed as SKIPPED and never the command's exit. bin/sdlc honours +# it from containerfile-lint and the smoke run only; anywhere else it is a 1. +SKIPPED = 4 class CannotRunError(Exception): @@ -572,6 +575,9 @@ def kind_of(cfg: dict, name: str) -> str | None: # host-up the same, after a first word that is a number of # seconds: a `podman run` that is still running when # they are up is a pass (container-smoke) +# host-up-advisory host-up whose finding is printed as ADVISORY and +# does not fail the command (container-smoke without +# `gate = true`) # # bin/sdlc refuses a host line that does not start with `podman`, so the host # contract is held by the launcher rather than trusted to the config. It splits @@ -582,7 +588,7 @@ def kind_of(cfg: dict, name: str) -> str | None: UNIT_SEPARATOR = "\x1f" # Modes whose payload is a separator-joined argv, already checked word by word. -HOST_MODES = ("host", "host-up") +HOST_MODES = ("host", "host-up", "host-up-advisory") FORBIDDEN_IN_PLAN = ("\t", "\n", "\r", UNIT_SEPARATOR) @@ -998,7 +1004,14 @@ def check_container_smoke(cfg: dict, name: str, s: dict) -> list[tuple[str, str, # says, and bin/sdlc checks the word that follows it. argv += ["--", container_image(cfg, s), *names(s.get("args"), f"{name}.args")] payload = UNIT_SEPARATOR.join([str(seconds), *(plain(a, name) for a in argv)]) - return [(name, "host-up", payload)] + # Advisory unless the repo says `gate = true`: the fleet's images need env, + # args, network or capabilities written down before a smoke run can judge + # them, and a wave that turned them all red would teach SDLC_SKIP. A + # finding is printed and marked ADVISORY, never a pass. + gate = s.get("gate", False) + if not isinstance(gate, bool): + cannot(f"{name}: gate must be true or false") + return [(name, "host-up" if gate else "host-up-advisory", payload)] # The gate's own image in a host argv. bin/sdlc replaces this word with the @@ -1069,8 +1082,13 @@ HOST_CHECKS = { # What the fleet does on purpose, so hadolint is told not to call it a defect. # DL3007: base tags float, because the Container Image profile rebuilds every # week to pick up errata through them. DL3041: dnf installs are not pinned to a -# package version, for the same reason. -HADOLINT_FLEET_IGNORES = ("DL3007", "DL3041") +# package version, for the same reason. SC1072: ShellCheck's "fix any mentioned +# problems" on an exec-form RUN ["python3", "-c", ...], which it misreads as +# shell -- the form a stage with no /bin/sh has to use. +HADOLINT_FLEET_IGNORES = ("DL3007", "DL3041", "SC1072") +# hadolint's parse failure (DL1000) prints as file:line:column, which no rule +# finding does. A Containerfile hadolint cannot read is not judged by it. +HADOLINT_PARSE_FAILURE = re.compile(r"^\S+:\d+:\d+ ", re.M) def t_containerfile_lint(s: dict) -> int: @@ -1101,11 +1119,19 @@ def t_containerfile_lint(s: dict) -> int: cannot(f"containerfile-lint: threshold must be one of {', '.join(HADOLINT_THRESHOLDS)}") cmd += ["--failure-threshold", str(threshold)] step(f"hadolint {' '.join(files)}") - rc = run([*cmd, *files])[0] + rc, out = run([*cmd, *files], tee=True) # hadolint has two answers: 0 clean, 1 found something at the threshold. # Anything else is the tool, not the Containerfile. if rc not in (CLEAN, FOUND): cannot(f"hadolint could not run (exit {rc})") + if rc == FOUND and HADOLINT_PARSE_FAILURE.search(out): + # A heredoc RUN, for one, which hadolint cannot parse though podman + # builds it. The build is the judge of that; this check says so. + print( + "ADVISORY: hadolint could not parse this Containerfile, so it is not " + "judged here; container-build still builds it" + ) + return SKIPPED return rc diff --git a/tests/contract.sh b/tests/contract.sh index a59d9b6..46da660 100755 --- a/tests/contract.sh +++ b/tests/contract.sh @@ -481,6 +481,29 @@ if [ "$rc" -eq 1 ]; then else bad "containerfile-lint on a relative WORKDIR exited $rc, want 1" fi +# Exec-form RUN with a quoted script: hadolint's ShellCheck pass raises SC1072 +# on it (about 13 mcp-* repos), so the defaults ignore SC1072. +printf 'FROM quay.io/hummingbird/python:3.14\nRUN ["python3", "-c", "import sys; print(sys.version)"]\n' \ + > "$CFIX/Containerfile" +rc=0 +out=$(lint) || rc=$? +if [ "$rc" -eq 0 ] && ! printf '%s' "$out" | grep -q SC1072; then + ok "containerfile-lint ignores SC1072 on an exec-form RUN" +else + bad "containerfile-lint on an exec-form RUN exited $rc" + printf '%s\n' "$out" | sed 's/^/ /' | tail -6 +fi +# A RUN heredoc hadolint cannot parse: advisory, printed, not a finding. +printf 'FROM quay.io/hummingbird/python:3.14\nRUN python3 - <<'"'"'EOF'"'"'\nprint(1)\nEOF\nRUN true\n' \ + > "$CFIX/Containerfile" +rc=0 +out=$(lint) || rc=$? +if [ "$rc" -eq 4 ] && printf '%s' "$out" | grep -q 'ADVISORY: hadolint could not parse'; then + ok "a Containerfile hadolint cannot parse is advisory (exit 4, printed)" +else + bad "containerfile-lint on an unparsable Containerfile exited $rc, want 4" + printf '%s\n' "$out" | sed 's/^/ /' | tail -6 +fi # A repo's own .hadolint.yaml takes the defaults away: here it asks for warnings. printf 'FROM registry.access.redhat.com/ubi10/ubi-init:latest\n' > "$CFIX/Containerfile" printf 'failure-threshold: warning\n' > "$CFIX/.hadolint.yaml" diff --git a/tests/test_container_stack.sh b/tests/test_container_stack.sh index e4fca8f..5565c67 100755 --- a/tests/test_container_stack.sh +++ b/tests/test_container_stack.sh @@ -29,6 +29,10 @@ fixture() { # name entrypoint-json git -C "$WORK/$1" -c user.email=t@t -c user.name=t commit -qm fixture } +gate() { # name: opt the fixture's smoke run into gating + printf '[tool.sdlc.ci.container-smoke]\ngate = true\n' >> "$WORK/$1/sdlc.toml" +} + fixture serves '["python3", "-m", "http.server", "8080"]' rc=0 (cd "$WORK/serves" && SDLC_IMAGE=$IMAGE "$HERE/bin/sdlc" ci) > "$WORK/serves.out" 2>&1 || rc=$? @@ -50,12 +54,24 @@ if [ "$rc" -ne 0 ]; then bad "the crashing fixture did not build (exit $rc), so its smoke run proves nothing" tail -10 "$WORK/build.out" | sed 's/^/ /' else + # Ungated, the smoke run is advisory: the finding is printed, the exit is 0. + rc=0 + (cd "$WORK/crashes" && SDLC_IMAGE=$IMAGE "$HERE/bin/sdlc" container-smoke) > "$WORK/crashes.out" 2>&1 || rc=$? + if [ "$rc" -eq 0 ] \ + && grep -q "No module named 'missing_module'" "$WORK/crashes.out" \ + && grep -q '== container-smoke FOUND SOMETHING, ADVISORY' "$WORK/crashes.out"; then + ok "ungated, an image that dies on start is an advisory finding (exit 0, printed)" + else + bad "advisory container-smoke on a crashing image exited $rc, want 0 with ADVISORY" + tail -10 "$WORK/crashes.out" | sed 's/^/ /' + fi + gate crashes rc=0 (cd "$WORK/crashes" && SDLC_IMAGE=$IMAGE "$HERE/bin/sdlc" container-smoke) > "$WORK/crashes.out" 2>&1 || rc=$? if [ "$rc" -eq 1 ] \ && grep -q "No module named 'missing_module'" "$WORK/crashes.out" \ && grep -q 'the container exited 1 within' "$WORK/crashes.out"; then - ok "an image that dies on start fails container-smoke (exit 1, its own error)" + ok "gated, an image that dies on start fails container-smoke (exit 1, its own error)" else bad "container-smoke on a crashing image exited $rc, want 1 from the entrypoint" tail -10 "$WORK/crashes.out" | sed 's/^/ /' @@ -70,6 +86,7 @@ rc=0 if [ "$rc" -ne 0 ]; then bad "the missing-entrypoint fixture did not build (exit $rc)" else + gate noentry rc=0 (cd "$WORK/noentry" && SDLC_IMAGE=$IMAGE "$HERE/bin/sdlc" container-smoke) > "$WORK/noentry.out" 2>&1 || rc=$? if [ "$rc" -eq 1 ] && grep -q 'could not start its entrypoint' "$WORK/noentry.out"; then diff --git a/tests/test_convert_repo.py b/tests/test_convert_repo.py index 3f24896..e3bcc8c 100755 --- a/tests/test_convert_repo.py +++ b/tests/test_convert_repo.py @@ -27,6 +27,7 @@ import subprocess import sys import tempfile +import tomllib from pathlib import Path import yaml @@ -668,6 +669,168 @@ def load(text: str) -> dict: print(exc) check("the guard accepts the floating wiring", accepted) +# ------------------------------- config: a converted repo never exits 2 at once + + +HOOKED = {".pre-commit-config.yaml": PINNED_HOOKS} + + +def sdlc_cfg(repo: Path, name: str = "sdlc.toml") -> dict: + return tomllib.loads(snapshot(repo).get(name, "")).get("tool", {}).get("sdlc", {}) + + +for label, files, want in ( + ("node", {"package.json": "{}\n"}, ["node"]), + ("bootc", {"Containerfile": "FROM quay.io/fedora/fedora-bootc:42\n"}, ["bootc"]), + ("docs", {"README.md": "# hi\n"}, ["docs"]), +): + repo = make_repo({**HOOKED, **files}) + first = convert(repo, apply=True) + again = convert(repo, apply=True) + check( + f"a {label} repo gets skip-stacks for the stack this image cannot check yet", + first.returncode == 0 and sdlc_cfg(repo).get("skip-stacks") == want, + first.stderr + first.stdout, + ) + check( + f"a {label} repo: the second run changes nothing", + again.returncode == 0 and again.stdout == "", + again.stderr + again.stdout, + ) + +repo = make_repo( + { + **HOOKED, + "package.json": "{}\n", + "Containerfile": "FROM quay.io/fedora/fedora-bootc:42\n", + "sdlc.toml": '[tool.sdlc]\nskip-stacks = ["node"]\n', + } +) +refused = convert(repo, apply=True) +check( + "a skip-stacks that leaves an unchecked stack out is refused, not rewritten", + refused.returncode == REFUSED + and "bootc" in refused.stderr + and sdlc_cfg(repo).get("skip-stacks") == ["node"], + refused.stderr, +) + +RHSM_CONTAINERFILE = ( + "FROM registry.access.redhat.com/ubi10/ubi:latest\n" + "RUN --mount=type=secret,id=rhsm subscription-manager register && dnf -y install httpd\n" +) +repo = make_repo( + { + **HOOKED, + "Containerfile": RHSM_CONTAINERFILE, + "sdlc.toml": '[tool.sdlc.push]\nskip = ["constitution"]\n', + } +) +first = convert(repo, apply=True) +again = convert(repo, apply=True) +cfg = sdlc_cfg(repo) +check( + "a Containerfile that needs build secrets skips the container checks in `sdlc ci`", + first.returncode == 0 + and cfg.get("ci", {}).get("skip") == ["container-build", "container-smoke", "container-scan"] + and cfg.get("push", {}).get("skip") == ["constitution"], + first.stderr + snapshot(repo).get("sdlc.toml", ""), +) +check("build secrets: the second run changes nothing", again.returncode == 0 and again.stdout == "") + +repo = make_repo( + { + **HOOKED, + "Containerfile": "FROM quay.io/hummingbird/python:3.14\n# no RHSM needed: UBI content only\n", + } +) +done = convert(repo, apply=True) +check( + "a comment that mentions RHSM is not a build secret", + done.returncode == 0 and "sdlc.toml" not in snapshot(repo), + done.stderr + done.stdout, +) + +repo = make_repo( + { + **HOOKED, + "Containerfile": RHSM_CONTAINERFILE, + "sdlc.toml": '[tool.sdlc.ci]\nskip = ["container-scan"]\n', + } +) +refused = convert(repo, apply=True) +check( + "build secrets beside an existing ci skip are refused, not merged", + refused.returncode == REFUSED and sdlc_cfg(repo)["ci"]["skip"] == ["container-scan"], + refused.stderr, +) + +COMPAT = '[project]\nname = "x"\nversion = "0"\n' +repo = make_repo({**HOOKED, "pyproject.toml": COMPAT, "package.json": "{}\n"}) +done = convert(repo, apply=True) +check( + "a Python repo naming no stacks keeps its pyproject untouched", + done.returncode == 0 and snapshot(repo)["pyproject.toml"] == COMPAT, + done.stderr, +) + +# ----------------------------- gatehouse: an older guard is brought up to date + +# The guard as the fleet copied it, before gatehouse#150 added .sdlc/. +OLD_GUARD = ( + GATEHOUSE.replace("^(\\.github/workflows|\\.sdlc)/", "^\\.github/workflows/") + .replace( + "a file out of .github/workflows/ or .sdlc/ unseen", + "a workflow out of .github/workflows/ unseen", + ) + .replace("modifies .github/workflows/ or .sdlc/ —", "modifies .github/workflows/ —") +) +repo = make_repo({**HOOKED, ".github/workflows/gatehouse.yml": OLD_GUARD}) +done = convert(repo, apply=True) +check( + "an older gatehouse.yml guard is replaced by the example's, which covers .sdlc/", + OLD_GUARD != GATEHOUSE + and done.returncode == 0 + and snapshot(repo)[".github/workflows/gatehouse.yml"] == GATEHOUSE, + done.stderr, +) +CUSTOM = GATEHOUSE.replace("review.yml@v0", "review.yml@v1") +repo = make_repo({**HOOKED, ".github/workflows/gatehouse.yml": CUSTOM}) +done = convert(repo, apply=True) +check( + "a gatehouse.yml customized beyond the guard is kept", + CUSTOM != GATEHOUSE + and done.returncode == 0 + and snapshot(repo)[".github/workflows/gatehouse.yml"] == CUSTOM, + done.stderr, +) +GUARD_CHANGED = OLD_GUARD.replace( + 'echo "No workflow files changed."', 'echo "No workflow files changed."\n make audit' +) +repo = make_repo({**HOOKED, ".github/workflows/gatehouse.yml": GUARD_CHANGED}) +done = convert(repo, apply=True) +check( + "a guard script changed beyond gatehouse#150's lines is kept, not overwritten", + GUARD_CHANGED != OLD_GUARD + and done.returncode == 0 + and snapshot(repo)[".github/workflows/gatehouse.yml"] == GUARD_CHANGED, + done.stderr, +) +GUARD_ENV = OLD_GUARD.replace( + " EVENT: ${{ github.event_name }}\n", + " EVENT: ${{ github.event_name }}\n EXTRA: kept\n", +) +repo = make_repo({**HOOKED, ".github/workflows/gatehouse.yml": GUARD_ENV}) +done = convert(repo, apply=True) +check( + "an older guard whose step has its own env is kept, not overwritten", + GUARD_ENV != OLD_GUARD + and done.returncode == 0 + and snapshot(repo)[".github/workflows/gatehouse.yml"] == GUARD_ENV, + done.stderr, +) +check("the gatehouse example's guard covers .sdlc/", "|\\.sdlc)/" in GATEHOUSE) + def main() -> int: print() diff --git a/tests/test_host_contract.sh b/tests/test_host_contract.sh index cfbc468..d0d386c 100755 --- a/tests/test_host_contract.sh +++ b/tests/test_host_contract.sh @@ -103,6 +103,7 @@ case " \$* " in go-up-dies-255) _full='false 255 false' ;; go-up-dies-3) _full='false 3 false' ;; go-up-oom) _full='false 137 true' ;; + go-up-exit0-oom) _full='false 0 true' ;; *) exit 125 ;; esac case " \$* " in @@ -180,6 +181,17 @@ case " \$* " in *" plan lanes-skip "*) printf 'b${TAB}one${TAB}image-ro${TAB}quiet-ok\n' printf 'up-systemd-ep${TAB}two${TAB}host-up${TAB}1${US}podman${US}run${US}--${US}localhost/systemd-ep:sdlc\n' ;; + *" plan up-adv-dies "*) + printf 'up-adv-dies${TAB}main${TAB}host-up-advisory${TAB}1${US}podman${US}run${US}--${US}localhost/x:sdlc${US}go-up-dies-255\n' ;; + *" plan up-adv-podman "*) + printf 'up-adv-podman${TAB}main${TAB}host-up-advisory${TAB}1${US}podman${US}run${US}--${US}localhost/x:sdlc${US}go-up-podman\n' ;; + *" plan lanes-adv "*) + printf 'b${TAB}one${TAB}image-ro${TAB}quiet-ok\n' + printf 'up-adv-dies${TAB}two${TAB}host-up-advisory${TAB}1${US}podman${US}run${US}--${US}localhost/x:sdlc${US}go-up-dies-255\n' ;; + *" plan lint-unparsed "*) + printf 'containerfile-lint${TAB}main${TAB}image-ro${TAB}containerfile-lint\n' ;; + *" plan rc-five "*) + printf 'rc-five${TAB}main${TAB}image-ro${TAB}exit-five\n' ;; *" plan rc-four "*) printf 'rc-four${TAB}main${TAB}image-ro${TAB}exit-four\n' ;; *" plan up-"*) @@ -211,6 +223,8 @@ case "\$_last" in rc-two) printf 'RAN rc-two\n'; exit 2 ;; rc-three) printf 'RAN rc-three\n'; exit 3 ;; exit-four) printf 'RAN exit-four\n'; exit 4 ;; + exit-five) printf 'RAN exit-five\n'; exit 5 ;; + containerfile-lint) printf 'ADVISORY: hadolint could not parse\n'; exit 4 ;; slow-rc-one) printf 'RAN slow-rc-one\n'; sleep 2; exit 1 ;; very-slow-rc-one) printf 'RAN very-slow-rc-one\n'; sleep 6; printf 'LAST LINE\n'; exit 1 ;; step-one) printf 'RAN step-one\n'; sleep 1 ;; @@ -272,7 +286,7 @@ printf '\n== a run given time to stay up\n\n' # passes; any other exit, the OOM killer, or an entrypoint that cannot start # (126/127) is a finding; podman failing to create it (125) is a 2. A 255 is # just another exit -- no clock is consulted. -for case_ in up-stays:0 up-exits-0:0 up-dies-255:1 up-dies-3:1 up-oom:1 up-127:1 \ +for case_ in up-stays:0 up-exits-0:0 up-exit0-oom:0 up-dies-255:1 up-dies-3:1 up-oom:1 up-127:1 \ up-podman:2 up-build:2 up-noseconds:2; do name=${case_%:*}; want=${case_#*:} log_mark @@ -335,6 +349,47 @@ else bad "rc-four exited $rc: $(tail -2 "$RUN/out")" fi +# container-smoke is advisory unless a repo gates it: a finding is printed as +# FOUND SOMETHING, ADVISORY -- never "passed" -- and does not fail the command. +# Podman failing to create the container is still a 2. +rc=0 +PATH="$SHIM" sh bin/sdlc up-adv-dies > "$RUN/out" 2>&1 || rc=$? +if [ "$rc" -eq 0 ] && grep -q '== up-adv-dies FOUND SOMETHING, ADVISORY: not gating' "$RUN/out" \ + && grep -q 'gate = true in \[tool.sdlc.ci.up-adv-dies\]' "$RUN/out" && ! grep -q 'passed in' "$RUN/out"; then + ok "an advisory smoke finding is printed as ADVISORY and the command exits 0" +else + bad "up-adv-dies exited $rc: $(tail -3 "$RUN/out")" +fi +rc=0 +PATH="$SHIM" sh bin/sdlc up-adv-podman > "$RUN/out" 2>&1 || rc=$? +if [ "$rc" -eq 2 ]; then + ok "an advisory smoke run podman could not create is still a 2" +else + bad "up-adv-podman exited $rc, want 2" +fi +rc=0 +PATH="$SHIM" SDLC_CPUS=4 sh bin/sdlc lanes-adv > "$RUN/out" 2>&1 || rc=$? +if [ "$rc" -eq 0 ] && grep -q '== up-adv-dies FOUND SOMETHING, ADVISORY' "$RUN/out" \ + && grep -q '== b passed' "$RUN/out"; then + ok "in a lane, an advisory finding is printed as ADVISORY and the run exits 0" +else + bad "lanes-adv exited $rc: $(tail -4 "$RUN/out")" +fi +rc=0 +PATH="$SHIM" sh bin/sdlc rc-five > "$RUN/out" 2>&1 || rc=$? +if [ "$rc" -eq 1 ] && ! grep -q ADVISORY "$RUN/out"; then + ok "a 5 from a check in the image is a finding, not advisory" +else + bad "rc-five exited $rc: $(tail -2 "$RUN/out")" +fi +rc=0 +PATH="$SHIM" sh bin/sdlc lint-unparsed > "$RUN/out" 2>&1 || rc=$? +if [ "$rc" -eq 0 ] && grep -q '== containerfile-lint SKIPPED, not judged' "$RUN/out"; then + ok "containerfile-lint may report SKIPPED when hadolint cannot parse" +else + bad "lint-unparsed exited $rc: $(tail -2 "$RUN/out")" +fi + # Every smoke cleanup, whatever the case, takes the anonymous volumes with it. if grep -q -- '^rm -f -v sdlc-smoke-' "$LOG" && ! grep -q -- '^rm -f sdlc-smoke-' "$LOG"; then ok "every smoke container is removed with -v" diff --git a/tests/test_plan.py b/tests/test_plan.py index 8e325ab..8c07cf3 100644 --- a/tests/test_plan.py +++ b/tests/test_plan.py @@ -681,10 +681,18 @@ def container_plan(target: str, sdlc_toml: str = "", **env) -> list[list[str]]: smoke = by_check["container-smoke"] words = smoke[3].split(S.UNIT_SEPARATOR) check( - "container-smoke is host-up, its first word the seconds", - smoke[2] == "host-up" and words[0] == str(S.SMOKE_SECONDS) and words[1:3] == ["podman", "run"], + "container-smoke is advisory by default, its first word the seconds", + smoke[2] == "host-up-advisory" + and words[0] == str(S.SMOKE_SECONDS) + and words[1:3] == ["podman", "run"], str(smoke), ) +gated = {r[0]: r for r in container_plan("ci", "[tool.sdlc.ci.container-smoke]\ngate = true\n")} +check("container-smoke gates with gate = true", gated["container-smoke"][2] == "host-up") +check( + "container-smoke gate must be a boolean", + cannot_run(container_plan, "ci", '[tool.sdlc.ci.container-smoke]\ngate = "yes"\n') is not None, +) check("container-smoke has no network by default", "--network" in words and "none" in words) check("container-smoke never pulls what it judges", "--pull=never" in words, str(words)) scan = by_check["container-scan"][3].split(S.UNIT_SEPARATOR)