#!/usr/bin/env bash # Forgejo API helper for the Phoebus repository. # # The token is read from $FORGEJO_TOKEN or ~/.config/phoebus/forgejo.token and # is never printed, never passed on a command line and never written to git. # # Usage: # scripts/forgejo.sh init # prompt for the token (hidden), # # save it, then make the repo public # scripts/forgejo.sh whoami # scripts/forgejo.sh repo-info # scripts/forgejo.sh set-public # scripts/forgejo.sh set-private # scripts/forgejo.sh list-repos # scripts/forgejo.sh create-repo NAME [--private] # scripts/forgejo.sh create-release TAG [--name NAME] [--notes FILE] # scripts/forgejo.sh list-releases # # Environment: # FORGEJO_HOST default https://git.phoebus.club # FORGEJO_TOKEN overrides the token file # FORGEJO_REPO default derived from the origin remote (owner/repo) set -euo pipefail host="${FORGEJO_HOST:-https://git.phoebus.club}" token_file="${HOME}/.config/phoebus/forgejo.token" die() { printf 'forgejo: %s\n' "$*" >&2; exit 1; } need_token() { [[ -n "${FORGEJO_TOKEN:-}" || -r "$token_file" ]] || \ die "no token. Run 'scripts/forgejo.sh init' (or set FORGEJO_TOKEN)." } read_token() { if [[ -n "${FORGEJO_TOKEN:-}" ]]; then printf '%s' "$FORGEJO_TOKEN" return 0 fi [[ -r "$token_file" ]] || \ die "no token. Run 'scripts/forgejo.sh init' (or set FORGEJO_TOKEN)." tr -d '\n' <"$token_file" } repo_slug() { if [[ -n "${FORGEJO_REPO:-}" ]]; then printf '%s' "$FORGEJO_REPO" return 0 fi local url url="$(git remote get-url origin 2>/dev/null || true)" if [[ "$url" =~ :([^/]+)/([^/]+)\.git$ ]]; then printf '%s/%s' "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" elif [[ "$url" =~ /([^/]+)/([^/]+)\.git$ ]]; then printf '%s/%s' "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" else printf 'phoebus/phoebus_os' fi } # api METHOD PATH [JSON] # # The token is written to a 0600 temp curl config and removed immediately, so # it never appears in argv. The config path (not the token) is what curl sees. api() { local method="$1" path="$2" data="${3:-}" local token cfg rc=0 token="$(read_token)" cfg="$(mktemp)" chmod 600 "$cfg" printf 'header = "Authorization: token %s"\n' "$token" >"$cfg" if [[ -n "$data" ]]; then curl -fsS -K "$cfg" -H 'Content-Type: application/json' \ -X "$method" -d "$data" "${host}${path}" || rc=$? else curl -fsS -K "$cfg" -X "$method" "${host}${path}" || rc=$? fi rm -f "$cfg" unset token return "$rc" } cmd_init() { mkdir -p "$(dirname "$token_file")" local token read -r -s -p 'paste Forgejo token (input hidden): ' token printf '\n' [[ -n "$token" ]] || die "empty token" umask 077 printf '%s' "$token" >"$token_file" chmod 600 "$token_file" printf 'saved token to %s\n' "$token_file" unset token printf 'authenticated as: ' api GET /api/v1/user | python3 -c 'import sys,json; d=json.load(sys.stdin); print(d.get("login"))' if cmd_repo_exists; then cmd_set_visible public else printf 'note: %s does not exist yet; push main first, then run set-public\n' "$(repo_slug)" fi } cmd_whoami() { api GET /api/v1/user | python3 -m json.tool } cmd_repo_exists() { local slug; slug="$(repo_slug)" api GET "/api/v1/repos/${slug}" >/dev/null 2>&1 } cmd_repo_info() { local slug; slug="$(repo_slug)" api GET "/api/v1/repos/${slug}" | python3 -c ' import sys, json d = json.load(sys.stdin) print("full_name :", d.get("full_name")) print("private :", d.get("private")) print("visibility:", d.get("visibility")) print("default :", d.get("default_branch")) print("ssh_url :", d.get("ssh_url"))' } cmd_set_visible() { local visibility="$1" local slug; slug="$(repo_slug)" local private case "$visibility" in public) private=false ;; private) private=true ;; *) die "visibility must be public or private" ;; esac api PATCH "/api/v1/repos/${slug}" "{\"private\":${private}}" >/dev/null cmd_repo_info if [[ "$visibility" == "public" ]]; then local code code="$(curl -s -o /dev/null -w '%{http_code}' "${host}/api/v1/repos/${slug}")" [[ "$code" == "200" ]] && printf 'confirmed public (HTTP 200)\n' fi } cmd_list_repos() { api GET '/api/v1/user/repos?limit=50' | python3 -c ' import sys, json for r in json.load(sys.stdin): name = r.get("full_name") print("%-40s private=%s" % (name, r.get("private")))' } cmd_create_repo() { local name="$1"; shift local private=false [[ "${1:-}" == "--private" ]] && private=true local login login="$(api GET /api/v1/user | python3 -c 'import sys,json; print(json.load(sys.stdin)["login"])')" api POST /api/v1/user/repos "{\"name\":\"${name}\",\"private\":${private}}" >/dev/null printf 'created %s/%s (private=%s)\n' "$login" "$name" "$private" } cmd_create_release() { local tag="$1"; shift local name="$tag" notes="" while (($#)); do case "$1" in --name) name="$2"; shift 2 ;; --notes) notes="$(cat "$2")"; shift 2 ;; *) die "unknown option $1" ;; esac done local slug; slug="$(repo_slug)" api POST "/api/v1/repos/${slug}/releases" \ "$(python3 -c 'import json,sys; print(json.dumps({"tag_name":sys.argv[1],"name":sys.argv[2],"body":sys.argv[3]}))' "$tag" "$name" "$notes")" >/dev/null printf 'created release %s\n' "$tag" } cmd_list_releases() { local slug; slug="$(repo_slug)" api GET "/api/v1/repos/${slug}/releases" | python3 -c ' import sys, json for r in json.load(sys.stdin): print(r.get("tag_name"), "-", r.get("name"))' } cmd_protect() { local slug; slug="$(repo_slug)" # Keep direct pushes working (publish.sh pushes main) while blocking # force-push and branch deletion. api POST "/api/v1/repos/${slug}/branch_protections" \ '{"branch_name":"main","enable_push":true,"enable_push_whitelist":true,"push_whitelist_usernames":["phoebus"],"enable_force_push":false}' >/dev/null printf 'protected %s:main (push allowed for phoebus; force-push blocked)\n' "$slug" } cmd_unprotect() { local slug; slug="$(repo_slug)" api DELETE "/api/v1/repos/${slug}/branch_protections/main" >/dev/null printf 'removed branch protection on %s:main\n' "$slug" } usage() { cat <<'USAGE' scripts/forgejo.sh init prompt for the token (hidden), save it, then make the repository public whoami show the authenticated user repo-info show owner/repo, visibility, ssh url set-public | set-private change repository visibility protect | unprotect branch protection for main list-repos list repositories for the user create-repo NAME [--private] create a repository create-release TAG [--name NAME] [--notes FILE] list-releases Token is read from $FORGEJO_TOKEN or ~/.config/phoebus/forgejo.token and is never printed. Set FORGEJO_REPO=owner/repo to override the detected repo. USAGE } case "${1:-help}" in init) cmd_init ;; whoami) need_token; cmd_whoami ;; repo-info) need_token; cmd_repo_info ;; set-public) need_token; cmd_set_visible public ;; set-private) need_token; cmd_set_visible private ;; protect) need_token; cmd_protect ;; unprotect) need_token; cmd_unprotect ;; list-repos) need_token; cmd_list_repos ;; create-repo) need_token; shift; cmd_create_repo "$@" ;; create-release) need_token; shift; cmd_create_release "$@" ;; list-releases) need_token; cmd_list_releases ;; help|-h|--help) usage ;; *) die "unknown command '${1}'. Run scripts/forgejo.sh help" ;; esac