#!/usr/bin/env bash # Mint a Gitea API token and store it as the repo Actions secret GITEA_SETTINGS_TOKEN, # so ci/gitea-settings.sh can read the live branch-protection rule during the gate. # # The token value is NEVER printed. It goes from the token API into the secrets API and # nowhere else -- not to stdout, not to a file, not into an agent transcript. # # Scope: `read:repository` + `read:admin` are what the check needs (the rule object, and the # runner list). Gitea token scopes are coarse; grant the least that works on your version and # confirm the check goes green rather than assuming the scope name. set -euo pipefail HOST=gitea.home.keithjackson.dev; OWNER=Home-Network; REPO=home-network-k3s-impl NAME="ci-gitea-settings-$(date +%Y%m%d%H%M%S)" creds="$(printf 'protocol=https\nhost=%s\n\n' "$HOST" | git credential fill)" U="$(sed -n 's/^username=//p' <<<"$creds")"; P="$(sed -n 's/^password=//p' <<<"$creds")" basic() { curl -sS --config <(printf 'user = "%s:%s"\n' "$U" "$P") "$@"; } echo "==> minting token '${NAME}' as ${U}" TOKEN="$(basic -X POST -H 'Content-Type: application/json' \ -d "{\"name\":\"${NAME}\",\"scopes\":[\"read:repository\",\"read:admin\"]}" \ "https://${HOST}/api/v1/users/${U}/tokens" | jq -r '.sha1 // empty')" [ -n "$TOKEN" ] || { echo "token creation failed (check scopes for your Gitea version)" >&2; exit 1; } echo "==> got a ${#TOKEN}-character token (value not shown)" echo "==> storing it as repo Actions secret GITEA_SETTINGS_TOKEN" basic -X PUT -H 'Content-Type: application/json' \ -d "$(jq -n --arg d "$TOKEN" '{data:$d}')" \ "https://${HOST}/api/v1/repos/${OWNER}/${REPO}/actions/secrets/GITEA_SETTINGS_TOKEN" >/dev/null unset TOKEN echo "==> stored." cat <<'NOTE' Next: 1. .gitea/workflows/gate.yml already passes this secret to the gate step. 2. Open any PR and confirm the gate prints "gitea-settings: green" -- it SKIPS silently without the token, so a green run is the only proof the wiring works. 3. THEN set REQUIRE_GITEA_SETTINGS=true in gate.yml, so a future missing token BLOCKS instead of skipping. Do that only after you have seen it green, or you lock the repo. NOTE