feat(git): add Forgejo API helper and one-command token setup

Adds scripts/forgejo.sh: 'init' prompts for the token with hidden input,
stores it 0600 at ~/.config/phoebus/forgejo.token, verifies the account and
makes the repository public. Also whoami/repo-info/set-public/list-repos/
create-repo/create-release, using a 0600 temp curl config so the token never
appears in argv, logs or history. Wires make forgejo-init/public/release and
documents the flow in docs/GIT-WORKFLOW.md.
This commit is contained in:
Phoebus 2026-09-20 02:25:48 -07:00
parent 2e3888feeb
commit e680084f83
3 changed files with 263 additions and 1 deletions

View file

@ -8,7 +8,7 @@
SHELL := /bin/bash SHELL := /bin/bash
.PHONY: help test test-bash test-rust fmt fmt-check clippy check build static vm hooks commit publish clean .PHONY: help test test-bash test-rust fmt fmt-check clippy check build static vm hooks commit publish forgejo-init public private release clean
help: help:
@printf 'Phoebus OS targets:\n' @printf 'Phoebus OS targets:\n'
@ -22,6 +22,9 @@ help:
@printf ' make hooks enable the committed git hooks\n' @printf ' make hooks enable the committed git hooks\n'
@printf ' make commit MSG="..." stage, check and commit\n' @printf ' make commit MSG="..." stage, check and commit\n'
@printf ' make publish push to the configured remote\n' @printf ' make publish push to the configured remote\n'
@printf ' make forgejo-init store the API token and make the repo public\n'
@printf ' make public make the Forgejo repository public\n'
@printf ' make release TAG=v0.2.0 create a Forgejo release\n'
test: test-bash test-rust test: test-bash test-rust
@ -59,5 +62,18 @@ commit:
publish: publish:
scripts/publish.sh scripts/publish.sh
forgejo-init:
scripts/forgejo.sh init
public:
scripts/forgejo.sh set-public
private:
scripts/forgejo.sh set-private
release:
@if [ -z "$(TAG)" ]; then echo 'usage: make release TAG=v0.2.0'; exit 2; fi
scripts/forgejo.sh create-release "$(TAG)" --name "$(TAG)"
clean: clean:
cd installer-rs && cargo clean cd installer-rs && cargo clean

View file

@ -36,6 +36,35 @@ scripts/publish.sh origin main
git push origin v0.1.0 # publish release tags explicitly git push origin v0.1.0 # publish release tags explicitly
``` ```
### API token setup (one command after the browser step)
Creating a token is the only manual step because it needs your browser login:
**User Settings -> Applications -> Generate New Token**, name it (e.g.
`phoebus-cli`), set Repository and Organization Access to **All (public,
private, and limited)** (the repo starts private), and give `repository`
**Read and write** (everything else "No access"). Then:
```sh
scripts/forgejo.sh init # or: make forgejo-init
```
`init` prompts for the token with hidden input, stores it at
`~/.config/phoebus/forgejo.token` (0600), verifies the account and makes the
repository public. The token is never printed, never passed on a command line
and never committed.
Other helpers:
```sh
scripts/forgejo.sh repo-info
scripts/forgejo.sh list-repos
scripts/forgejo.sh set-public # or: make public
scripts/forgejo.sh create-release v0.2.0 --name v0.2.0 # or: make release TAG=v0.2.0
```
The helper reads `$FORGEJO_TOKEN` if set, otherwise the token file, and uses a
0600 temp curl config so the token stays out of `ps`, logs and history.
## One-time migration (already performed) ## One-time migration (already performed)
```sh ```sh

217
scripts/forgejo.sh Executable file
View file

@ -0,0 +1,217 @@
#!/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"))'
}
usage() {
cat <<'USAGE'
scripts/forgejo.sh <command>
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
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 ;;
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