Some checks failed
ci / cargo test (abacus) (push) Has been cancelled
ci / cargo test (agent-runtime) (push) Has been cancelled
ci / cargo test (context-passport) (push) Has been cancelled
ci / cargo test (game-economy) (push) Has been cancelled
ci / cargo test (ledgerline) (push) Has been cancelled
ci / cargo test (mint) (push) Has been cancelled
ci / cargo test (rago) (push) Has been cancelled
ci / cargo test (relay) (push) Has been cancelled
ci / cargo test (toolgate) (push) Has been cancelled
ci / cargo test (turnstile) (push) Has been cancelled
ci / cargo test (vend) (push) Has been cancelled
ci / node suites (push) Has been cancelled
- apps/rit: `rit login` (PKCE OAuth2 against a new first-party "Rit CLI" Gitea app, loopback callback, no token paste) and `rit import github` (lists own repos+forks via `gh auth token`, smallest-first, skip- existing, mirror=true). Wired as a transparent credential fallback into remote.rs so push/fetch/pull/clone/issue/pr all work after one login. `rit push` also syncs a `github` remote if present via system git. - apps/gitea/scripts: oauth-auto-import.sh (path 1, cron-driven, triggered by Gitea's own GitHub OAuth login source, flock+load-gated) and rithub-agent.sh (path 3, plain-words trigger) share lib/bulk-migrate.sh as their core. - Found+fixed two real API bugs via live testing against rithub.dev (MigrateRepoOptions.service is a string not an int; a bash bool- interpolation bug), and one real production bug (orphan GitHub comments on a deleted issue/PR 500 Gitea's comment-import phase even though the repo+code already landed -- now detected and reported as a partial success with a note instead of a false failure). - Full real end-to-end verification with a throwaway mail.tm Rithub user, fully cleaned up after. 121/121 existing rit tests still pass. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
92 lines
4.2 KiB
Bash
Executable File
92 lines
4.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Path 1 (owner, 2026-11-18): "Sign in with GitHub" -> automatic. Runs on
|
|
# railyard-box on a timer (see the cron line this script's own header docs
|
|
# below). Finds users who signed up/logged in via the GitHub OAuth2 login
|
|
# source (once RITHUB_GITHUB_CLIENT_ID/SECRET land and
|
|
# add-rithub-oauth-sources.sh has wired it — see plans/OWNER-KEYS-2026-11-18.md)
|
|
# and runs the same bulk migrate everyone else uses, with zero clicks from
|
|
# the user beyond the one "Continue with GitHub" they already did.
|
|
#
|
|
# Safe to install now even before the GitHub OAuth source exists: the query
|
|
# below just returns zero rows until it does, so this is a no-op until the
|
|
# owner step lands, then starts working with no further deploy.
|
|
#
|
|
# Install (one-time, on railyard-box):
|
|
# sudo mkdir -p /var/lib/rithub && sudo chown git:git /var/lib/rithub
|
|
# sudo crontab -u git -l 2>/dev/null | { cat; echo "*/5 * * * * /srv/railyard-gitea/scripts/oauth-auto-import.sh >> /var/log/rithub-oauth-auto-import.log 2>&1"; } | sudo crontab -u git -
|
|
set -euo pipefail
|
|
|
|
LOCK_FILE="/var/lock/rithub-bulk-migrate.lock"
|
|
DONE_FILE="/var/lib/rithub/oauth-import-done.list"
|
|
GITEA_CONTAINER="railyard-gitea-gitea-1"
|
|
GITEA_DB_CONTAINER="railyard-gitea-gitea-postgres-1"
|
|
RITHUB_HOST="${RITHUB_HOST:-rithub.dev}"
|
|
|
|
mkdir -p "$(dirname "$DONE_FILE")" 2>/dev/null || true
|
|
touch "$DONE_FILE" 2>/dev/null || true
|
|
|
|
# Global concurrency 1 across ALL trigger paths (this poller, the plain-
|
|
# words agent, and any future caller) -- not just against itself. Skip
|
|
# quietly (next timer tick retries) rather than blocking a cron job.
|
|
exec 9>"$LOCK_FILE"
|
|
if ! flock -n 9; then
|
|
exit 0
|
|
fi
|
|
|
|
# Load-aware: railyard-box has 2 CPUs. Skip this tick if the box is busy —
|
|
# same >=6 gate used elsewhere in this repo for deploys/heavy jobs — or if
|
|
# a docker build/buildx is running (an actual build matters more than a
|
|
# background repo sync).
|
|
load1=$(cut -d' ' -f1 /proc/loadavg)
|
|
if awk "BEGIN{exit !($load1>=6)}"; then
|
|
exit 0
|
|
fi
|
|
if pgrep -f "docker.*build|buildx" >/dev/null 2>&1; then
|
|
exit 0
|
|
fi
|
|
|
|
source "$(dirname "$0")/lib/bulk-migrate.sh"
|
|
|
|
# Read-only SELECT only (standing rule for this DB — plans/railyard.md).
|
|
# One eligible (user, token) pair per GitHub-linked login that has a real
|
|
# access_token and hasn't been processed before.
|
|
rows=$(docker exec -i "$GITEA_DB_CONTAINER" psql -U gitea -d gitea -t -A -F $'\t' -c "
|
|
SELECT u.name, elu.access_token
|
|
FROM external_login_user elu
|
|
JOIN \"user\" u ON u.id = elu.user_id
|
|
JOIN login_source ls ON ls.id = elu.login_source_id
|
|
WHERE ls.name = 'GitHub'
|
|
AND elu.access_token IS NOT NULL AND elu.access_token != ''
|
|
" 2>/dev/null || true)
|
|
|
|
[ -z "$rows" ] && exit 0
|
|
|
|
while IFS=$'\t' read -r gitea_user github_token; do
|
|
[ -z "$gitea_user" ] && continue
|
|
grep -qx "$gitea_user" "$DONE_FILE" && continue
|
|
|
|
# Mint a short-lived admin-issued token to act as this user (we never
|
|
# store or ask for their Rithub password/PAT — only the GitHub token
|
|
# Gitea already captured from their own "Sign in with GitHub" click).
|
|
token_out=$(docker exec -u git "$GITEA_CONTAINER" gitea admin user generate-access-token \
|
|
--username "$gitea_user" --token-name "oauth-auto-import-$(date +%s)" \
|
|
--scopes write:repository,read:user 2>&1 || true)
|
|
gitea_token=$(echo "$token_out" | grep -o '[0-9a-f]\{40\}' | tail -1)
|
|
if [ -z "$gitea_token" ]; then
|
|
echo "$(date -u +%FT%TZ) could not mint a token for $gitea_user, skipping"
|
|
continue
|
|
fi
|
|
|
|
echo "$(date -u +%FT%TZ) auto-importing GitHub repos for $gitea_user"
|
|
bulk_migrate_user "$gitea_user" "$gitea_token" "$github_token" || true
|
|
|
|
# Best-effort revoke (Gitea's token-delete API needs basic auth, not
|
|
# token auth, for a token to revoke itself — same known limitation
|
|
# documented elsewhere in plans/railyard.md for leftover QA tokens).
|
|
token_id=$(curl -s -u "${gitea_user}:${gitea_token}" "https://${RITHUB_HOST}/api/v1/user/tokens" \
|
|
| python3 -c "import json,sys;[print(t['id']) for t in json.load(sys.stdin) if t['name'].startswith('oauth-auto-import')]" 2>/dev/null | tail -1)
|
|
[ -n "$token_id" ] && curl -s -o /dev/null -u "${gitea_user}:${gitea_token}" -X DELETE "https://${RITHUB_HOST}/api/v1/user/tokens/$token_id" || true
|
|
|
|
echo "$gitea_user" >> "$DONE_FILE"
|
|
done <<<"$rows"
|