From 526b7fb73a8b2d7a370ba7c3dbf34f867f7fb347 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 19 Apr 2026 20:12:12 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20tool:=20disinto=20backup=20create=20?= =?UTF-8?q?=E2=80=94=20export=20Forgejo=20issues=20+=20disinto-ops=20git?= =?UTF-8?q?=20bundle=20(#1057)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.6 (1M context) --- bin/disinto | 21 ++++++++ lib/backup.sh | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 lib/backup.sh diff --git a/bin/disinto b/bin/disinto index 7f6379d..3740898 100755 --- a/bin/disinto +++ b/bin/disinto @@ -12,6 +12,7 @@ # disinto secrets Manage encrypted secrets # disinto run Run action in ephemeral runner container # disinto ci-logs [--step ] Read CI logs from Woodpecker SQLite +# disinto backup create Export factory state for migration # # Usage: # disinto init https://github.com/user/repo @@ -39,6 +40,7 @@ source "${FACTORY_ROOT}/lib/generators.sh" source "${FACTORY_ROOT}/lib/forge-push.sh" source "${FACTORY_ROOT}/lib/ci-setup.sh" source "${FACTORY_ROOT}/lib/release.sh" +source "${FACTORY_ROOT}/lib/backup.sh" source "${FACTORY_ROOT}/lib/claude-config.sh" # ── Helpers ────────────────────────────────────────────────────────────────── @@ -62,6 +64,7 @@ Usage: disinto hire-an-agent [--formula ] [--local-model ] [--model ] Hire a new agent (create user + .profile repo; re-run to rotate credentials) disinto agent Manage agent state (enable/disable) + disinto backup create Export factory state (issues + ops bundle) disinto edge [options] Manage edge tunnel registrations Edge subcommands: @@ -2893,6 +2896,23 @@ EOF esac } +# ── backup command ──────────────────────────────────────────────────────────── +# Usage: disinto backup create +disinto_backup() { + local subcmd="${1:-}" + shift || true + + case "$subcmd" in + create) + backup_create "$@" + ;; + *) + echo "Usage: disinto backup create " >&2 + exit 1 + ;; + esac +} + # ── Main dispatch ──────────────────────────────────────────────────────────── case "${1:-}" in @@ -2909,6 +2929,7 @@ case "${1:-}" in hire-an-agent) shift; disinto_hire_an_agent "$@" ;; agent) shift; disinto_agent "$@" ;; edge) shift; disinto_edge "$@" ;; + backup) shift; disinto_backup "$@" ;; -h|--help) usage ;; *) usage ;; esac diff --git a/lib/backup.sh b/lib/backup.sh new file mode 100644 index 0000000..8b4c858 --- /dev/null +++ b/lib/backup.sh @@ -0,0 +1,131 @@ +#!/usr/bin/env bash +# ============================================================================= +# disinto backup — export factory state for migration +# +# Usage: source this file, then call backup_create +# Requires: FORGE_URL, FORGE_TOKEN, FORGE_REPO, FORGE_OPS_REPO, OPS_REPO_ROOT +# ============================================================================= +set -euo pipefail + +# Fetch all issues (open + closed) for a repo slug and emit the normalized JSON array. +# Usage: _backup_fetch_issues +_backup_fetch_issues() { + local repo_slug="$1" + local api_url="${FORGE_API_BASE}/repos/${repo_slug}" + + local all_issues="[]" + for state in open closed; do + local page=1 + while true; do + local page_items + page_items=$(curl -sf -X GET \ + -H "Authorization: token ${FORGE_TOKEN}" \ + -H "Content-Type: application/json" \ + "${api_url}/issues?state=${state}&type=issues&limit=50&page=${page}") || { + echo "ERROR: failed to fetch ${state} issues from ${repo_slug} (page ${page})" >&2 + return 1 + } + local count + count=$(printf '%s' "$page_items" | jq 'length' 2>/dev/null) || count=0 + [ -z "$count" ] && count=0 + [ "$count" -eq 0 ] && break + all_issues=$(printf '%s\n%s' "$all_issues" "$page_items" | jq -s 'add') + [ "$count" -lt 50 ] && break + page=$((page + 1)) + done + done + + # Normalize to the schema: number, title, body, labels, state + printf '%s' "$all_issues" | jq '[.[] | { + number: .number, + title: .title, + body: .body, + labels: [.labels[]?.name], + state: .state + }] | sort_by(.number)' +} + +# Create a backup tarball of factory state. +# Usage: backup_create +backup_create() { + local outfile="${1:-}" + if [ -z "$outfile" ]; then + echo "Error: output file required" >&2 + echo "Usage: disinto backup create " >&2 + return 1 + fi + + # Resolve to absolute path before cd-ing into tmpdir + case "$outfile" in + /*) ;; + *) outfile="$(pwd)/${outfile}" ;; + esac + + # Validate required env + : "${FORGE_URL:?FORGE_URL must be set}" + : "${FORGE_TOKEN:?FORGE_TOKEN must be set}" + : "${FORGE_REPO:?FORGE_REPO must be set}" + + local forge_ops_repo="${FORGE_OPS_REPO:-${FORGE_REPO}-ops}" + local ops_repo_root="${OPS_REPO_ROOT:-}" + + if [ -z "$ops_repo_root" ] || [ ! -d "$ops_repo_root/.git" ]; then + echo "Error: OPS_REPO_ROOT (${ops_repo_root:-}) is not a valid git repo" >&2 + return 1 + fi + + local tmpdir + tmpdir=$(mktemp -d) + trap 'rm -rf "$tmpdir"' EXIT + + local project_name="${FORGE_REPO##*/}" + + echo "=== disinto backup create ===" + echo "Forge: ${FORGE_URL}" + echo "Repos: ${FORGE_REPO}, ${forge_ops_repo}" + + # ── 1. Export issues ────────────────────────────────────────────────────── + mkdir -p "${tmpdir}/issues" + + echo "Fetching issues for ${FORGE_REPO}..." + _backup_fetch_issues "$FORGE_REPO" > "${tmpdir}/issues/${project_name}.json" + local main_count + main_count=$(jq 'length' "${tmpdir}/issues/${project_name}.json") + echo " ${main_count} issues exported" + + echo "Fetching issues for ${forge_ops_repo}..." + _backup_fetch_issues "$forge_ops_repo" > "${tmpdir}/issues/${project_name}-ops.json" + local ops_count + ops_count=$(jq 'length' "${tmpdir}/issues/${project_name}-ops.json") + echo " ${ops_count} issues exported" + + # ── 2. Git bundle of ops repo ──────────────────────────────────────────── + mkdir -p "${tmpdir}/repos" + + echo "Creating git bundle for ${forge_ops_repo}..." + git -C "$ops_repo_root" bundle create "${tmpdir}/repos/${project_name}-ops.bundle" --all 2>&1 + echo " bundle created ($(du -h "${tmpdir}/repos/${project_name}-ops.bundle" | cut -f1))" + + # ── 3. Metadata ────────────────────────────────────────────────────────── + local created_at + created_at=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + + jq -n \ + --arg created_at "$created_at" \ + --arg source_host "$(hostname)" \ + --argjson schema_version 1 \ + --arg forgejo_url "$FORGE_URL" \ + '{ + created_at: $created_at, + source_host: $source_host, + schema_version: $schema_version, + forgejo_url: $forgejo_url + }' > "${tmpdir}/metadata.json" + + # ── 4. Pack tarball ────────────────────────────────────────────────────── + echo "Creating tarball: ${outfile}" + tar -czf "$outfile" -C "$tmpdir" metadata.json issues repos + local size + size=$(du -h "$outfile" | cut -f1) + echo "=== Backup complete: ${outfile} (${size}) ===" +}