diff --git a/.github/workflows/pr-title.yml b/.github/workflows/pr-title.yml new file mode 100644 index 000000000..a26fad013 --- /dev/null +++ b/.github/workflows/pr-title.yml @@ -0,0 +1,69 @@ +# Copyright 2026 ExtendDB contributors +# SPDX-License-Identifier: Apache-2.0 +# +# Checks that a PR title is a conventional commit subject +# (devtools/check-pr-title). Squash is the only merge method and a squash +# commit takes the PR title as its subject (CONTRIBUTING.md). The release +# automation tracked in #1395 will build the changelog from those subjects. +# +# The title is read from the event payload and reaches the script through +# the environment, never by interpolation into the script source. The job +# checks out devtools/ (in cone mode, also the files at the repository +# root) from the default branch, so a PR that edits the checker is checked +# by main's version, and a PR whose base has no checker yet (an older PR, +# or one stacked on another branch) still gets one. This file itself runs +# from the PR's merge ref, as every pull_request workflow does, so a change +# to it is protected only by the review of .github/. Until main has a +# checker (the PR that adds it, and PRs stacked on it), the job uses the +# PR's own copy; that trusts the PR no more than running this file from +# its merge ref already does. The job holds no credential. It runs again when the title is edited. In a merge queue run +# there is no PR title to check, and the job is skipped, which counts as +# passing. + +name: PR title + +on: + pull_request: + types: [opened, edited, reopened, synchronize] + merge_group: + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }} + cancel-in-progress: true + +jobs: + conventional: + if: github.event_name == 'pull_request' + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - name: Check out devtools from the default branch + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: ${{ github.event.repository.default_branch }} + sparse-checkout: devtools + persist-credentials: false + + - name: Check out the PR's devtools, while the default branch has no checker + if: hashFiles('devtools/check-pr-title') == '' + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: ${{ github.event.pull_request.head.sha }} + sparse-checkout: devtools + path: pr-head + persist-credentials: false + + - name: Check the title + env: + TITLE: ${{ github.event.pull_request.title }} + run: | + set -euo pipefail + CHECKER=devtools/check-pr-title + if [[ ! -f "$CHECKER" ]]; then + CHECKER=pr-head/devtools/check-pr-title + echo "::notice::the default branch has no ${CHECKER#pr-head/} yet; using this PR's copy" + fi + python3 "$CHECKER" "$TITLE" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f0972b4ae..716e7d451 100755 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -34,6 +34,12 @@ We suggest following the workflow below for proposing and contributing improveme 1. Ensure all tests pass and code is properly formatted. 1. Submit your changes as a pull request, linked to the issue and (if applicable) RFC that your work addresses. +1. Title the pull request as a [conventional commit](https://www.conventionalcommits.org/) subject, + `type(scope): summary`, for example `fix(storage-postgres): retry a serialization failure`. Squash + is the only merge method, and the repository settings make the PR title the commit subject and the + PR description the commit message. The PR title check (`devtools/check-pr-title`) relies on these + settings and lists the accepted types. To revert a change, title the PR + `revert: `; GitHub's default `Revert "..."` title fails the check. ### Build diff --git a/devtools/check-pr-title b/devtools/check-pr-title new file mode 100755 index 000000000..279f8bde0 --- /dev/null +++ b/devtools/check-pr-title @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +# Copyright 2026 ExtendDB contributors +# SPDX-License-Identifier: Apache-2.0 +"""Fail when a PR title is not a conventional commit subject. + + devtools/check-pr-title "" + +Squash is the only merge method, and a squash commit takes the PR title as +its subject. The release automation tracked in #1395 will build the changelog +from those subjects. A title passes when it has the form + + type(scope)!: summary + +where `type` is one of TYPES, `(scope)` and `!` are optional, and the colon +is followed by one space and the summary. A scope is lowercase letters, +digits, `.`, `_`, `/` and `-`, and starts with a letter or a digit. + +Exit 0 when the title passes, 1 otherwise, with the reason on stderr. +""" + +import re +import sys + +TYPES = ( + "build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "revert", "style", "test", +) +SHAPE_RE = re.compile(r"^(?P<type>[^(!:\s]+)(?:\((?P<scope>[^)]*)\))?(?P<breaking>!)?:(?P<rest>.*)$") +SCOPE_RE = re.compile(r"[a-z0-9][a-z0-9._/-]*") +SUMMARY_RE = re.compile(r" \S") + + +def check(title): + """Return None when the title passes, or the reason it does not.""" + if title != title.strip(): + return "the title starts or ends with white space" + if "\n" in title or "\r" in title: + return "the title has more than one line" + match = SHAPE_RE.match(title) + if match is None: + return ( + "the title is not `type(scope): summary`; the scope and a `!` before the colon " + "are optional" + ) + if match.group("type") not in TYPES: + return f"`{match.group('type')}` is not one of {', '.join(TYPES)}" + scope = match.group("scope") + if scope == "": + return "the parentheses hold no scope; leave them out or name a scope" + if scope is not None and not SCOPE_RE.fullmatch(scope): + return ( + f"the scope `{scope}` is not lowercase letters, digits, `.`, `_`, `/` and `-`, " + "starting with a letter or a digit" + ) + if not SUMMARY_RE.match(match.group("rest")): + return "the colon is not followed by one space and the summary" + return None + + +def main(argv): + if len(argv) != 2: + print(__doc__.split("\n\n")[1], file=sys.stderr) + return 2 + reason = check(argv[1]) + if reason is not None: + print(f"FAIL: {argv[1]!r}: {reason}.", file=sys.stderr) + print("The title becomes the commit subject on main; see CONTRIBUTING.md.", file=sys.stderr) + return 1 + print(f"ok: {argv[1]!r}") + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/devtools/check_pr_title_test.py b/devtools/check_pr_title_test.py new file mode 100644 index 000000000..65053a843 --- /dev/null +++ b/devtools/check_pr_title_test.py @@ -0,0 +1,101 @@ +# Copyright 2026 ExtendDB contributors +# SPDX-License-Identifier: Apache-2.0 +"""Tests of devtools/check-pr-title. + + python3 -m unittest discover -s devtools -p '*_test.py' +""" +import subprocess +import sys +import unittest +from importlib.machinery import SourceFileLoader +from importlib.util import module_from_spec, spec_from_loader +from pathlib import Path + +SCRIPT = Path(__file__).resolve().parent / "check-pr-title" +_loader = SourceFileLoader("check_pr_title", str(SCRIPT)) +_spec = spec_from_loader("check_pr_title", _loader) +cpt = module_from_spec(_spec) +_loader.exec_module(cpt) + + +class CheckPrTitleTest(unittest.TestCase): + def test_accepts_the_shapes_used_on_main(self): + for title in ( + "fix(storage-cassandra): retry a contended DeleteItem", + "feat(backup): add backup sync", + "ci: pin the trivy image", + "test(devtools): cover the shard weights", + "docs(server): describe the health endpoint", + "chore(release): 1.1.0", + "feat(engine)!: drop the legacy reader", + "fix!: refuse a malformed key", + "perf(storage-postgres): batch the index writes", + "revert: fix(app): retry the bind", + "build(deps): bump tokio to 1.48", + "style: format the storage crate", + "fix(2fa): retry", + "fix(a.b/c-d_e): retry", + ): + with self.subTest(title=title): + self.assertIsNone(cpt.check(title)) + + def test_refuses_titles_outside_the_accepted_shape(self): + shape = "not `type(scope): summary`" + space = "not followed by one space and the summary" + scope = "is not lowercase letters, digits" + cases = { + "Add new code owners for multiple directories": shape, + "Merge main into ci/fast-checks": shape, + 'Revert "fix(app): retry the bind"': shape, + "fix(app) : retry": shape, + "fix(app)!!: retry": shape, + "fix!(app): retry": shape, + "devtools: add test-shards": "`devtools` is not one of", + "Fix(app): retry": "`Fix` is not one of", + "feature(app): add": "`feature` is not one of", + "fix(): retry": "the parentheses hold no scope", + "fix(App): retry": "the scope `App` " + scope, + "fix(app,engine): retry": "the scope `app,engine` " + scope, + "fix(app engine): retry": "the scope `app engine` " + scope, + "fix(-app): retry": "the scope `-app` " + scope, + "fix(.app): retry": "the scope `.app` " + scope, + "fix(app):retry": space, + "fix(app): retry": space, + "fix(app):\tretry": space, + "fix(app):": space, + "fix!:retry": space, + " fix(app): retry": "white space", + "fix(app): retry ": "white space", + "fix(app): retry\nsecond line": "more than one line", + "fix(app): retry\rsecond line": "more than one line", + } + for title, reason in cases.items(): + with self.subTest(title=title): + self.assertIn(reason, cpt.check(title) or "") + + def test_the_script_exits_1_with_the_reason(self): + result = subprocess.run( + [sys.executable, str(SCRIPT), "Adding a feature"], + capture_output=True, text=True, check=False, + ) + self.assertEqual(result.returncode, 1) + self.assertIn("not `type(scope): summary`", result.stderr) + + def test_the_script_exits_0_on_a_good_title(self): + result = subprocess.run( + [sys.executable, str(SCRIPT), "fix(app): retry"], + capture_output=True, text=True, check=False, + ) + self.assertEqual(result.returncode, 0, result.stderr) + + def test_the_script_exits_2_without_exactly_one_argument(self): + for args in ([], ["fix: a", "fix: b"]): + with self.subTest(args=args): + result = subprocess.run( + [sys.executable, str(SCRIPT), *args], capture_output=True, text=True, check=False, + ) + self.assertEqual(result.returncode, 2) + + +if __name__ == "__main__": + unittest.main()