diff --git a/.github/rulesets/README.md b/.github/rulesets/README.md new file mode 100644 index 0000000..179cd33 --- /dev/null +++ b/.github/rulesets/README.md @@ -0,0 +1,85 @@ +# Repository rulesets + +Ruleset definitions for `ahauy/FlowSnap`, enforced by GitHub on **push and PR merge**, not by +any tool in the repo. + +GitHub does not read this folder by itself. Each file is the exact request body for the +[create-ruleset API](https://docs.github.com/en/rest/repos/rules). + +## Apply + +```sh +# See the effect for a week without blocking anything: set "enforcement": "evaluate" first. +gh api -X POST repos/ahauy/FlowSnap/rulesets --input .github/rulesets/main.json + +# Confirm +gh api repos/ahauy/FlowSnap/rulesets --jq '.[] | {id, name, enforcement, .conditions?}' + +# Update an existing ruleset instead of creating a duplicate +gh api -X PATCH repos/ahauy/FlowSnap/rulesets/ --input .github/rulesets/main.json + +# Remove +gh api -X DELETE repos/ahauy/FlowSnap/rulesets/ +``` + +## Files + +| File | Enforces | +| --- | --- | +| `main.json` | A PR is required to land on `main`; `Swift unit tests (macOS)` and `GitGuardian Security Checks` must be green and current (`strict_required_status_checks_policy: true`); no force push (`non_fast_forward`); `main` cannot be deleted; review threads must be resolved; squash or merge only. | + +## Applied state (verified by reading the API back) + +`main.json` is live as ruleset **id `26587443`**, `enforcement: active`, conditions +`refs/heads/main` + `refs/heads/master`, and `bypass_actors` = RepositoryRole `5` (Admin) with +`bypass_mode: always`. GitHub also defaulted `require_extra_approval_for_unattributed_changes: true`. + +Because that ruleset requires `Swift unit tests (macOS)` on every PR, the workflow's `paths:` +filters were removed so the check always runs — a docs-only PR would otherwise hang on +"Expected — waiting" forever. + +## Pattern rules are not available on this plan + +Probing the same endpoint on 2026-11-05: + +| Rule tried | Result | +| --- | --- | +| `commit_message_pattern` (`operator: regex`, `matches`, with and without `positive_match`) | `422 Invalid rule 'commit_message_pattern'` | +| `branch_name_pattern` | `422 Invalid rule 'branch_name_pattern'` | +| `max_file_size` | `422 Invalid property /rules/0` (parameter shape also differs from the docs) | +| `required_signatures` | accepted | + +So the single-line Conventional Commit rule from AGENTS.md **cannot** be enforced by a ruleset on +this account. It is enforced two other ways instead: + +- `.qoder/hooks/flowgate.sh` + `.qoder/hooks/policy.json` block an agent's `git commit` unless the + user approved it — the message shape is the agent's responsibility there. +- A CI job can reject non-conforming subjects on a PR if you want it enforced for humans too; + that is the remaining gap. + +Do not re-add `feature-branches.json` until the plan supports pattern rules; it will fail to apply. + +## Decisions worth knowing + +- `required_approving_review_count: 0`. The repo has one owner, and GitHub does not count the + author's own review. Any number above 0 makes every PR unmergeable. +- `integration_id` values are read from real check runs, not guessed: GitHub Actions is `15368` + (context `Swift unit tests (macOS)`), GitGuardian is `46505`. +- `bypass_actors` grants the Admin repository role (`actor_id: 5`) bypass, so you can still + hot-fix `main` from the web UI. Delete that block for zero exceptions. +- `update` and `creation` are deliberately absent on `main`: they block direct pushes and can + also reject the merge commit a PR produces. + +## The commit-message regex, kept for a future CI check + +`git log --format=%s` style subjects only, because a ruleset cannot enforce it here: + +``` +\A(feat|fix|docs|test|chore|refactor|perf|ci|build|revert)(\([a-z0-9._-]+\))?: [^\n]+\z +``` + +Measured with Ruby `Regexp` (same engine GitHub uses for these patterns): the 12 most recent +subjects on `feat/web-i18n-ux-refactor` match, while `multiline body`, `no type prefix`, +`uppercase type`, `no colon space` and `empty desc` are all rejected. A PR-time job should match +this against each commit subject in the range, and skip merge commits. + diff --git a/.github/rulesets/main.json b/.github/rulesets/main.json new file mode 100644 index 0000000..e7a13ea --- /dev/null +++ b/.github/rulesets/main.json @@ -0,0 +1,57 @@ +{ + "name": "Protect main — PR, required checks, no force push", + "target": "branch", + "source_type": "Repository", + "enforcement": "active", + "conditions": { + "ref_name": { + "exclude": [], + "include": ["refs/heads/main", "refs/heads/master"] + } + }, + "rules": [ + { + "type": "deletion", + "parameters": {} + }, + { + "type": "non_fast_forward", + "parameters": {} + }, + { + "type": "required_status_checks", + "parameters": { + "do_not_enforce_on_create": false, + "strict_required_status_checks_policy": true, + "required_status_checks": [ + { + "context": "Swift unit tests (macOS)", + "integration_id": 15368 + }, + { + "context": "GitGuardian Security Checks", + "integration_id": 46505 + } + ] + } + }, + { + "type": "pull_request", + "parameters": { + "required_approving_review_count": 0, + "require_code_owner_review": false, + "require_last_push_approval": false, + "dismiss_stale_reviews_on_push": true, + "required_review_thread_resolution": true, + "allowed_merge_methods": ["squash", "merge"] + } + } + ], + "bypass_actors": [ + { + "actor_type": "RepositoryRole", + "actor_id": 5, + "bypass_mode": "always" + } + ] +} diff --git a/.github/workflows/commit-lint.yml b/.github/workflows/commit-lint.yml new file mode 100644 index 0000000..a22b315 --- /dev/null +++ b/.github/workflows/commit-lint.yml @@ -0,0 +1,32 @@ +name: Conventional Commits + +on: + pull_request: + workflow_dispatch: + +concurrency: + group: commit-lint-${{ github.ref }} + cancel-in-progress: true + +jobs: + subjects: + name: Conventional commit subjects + runs-on: ubuntu-latest + steps: + - name: Checkout full history + uses: actions/checkout@v4 + with: + fetch-depth: 0 + persist-credentials: false + + - name: Check every commit subject and body + env: + BASE_SHA: ${{ github.event.pull_request.base.sha }} + HEAD_SHA: ${{ github.event.pull_request.head.sha }} + run: | + if [ -z "${BASE_SHA:-}" ] || [ -z "${HEAD_SHA:-}" ]; then + HEAD_SHA=$(git rev-parse HEAD) + BASE_SHA=$(git rev-parse HEAD~1) + fi + export BASE_SHA HEAD_SHA + python3 scripts/check-commit-messages.py diff --git a/.github/workflows/macos-tests.yml b/.github/workflows/macos-tests.yml index c3e82a3..b8ed61c 100644 --- a/.github/workflows/macos-tests.yml +++ b/.github/workflows/macos-tests.yml @@ -4,19 +4,7 @@ on: push: branches: - main - paths: - - "FlowSnap/**" - - "FlowSnapTests/**" - - "FlowSnap.xcodeproj/**" - - "project.yml" - - ".github/workflows/macos-tests.yml" pull_request: - paths: - - "FlowSnap/**" - - "FlowSnapTests/**" - - "FlowSnap.xcodeproj/**" - - "project.yml" - - ".github/workflows/macos-tests.yml" workflow_dispatch: concurrency: diff --git a/scripts/check-commit-messages.py b/scripts/check-commit-messages.py new file mode 100644 index 0000000..d96949e --- /dev/null +++ b/scripts/check-commit-messages.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 +"""Fail if any commit in a range breaks AGENTS.md's single-line Conventional Commit rule.""" + +import os +import re +import subprocess +import sys + +SUBJECT = re.compile( + r"(feat|fix|docs|test|chore|refactor|perf|ci|build|revert)" + r"(\([a-z0-9._-]+\))?(!)?: [^\s].*" +) +SKIP_AUTHORS = {"dependabot[bot]", "renovate[bot]", "github-actions[bot]"} +MERGE_PREFIXES = ("Merge ", "Revert \"", "revert \"") + + +def git(*args: str) -> str: + result = subprocess.run(["git", *args], capture_output=True, text=True) + if result.returncode: + # 2 = the check itself could not run (bad revision, shallow clone), + # which is different from 1 = a real commit-message violation. + print(f"git {' '.join(args)} failed: {result.stderr.strip()}", file=sys.stderr) + raise SystemExit(2) + return result.stdout + + +def commits(base: str, head: str) -> list[tuple[str, str]]: + out = git("log", "--no-merges", "--format=%H\x1f%an", f"{base}..{head}") + return [tuple(line.split("\x1f")) for line in out.splitlines() if line.strip()] + + +def body_lines(sha: str) -> int: + return len([line for line in git("show", "-s", "--format=%b", sha).splitlines() if line.strip()]) + + +def main() -> int: + base = os.environ.get("BASE_SHA") or sys.argv[1] + head = os.environ.get("HEAD_SHA") or sys.argv[2] + problems = [] + + for sha, author in commits(base, head): + subject = git("show", "-s", "--format=%s", sha).strip() + if author in SKIP_AUTHORS or subject.startswith(MERGE_PREFIXES): + continue + short = sha[:8] + if not SUBJECT.fullmatch(subject): + problems.append(f"{short} subject not Conventional Commits: {subject!r}") + extra = body_lines(sha) + if extra: + problems.append(f"{short} has a {extra}-line body; AGENTS.md requires a single line") + + if problems: + print("Commit message check failed:\n") + print("\n".join(f" - {p}" for p in problems)) + print( + "\nExpected: type(scope): summary on ONE line, no body.\n" + "Types: feat fix docs test chore refactor perf ci build revert" + ) + return 1 + + print(f"OK: {len(commits(base, head))} commit(s) follow single-line Conventional Commits.") + return 0 + + +if __name__ == "__main__": + sys.exit(main())