railyard/apps/gitea/scripts/rithub-agent.sh
caelum0x ec98430794
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
feat(rit,gitea): zero-manual-step GitHub->Rithub import (login, CLI, agent) + orphan-comment migrate bug fix
- 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>
2026-11-19 00:12:57 +03:00

84 lines
3.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Path 3 (owner, 2026-11-18): a Rithub agent the user can ask in plain
# words. v1 handles the one phrase this task asked for literally — "move
# my GitHub here" and close variants — by reusing the exact same shared
# core as path 1 (oauth-auto-import.sh) and the `rit import github` CLI.
# This is real keyword matching, not an LLM call: scoped on purpose to
# keep it deployable today without adding a model dependency to a cron-
# adjacent script. A fuller natural-language agent (and "fix this issue")
# is future work — see plans/railyard.md and
# ai-knowledges/agent-harness/deepseek-superagent-design.md for the
# intended shape of that next step.
#
# Usage: rithub-agent.sh <gitea-username> "<plain text request>"
# Finds the user's own GitHub token the same way oauth-auto-import.sh does
# (their stored "Sign in with GitHub" link) — if they haven't linked
# GitHub yet, says so instead of asking for a token to paste.
set -euo pipefail
GITEA_CONTAINER="railyard-gitea-gitea-1"
GITEA_DB_CONTAINER="railyard-gitea-gitea-postgres-1"
RITHUB_HOST="${RITHUB_HOST:-rithub.dev}"
LOCK_FILE="/var/lock/rithub-bulk-migrate.lock"
gitea_user="${1:-}"
request="${2:-}"
if [ -z "$gitea_user" ] || [ -z "$request" ]; then
echo "usage: rithub-agent.sh <gitea-username> \"<plain text request>\"" >&2
exit 2
fi
lower=$(echo "$request" | tr '[:upper:]' '[:lower:]')
if ! echo "$lower" | grep -qE 'github'; then
echo "I can move repos from GitHub onto Rithub, but I didn't see \"GitHub\" in that request — try \"move my GitHub here\"."
exit 0
fi
if ! echo "$lower" | grep -qE 'move|import|bring|copy|migrate|sync'; then
echo "Did you mean: move your GitHub repos onto Rithub? Try \"move my GitHub here\"."
exit 0
fi
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
echo "Already moving repos for someone else right now — try again in a minute."
exit 0
fi
load1=$(cut -d' ' -f1 /proc/loadavg)
if awk "BEGIN{exit !($load1>=6)}"; then
echo "Rithub is busy right now — try again shortly."
exit 0
fi
source "$(dirname "$0")/lib/bulk-migrate.sh"
github_token=$(docker exec -i "$GITEA_DB_CONTAINER" psql -U gitea -d gitea -t -A -c "
SELECT 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 u.name = '$gitea_user'
AND elu.access_token IS NOT NULL AND elu.access_token != ''
LIMIT 1
" 2>/dev/null | tr -d '[:space:]' || true)
if [ -z "$github_token" ]; then
echo "You haven't connected GitHub on Rithub yet — click \"Continue with GitHub\" once on $RITHUB_HOST, then ask me again. (Or run \`rit import github\` from your own machine — it uses your \`gh\` login directly, no connecting needed.)"
exit 0
fi
token_out=$(docker exec -u git "$GITEA_CONTAINER" gitea admin user generate-access-token \
--username "$gitea_user" --token-name "agent-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 "Something went wrong setting that up — try again in a minute."
exit 1
fi
echo "On it — moving your GitHub repos onto Rithub now. This list updates as each one lands:"
bulk_migrate_user "$gitea_user" "$gitea_token" "$github_token"
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('agent-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