|
| 1 | +const planToGitHookPathsTemplate = String.raw`PLAN_TO_GIT_SYNC_HELPER="$HOOKS_DIR/plan-to-git-sync" |
| 2 | +PLAN_TO_GIT_CODEX_HOOK="$HOOKS_DIR/plan-to-git-codex-hook" |
| 3 | +PLAN_TO_GIT_CLAUDE_HOOK="$HOOKS_DIR/plan-to-git-claude-hook" |
| 4 | +CODEX_REQUIREMENTS_FILE="/etc/codex/requirements.toml" |
| 5 | +CLAUDE_PLAN_TO_GIT_SETTINGS_FILE="$CLAUDE_CONFIG_DIR/settings.json"` |
| 6 | + |
| 7 | +const planToGitSyncHelperInstallTemplate = String.raw`cat <<'EOF' > "$PLAN_TO_GIT_SYNC_HELPER" |
| 8 | +#!/usr/bin/env bash |
| 9 | +set -euo pipefail |
| 10 | +
|
| 11 | +if [ "${"${"}DOCKER_GIT_SKIP_PLAN_TO_GIT:-}" = "1" ]; then |
| 12 | + exit 0 |
| 13 | +fi |
| 14 | +
|
| 15 | +if ! command -v plan-to-git >/dev/null 2>&1; then |
| 16 | + echo "[plan-to-git] Error: plan-to-git not found" >&2 |
| 17 | + exit 1 |
| 18 | +fi |
| 19 | +
|
| 20 | +export PLAN_TO_GIT_STATE_DIR="${"${"}PLAN_TO_GIT_STATE_DIR:-/tmp/plan-to-git}" |
| 21 | +
|
| 22 | +docker_git_plan_to_git_explicit_pr_supported() { |
| 23 | + plan-to-git sync --help 2>/dev/null | grep -q -- "--pr <PR>" |
| 24 | +} |
| 25 | +
|
| 26 | +docker_git_plan_to_git_resolve_pr_number() { |
| 27 | + local candidate="" |
| 28 | + local key="" |
| 29 | + for key in DOCKER_GIT_PR_NUMBER PR_NUMBER GITHUB_PR_NUMBER; do |
| 30 | + candidate="${"${"}!key:-}" |
| 31 | + if [[ "$candidate" =~ ^[0-9]+$ ]]; then |
| 32 | + printf "%s\n" "$candidate" |
| 33 | + return 0 |
| 34 | + fi |
| 35 | + done |
| 36 | +
|
| 37 | + candidate="${"${"}REPO_REF:-}" |
| 38 | + if [[ "$candidate" =~ ^refs/pull/([0-9]+)/head$ ]]; then |
| 39 | + printf "%s\n" "${"${"}BASH_REMATCH[1]}" |
| 40 | + return 0 |
| 41 | + fi |
| 42 | + if [[ "$candidate" =~ ^pull/([0-9]+)$ ]]; then |
| 43 | + printf "%s\n" "${"${"}BASH_REMATCH[1]}" |
| 44 | + return 0 |
| 45 | + fi |
| 46 | +
|
| 47 | + if command -v gh >/dev/null 2>&1; then |
| 48 | + candidate="$(gh pr view --json number --jq .number 2>/dev/null || true)" |
| 49 | + if [[ "$candidate" =~ ^[0-9]+$ ]]; then |
| 50 | + printf "%s\n" "$candidate" |
| 51 | + return 0 |
| 52 | + fi |
| 53 | + fi |
| 54 | +
|
| 55 | + return 0 |
| 56 | +} |
| 57 | +
|
| 58 | +docker_git_plan_to_git_sync() { |
| 59 | + local pr_number="" |
| 60 | + pr_number="$(docker_git_plan_to_git_resolve_pr_number || true)" |
| 61 | +
|
| 62 | + if [[ -n "$pr_number" ]] && docker_git_plan_to_git_explicit_pr_supported; then |
| 63 | + echo "[plan-to-git] Syncing queued agent plans to PR #$pr_number" |
| 64 | + plan-to-git sync --pr "$pr_number" |
| 65 | + return 0 |
| 66 | + fi |
| 67 | +
|
| 68 | + echo "[plan-to-git] Syncing queued agent plans via current branch discovery" |
| 69 | + plan-to-git sync |
| 70 | +} |
| 71 | +
|
| 72 | +docker_git_plan_to_git_sync |
| 73 | +EOF |
| 74 | +chmod 0755 "$PLAN_TO_GIT_SYNC_HELPER"` |
| 75 | + |
| 76 | +const planToGitPostPushSyncTemplate = String |
| 77 | + .raw`# CHANGE: backfill agent session plans before syncing the current branch or explicit PR. |
| 78 | +# WHY: live agent hooks can be unavailable in already-running sessions; session logs are the durable fallback. |
| 79 | +# QUOTE(ТЗ): "что бы всё уходило на гитхаб автоматически" |
| 80 | +# REF: issue-397 |
| 81 | +if [ "${"${"}DOCKER_GIT_SKIP_PLAN_TO_GIT:-}" != "1" ]; then |
| 82 | + if ! command -v plan-to-git >/dev/null 2>&1; then |
| 83 | + echo "[plan-to-git] Error: plan-to-git not found" >&2 |
| 84 | + exit 1 |
| 85 | + fi |
| 86 | + plan-to-git import-codex --no-sync |
| 87 | + plan-to-git import-claude --no-sync |
| 88 | + PLAN_TO_GIT_SYNC_HELPER="${"${"}DOCKER_GIT_PLAN_TO_GIT_SYNC_HELPER:-/opt/docker-git/hooks/plan-to-git-sync}" |
| 89 | + if [[ -x "$PLAN_TO_GIT_SYNC_HELPER" ]]; then |
| 90 | + "$PLAN_TO_GIT_SYNC_HELPER" |
| 91 | + else |
| 92 | + echo "[plan-to-git] Sync helper not found; falling back to current branch discovery" >&2 |
| 93 | + plan-to-git sync |
| 94 | + fi |
| 95 | +fi` |
| 96 | + |
| 97 | +const planToGitAgentHooksInstallTemplate = String.raw`cat <<'EOF' > "$PLAN_TO_GIT_CODEX_HOOK" |
| 98 | +#!/usr/bin/env bash |
| 99 | +set -euo pipefail |
| 100 | +
|
| 101 | +if [ "${"${"}DOCKER_GIT_SKIP_PLAN_TO_GIT:-}" = "1" ]; then |
| 102 | + exit 0 |
| 103 | +fi |
| 104 | +
|
| 105 | +if ! command -v plan-to-git >/dev/null 2>&1; then |
| 106 | + echo "[plan-to-git] Error: plan-to-git not found" >&2 |
| 107 | + exit 1 |
| 108 | +fi |
| 109 | +
|
| 110 | +export PLAN_TO_GIT_STATE_DIR="${"${"}PLAN_TO_GIT_STATE_DIR:-/tmp/plan-to-git}" |
| 111 | +plan-to-git hook --source codex |
| 112 | +PLAN_TO_GIT_SYNC_HELPER="${"${"}DOCKER_GIT_PLAN_TO_GIT_SYNC_HELPER:-/opt/docker-git/hooks/plan-to-git-sync}" |
| 113 | +"$PLAN_TO_GIT_SYNC_HELPER" >&2 || true |
| 114 | +EOF |
| 115 | +chmod 0755 "$PLAN_TO_GIT_CODEX_HOOK" |
| 116 | +
|
| 117 | +cat <<'EOF' > "$PLAN_TO_GIT_CLAUDE_HOOK" |
| 118 | +#!/usr/bin/env bash |
| 119 | +set -euo pipefail |
| 120 | +
|
| 121 | +if [ "${"${"}DOCKER_GIT_SKIP_PLAN_TO_GIT:-}" = "1" ]; then |
| 122 | + exit 0 |
| 123 | +fi |
| 124 | +
|
| 125 | +if ! command -v plan-to-git >/dev/null 2>&1; then |
| 126 | + echo "[plan-to-git] Error: plan-to-git not found" >&2 |
| 127 | + exit 1 |
| 128 | +fi |
| 129 | +
|
| 130 | +export PLAN_TO_GIT_STATE_DIR="${"${"}PLAN_TO_GIT_STATE_DIR:-/tmp/plan-to-git}" |
| 131 | +plan-to-git hook --source claude |
| 132 | +PLAN_TO_GIT_SYNC_HELPER="${"${"}DOCKER_GIT_PLAN_TO_GIT_SYNC_HELPER:-/opt/docker-git/hooks/plan-to-git-sync}" |
| 133 | +"$PLAN_TO_GIT_SYNC_HELPER" >&2 || true |
| 134 | +EOF |
| 135 | +chmod 0755 "$PLAN_TO_GIT_CLAUDE_HOOK" |
| 136 | +
|
| 137 | +mkdir -p "$(dirname "$CODEX_REQUIREMENTS_FILE")" |
| 138 | +cat <<'EOF' > "$CODEX_REQUIREMENTS_FILE" |
| 139 | +# docker-git managed Codex requirements |
| 140 | +
|
| 141 | +[features] |
| 142 | +hooks = true |
| 143 | +
|
| 144 | +[hooks] |
| 145 | +managed_dir = "/opt/docker-git/hooks" |
| 146 | +
|
| 147 | +[[hooks.UserPromptSubmit]] |
| 148 | +[[hooks.UserPromptSubmit.hooks]] |
| 149 | +type = "command" |
| 150 | +command = "/opt/docker-git/hooks/plan-to-git-codex-hook" |
| 151 | +statusMessage = "Capturing plan decision" |
| 152 | +
|
| 153 | +[[hooks.Stop]] |
| 154 | +[[hooks.Stop.hooks]] |
| 155 | +type = "command" |
| 156 | +command = "/opt/docker-git/hooks/plan-to-git-codex-hook" |
| 157 | +statusMessage = "Capturing agent plan" |
| 158 | +EOF |
| 159 | +chmod 0644 "$CODEX_REQUIREMENTS_FILE" |
| 160 | +
|
| 161 | +docker_git_install_claude_plan_to_git_hooks() { |
| 162 | + if [ "${"${"}DOCKER_GIT_SKIP_PLAN_TO_GIT:-}" = "1" ]; then |
| 163 | + return 0 |
| 164 | + fi |
| 165 | +
|
| 166 | + CLAUDE_PLAN_TO_GIT_SETTINGS_FILE="${"${"}CLAUDE_PLAN_TO_GIT_SETTINGS_FILE:-${"${"}CLAUDE_CONFIG_DIR:-/home/dev/.claude}/settings.json}" |
| 167 | + CLAUDE_PLAN_TO_GIT_SETTINGS_FILE="$CLAUDE_PLAN_TO_GIT_SETTINGS_FILE" PLAN_TO_GIT_CLAUDE_HOOK="$PLAN_TO_GIT_CLAUDE_HOOK" node - <<'NODE' |
| 168 | +const fs = require("node:fs") |
| 169 | +const path = require("node:path") |
| 170 | +
|
| 171 | +const settingsPath = process.env.CLAUDE_PLAN_TO_GIT_SETTINGS_FILE |
| 172 | +const hookCommand = process.env.PLAN_TO_GIT_CLAUDE_HOOK || "/opt/docker-git/hooks/plan-to-git-claude-hook" |
| 173 | +if (typeof settingsPath !== "string" || settingsPath.length === 0) { |
| 174 | + process.exit(0) |
| 175 | +} |
| 176 | +
|
| 177 | +const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value) |
| 178 | +
|
| 179 | +let settings = {} |
| 180 | +try { |
| 181 | + const parsed = JSON.parse(fs.readFileSync(settingsPath, "utf8")) |
| 182 | + settings = isRecord(parsed) ? parsed : {} |
| 183 | +} catch { |
| 184 | + settings = {} |
| 185 | +} |
| 186 | +
|
| 187 | +const currentHooks = isRecord(settings.hooks) ? settings.hooks : {} |
| 188 | +const nextHooks = { ...currentHooks } |
| 189 | +const managedHook = { type: "command", command: hookCommand } |
| 190 | +const ensureEventHook = (eventName) => { |
| 191 | + const currentEventHooks = Array.isArray(nextHooks[eventName]) ? nextHooks[eventName] : [] |
| 192 | + const alreadyInstalled = currentEventHooks.some((entry) => |
| 193 | + isRecord(entry) && |
| 194 | + Array.isArray(entry.hooks) && |
| 195 | + entry.hooks.some((hook) => isRecord(hook) && hook.type === "command" && hook.command === hookCommand) |
| 196 | + ) |
| 197 | + nextHooks[eventName] = alreadyInstalled ? currentEventHooks : [...currentEventHooks, { hooks: [managedHook] }] |
| 198 | +} |
| 199 | +
|
| 200 | +ensureEventHook("UserPromptSubmit") |
| 201 | +ensureEventHook("Stop") |
| 202 | +
|
| 203 | +const nextSettings = { ...settings, hooks: nextHooks } |
| 204 | +if (JSON.stringify(settings) === JSON.stringify(nextSettings)) { |
| 205 | + process.exit(0) |
| 206 | +} |
| 207 | +
|
| 208 | +fs.mkdirSync(path.dirname(settingsPath), { recursive: true }) |
| 209 | +fs.writeFileSync(settingsPath, JSON.stringify(nextSettings, null, 2) + "\n", { mode: 0o600 }) |
| 210 | +NODE |
| 211 | + chmod 0600 "$CLAUDE_PLAN_TO_GIT_SETTINGS_FILE" 2>/dev/null || true |
| 212 | + chown 1000:1000 "$CLAUDE_PLAN_TO_GIT_SETTINGS_FILE" 2>/dev/null || true |
| 213 | +} |
| 214 | +
|
| 215 | +docker_git_install_claude_plan_to_git_hooks` |
| 216 | + |
| 217 | +export const renderPlanToGitHookPaths = (): string => planToGitHookPathsTemplate |
| 218 | + |
| 219 | +export const renderPlanToGitSyncHelperInstall = (): string => planToGitSyncHelperInstallTemplate |
| 220 | + |
| 221 | +export const renderPlanToGitPostPushSync = (): string => planToGitPostPushSyncTemplate |
| 222 | + |
| 223 | +export const renderPlanToGitAgentHooksInstall = (): string => planToGitAgentHooksInstallTemplate |
0 commit comments