#!/usr/bin/env bash # repo-bootstrap.sh — pre-clone repos into ~agent/Projects so that # scrum4me-mcp's `wait_for_job` finds a working repoRoot via the # convention-fallback `~/Projects//.git`. # # Idempotent: # - Sets up git credential helper using GH_TOKEN (HTTPS auth) # - For each entry in GH_PRECLONE_REPOS (comma-separated owner/name list): # * If ~/Projects/ exists → `git fetch origin --prune` # * Otherwise → fresh `git clone` # # Runs as the agent user (called from entrypoint.sh after `gosu agent …`). set -uo pipefail source /opt/agent/bin/_lib.sh : "${GH_TOKEN:=}" : "${GH_PRECLONE_REPOS:=}" if [[ -z "$GH_TOKEN" ]]; then log "GH_TOKEN not set — skipping clone bootstrap. wait_for_job will fail until repos exist." return 0 2>/dev/null || exit 0 fi if [[ -z "$GH_PRECLONE_REPOS" ]]; then log "GH_PRECLONE_REPOS empty — nothing to clone." return 0 2>/dev/null || exit 0 fi # ----- 1. configure git credential helper for HTTPS clone/push ----------- mkdir -p "$HOME" git config --global credential.helper store CREDS_FILE="$HOME/.git-credentials" if [[ ! -f "$CREDS_FILE" ]] || ! grep -q "oauth2:${GH_TOKEN}@github.com" "$CREDS_FILE" 2>/dev/null; then printf 'https://oauth2:%s@github.com\n' "$GH_TOKEN" > "$CREDS_FILE" chmod 600 "$CREDS_FILE" log "git credentials helper configured at ${CREDS_FILE}" fi # Prevent the token from leaking into commit-author of automated commits. git config --global user.name "${GIT_AUTHOR_NAME:-Scrum4Me Agent}" git config --global user.email "${GIT_AUTHOR_EMAIL:-agent@scrum4me.local}" # ----- 2. clone-or-fetch each repo -------------------------------------- mkdir -p "$HOME/Projects" IFS=',' read -ra REPOS <<< "$GH_PRECLONE_REPOS" for repo in "${REPOS[@]}"; do repo=$(echo "$repo" | tr -d '[:space:]') [[ -z "$repo" ]] && continue name=$(basename "$repo") target="$HOME/Projects/$name" if [[ -d "$target/.git" ]]; then log "fetching ${repo} into ${target}" git -C "$target" fetch origin --prune --quiet \ || log "WARN: fetch failed for ${repo} (continuing)" else log "cloning ${repo} into ${target}" rm -rf "$target" git clone --quiet "https://github.com/${repo}.git" "$target" \ || { log "ERROR: clone failed for ${repo}"; continue; } fi done log "repo-bootstrap done"