Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ scripts/configure.sh <owner/repo> [--apply|--diff] # dry run by default
`conventions.yml` fetches it from this repo at the caller's pinned commit
(`job.workflow_sha`) rather than copying it into each repo.
- `settings/` holds the repository settings as data: merge types and the Actions
token (`repository.json`, `actions-workflow.json`), security features, and the
branch ruleset. `configure.sh` sends them, requiring each gate only where the
repo has the workflow that reports it (`scripts/ruleset.jq`); `--diff` reports
drift from them.
token (`repository.json`, `actions-workflow.json`), security features, the
branch ruleset and the release tag ruleset. `configure.sh` sends them,
requiring each gate only where the repo has the workflow that reports it
(`scripts/ruleset.jq`); `--diff` reports drift from them.

## Conventions

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ counts.
- **A template**: a shared core (issue and PR templates, Dependabot,
`SECURITY.md`, `AGENTS.md`, lint settings, caller workflows) plus an overlay
for Node, Python or TeX.
- **Repository settings** as code: squash only merges, security features and a
branch ruleset that requires the CI gates.
- **Repository settings** as code: squash only merges, security features, a
branch ruleset that requires the CI gates, and a tag ruleset that keeps
release tags from moving.

## Usage

Expand Down
18 changes: 18 additions & 0 deletions STANDARDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ workflow can open or approve a PR it was not built to.
| Private vulnerability reporting | On (public repos) |
| CodeQL default setup | On (public repos) |
| Ruleset `main` | See below (public repos) |
| Ruleset `release tags` | See below (public repos) |

The ruleset (`settings/ruleset-main.json`) blocks deletion and force pushes,
requires linear history, a PR with resolved conversations and squash merge, and
Expand Down Expand Up @@ -374,6 +375,23 @@ another PR lands, and in a solo repo two PRs rarely collide in a way the gates
would catch. CI on `main` after the merge still does. "Update branch" stays
available for when it is wanted.

**Release tags cannot move.** Callers pin the reusable workflows to an exact tag
such as `v1.1.0`, and `conventions.yml` fetches its checker at that commit, so a
moved or deleted tag would change what every caller runs. The `release tags`
ruleset (`settings/ruleset-tags.json`) blocks updating, force pushing and
deleting any tag matching `v*.*.*`. Creating one stays open, since
release-please creates it with `GITHUB_TOKEN`. The pattern is not `v*`:
atdr/latex-build moves its floating `v1` tag on every release, and `build-*` and
`prerelease/*` tags must stay free. It has no bypass actors, even in a repo that
commits `.github/admin-bypass`: committing to the default branch never needs a
release tag moved, and a botched release gets a new version instead.

It is a ruleset rather than GitHub's immutable releases, which lock a release's
assets once it is published. Some repos upload to an existing release
(latex-build with `gh release upload --clobber`, capsule-caffeine from its
release workflows), and the badge would serve nobody: no third party pins these
tags, and the npm and PyPI packages already carry registry provenance.

**The ruleset replaces classic branch protection.** GitHub enforces both, so
protection left over from before a repo adopted the gates still requires the
check names its old workflows reported, and blocks every merge once those checks
Expand Down
81 changes: 74 additions & 7 deletions scripts/configure.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env bash
# Applies the repository settings in STANDARDS.md to a GitHub repo: merge
# rules, security features, the default workflow token, the main ruleset, and
# the deployment environments its workflows publish from.
# rules, security features, the default workflow token, the main ruleset, the
# release tag ruleset, and the deployment environments its workflows publish
# from.
#
# Usage: scripts/configure.sh <owner/repo> [--apply|--diff]
#
Expand All @@ -15,8 +16,9 @@
#
# Prints what it would change, and changes nothing, unless --apply is given.
# --diff compares the repo's live merge and Actions settings with settings/,
# reports classic branch protection, which the ruleset does not replace, and
# checks the branch policy of each publishing environment the workflows name.
# reports classic branch protection, which the ruleset does not replace,
# checks the release tag ruleset in a public repo, and checks the branch policy
# of each publishing environment the workflows name.
# Actions may open PRs only in a repo whose workflows run release-please.
# Needs an authenticated gh. Written for bash 3.2 (macOS's stock /bin/bash).

Expand Down Expand Up @@ -46,6 +48,46 @@ drift() {
| " \(.key): \($live[.key]) -> \(.value)"'
}

# The release tag ruleset's id, found by its name in settings/ruleset-tags.json;
# empty where the repo has none.
tag_ruleset_id() {
local name
name=$(jq -r .name "$settings/ruleset-tags.json")
# includes_parents=false: an organization ruleset of the same name is
# listed too by default, and can't be updated through the repo.
gh api --paginate "repos/$repo/rulesets?includes_parents=false" \
--jq ".[] | select(.name == \"$name\") | .id"
}

# Where the tag ruleset differs from settings/ruleset-tags.json, as
# `key: live -> tracked` lines, or one line where it is missing. Compared on
# what the file sets: target, enforcement, the tags it covers, bypass actors
# and which rules it has. GitHub leaves bypass_actors out for a caller who
# can't edit the ruleset, so an absent list is reported as unreadable, never
# as the empty one tracked. `|| return` because a failed read inside $(...)
# would otherwise read as a missing ruleset.
tag_ruleset_drift() {
local id name
name=$(jq -r .name "$settings/ruleset-tags.json")
id=$(tag_ruleset_id) || return
if [ -z "$id" ]; then
echo " ruleset $name: missing -> $(jq -r '.conditions.ref_name.include | join(", ")' \
"$settings/ruleset-tags.json")"
return
fi
gh api "repos/$repo/rulesets/$id" | jq -r --arg name "$name" \
--slurpfile want "$settings/ruleset-tags.json" '
def shape: {target, enforcement,
include: (.conditions.ref_name.include // [] | sort),
exclude: (.conditions.ref_name.exclude // [] | sort),
bypass_actors: (if has("bypass_actors") then .bypass_actors
else "unreadable without admin access" end),
rules: ([.rules[]?.type] | sort)};
shape as $live | $want[0] | shape | to_entries[]
| select($live[.key] != .value)
| " ruleset \($name) \(.key): \($live[.key] | tojson) -> \(.value | tojson)"'
}

# A repo running release-please lets Actions open PRs, because release-please
# opens its release PR with GITHUB_TOKEN; every other repo keeps it off.
#
Expand Down Expand Up @@ -297,6 +339,12 @@ if [ "$mode" = --diff ]; then
if has_classic_protection; then
classic=" classic branch protection on $default_branch ($(protection_summary)) -> none, the ruleset replaces it"
fi
# Like the main ruleset, only in a public repo: GitHub Free has no
# rulesets for private ones.
tags=
if [ "$visibility" = public ]; then
tags=$(tag_ruleset_drift)
fi
environment_drift=
for name in npm-release pypi; do
if names_environment "$name"; then
Expand All @@ -306,11 +354,12 @@ if [ "$mode" = --diff ]; then
if names_environment npm-prerelease; then
check_environment npm-prerelease any
fi
if [ -n "$repository$workflow$classic$environment_drift" ]; then
if [ -n "$repository$workflow$classic$tags$environment_drift" ]; then
echo "$repo differs from settings/ (live -> tracked):"
[ -z "$repository" ] || echo "$repository"
[ -z "$workflow" ] || echo "$workflow"
[ -z "$classic" ] || echo "$classic"
[ -z "$tags" ] || echo "$tags"
[ -z "$environment_drift" ] || printf '%s' "$environment_drift"
exit 1
fi
Expand Down Expand Up @@ -409,12 +458,30 @@ $workflows
if [ "$copilot_review" = true ]; then
echo "requests a Copilot review on every push: the repo has copilot-auto-merge.yml"
fi
existing=$(gh api "repos/$repo/rulesets" --jq '.[] | select(.name == "main") | .id')
existing=$(gh api --paginate "repos/$repo/rulesets?includes_parents=false" \
--jq '.[] | select(.name == "main") | .id')
if [ -n "$existing" ]; then
run gh api -X PUT "repos/$repo/rulesets/$existing" --silent --input - <<<"$ruleset"
else
run gh api -X POST "repos/$repo/rulesets" --silent --input - <<<"$ruleset"
fi
# Release tags: no one may move or delete a v*.*.* tag once it exists.
# Creating one stays open, since release-please creates it with
# GITHUB_TOKEN; floating tags such as v1, and build-* or prerelease/*,
# stay free. .github/admin-bypass is not honoured here: it exists so the
# owner can commit to the default branch, which never needs a release tag
# moved, and a bypass would leave the tags callers pin as movable as
# before. A botched release gets a new version instead.
existing=$(tag_ruleset_id)
echo "protects release tags: no update, force push or deletion of" \
"$(jq -r '.conditions.ref_name.include | join(", ")' "$settings/ruleset-tags.json")"
if [ -n "$existing" ]; then
run gh api -X PUT "repos/$repo/rulesets/$existing" --silent \
--input "$settings/ruleset-tags.json"
else
run gh api -X POST "repos/$repo/rulesets" --silent \
--input "$settings/ruleset-tags.json"
fi
if [ "$auto_merge" = true ]; then
run gh api -X PATCH "repos/$repo" --silent -F allow_auto_merge=true
fi
Expand All @@ -441,7 +508,7 @@ else
# GitHub Free has no rulesets, branch protection, secret scanning or
# private vulnerability reporting for private repos.
echo "skipped for a private repo on GitHub Free: secret scanning, private" \
"vulnerability reporting, code scanning, the main ruleset"
"vulnerability reporting, code scanning, the main and release tag rulesets"
fi

if names_environment npm-release; then
Expand Down
17 changes: 17 additions & 0 deletions settings/ruleset-tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "release tags",
"target": "tag",
"enforcement": "active",
"conditions": {
"ref_name": {
"include": ["refs/tags/v*.*.*"],
"exclude": []
}
},
"bypass_actors": [],
"rules": [
{ "type": "deletion" },
{ "type": "non_fast_forward" },
{ "type": "update" }
]
}
11 changes: 7 additions & 4 deletions tests/test_configure_environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
)

# Answers the read calls configure.sh makes, as a public repo with no gate, no
# classic protection and no ruleset, whose merge and Actions settings match
# settings/. Environments live in $FAKE/state.json; every call is logged.
# classic protection and no main ruleset, whose merge, Actions and release tag
# settings match settings/. Environments live in $FAKE/state.json; every call is logged.
FAKE_GH = r"""#!{python}
import base64, json, os, subprocess, sys

Expand Down Expand Up @@ -88,8 +88,10 @@ def out(value):
out({{"content": base64.b64encode(open(os.path.join(fake, "publish.yml"), "rb").read()).decode()}})
elif method == "GET" and rest == "/actions/permissions/workflow":
out(json.load(open({workflow!r})))
elif method == "GET" and rest == "/rulesets":
out([])
elif method == "GET" and rest == "/rulesets?includes_parents=false":
out([{{"id": 1, "name": "release tags"}}])
elif method == "GET" and rest == "/rulesets/1":
out(dict(json.load(open({tags!r})), id=1))
elif parts[:1] == ["environments"]:
name = parts[1]
env = envs.get(name)
Expand Down Expand Up @@ -146,6 +148,7 @@ def fake(tmp_path):
python=sys.executable,
repository=str(SETTINGS / "repository.json"),
workflow=str(SETTINGS / "actions-workflow.json"),
tags=str(SETTINGS / "ruleset-tags.json"),
)
)
gh.chmod(0o755)
Expand Down
Loading
Loading