#!/usr/bin/env python3 """PR CI: does this pull request follow the development cycle? develop -> feature/*, fix/*, ... -> release/vX.Y.Z -> production (collects features) (release: tag + Packagist) production -> develop (the sync after a release, by the pipeline) Pull requests go to a release branch or to production, never to develop. - to `release/vX.Y.Z`: the release must not be out yet (its version is above the latest tag), and the pull request does not come from production, develop or another release branch. - to `production`: only from `release/vX.Y.Z`, with a version above the latest tag, a section for it in CHANGELOG.md, and no "version" in composer.json (Packagist reads the tag). The one other way in is `renovate/*`: the nightly dependency update of the homelab (release.py makes a patch release of it, unless a release branch is open). Environment: BASE_REF, HEAD_REF. Run in a checkout with the tags (fetch-depth 0). Standard library + git only. """ import json import os import subprocess import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import semver # noqa: E402 def tags(): result = subprocess.run(["git", "tag", "--list", "v*"], capture_output=True, text=True) return result.stdout.split() def problems(base, head, existing_tags, changelog, composer): found = [] newest = semver.latest(existing_tags) if base == "production": if head.startswith("renovate/"): return [] version = semver.release_version(head) if version is None: return [f"A pull request to production comes from a branch named release/vX.Y.Z, not from {head or '?'}."] found += above_latest(version, newest) if changelog is None or semver.changelog_section(changelog, version) is None: found.append(f"CHANGELOG.md has no section '## v{semver.text(version)}' for this release.") if isinstance(composer, dict) and "version" in composer: found.append("composer.json must not have a \"version\": Packagist takes the version from the tag.") return found if base.startswith("release/"): version = semver.release_version(base) if version is None: return [f"{base} is not a release branch name: use release/vX.Y.Z."] found += above_latest(version, newest, "That release is already out: start the next one.") if head in ("production", "develop") or head.startswith("release/"): found.append(f"A release branch takes features (feature/*, fix/*), not {head}.") return found return [f"Pull requests go to release/vX.Y.Z or to production, not to {base or '?'}: develop only takes the sync after a release."] def above_latest(version, newest, hint="The version of a release must be above the latest tag."): if newest is not None and version <= newest: return [f"v{semver.text(version)} is not above the latest tag v{semver.text(newest)}. {hint}"] return [] def main(): base = os.environ.get("BASE_REF", "").strip() head = os.environ.get("HEAD_REF", "").strip() changelog = open("CHANGELOG.md", encoding="utf-8").read() if os.path.exists("CHANGELOG.md") else None try: composer = json.load(open("composer.json", encoding="utf-8")) except (OSError, ValueError): composer = None found = problems(base, head, tags(), changelog, composer) for line in found: print(f"::error::{line}") if found: return 1 print(f"{head} -> {base}: follows the development cycle.") return 0 if __name__ == "__main__": sys.exit(main())