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
18 changes: 17 additions & 1 deletion .github/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
tags and prints the next `tag=` / `version=` / `prerelease=` for
`../workflows/release.yml`, given a release channel and a SemVer 2
bump.
- `setup_environments.sh` — creates or updates the `release`, `moderation`
and `template-sync` GitHub environments (default-branch-only deployment
rule) and prints the commands that move their secrets in; dry run unless
`--apply` (see `../../docs/TEMPLATE.md`, "Moderation and template-sync
environments"). `test_environments.py` runs the workflows' environment
check step against a stub `gh`.
- `template_sync_manifest.py` — parses `../template-sync-manifest.yml`
and checks that every git-tracked path matches exactly one tier;
backs both the `template-sync-manifest` prek hook and
Expand All @@ -30,6 +36,16 @@
`conflicts=0|1` line to `$GITHUB_OUTPUT`. `apply` catches its broad failure with
`logging.exception` rather than a `noqa`, so instances that don't
select ruff's `BLE` rules see no unused-`noqa` finding.
`apply` also creates the `ignore`-tier files a release introduces: an
exact-path `ignore` entry (not a glob or directory) whose file is in the new
tag but absent from the previous synced tag and from the instance is copied
in and listed under "New instance-owned files" in the PR body. An existing
file is never touched; a file the previous tag already had is not recreated,
so a deliberate deletion sticks (the state file records only the synced tag,
so the previous tag's tree is what tells a new path from a deleted one).
Which tier a path has is decided by the *upstream* tag's manifest alone:
the instance's own manifest, which classifies its tree for its own
instances, plays no part in a sync from its parent.
`apply` is not atomic: on exit 2, files written before the error stay on
disk. CI is safe because the runner checkout is discarded and the sync
state does not advance. After a failed manual run, reset the working tree
Expand All @@ -40,7 +56,7 @@
or `git init --bare` act on the outer repository, e.g. flipping its
`core.bare`) and keeps only a test identity.
- `test_template_sync.py` — stdlib `unittest` tests for
`template_sync.py`'s replace and merge tiers (including the executable
`template_sync.py`'s replace and merge tiers, new ignore-tier files (including the executable
bit, re-running a merge, symlinked destinations, and `git merge-file`
errors raising instead of counting as conflicts, binary files counting
as conflicts, symlinked destinations kept on removal, one tree walk for
Expand Down
209 changes: 209 additions & 0 deletions .github/scripts/setup_environments.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
#!/usr/bin/env bash
# Create or update the GitHub environments the template's workflows run
# privileged jobs in, so each only runs from the default branch, and print the
# commands that move its secrets into it:
#
# release release.yml OCI_REGISTRY_USERNAME OCI_REGISTRY_PASSWORD
# NPM_TOKEN CARGO_REGISTRY_TOKEN
# moderation moderate-*.yml ANTHROPIC_API_KEY
# template-sync template-sync.yml TEMPLATE_SYNC_TOKEN
#
# Dry run by default: only `gh api` GETs. `--apply` is required to PUT an
# environment and POST its branch policy. Secrets are never moved or deleted
# (values can't be read back): the script prints the `gh secret set --env` /
# `gh secret delete` commands for a human to run. Organization secrets are
# listed by name and visibility, and the ones visible to every repository are
# flagged (a branch workflow can read them; an environment can't stop that).
# Existing reviewers and wait timers on an environment are kept.
#
# Usage:
# setup_environments.sh [--apply] [--env NAME]... [--org ORG] REPO...
#
# REPO is `owner/name`, or a bare name taken under --org (default: the owner of
# the current directory's `gh repo view`). --env may repeat and defaults to
# all three. Needs `gh` authenticated as a repository admin and `jq`.
set -euo pipefail

ORG=
APPLY=false
ENVS=()

secrets_of() {
case "$1" in
release) echo OCI_REGISTRY_USERNAME OCI_REGISTRY_PASSWORD NPM_TOKEN CARGO_REGISTRY_TOKEN ;;
moderation) echo ANTHROPIC_API_KEY ;;
template-sync) echo TEMPLATE_SYNC_TOKEN ;;
esac
}
workflow_of() {
case "$1" in
release) echo release.yml ;;
moderation) echo moderate-bug-triage.yml ;;
template-sync) echo template-sync.yml ;;
esac
}

usage() {
sed -n '2,/^set -euo/p' "$0" | sed '$d' | sed 's/^# \{0,1\}//'
}

repos=()
while [ $# -gt 0 ]; do
case "$1" in
--apply) APPLY=true ;;
--dry-run) APPLY=false ;;
--env)
case "${2:-}" in release | moderation | template-sync) ENVS+=("$2") ;;
*) echo "--env must be release, moderation or template-sync" >&2; exit 2 ;;
esac
shift ;;
--org) ORG="${2:?--org needs a value}"; shift ;;
-h | --help) usage; exit 0 ;;
-*) echo "unknown option: $1" >&2; usage >&2; exit 2 ;;
*) repos+=("$1") ;;
esac
shift
done
if [ "${#repos[@]}" -eq 0 ]; then
usage >&2
exit 2
fi
[ "${#ENVS[@]}" -gt 0 ] || ENVS=(release moderation template-sync)
[ -n "$ORG" ] || ORG=$(gh repo view --json owner -q .owner.login)

echo "Mode: $($APPLY && echo APPLY || echo "DRY RUN (no changes; pass --apply)"); environments: ${ENVS[*]}"

# gh api GET that prints nothing instead of failing on 404.
get() {
local out
if out=$(gh api "$@" 2> /dev/null); then
printf '%s\n' "$out"
fi
}

status=0

echo
echo "== organization $ORG: secrets"
org_json=$(get "orgs/$ORG/actions/secrets")
if [ -z "$org_json" ]; then
echo " (cannot list organization secrets: needs org admin; skipped)"
else
while IFS=' ' read -r name vis; do
[ -n "$name" ] || continue
owner_env=
for e in "${ENVS[@]}"; do
for s in $(secrets_of "$e"); do [ "$s" = "$name" ] && owner_env=$e; done
done
[ -n "$owner_env" ] || continue
echo " $name: visibility=$vis ($owner_env secret)"
if [ "$vis" = all ]; then
echo " FLAG: visible to every repository, from any branch. Remediate (not run by this script):"
echo " per repo that needs it: gh secret set $name --env $owner_env -R $ORG/<repo>"
echo " then: gh secret delete $name --org $ORG"
fi
done < <(jq -r '.secrets[]? | "\(.name) \(.visibility)"' <<< "$org_json")
fi

for r in "${repos[@]}"; do
case "$r" in */*) repo="$r" ;; *) repo="$ORG/$r" ;; esac
echo
echo "== $repo"
default=$(gh api "repos/$repo" -q .default_branch) || {
echo " ERROR: cannot read the repository (no access?)"
status=1
continue
}
repo_json=$(get "repos/$repo/actions/secrets")
if [ -z "$repo_json" ]; then
echo " ERROR: cannot list repository secrets (needs admin access)"
status=1
continue
fi
repo_secrets=$(jq -r '.secrets[]?.name' <<< "$repo_json")

for env_name in "${ENVS[@]}"; do
echo " -- environment $env_name"
env_json=$(get "repos/$repo/environments/$env_name")
want_flags='{"protected_branches":false,"custom_branch_policies":true}'
have_flags=null
[ -z "$env_json" ] || have_flags=$(jq -c '.deployment_branch_policy // null' <<< "$env_json")
if [ -z "$env_json" ] || [ "$have_flags" != "$want_flags" ]; then
# A PUT replaces what it omits, so carry over existing protection rules.
body=$(jq -c --argjson flags "$want_flags" '
{deployment_branch_policy: $flags}
+ (if . == null then {} else
([.protection_rules[]? | select(.type == "wait_timer") | .wait_timer][0]
// null | if . == null then {} else {wait_timer: .} end)
+ ([.protection_rules[]? | select(.type == "required_reviewers")][0]
// null | if . == null then {} else
{prevent_self_review: .prevent_self_review,
reviewers: [.reviewers[] | {type: .type, id: .reviewer.id}]} end)
end)' <<< "${env_json:-null}")
[ -n "$env_json" ] && echo " exists, policy is $have_flags -> custom branch policies" \
|| echo " missing -> create with custom branch policies"
echo " PUT repos/$repo/environments/$env_name $body"
if $APPLY; then
gh api --method PUT "repos/$repo/environments/$env_name" --input - <<< "$body" > /dev/null
echo " done"
fi
else
echo " ok (custom branch policies)"
fi

# Policies can be listed only once custom policies are on (else 404).
policies=$(get "repos/$repo/environments/$env_name/deployment-branch-policies")
have_default=false
if [ -n "$policies" ]; then
have_default=$(jq --arg b "$default" '[.branch_policies[]? | select(.name == $b and .type == "branch")] | length > 0' <<< "$policies")
while IFS= read -r extra; do
[ -n "$extra" ] || continue
echo " policy: unexpected '$extra' (not removed by this script; delete it by hand: the workflows' check wants exactly one rule)"
status=1
done < <(jq -r --arg b "$default" '.branch_policies[]? | select(.name != $b or .type != "branch") | "\(.type):\(.name)"' <<< "$policies")
fi
if [ "$have_default" = true ]; then
echo " policy: branch '$default' ok"
else
echo " policy: add branch '$default'"
echo " POST repos/$repo/environments/$env_name/deployment-branch-policies name=$default type=branch"
if $APPLY; then
gh api --method POST "repos/$repo/environments/$env_name/deployment-branch-policies" \
-f name="$default" -f type=branch > /dev/null
echo " done"
fi
fi

env_secrets=$(get "repos/$repo/environments/$env_name/secrets" | jq -r '.secrets[]?.name')
wf=$(workflow_of "$env_name")
ready=no
if get "repos/$repo/contents/.github/workflows/$wf" -H "Accept: application/vnd.github.raw" \
| grep -q "environment: $env_name"; then
ready=yes
fi
echo " $wf on $default declares 'environment: $env_name': $ready"
found=false
for s in $(secrets_of "$env_name"); do
in_repo=false
in_env=false
grep -qx "$s" <<< "$repo_secrets" && in_repo=true
grep -qx "$s" <<< "$env_secrets" && in_env=true
$in_repo || $in_env || continue
found=true
echo " secret $s: repo-level=$in_repo, $env_name-environment=$in_env"
if $in_repo && ! $in_env; then
echo " gh secret set $s --env $env_name -R $repo # paste the value; it can't be read back"
fi
if $in_repo; then
echo " gh secret delete $s -R $repo # only after the environment copy exists and the workflow has run once"
fi
done
$found || echo " secrets: none of $(secrets_of "$env_name") at repo or environment level"
if [ "$ready" = no ] && [ "$found" = true ]; then
echo " NOTE: move secrets only after this repo's $wf has 'environment: $env_name' (template sync);"
echo " an older workflow reads repository secrets and would find none."
fi
done
done

exit "$status"
61 changes: 60 additions & 1 deletion .github/scripts/template_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
release channel, print the latest tag in that channel.
- ``apply``: 3-way-apply the ``replace``/``merge`` tiers from a manifest
between two template checkouts (the previously-synced tag and the new one)
and the instance's own working tree; print a summary and list any
and the instance's own working tree, and create the exact-path ``ignore``
files the new release introduces; print a summary and list any
merge conflicts.
- ``check-apply``: confirm an ``apply`` exit status of 0 or 1 against its summary
file, so a crash that exits 1 is not read as merge conflicts.
Expand Down Expand Up @@ -368,6 +369,49 @@ def apply_merge(
return {"merged": merged, "conflicts": conflicts, "binary": binary}


def starter_paths(root: Path, tiers: dict[str, list[str]]) -> set[str]:
"""Return the ``ignore``-tier files under ``root`` named by an exact path.

Only an exact-path entry is a starting copy the template hands over. A
glob (``docs/plans/[0-9]*.md``) or a directory entry matches the template's
own working documents or an instance's own content, which no instance
should receive.
"""
exact = {
pattern
for pattern in tiers["ignore"]
if not pattern.endswith("/") and "*" not in pattern and "?" not in pattern
}
return {
path
for path in exact
if (root / path).is_file() and not is_bytecode(path) and classify(path, tiers) == ["ignore"]
}


def apply_ignore_new(
old_root: Path, new_root: Path, instance_root: Path, paths: set[str]
) -> dict[str, list[str]]:
"""Create ignore-tier files this release introduces; return {"created": [...]}.

The instance owns an ``ignore`` file, so one that exists (or is a symlink)
is never touched. A file is created only when the previous release did not
have it at all, so a file the instance deleted on purpose is not brought
back by every sync. The state file records only the synced tag, so the
previous release's tree is the record of which paths are new.
"""
created: list[str] = []
for path in sorted(paths):
src = new_root / path
dst = instance_root / path
if os.path.lexists(old_root / path) or os.path.lexists(dst):
continue
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src, dst)
created.append(path)
return {"created": created}


def _annotation_escape(value: str, *, prop: bool = False) -> str:
"""Percent-encode ``value`` for a GitHub workflow command (property or message)."""
value = value.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A")
Expand Down Expand Up @@ -395,6 +439,9 @@ def cmd_apply(args: argparse.Namespace) -> int:
summary = {
"replace": apply_replace(args.old_ref, args.new_ref, args.instance_root, replace_paths),
"merge": apply_merge(args.old_ref, args.new_ref, args.instance_root, merge_paths),
"ignore": apply_ignore_new(
args.old_ref, args.new_ref, args.instance_root, starter_paths(args.new_ref, tiers)
),
}
except Exception as err:
# Any failure must exit 2: an uncaught exception exits 1, which reads
Expand Down Expand Up @@ -480,6 +527,18 @@ def pr_body(summary: dict[str, dict[str, list[str]]], old_tag: str, new_tag: str
"Resolve every conflict above before merging: text files have `<<<<<<<` markers; "
"a binary file has none (it is left at the instance's version).",
]
created = summary.get("ignore", {}).get("created", [])
if created:
lines += [
"",
"### New instance-owned files",
"",
"This release introduces these `ignore`-tier files. They were created from the "
"template because this repo lacked them; this repo owns them from now on, and "
"later syncs never overwrite them.",
"",
]
lines += [f"- `{path}`" for path in created]
if "pyproject.toml" in summary["merge"]["merged"] + summary["merge"]["conflicts"]:
lines += [
"",
Expand Down
Loading
Loading