diff --git a/.agents/generated-rules.json b/.agents/generated-rules.json index b23dbbb..abf47e8 100644 --- a/.agents/generated-rules.json +++ b/.agents/generated-rules.json @@ -1,7 +1,7 @@ { - "copier-template": "c297313eea2b16f7b713e0faf4a3672de05d6ea27a4ff374e659d3960d9784bc", + "copier-template": "d4711ceadf0cf1bb10d81a3a65b892b6bf0a0a20f40496fcb32407024152d15e", "documentation-toc": "52734c4c63f46babf96aeae1260407add66dbec2d997f90a25ef01b7e1479127", - "github-actions": "5b7aa1678f3d3798125fb1203a68a39d2122ff2e4f655df17d4e8db621a5a684", + "github-actions": "c7bc81aede69023b0715ce8c0712f8e147cf5c2b6d90acf6a47a912ba99a182f", "markdown-documentation": "027f47636d9d510e6cbd5505f9339c1aeee6f41dd823c0b478d409dde1ae4538", "shell-scripts": "bdeed1f2724292989168f25949c8d83245f1436c24a3b82dab10429f60790d64" } diff --git a/.agents/rules/copier-template.md b/.agents/rules/copier-template.md index 8fd82c2..83470db 100644 --- a/.agents/rules/copier-template.md +++ b/.agents/rules/copier-template.md @@ -4,7 +4,7 @@ These files are **rendered into consumer repositories** by `copier copy` and mer ## `copier.yml` -- `_skip_if_exists` lists **bootstrap-only** files (`pyproject.toml`, `.github/workflows/test.yml`); everything else under `template/` is updated on `copier update`. `_skip_if_exists` only protects files that exist: a file a consumer deletes is re-created on update, so optional files use a question-gated filename (`reusable_lint`, `test_workflow`). +- `_skip_if_exists` lists **bootstrap-only** files (`pyproject.toml`, agent guidance); everything else under `template/` is updated on `copier update`. `_skip_if_exists` only protects files that exist: a file a consumer deletes is re-created on update, so optional files use a question-gated filename (`agent_guidance`). A file removed from the template is deleted from consumers, local edits included. - Renaming or removing a question is **breaking** for existing `.copier-answers.yml` files; document it in **`CHANGELOG.md`** and **`docs/migration-guide.md`**. ## `template/baselines/ruff.toml` @@ -19,9 +19,9 @@ These files are **rendered into consumer repositories** by `copier copy` and mer ## `template/.pre-commit-config.yaml` -- Hook **`rev:`** values should match the versions pinned in **`template/pyproject.toml.jinja`** where applicable (ruff, mypy, pre-commit). +- Ruff and mypy are **local hooks** run with `uv run --frozen python -m`, so their versions come from the consumer's `uv.lock`; do not reintroduce mirrored `rev:` pins. Remote hooks keep a pinned **`rev:`**. - **`fail_fast: true`** is intentional baseline behavior unless policy changes. ## `template/.github/workflows/` -See **`.agents/rules/github-actions.md`** for reusable vs template caller expectations and pinning. +See **`.agents/rules/github-actions.md`** for the managed caller workflow and its pins. diff --git a/.agents/rules/github-actions.md b/.agents/rules/github-actions.md index 3905c71..6a15122 100644 --- a/.agents/rules/github-actions.md +++ b/.agents/rules/github-actions.md @@ -1,15 +1,16 @@ # Review: GitHub Actions -## Callable workflows (this repository) +## Workflows of this repository -- Files under **`.github/workflows/`** that define **`on: workflow_call`** are **reusable entry points**. This repo uses the **`reusable-` filename prefix** as an org convention for discoverability (see `docs/reusable-workflows.md`). Renaming a published reusable file is a **breaking change** for every consumer `uses:` path. +- This repository publishes **no callable workflow**: Python CI callees live in github-workflows (see `docs/reusable-workflows.md`). - **`release-tag.yml`** runs on pushes to `main`. On a version bump, its shared workflow creates the tag and publishes the GitHub Release. The calling job grants `contents: write` for publication. Its `release-please` job opens and maintains the release PR from conventional PR titles. See **`docs/versioning.md`**. -- **`reusable-pre-commit.yml`** should remain a thin, predictable pipeline: checkout → setup Python (with pip cache) → optional pre-install → install → `pre-commit run --all-files` (or overridden command). Avoid surprising side effects in defaults; document new **`inputs`** in `docs/reusable-workflows.md` and **`CHANGELOG.md`** when behavior or contract changes. -## Template caller workflows (`template/.github/workflows/`) +## Template caller workflow (`template/.github/workflows/ci.yml.jinja`) -- **`lint.yml`** is the **Tier A** pattern: a single job that **`uses:`** the org reusable workflow with a **pinned ref**. Its pin carries the `x-release-please-version` marker so release preparation bumps it with `STANDARDS_VERSION`, and it reaches consumers via `copier update`. -- **`test.yml`** is a **starter only** for consumer repos. This standards repo does **not** ship a reusable test matrix. Reviews should resist moving project-specific test logic here unless the README and docs explicitly expand that scope. +- Managed by Copier and **public API**: job ids decide the required check contexts of every adopter, so renaming one is a breaking change. +- Every `uses:` of github-workflows is pinned to the same ref, bumped here and delivered by `copier update`. +- The `Tests success` program must stay identical to the scoped gate accepted by github-config `scripts/check-required-gates.sh`. +- Test logic stays in `scripts/check.sh`; do not add project-specific steps to the template. ## Review checklist diff --git a/.claude/rules/copier-template.md b/.claude/rules/copier-template.md index ea4884f..c18365e 100644 --- a/.claude/rules/copier-template.md +++ b/.claude/rules/copier-template.md @@ -10,7 +10,7 @@ These files are **rendered into consumer repositories** by `copier copy` and mer ## `copier.yml` -- `_skip_if_exists` lists **bootstrap-only** files (`pyproject.toml`, `.github/workflows/test.yml`); everything else under `template/` is updated on `copier update`. `_skip_if_exists` only protects files that exist: a file a consumer deletes is re-created on update, so optional files use a question-gated filename (`reusable_lint`, `test_workflow`). +- `_skip_if_exists` lists **bootstrap-only** files (`pyproject.toml`, agent guidance); everything else under `template/` is updated on `copier update`. `_skip_if_exists` only protects files that exist: a file a consumer deletes is re-created on update, so optional files use a question-gated filename (`agent_guidance`). A file removed from the template is deleted from consumers, local edits included. - Renaming or removing a question is **breaking** for existing `.copier-answers.yml` files; document it in **`CHANGELOG.md`** and **`docs/migration-guide.md`**. ## `template/baselines/ruff.toml` @@ -25,9 +25,9 @@ These files are **rendered into consumer repositories** by `copier copy` and mer ## `template/.pre-commit-config.yaml` -- Hook **`rev:`** values should match the versions pinned in **`template/pyproject.toml.jinja`** where applicable (ruff, mypy, pre-commit). +- Ruff and mypy are **local hooks** run with `uv run --frozen python -m`, so their versions come from the consumer's `uv.lock`; do not reintroduce mirrored `rev:` pins. Remote hooks keep a pinned **`rev:`**. - **`fail_fast: true`** is intentional baseline behavior unless policy changes. ## `template/.github/workflows/` -See **`.agents/rules/github-actions.md`** for reusable vs template caller expectations and pinning. +See **`.agents/rules/github-actions.md`** for the managed caller workflow and its pins. diff --git a/.claude/rules/github-actions.md b/.claude/rules/github-actions.md index 9c32913..6d5e618 100644 --- a/.claude/rules/github-actions.md +++ b/.claude/rules/github-actions.md @@ -7,16 +7,17 @@ paths: # Review: GitHub Actions -## Callable workflows (this repository) +## Workflows of this repository -- Files under **`.github/workflows/`** that define **`on: workflow_call`** are **reusable entry points**. This repo uses the **`reusable-` filename prefix** as an org convention for discoverability (see `docs/reusable-workflows.md`). Renaming a published reusable file is a **breaking change** for every consumer `uses:` path. +- This repository publishes **no callable workflow**: Python CI callees live in github-workflows (see `docs/reusable-workflows.md`). - **`release-tag.yml`** runs on pushes to `main`. On a version bump, its shared workflow creates the tag and publishes the GitHub Release. The calling job grants `contents: write` for publication. Its `release-please` job opens and maintains the release PR from conventional PR titles. See **`docs/versioning.md`**. -- **`reusable-pre-commit.yml`** should remain a thin, predictable pipeline: checkout → setup Python (with pip cache) → optional pre-install → install → `pre-commit run --all-files` (or overridden command). Avoid surprising side effects in defaults; document new **`inputs`** in `docs/reusable-workflows.md` and **`CHANGELOG.md`** when behavior or contract changes. -## Template caller workflows (`template/.github/workflows/`) +## Template caller workflow (`template/.github/workflows/ci.yml.jinja`) -- **`lint.yml`** is the **Tier A** pattern: a single job that **`uses:`** the org reusable workflow with a **pinned ref**. Its pin carries the `x-release-please-version` marker so release preparation bumps it with `STANDARDS_VERSION`, and it reaches consumers via `copier update`. -- **`test.yml`** is a **starter only** for consumer repos. This standards repo does **not** ship a reusable test matrix. Reviews should resist moving project-specific test logic here unless the README and docs explicitly expand that scope. +- Managed by Copier and **public API**: job ids decide the required check contexts of every adopter, so renaming one is a breaking change. +- Every `uses:` of github-workflows is pinned to the same ref, bumped here and delivered by `copier update`. +- The `Tests success` program must stay identical to the scoped gate accepted by github-config `scripts/check-required-gates.sh`. +- Test logic stays in `scripts/check.sh`; do not add project-specific steps to the template. ## Review checklist diff --git a/.github/workflows/reusable-pre-commit.yml b/.github/workflows/reusable-pre-commit.yml deleted file mode 100644 index 85eb271..0000000 --- a/.github/workflows/reusable-pre-commit.yml +++ /dev/null @@ -1,50 +0,0 @@ -name: Reusable Pre-commit - -on: - workflow_call: - inputs: - runs-on: - required: false - type: string - default: ubuntu-latest - python-version: - required: false - type: string - default: "3.12" - pre-install-command: - required: false - type: string - default: "" - install-command: - required: false - type: string - default: | - python -m pip install --upgrade pip - pip install -e ".[dev]" - pre-commit-command: - required: false - type: string - default: pre-commit run --all-files - -jobs: - pre-commit: - timeout-minutes: 15 - name: Pre-commit - runs-on: ${{ inputs.runs-on }} - steps: - - uses: actions/checkout@v6 - - - uses: actions/setup-python@v6 - with: - python-version: ${{ inputs.python-version }} - cache: pip - - - name: Pre-install step - if: inputs.pre-install-command != '' - run: ${{ inputs.pre-install-command }} - - - name: Install dependencies - run: ${{ inputs.install-command }} - - - name: Run pre-commit - run: ${{ inputs.pre-commit-command }} diff --git a/AGENTS.md b/AGENTS.md index c53a2cd..43e4fde 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,6 +1,6 @@ # Python project standards -This repository owns the Copier template, reusable workflows and shared Python standards. Read `README.md` and `docs/development.md` for the current contract; there is no root Python package. Follow the actual Copier update and release procedures when changing templates or versions. +This repository owns the Copier template, the Python CI contract and shared Python standards. Read `README.md` and `docs/development.md` for the current contract; there is no root Python package. Follow the actual Copier update and release procedures when changing templates or versions. For substantive changes read `.agents/rules/changelog-alignment.md`. For commits read `.agents/rules/commit-message-convention.md`; for dependencies read `.agents/rules/dependency-pinning.md`; for PRs read `.agents/rules/pr-description-workflow.md`. For Python string enums read `.agents/rules/strenum-string-enums.md` and `docs/string-enums.md`. @@ -12,11 +12,11 @@ If graphify is installed and a graph exists, it may help explore the repository; ## Repository review focus -This repository is an organization-wide standards and template bundle, not an installable application: there is no root `pyproject.toml`. Consumers render the Copier template (`copier.yml`, `template/`) and call the reusable workflows in `.github/workflows/`. +This repository is an organization-wide standards and template bundle, not an installable application: there is no root `pyproject.toml`. Consumers render the Copier template (`copier.yml`, `template/`), whose `ci.yml` calls the reusable workflows of github-workflows. - Pinning: workflow `uses:` lines and documented examples use release tags (`@vX.Y.Z`) or commit SHAs, never long-lived `@main`. - Consumer guidance: changes to `template/` stay consistent with `docs/development.md`, `docs/reusable-workflows.md` and `README.md`. -- Reusable workflow contract: `.github/workflows/reusable-pre-commit.yml` is a public API; input renames, default changes or step removals are breaking unless documented in `CHANGELOG.md`. -- Validate: `copier copy --vcs-ref HEAD . "$TMPDIR/demo" --defaults --data package_name=demo && cd "$TMPDIR/demo" && git init -q && git add -A && pre-commit run --all-files`. -- Python policy follows `docs/string-enums.md` (`StrEnum`, not `(str, Enum)`), `template/baselines/ruff.toml` (`UP` selected) and strict mypy plus pytest-cov in `template/pyproject.toml.jinja`; tools and pre-commit `rev:` stay pinned. +- CI contract: `scripts/check.sh` targets and the job ids of the template `ci.yml` are public API; changing them is breaking and documented in `docs/migration-guide.md`. +- Validate: `copier copy --vcs-ref HEAD . "$TMPDIR/demo" --defaults --data package_name=demo && cd "$TMPDIR/demo" && git init -q && uv lock && git add -A && bash scripts/check.sh lint`. +- Python policy follows `docs/string-enums.md` (`StrEnum`, not `(str, Enum)`), `template/baselines/ruff.toml` (`UP` selected) and strict mypy plus pytest-cov in `template/pyproject.toml.jinja`; tools and remote pre-commit `rev:` stay pinned. - `README.md` and `docs/development.md` are the source of truth. diff --git a/README.md b/README.md index 7a28584..928ba45 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,8 @@ Organization-wide baseline standards for Python repositories: pinned tooling, pr - [Design Principles](#design-principles) - [Quick Start (Consumer Repo)](#quick-start-consumer-repo) - [Adoption tiers](#adoption-tiers) -- [Reusable GitHub Actions](#reusable-github-actions) +- [CI contract](#ci-contract) +- [Releases](#releases) - [Status](#status) ## Purpose @@ -33,16 +34,16 @@ Portable agent guidance is an optional Copier bootstrap; see [development guidan ## What Is Standardized - Shared policy and doc index in **[docs/development.md](docs/development.md)** (includes links to style pages under `docs/`); -- `pyproject.toml` tooling sections (`ruff` via **`baselines/ruff.toml`** + thin overlay, strict **`mypy`** keys, `pytest`, **`pytest-cov`** in template dev extras for `pytest --cov=…`); -- `.pre-commit-config.yaml` with pinned hook revisions; -- CI workflow baseline: **Tier A** `lint.yml` delegates to org **`reusable-pre-commit.yml`** (pin `@v…`); tests live in the consumer repo (start from **`template/.github/workflows/test.yml`**); +- `pyproject.toml` tooling sections (`ruff` via **`baselines/ruff.toml`** + thin overlay, strict **`mypy`** keys, `pytest` with branch coverage through **`pytest-cov`**); +- `.pre-commit-config.yaml`: ruff and mypy run from `uv.lock` as local hooks, other hooks pin their revisions; +- `scripts/check.sh [lint|test]`, the single validation entry point, locally and in CI; +- `.github/workflows/ci.yml`, calling the reusable workflows of [github-workflows](https://github.com/BehindTheMusicTree/github-workflows); - migration and versioning guidance. ## Repository Layout -- `copier.yml` + `template/`: **[Copier](https://copier.readthedocs.io/)** template rendered into consumer repos — `.pre-commit-config.yaml`, `baselines/ruff.toml`, optional `.github/workflows/lint.yml` (updated by `copier update`), plus bootstrap-only `pyproject.toml` and `.github/workflows/test.yml`. -- `.github/workflows/reusable-*.yml`: **callable** workflows (shared pre-commit) for repos that reference this repository instead of duplicating lint YAML. The **`reusable-` filename prefix** is an org convention for discoverability, not a GitHub requirement — see the **Naming** section in [docs/reusable-workflows.md](docs/reusable-workflows.md). -- `docs/`: **[development.md](docs/development.md)** (hub), migration guide, versioning, reusable workflows, and style notes (e.g. [`string-enums.md`](docs/string-enums.md)). +- `copier.yml` + `template/`: **[Copier](https://copier.readthedocs.io/)** template rendered into consumer repos — `.pre-commit-config.yaml`, `baselines/ruff.toml`, `scripts/check.sh`, `.github/workflows/ci.yml` (updated by `copier update`), plus a bootstrap-only `pyproject.toml`. +- `docs/`: **[development.md](docs/development.md)** (hub), migration guide, versioning, CI contract, and style notes (e.g. [`string-enums.md`](docs/string-enums.md)). - `scripts/tests/`: repository checks, including the read-only sync between **`STANDARDS_VERSION`** and every pin carrying the Release Please version marker (see [docs/versioning.md](docs/versioning.md)). ## Usage Model @@ -71,30 +72,20 @@ For an existing repository, see [docs/migration-guide.md](docs/migration-guide.m ## Adoption tiers -Not every Python repo should use the same CI shape. Use these tiers: +All tiers share the [CI contract](#ci-contract); they differ in what they add to the managed `ci.yml`. -| Tier | Typical repo | Use from this repo | Keep local | -|------|----------------|-------------------|------------| -| **A — Library** | Packaged library, multi-OS/Python matrix, `pyproject.toml` dev extras | **Delegated** [`reusable-pre-commit.yml`](.github/workflows/reusable-pre-commit.yml) for lint; **owned** `test.yml` (see [`template/.github/workflows/test.yml`](template/.github/workflows/) + local matrix/coverage) | Thin `lint.yml` caller; full control over test CI | -| **B — Service / API** | Django/FastAPI apps, Docker, DB, secrets, long integration jobs | [`reusable-pre-commit.yml`](.github/workflows/reusable-pre-commit.yml), pre-commit + policy templates | Full test / deploy workflows in the app repository | +| Tier | Typical repo | Added locally | +|------|--------------|---------------| +| **A — Library** | Packaged library | Usually nothing | +| **B — Service / API** | Django/FastAPI apps, database, long integration jobs | Environment, testcontainers, extra jobs (migrations, OpenAPI), deploy workflows | -**Pinning:** Consumer workflows should reference a **release tag** such as **`@v5.3.1`** (or a commit SHA), not **`@main`**; `copier update` bumps the template `lint.yml` pin. See [docs/versioning.md](docs/versioning.md). +## CI contract -**Example Tier B:** [hear-the-music-tree-api](https://github.com/BehindTheMusicTree/hear-the-music-tree-api) keeps database and containerized pytest in its own workflow and may call **reusable pre-commit** only. See that repo’s `docs/ci/python-project-standards.md`. - -## Reusable GitHub Actions - -For orgs that keep this repo as the single source of truth, consumer workflows can call: - -- `.github/workflows/reusable-pre-commit.yml` — checkout, install, run `pre-commit` (Tier A and Tier B). - -There is **no** reusable test matrix; use [`template/.github/workflows/test.yml`](template/.github/workflows/) in the consumer repo and extend it as needed. - -See [docs/reusable-workflows.md](docs/reusable-workflows.md) for caller examples and the full input list. +CI runs `bash scripts/check.sh lint` and `bash scripts/check.sh test` through `python-check.yml`, routes pull requests with `pr-route.yml`, checks coverage with `coverage.yml` and aggregates the result in the `Tests success` gate. See [docs/reusable-workflows.md](docs/reusable-workflows.md). ## Releases -Versions are **SemVer** (`v1.2.3` tags, `STANDARDS_VERSION` without `v`). [Release Please](https://github.com/googleapis/release-please) opens a release PR from conventional PR titles; merging it updates `STANDARDS_VERSION`, the marked pins and **`CHANGELOG.md`**, then CI creates **`vX.Y.Z`** and publishes the **GitHub Release**. Consumers pin callable workflows to that tag (or a commit SHA), not to `main` long term. See **[docs/versioning.md](docs/versioning.md)**. +Versions are **SemVer** (`v1.2.3` tags, `STANDARDS_VERSION` without `v`). [Release Please](https://github.com/googleapis/release-please) opens a release PR from conventional PR titles; merging it updates `STANDARDS_VERSION`, the marked pins and **`CHANGELOG.md`**, then CI creates **`vX.Y.Z`** and publishes the **GitHub Release**. Consumers adopt a release with `copier update`. See **[docs/versioning.md](docs/versioning.md)**. ## Status diff --git a/copier.yml b/copier.yml index 08e2a96..0590687 100644 --- a/copier.yml +++ b/copier.yml @@ -1,7 +1,6 @@ _subdirectory: template _skip_if_exists: - pyproject.toml - - .github/workflows/test.yml - AGENTS.md - CLAUDE.md - .github/workflows/agent-drift.yml @@ -10,16 +9,6 @@ package_name: type: str help: Distribution name for pyproject.toml -reusable_lint: - type: bool - default: true - help: Add .github/workflows/lint.yml calling the org reusable pre-commit workflow - -test_workflow: - type: bool - default: true - help: Add starter .github/workflows/test.yml (false when the repo runs tests in its own workflow) - agent_guidance: type: bool default: false diff --git a/docs/development.md b/docs/development.md index e655328..ed9b085 100644 --- a/docs/development.md +++ b/docs/development.md @@ -12,9 +12,9 @@ This document is the **entry point** for cross-repository Python standards maint ## What lives in python-project-standards - **Repository overview** — purpose, adoption tiers (library vs service/API), quick start: [README](../README.md). -- **Adopting the baseline in an existing repo** — Copier template vs callable workflows, `copier copy` / `copier update`: [migration-guide.md](migration-guide.md). +- **Adopting the baseline in an existing repo** — the Copier template, `copier copy` / `copier update`, version migrations: [migration-guide.md](migration-guide.md). - **Versioning and releases** — SemVer tags, pinning `@v…` vs SHA, consumer bumps: [versioning.md](versioning.md). -- **Reusable GitHub Actions** — `reusable-pre-commit.yml`, inputs and examples: [reusable-workflows.md](reusable-workflows.md). +- **CI contract** — `scripts/check.sh`, the managed `ci.yml` and the reusable workflows it calls: [reusable-workflows.md](reusable-workflows.md). ## Python style (shared) @@ -26,7 +26,7 @@ Further style rules may be added under `docs/` over time; this page will link th Baseline **pre-commit**, **`pyproject.toml`** tooling sections and **`baselines/ruff.toml`** ship as a **Copier template** ([`template/`](../template/), [`copier.yml`](../copier.yml)), described in the [README](../README.md) (**Repository layout**, **What Is Standardized**, **Quick Start**) and [migration-guide.md](migration-guide.md). -**Validation contract:** every repo validates with `bash scripts/check.sh` (managed by Copier, so `copier update` keeps it current). It needs uv, runs `uv sync --frozen` from `uv.lock` (failing instead of re-locking if the lock is missing or stale), then pre-commit and pytest, and CI runs the same script. Run `uv lock` and commit `uv.lock` once on a new repo; the script stops with that hint when it is missing. Set the Copier `uv_sync_args` answer (for example `--extra processing`) for extras the checks need; it is interpolated verbatim into the shell command, so keep it to simple, unquoted arguments. Dev tools live in `[dependency-groups] dev`. A repo adopting the template for the first time follows the [first-adoption checklist](migration-guide.md#first-adoption-checklist). +**Validation contract:** every repo validates with `bash scripts/check.sh` (managed by Copier, so `copier update` keeps it current). It needs uv, runs `uv sync --frozen` from `uv.lock` (failing instead of re-locking if the lock is missing or stale), then pre-commit and pytest; `lint` or `test` as argument runs one of the two, and CI runs the same script. Run `uv lock` and commit `uv.lock` once on a new repo; the script stops with that hint when it is missing. Set the Copier `uv_sync_args` answer (for example `--extra processing`) for extras the checks need; it is interpolated verbatim into the shell command, so keep it to simple, unquoted arguments. Dev tools live in `[dependency-groups] dev`. A repo adopting the template for the first time follows the [first-adoption checklist](migration-guide.md#first-adoption-checklist). ## Portable agent guidance diff --git a/docs/migration-guide.md b/docs/migration-guide.md index 61cc76b..ed65d1a 100644 --- a/docs/migration-guide.md +++ b/docs/migration-guide.md @@ -4,24 +4,20 @@ For a map of all org Python standards docs, start with **[development.md](develo Use this guide to adopt the baseline in an existing repository. -## Copier template versus reusable workflows +## Table of Contents -| Artifact | What it is | When to use | -|----------|------------|-------------| -| [`template/`](../template/) + [`copier.yml`](../copier.yml) | **[Copier](https://copier.readthedocs.io/)** template: `.pre-commit-config.yaml`, `baselines/ruff.toml`, `lint.yml`, starter `pyproject.toml` / `test.yml` | Every consumer; `copier update` merges new releases into the repo while keeping local edits | -| `.github/workflows/reusable-*.yml` | **Callable** workflows in `python-project-standards` | Repos that call `uses: org/python-project-standards/.github/workflows/....yml@ref` and pass `with:` inputs | +- [What the template manages](#what-the-template-manages) +- [1. Adopt the template](#1-adopt-the-template) +- [2. Install and validate](#2-install-and-validate) +- [3. Add project-specific overrides](#3-add-project-specific-overrides) +- [4. Update to a new release](#4-update-to-a-new-release) +- [5. Migrating from v5 to v6 (single CI contract)](#5-migrating-from-v5-to-v6-single-ci-contract) +- [6. Migrating from v4 (copied templates) to v5 (Copier)](#6-migrating-from-v4-copied-templates-to-v5-copier) +- [Optional agent guidance](#optional-agent-guidance) -Reusable workflows are resolved **at CI runtime** from this repository. Template files are rendered into the consumer and updated with **`copier update`** (3-way merge against the previously adopted release). +## What the template manages -## Pinning callee refs - -Prefer a **release tag** or **commit SHA**, not `main`: - -```yaml -uses: BehindTheMusicTree/python-project-standards/.github/workflows/reusable-pre-commit.yml@v5.3.1 # x-release-please-version -``` - -The template’s `lint.yml` carries this pin, so `copier update` bumps it. Repos with their own caller workflow (`reusable_lint: false`) bump the pin by hand. See [versioning.md](versioning.md). +[`template/`](../template/) and [`copier.yml`](../copier.yml) form a **[Copier](https://copier.readthedocs.io/)** template: `.pre-commit-config.yaml`, `baselines/ruff.toml`, `scripts/check.sh`, `.github/workflows/ci.yml` and a starter `pyproject.toml`. `copier update` merges new releases into the repo while keeping local edits (3-way merge against the previously adopted release). CI runs through reusable workflows of github-workflows; see [reusable-workflows.md](reusable-workflows.md). ## 1. Adopt the template @@ -35,24 +31,22 @@ git add -p # keep repo-specific hooks and settings the template overwrote Questions (`copier.yml`): - **`package_name`** — `[project] name` in the starter `pyproject.toml`. -- **`reusable_lint`** — `true` renders `.github/workflows/lint.yml` delegating to [reusable-pre-commit.yml](../.github/workflows/reusable-pre-commit.yml); `false` when the repo already runs pre-commit in its own workflow (e.g. Tier B services). -- **`test_workflow`** — `true` renders a starter `.github/workflows/test.yml`; `false` when tests run in a differently named workflow (otherwise `copier update` re-creates the deleted file). +- **`agent_guidance`** — see [Optional agent guidance](#optional-agent-guidance). +- **`uv_sync_args`** — extra `uv sync` arguments for `scripts/check.sh` and `ci.yml` (for example `--extra processing`). -`pyproject.toml` and `.github/workflows/test.yml` are **bootstrap-only** (`_skip_if_exists`): rendered for new repos, never touched in existing ones. Move dev tools into `[dependency-groups] dev` (PEP 735) and merge relevant sections from [`template/pyproject.toml.jinja`](../template/pyproject.toml.jinja) by hand (metadata, dependencies, **`[tool.mypy]`**, pytest). Tests stay in the consumer repo; there is no org reusable test workflow (see [reusable-workflows.md](reusable-workflows.md)). +`pyproject.toml` is **bootstrap-only** (`_skip_if_exists`): rendered for new repos, never touched in existing ones. Move dev tools into `[dependency-groups] dev` (PEP 735) and merge relevant sections from [`template/pyproject.toml.jinja`](../template/pyproject.toml.jinja) by hand (metadata, dependencies, **`[tool.mypy]`**, pytest). Commit **`.copier-answers.yml`**: it records the adopted release (`_commit`) and answers so later updates know what to merge. -A repo that already has its own `scripts/check.sh` gets a Copier conflict on the first update: reconcile it with the template version. +A repo that already has its own `scripts/check.sh` or `.github/workflows/ci.yml` gets a Copier conflict on adoption: reconcile it with the template version. ### First-adoption checklist For a repo without `.copier-answers.yml`, `scripts/check.sh` only passes once the repo satisfies this contract: -- `pre-commit`, `ruff`, `mypy` and `pytest` are in `[dependency-groups] dev`. `check.sh` runs them with `python -m` inside the uv environment, so a globally installed binary never satisfies it, and it stops with a hint when `pre-commit` is missing. -- `pyproject.toml` has `[tool.ruff] extend = "baselines/ruff.toml"` without its own `line-length` or `select`, and a `[tool.mypy]` section copied from [`template/pyproject.toml.jinja`](../template/pyproject.toml.jinja). -- The ruff and mypy versions in `.pre-commit-config.yaml` match the ones in `uv.lock`. -- `reusable_lint: false` when CI already runs `bash scripts/check.sh`, so the template does not add a second lint job. -- `test_workflow: false` when tests run in another workflow. Do not rerun pytest in a separate job without a reason, because `check.sh` already runs it. +- `pre-commit`, `ruff`, `mypy`, `pytest` and `pytest-cov` are in `[dependency-groups] dev`. `check.sh` runs them with `python -m` inside the uv environment, so a globally installed binary never satisfies it, and it stops with a hint when `pre-commit` is missing. +- `pyproject.toml` has `[tool.ruff] extend = "baselines/ruff.toml"` without its own `line-length` or `select`, and the `[tool.mypy]`, `[tool.coverage.run]`, `[tool.coverage.report]` and `[tool.pytest.ini_options]` sections of [`template/pyproject.toml.jinja`](../template/pyproject.toml.jinja), including `files`, the coverage `source` (a single root, see the comment there) and the coverage `addopts`. +- Existing lint and test workflows are removed, or reduced to jobs `ci.yml` does not cover. ## 2. Install and validate @@ -73,12 +67,26 @@ pre-commit install ```bash copier update # latest tag; or --vcs-ref vX.Y.Z -pre-commit run --all-files +bash scripts/check.sh ``` Resolve any conflicts like a git merge and commit the result together with the updated `.copier-answers.yml`. -## 5. Migrating from v4 (copied templates) to v5 (Copier) +## 5. Migrating from v5 to v6 (single CI contract) + +v6 replaces `lint.yml`, `test.yml` and this repository's `reusable-pre-commit.yml` with one managed `ci.yml`; the `reusable_lint` and `test_workflow` questions are gone. + +1. Note what `.github/workflows/lint.yml` and `test.yml` do locally. `copier update` **deletes both, including local edits**, without a conflict. +2. Run `copier update`, then port those local needs into `ci.yml` (see [Repo-specific needs](reusable-workflows.md#repo-specific-needs)). Replace `services:` containers with testcontainers. +3. Delete any other workflow calling `reusable-pre-commit.yml` or duplicating the lint and test jobs. +4. `pyproject.toml` is not updated by Copier: add `[tool.mypy] files = ["."]` (or your paths), the coverage `addopts` (`--cov --cov-branch --cov-report=xml:coverage.xml`), `[tool.coverage.run] source` set to a single root holding your packages (`src`, or `.` with `omit = [".venv/*", "tests/*"]` plus any tooling directory; without it, files no test imports are not measured), `[tool.coverage.report] include_namespace_packages = true` and `pytest-cov` in the dev group. Do not list several roots: `coverage.yml` merges same-named files across them. +5. In `.pre-commit-config.yaml`, keep the local ruff and mypy hooks; their versions now come from `uv.lock`. +6. Check that `uv.lock` is current, then run `bash scripts/check.sh`. Tests now run with `python -P`, which keeps the repository root off `sys.path`: if imports fail with `ModuleNotFoundError`, install the package (a `src/` layout with `[build-system]`), or declare the needed directories in `[tool.pytest.ini_options] pythonpath` (the simpler fix for a flat layout). +7. Update the required status checks: `lint / check` replaces the former lint context and `Tests success` the test contexts (see [Required status checks](reusable-workflows.md#required-status-checks)). + +Coverage now applies the organization standard of `coverage.yml` to the pull request. + +## 6. Migrating from v4 (copied templates) to v5 (Copier) v5 removes the copy-and-verify machinery. In each consumer: @@ -87,17 +95,6 @@ v5 removes the copy-and-verify machinery. In each consumer: 3. Remove the **`verify-python-project-standards`** hook from `.pre-commit-config.yaml`. 4. Agent/editor rules are no longer distributed (`templates/cursor-rules/` is gone); rules each repo already copied are now owned by that repo. -## 6. Migrating off `reusable-test-matrix.yml` (standards v2.x → v3+) - -**`reusable-test-matrix.yml` was removed in v3.0.0.** Consumers that called it should either: - -- Stay on **`@v2.3.0`** (or another **v2.x** tag) until they replace the call, or -- Remove the `uses: …/reusable-test-matrix.yml@…` job and implement tests locally (start from [`template/.github/workflows/test.yml`](../template/.github/workflows/), add `strategy.matrix`, `pytest --cov`, etc., as needed). - -## 7. Tier B (API / service) - -Keep Docker, databases, and secrets in local workflows; call [reusable-pre-commit.yml](../.github/workflows/reusable-pre-commit.yml) for shared pre-commit in CI only, or answer `reusable_lint: false` and run pre-commit inline. - ## Optional agent guidance Set Copier answer `agent_guidance` to `true` to create project-owned starter guidance and a pinned drift-check workflow. Existing `AGENTS.md`, `CLAUDE.md`, and agent drift workflow files are preserved; reconcile their contents with the agent-config contract before enabling the check. The default is `false` for existing and new consumers. diff --git a/docs/reusable-workflows.md b/docs/reusable-workflows.md index 44be578..2b64d44 100644 --- a/docs/reusable-workflows.md +++ b/docs/reusable-workflows.md @@ -1,71 +1,62 @@ -# Reusable GitHub Actions workflows +# Python CI contract and reusable workflows -This repository publishes **callable** workflows under `.github/workflows/`. Consumer repositories run them with `jobs..uses` so lint logic stays centralized. +Every Python repository validates through one script, `bash scripts/check.sh [lint|test]`, and runs it in CI through the reusable workflows of [github-workflows](https://github.com/BehindTheMusicTree/github-workflows). This repository publishes no callable workflow of its own: it ships the script and the caller workflow through the Copier template. -## Naming: `reusable-` prefix +## Table of Contents -**GitHub does not require any filename pattern.** A workflow is reusable because its `on:` block includes `workflow_call`, not because of its name. +- [The check script](#the-check-script) +- [The managed ci.yml](#the-managed-ciyml) +- [Reusable workflows](#reusable-workflows) +- [Path routing](#path-routing) +- [Repo-specific needs](#repo-specific-needs) +- [Required status checks](#required-status-checks) -This repository uses the **`reusable-` prefix** as an **organizational convention**: +## The check script -- **Clarity:** In `.github/workflows/`, it is obvious which files exist only to be **`uses:`’d** by other repos (or by thin caller workflows here) versus workflows that **trigger on `push` / `pull_request`**. -- **Discovery:** Contributors and reviewers can grep for `reusable-` when looking for callable entry points. +| Command | Runs | +|---------|------| +| `bash scripts/check.sh` | `uv sync --frozen`, pre-commit on all files, pytest | +| `bash scripts/check.sh lint` | `uv sync --frozen`, pre-commit on all files | +| `bash scripts/check.sh test` | `uv sync --frozen`, pytest | -**Alternatives** that are equally valid elsewhere: no prefix with a subfolder (e.g. `.github/workflows/callable/lint.yml`), or another consistent scheme. Pick one convention per repository and stick with it. +Any other argument exits with status 2. Ruff and mypy are local pre-commit hooks run with `python -m` inside the uv environment, so their versions come from `uv.lock` alone. Pytest runs with `python -P`, and its coverage options live in `[tool.pytest.ini_options] addopts` of `pyproject.toml`. -**Renaming** a published reusable file is a **breaking change** for every consumer `uses:` path; treat renames like API changes (semver major or migration notes). +## The managed ci.yml -## Requirements +Copier renders `.github/workflows/ci.yml` and updates it on every `copier update`: -- The consumer repo must be allowed to use workflows from this repository (typically same GitHub organization and org settings that permit reusable workflows). -- Pin the callee ref (**prefer `@v5.3.1` or a commit SHA**). Avoid `@main` in production CI so standards updates do not surprise you. +| Job | Calls | Runs when | +|-----|-------|-----------| +| `route` | `pr-route.yml` | always | +| `lint` | `python-check.yml`, target `lint` | always | +| `pytest` | `python-check.yml`, target `test` | the route is applicable | +| `coverage` | `coverage.yml` | the route is applicable | +| `tests-success` (`Tests success`) | gate over `route`, `pytest`, `coverage` | always | -## Reference workflows +The `uses:` pins live in the template, so adopters receive new github-workflows versions through `copier update`. Do not bump them by hand. -| File | Purpose | -|------|---------| -| [`reusable-pre-commit.yml`](../.github/workflows/reusable-pre-commit.yml) | Checkout, install, run `pre-commit` (Tier A and Tier B). | +## Reusable workflows -**Tests:** this repository does **not** ship a reusable test matrix. Use [`template/.github/workflows/test.yml`](../template/.github/workflows/) as a starting point in the consumer repo and add `strategy.matrix`, coverage, or service containers locally. +The inputs are documented in each callee; the ones adopters meet most: -## Example: pre-commit only (Tier B) +| Workflow | Inputs | +|----------|--------| +| [`python-check.yml`](https://github.com/BehindTheMusicTree/github-workflows/blob/main/.github/workflows/python-check.yml) | `target` (required, `lint` or `test`), `python-version`, `runs-on`, `pre-install-command`, `env` (`KEY=VALUE` lines), `uv-sync-args`, `coverage-artifact` (`coverage-*`), `timeout-minutes` | +| [`pr-route.yml`](https://github.com/BehindTheMusicTree/github-workflows/blob/main/.github/workflows/pr-route.yml) | `rules-file`, `runs-on`, `timeout-minutes`; output `applicable` | +| [`coverage.yml`](https://github.com/BehindTheMusicTree/github-workflows/blob/main/.github/workflows/coverage.yml) | `line`, `branch`, `patch` (thresholds can only be raised), `report-artifacts`, `base-ref` | -```yaml -jobs: - pre-commit: - uses: YOUR_ORG/python-project-standards/.github/workflows/reusable-pre-commit.yml@v5.3.1 # x-release-please-version - with: - python-version: "3.14" - install-command: | - python -m pip install --upgrade pip - pip install pre-commit==4.5.1 -``` +`python-check.yml` runs `uv sync --locked` followed by `bash scripts/check.sh `. -## Example: pre-commit caller (Tier A `lint.yml`) +## Path routing -Create `.github/workflows/lint.yml` in the consumer repository: +`pr-route.yml` reads `.github/pr-route.json` from the pull request base commit, with exactly the string arrays `skip_files`, `skip_prefixes`, `skip_suffixes` and `build_inputs`. `applicable` is `false` only when every changed path is listed as skippable; without the file, or with an invalid one, everything runs. The file belongs to the consumer repository. -```yaml -name: Lint +## Repo-specific needs -on: - pull_request: - push: - branches: [main] +- **Settings of the standard jobs:** pass `env`, `pre-install-command`, `runs-on` or `timeout-minutes` in `ci.yml`. Copier merges local edits with upstream changes, like a git merge. Extras for `uv sync` go through the `uv_sync_args` Copier answer. +- **Service containers:** `python-check.yml` has no `services:` input. Start databases from the tests with testcontainers. +- **Extra jobs** (migrations, OpenAPI, performance): add them to `ci.yml` with the same `needs` and `if` as `pytest`, and list them in the `needs` of `tests-success`, which only aggregates the jobs it names. -jobs: - lint: - uses: YOUR_ORG/python-project-standards/.github/workflows/reusable-pre-commit.yml@v5.3.1 # x-release-please-version -``` +## Required status checks -Override inputs only when needed (see table below). - -## Inputs: `reusable-pre-commit.yml` - -| Input | Type | Default | Description | -|-------|------|---------|-------------| -| `runs-on` | string | `ubuntu-latest` | Runner label for the job. | -| `python-version` | string | `3.12` | Python version for `setup-python`. | -| `pre-install-command` | string | `""` | Optional shell command before install (skipped if empty). | -| `install-command` | string | `pip`-based editable dev install | Multi-line install script. | -| `pre-commit-command` | string | `pre-commit run --all-files` | Command to run after install. | +Require `lint / check` and `Tests success`. `route / route`, `pytest / check` and `coverage / coverage` are covered by the gate: declare them as optional jobs (`route/route`, `pytest/check`, `coverage/coverage`) in the organization's `required-checks.json` in [github-config](https://github.com/BehindTheMusicTree/github-config). diff --git a/docs/versioning.md b/docs/versioning.md index cb038dd..5322db1 100644 --- a/docs/versioning.md +++ b/docs/versioning.md @@ -24,16 +24,15 @@ This repository is versioned **independently** from consumer repositories. Versi | Pin style | Use when | |-----------|----------| -| **Version tag** (`@v1.2.3`) | Default for org repos: readable upgrades, matches the `_commit` tag in `.copier-answers.yml`. | -| **Commit SHA** | Strongest reproducibility and supply-chain hygiene; use for high-assurance CI or temporary pins while validating a fix on `main`. | +| **Version tag** (`vX.Y.Z`) | Default: `copier update` records it as `_commit` in `.copier-answers.yml`. | +| **Commit SHA** | Temporary `--vcs-ref` while validating a fix on `main`. | -Avoid long-lived **`@main`** (or other branch refs) in production workflows: standards evolve; branch heads change without a version signal. +Avoid adopting a branch ref: branch heads change without a version signal. After each **standards** release, consumer maintainers should: -1. Run **`copier update`** (records the new tag as `_commit` in **`.copier-answers.yml`** and bumps the template `lint.yml` pin). -2. Update any other **`uses: …/workflow.yml@v…`** to the **same** tag (or chosen SHA). -3. Read **CHANGELOG.md** and **docs/migration-guide.md** for this repo before bumping **major** versions. +1. Run **`copier update`**: it records the new tag as `_commit` in **`.copier-answers.yml`** and brings the workflow pins of the managed `ci.yml`. +2. Read **CHANGELOG.md** and **docs/migration-guide.md** for this repo before bumping **major** versions. ## Release notes (what to include) @@ -52,7 +51,7 @@ Each GitHub Release (and matching `CHANGELOG.md` section) should make adoption s 2. Release Please updates `STANDARDS_VERSION`, every pin marked `x-release-please-version` and `CHANGELOG.md` in its release PR. `scripts/tests/test_release_version_sync.py` fails if a marked pin differs or a current-version pin lacks its marker. 3. CI auto-merges the release PR once the required checks pass, then creates the tag and publishes the GitHub Release from the generated notes. Rerun a failed workflow to recover; an existing release is retained. -**Version markers:** the Release Please Generic updater replaces the first SemVer of each marked line. Mark only the line holding the standards pin: `` at the end of a prose line, ` # x-release-please-version` on a pin or `copier copy --vcs-ref` line in a copyable YAML or shell example, and `{# x-release-please-version #}` directly after the template pin so the rendered workflow carries no comment or trailing space. +**Version markers:** the Release Please Generic updater replaces the first SemVer of each marked line. Mark only the line holding the standards pin: `` at the end of a prose line, ` # x-release-please-version` on a `copier copy --vcs-ref` line in a copyable shell example. **Frequency:** Every merged `feat:`/`fix:` PR releases; title doc-only fixes `docs:` so they ride the next release. diff --git a/release-please-config.json b/release-please-config.json index 86afadc..3bdd36e 100644 --- a/release-please-config.json +++ b/release-please-config.json @@ -6,10 +6,7 @@ "include-component-in-tag": false, "version-file": "STANDARDS_VERSION", "extra-files": [ - "README.md", - "docs/migration-guide.md", - "docs/reusable-workflows.md", - "template/.github/workflows/{% if reusable_lint %}lint.yml{% endif %}.jinja" + "docs/migration-guide.md" ] } } diff --git a/scripts/tests/agent-template.sh b/scripts/tests/agent-template.sh index a85aa49..db65920 100644 --- a/scripts/tests/agent-template.sh +++ b/scripts/tests/agent-template.sh @@ -10,13 +10,41 @@ copier copy --defaults --data package_name=demo "$tmp/source" "$tmp/default" >/d [[ ! -e "$tmp/default/AGENTS.md" ]] [[ -x "$tmp/default/scripts/check.sh" ]] grep -qx 'uv sync --frozen' "$tmp/default/scripts/check.sh" -grep -qx "uv run --frozen python -m pre_commit run --all-files" "$tmp/default/scripts/check.sh" -grep -qx "uv run --frozen python -m pytest" "$tmp/default/scripts/check.sh" -grep -q "import pre_commit" "$tmp/default/scripts/check.sh" +actionlint "$tmp/default/.github/workflows/ci.yml" +# A stub uv records the calls; it fails the pre-commit import only when asked to. +mkdir "$tmp/bin" +cat > "$tmp/bin/uv" <<'STUB' +#!/usr/bin/env bash +printf '%s\n' "$*" >> "$UV_LOG" +[[ -z "${NO_PRE_COMMIT-}" || "$*" != *'import pre_commit'* ]] +STUB +printf '#!/usr/bin/env bash\nexit 0\n' > "$tmp/bin/pre-commit" +chmod +x "$tmp/bin/uv" "$tmp/bin/pre-commit" +touch "$tmp/default/uv.lock" +check() { : > "$tmp/uv.log"; UV_LOG="$tmp/uv.log" PATH="$tmp/bin:$PATH" bash "$tmp/default/scripts/check.sh" "$@" >/dev/null 2>&1; } +check +grep -q 'pre_commit run --all-files' "$tmp/uv.log" +grep -q -- 'python -P -m pytest' "$tmp/uv.log" +check lint +grep -q 'pre_commit run --all-files' "$tmp/uv.log" +if grep -q pytest "$tmp/uv.log"; then exit 1; fi +check test +grep -q -- 'python -P -m pytest' "$tmp/uv.log" +if grep -q pre_commit "$tmp/uv.log"; then exit 1; fi +for args in other '' 'lint test'; do + rc=0 + if [[ -z "$args" ]]; then check '' || rc=$?; else + # shellcheck disable=SC2086 + check $args || rc=$? + fi + [[ "$rc" == 2 && ! -s "$tmp/uv.log" ]] +done +# A global pre-commit binary must not stand in for the missing venv module. +if NO_PRE_COMMIT=1 check lint; then exit 1; fi copier copy --defaults --data package_name=demo --data 'uv_sync_args=--extra processing' "$tmp/source" "$tmp/extra" >/dev/null [[ -x "$tmp/extra/scripts/check.sh" ]] -actionlint "$tmp/extra/.github/workflows/test.yml" "$tmp/extra/.github/workflows/lint.yml" -grep -q -- 'uv sync --frozen --extra processing' "$tmp/extra/.github/workflows/lint.yml" +actionlint "$tmp/extra/.github/workflows/ci.yml" +[[ "$(grep -c 'uv-sync-args: "--extra processing"' "$tmp/extra/.github/workflows/ci.yml")" == 2 ]] grep -qx 'uv sync --frozen --extra processing' "$tmp/extra/scripts/check.sh" copier copy --defaults --data package_name=demo --data agent_guidance=true "$tmp/source" "$tmp/enabled" >/dev/null [[ -f "$tmp/enabled/AGENTS.md" && -f "$tmp/enabled/CLAUDE.md" ]] diff --git a/scripts/tests/test_release_version_sync.py b/scripts/tests/test_release_version_sync.py index 914b39a..c997f73 100644 --- a/scripts/tests/test_release_version_sync.py +++ b/scripts/tests/test_release_version_sync.py @@ -10,10 +10,7 @@ SEMVER = re.compile(r"\d+\.\d+\.\d+") # A missing marker leaves a pin behind on the next release, so the expected count is fixed. EXPECTED = { - "README.md": 1, - "docs/migration-guide.md": 2, - "docs/reusable-workflows.md": 3, - "template/.github/workflows/{% if reusable_lint %}lint.yml{% endif %}.jinja": 1, + "docs/migration-guide.md": 1, } diff --git a/template/.github/workflows/ci.yml.jinja b/template/.github/workflows/ci.yml.jinja new file mode 100644 index 0000000..086b2ca --- /dev/null +++ b/template/.github/workflows/ci.yml.jinja @@ -0,0 +1,53 @@ +# Managed by Copier: `copier update` bumps the pins and merges upstream changes with local edits. +# Add repo-specific jobs in this file: `Tests success` only aggregates the jobs listed in its `needs`. +name: CI + +on: + pull_request: + push: + branches: [main, develop] + +permissions: + contents: read + +concurrency: + group: ci-{% raw %}${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.pull_request.number) || github.ref }}{% endraw %} + cancel-in-progress: {% raw %}${{ github.event_name == 'pull_request' }}{% endraw %} + +jobs: + route: + uses: BehindTheMusicTree/github-workflows/.github/workflows/pr-route.yml@00092729e18625c0a305c96ec35d0247fdc78868 + + lint: + uses: BehindTheMusicTree/github-workflows/.github/workflows/python-check.yml@00092729e18625c0a305c96ec35d0247fdc78868 + with: + target: lint{% if uv_sync_args %} + uv-sync-args: {{ uv_sync_args | tojson }}{% endif %} + + pytest: + needs: [route] + if: needs.route.outputs.applicable == 'true' + uses: BehindTheMusicTree/github-workflows/.github/workflows/python-check.yml@00092729e18625c0a305c96ec35d0247fdc78868 + with: + target: test + coverage-artifact: coverage-pytest{% if uv_sync_args %} + uv-sync-args: {{ uv_sync_args | tojson }}{% endif %} + + # Repo-specific jobs go here, with the same `needs` and `if` as pytest. + + coverage: + needs: [route, pytest] + if: needs.route.outputs.applicable == 'true' + uses: BehindTheMusicTree/github-workflows/.github/workflows/coverage.yml@00092729e18625c0a305c96ec35d0247fdc78868 + + tests-success: + name: Tests success + if: always() + needs: [route, pytest, coverage] + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - env: + NEEDS: {% raw %}${{ toJSON(needs) }}{% endraw %} + run: | + jq -e --arg scope route '.[$scope] as $s | del(.[$scope]) as $rest | $s.result == "success" and ($rest | length) > 0 and (if $s.outputs.applicable == "true" then all($rest[]; .result == "success") elif $s.outputs.applicable == "false" then all($rest[]; .result == "skipped") else false end)' <<<"$NEEDS" diff --git a/template/.github/workflows/{% if reusable_lint %}lint.yml{% endif %}.jinja b/template/.github/workflows/{% if reusable_lint %}lint.yml{% endif %}.jinja deleted file mode 100644 index b98ca6f..0000000 --- a/template/.github/workflows/{% if reusable_lint %}lint.yml{% endif %}.jinja +++ /dev/null @@ -1,17 +0,0 @@ -# Shared pre-commit in CI — delegates to the org reusable workflow; `copier update` bumps the pin. -name: Lint - -on: - pull_request: - push: - branches: [main] - -jobs: - lint: - uses: BehindTheMusicTree/python-project-standards/.github/workflows/reusable-pre-commit.yml@v5.3.1{# x-release-please-version #} - with: - install-command: | - python -m pip install --upgrade pip uv - uv sync --frozen{% if uv_sync_args %} {{ uv_sync_args }}{% endif %} - - pre-commit-command: uv run --frozen python -m pre_commit run --all-files diff --git a/template/.github/workflows/{% if test_workflow %}test.yml{% endif %} b/template/.github/workflows/{% if test_workflow %}test.yml{% endif %} deleted file mode 100644 index fcee89c..0000000 --- a/template/.github/workflows/{% if test_workflow %}test.yml{% endif %} +++ /dev/null @@ -1,19 +0,0 @@ -# Copy-paste starter: runs the shared validation contract. Extend with strategy.matrix or service containers as needed. -name: Test - -on: - pull_request: - push: - branches: [main] - -jobs: - tests: - name: Validate - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v5.0.1 - - - uses: astral-sh/setup-uv@v6 - - - name: Validate - run: bash scripts/check.sh diff --git a/template/.pre-commit-config.yaml b/template/.pre-commit-config.yaml index 8c5ca84..2492122 100644 --- a/template/.pre-commit-config.yaml +++ b/template/.pre-commit-config.yaml @@ -11,14 +11,23 @@ repos: - id: end-of-file-fixer - id: trailing-whitespace - - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.15.9 + # Tool versions come from uv.lock; `python -m` never falls back to a globally installed binary. + - repo: local hooks: - id: ruff-format + name: ruff format + entry: uv run --frozen python -m ruff format --force-exclude + language: system + types_or: [python, pyi] - id: ruff - args: [--fix] - - - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.18.2 - hooks: + name: ruff check + entry: uv run --frozen python -m ruff check --force-exclude --fix + language: system + types_or: [python, pyi] - id: mypy + name: mypy + entry: uv run --frozen python -m mypy + language: system + types_or: [python, pyi] + pass_filenames: false + require_serial: true diff --git a/template/pyproject.toml.jinja b/template/pyproject.toml.jinja index f92d294..925ac69 100644 --- a/template/pyproject.toml.jinja +++ b/template/pyproject.toml.jinja @@ -23,6 +23,7 @@ dev = [ extend = "baselines/ruff.toml" [tool.mypy] +files = ["."] python_version = "3.12" warn_unused_configs = true warn_redundant_casts = true @@ -31,6 +32,15 @@ strict_equality = true disallow_untyped_defs = true no_implicit_optional = true +[tool.coverage.run] +# Measures source files no test imports. Keep a single root: coverage.yml cannot tell apart +# same-named files from several roots. Without src/, use "." with +# omit = [".venv/*", "tests/*"] (plus any tooling directory). +source = ["src"] + +[tool.coverage.report] +include_namespace_packages = true + [tool.pytest.ini_options] -addopts = "-q" +addopts = "-q --cov --cov-branch --cov-report=xml:coverage.xml" testpaths = ["tests"] diff --git a/template/scripts/check.sh.jinja b/template/scripts/check.sh.jinja index b31d253..c8f992a 100755 --- a/template/scripts/check.sh.jinja +++ b/template/scripts/check.sh.jinja @@ -1,10 +1,21 @@ #!/usr/bin/env bash # Single validation entry point: CI runs this same script. +# Usage: scripts/check.sh [lint|test] (no argument runs both). set -euo pipefail +target="${1-}" +if [ $# -gt 1 ] || { [ $# -eq 1 ] && [ "$target" != lint ] && [ "$target" != test ]; }; then + echo 'usage: scripts/check.sh [lint|test]' >&2 + exit 2 +fi cd "$(dirname "$0")/.." command -v uv >/dev/null || { echo 'uv is required: https://docs.astral.sh/uv/' >&2; exit 1; } [ -f uv.lock ] || { echo 'uv.lock is missing: run `uv lock` and commit it' >&2; exit 1; } uv sync --frozen{% if uv_sync_args %} {{ uv_sync_args }}{% endif %} -uv run --frozen python -c 'import pre_commit' || { echo 'check failed: if the error above is ModuleNotFoundError, add pre-commit to [dependency-groups] dev' >&2; exit 1; } -uv run --frozen python -m pre_commit run --all-files -uv run --frozen python -m pytest +if [ "$target" != test ]; then + uv run --frozen python -c 'import pre_commit' || { echo 'check failed: if the error above is ModuleNotFoundError, add pre-commit to [dependency-groups] dev' >&2; exit 1; } + uv run --frozen python -m pre_commit run --all-files +fi +if [ "$target" != lint ]; then + # -P keeps the working directory off sys.path, so a broken install is not hidden. + uv run --frozen python -P -m pytest +fi