diff --git a/.woodpecker/agent-smoke.sh b/.woodpecker/agent-smoke.sh index 591653e..40d95b8 100644 --- a/.woodpecker/agent-smoke.sh +++ b/.woodpecker/agent-smoke.sh @@ -91,7 +91,7 @@ echo "=== 2/2 Function resolution ===" # Functions provided by shared lib files (available to all agent scripts via source) LIB_FUNS=$( - for f in lib/agent-session.sh lib/env.sh lib/ci-helpers.sh lib/load-project.sh; do + for f in lib/agent-session.sh lib/env.sh lib/ci-helpers.sh lib/load-project.sh lib/file-action-issue.sh; do if [ -f "$f" ]; then get_fns "$f"; fi done | sort -u ) @@ -159,6 +159,7 @@ check_script dev/dev-poll.sh check_script dev/phase-test.sh check_script gardener/gardener-agent.sh lib/agent-session.sh check_script gardener/gardener-poll.sh +check_script gardener/gardener-run.sh check_script review/review-pr.sh check_script review/review-poll.sh check_script planner/planner-poll.sh diff --git a/gardener/gardener-run.sh b/gardener/gardener-run.sh index 2147123..79dc379 100755 --- a/gardener/gardener-run.sh +++ b/gardener/gardener-run.sh @@ -8,16 +8,17 @@ # ============================================================================= set -euo pipefail -SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" -FACTORY_ROOT="$(dirname "$SCRIPT_DIR")" +FACTORY_ROOT="$(cd "$(dirname "$0")/.." && pwd)" # Load shared environment (with optional project TOML override) # Usage: gardener-run.sh [projects/harb.toml] export PROJECT_TOML="${1:-}" # shellcheck source=../lib/env.sh source "$FACTORY_ROOT/lib/env.sh" +# shellcheck source=../lib/file-action-issue.sh +source "$FACTORY_ROOT/lib/file-action-issue.sh" -LOG_FILE="$SCRIPT_DIR/gardener.log" +LOG_FILE="$FACTORY_ROOT/gardener/gardener.log" LOCK_FILE="/tmp/gardener-run.lock" log() { echo "[$(date -u +%Y-%m-%dT%H:%M:%S)Z] $*" >> "$LOG_FILE"; } @@ -43,28 +44,7 @@ fi log "--- Gardener run start ---" -# ── Dedup: skip if an open run-gardener action issue already exists ─────── -OPEN_ACTIONS=$(codeberg_api GET "/issues?state=open&type=issues&labels=action&limit=50" 2>/dev/null || true) -if [ -n "$OPEN_ACTIONS" ] && [ "$OPEN_ACTIONS" != "null" ]; then - EXISTING=$(printf '%s' "$OPEN_ACTIONS" | \ - jq '[.[] | select(.title | test("run-gardener"))] | length' 2>/dev/null || echo 0) - if [ "${EXISTING:-0}" -gt 0 ]; then - log "poll: open run-gardener action issue already exists — skipping" - log "--- Gardener run done ---" - exit 0 - fi -fi - -# ── Fetch 'action' label ID ────────────────────────────────────────────── -ACTION_LABEL_ID=$(codeberg_api GET "/labels" 2>/dev/null | \ - jq -r '.[] | select(.name == "action") | .id' 2>/dev/null || true) - -if [ -z "$ACTION_LABEL_ID" ]; then - log "ERROR: 'action' label not found — cannot file gardener issue" - exit 1 -fi - -# ── File action issue ───────────────────────────────────────────────────── +# ── File action issue for run-gardener formula ──────────────────────────── ISSUE_BODY="--- formula: run-gardener model: opus @@ -76,21 +56,20 @@ AGENTS.md update, and commit-and-pr. Filed automatically by \`gardener-run.sh\`." -PAYLOAD=$(jq -nc \ - --arg title "action: run-gardener — periodic housekeeping" \ - --arg body "$ISSUE_BODY" \ - --argjson labels "[$ACTION_LABEL_ID]" \ - '{title: $title, body: $body, labels: $labels}') +_rc=0 +file_action_issue "run-gardener" "action: run-gardener — periodic housekeeping" "$ISSUE_BODY" || _rc=$? +case "$_rc" in + 0) ;; + 1) log "poll: open run-gardener action issue already exists — skipping" + log "--- Gardener run done ---" + exit 0 ;; + 2) log "ERROR: 'action' label not found — cannot file gardener issue" + exit 1 ;; + *) log "ERROR: failed to create action issue for run-gardener" + exit 1 ;; +esac -RESULT=$(codeberg_api POST "/issues" -d "$PAYLOAD" 2>/dev/null || true) -ISSUE_NUM=$(printf '%s' "$RESULT" | jq -r '.number // empty' 2>/dev/null || true) - -if [ -z "$ISSUE_NUM" ]; then - log "ERROR: failed to create action issue for run-gardener" - exit 1 -fi - -log "Filed action issue #${ISSUE_NUM} for run-gardener formula" -matrix_send "gardener" "Filed action #${ISSUE_NUM}: run-gardener — periodic housekeeping" 2>/dev/null || true +log "Filed action issue #${FILED_ISSUE_NUM} for run-gardener formula" +matrix_send "gardener" "Filed action #${FILED_ISSUE_NUM}: run-gardener — periodic housekeeping" 2>/dev/null || true log "--- Gardener run done ---" diff --git a/lib/file-action-issue.sh b/lib/file-action-issue.sh new file mode 100644 index 0000000..a8f9dfa --- /dev/null +++ b/lib/file-action-issue.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +# file-action-issue.sh — File an action issue for a formula run +# +# Usage: source this file, then call file_action_issue. +# Requires: codeberg_api() from lib/env.sh, jq +# +# file_action_issue <body> +# Sets FILED_ISSUE_NUM on success. +# Returns: 0=created, 1=duplicate exists, 2=label not found, 3=API error + +file_action_issue() { + local formula_name="$1" title="$2" body="$3" + FILED_ISSUE_NUM="" + + # Dedup: skip if an open action issue for this formula already exists + local open_actions + open_actions=$(codeberg_api GET "/issues?state=open&type=issues&labels=action&limit=50" 2>/dev/null || true) + if [ -n "$open_actions" ] && [ "$open_actions" != "null" ]; then + local existing + existing=$(printf '%s' "$open_actions" | \ + jq --arg f "$formula_name" '[.[] | select(.title | test($f))] | length' 2>/dev/null || echo 0) + if [ "${existing:-0}" -gt 0 ]; then + return 1 + fi + fi + + # Fetch 'action' label ID + local action_label_id + action_label_id=$(codeberg_api GET "/labels" 2>/dev/null | \ + jq -r '.[] | select(.name == "action") | .id' 2>/dev/null || true) + if [ -z "$action_label_id" ]; then + return 2 + fi + + # Create the issue + local payload result + payload=$(jq -nc \ + --arg title "$title" \ + --arg body "$body" \ + --argjson labels "[$action_label_id]" \ + '{title: $title, body: $body, labels: $labels}') + + result=$(codeberg_api POST "/issues" -d "$payload" 2>/dev/null || true) + FILED_ISSUE_NUM=$(printf '%s' "$result" | jq -r '.number // empty' 2>/dev/null || true) + + if [ -z "$FILED_ISSUE_NUM" ]; then + return 3 + fi +} diff --git a/planner/planner-poll.sh b/planner/planner-poll.sh index 6bd0f44..510709b 100755 --- a/planner/planner-poll.sh +++ b/planner/planner-poll.sh @@ -13,6 +13,8 @@ FACTORY_ROOT="$(dirname "$SCRIPT_DIR")" # shellcheck source=../lib/env.sh source "$FACTORY_ROOT/lib/env.sh" +# shellcheck source=../lib/file-action-issue.sh +source "$FACTORY_ROOT/lib/file-action-issue.sh" LOG_FILE="$SCRIPT_DIR/planner.log" LOCK_FILE="/tmp/planner-poll.lock" @@ -40,28 +42,7 @@ fi log "--- Planner poll start ---" -# ── Dedup: skip if an open run-planner action issue already exists ──────── -OPEN_ACTIONS=$(codeberg_api GET "/issues?state=open&type=issues&labels=action&limit=50" 2>/dev/null || true) -if [ -n "$OPEN_ACTIONS" ] && [ "$OPEN_ACTIONS" != "null" ]; then - EXISTING=$(printf '%s' "$OPEN_ACTIONS" | \ - jq '[.[] | select(.title | test("run-planner"))] | length' 2>/dev/null || echo 0) - if [ "${EXISTING:-0}" -gt 0 ]; then - log "poll: open run-planner action issue already exists — skipping" - log "--- Planner poll done ---" - exit 0 - fi -fi - -# ── Fetch 'action' label ID ────────────────────────────────────────────── -ACTION_LABEL_ID=$(codeberg_api GET "/labels" 2>/dev/null | \ - jq -r '.[] | select(.name == "action") | .id' 2>/dev/null || true) - -if [ -z "$ACTION_LABEL_ID" ]; then - log "ERROR: 'action' label not found — cannot file planner issue" - exit 1 -fi - -# ── File action issue ───────────────────────────────────────────────────── +# ── File action issue for run-planner formula ───────────────────────────── ISSUE_BODY="--- formula: run-planner model: opus @@ -73,21 +54,20 @@ strategic planning (resource+leverage gap analysis), and memory update. Filed automatically by \`planner-poll.sh\`." -PAYLOAD=$(jq -nc \ - --arg title "action: run-planner — periodic strategic planning" \ - --arg body "$ISSUE_BODY" \ - --argjson labels "[$ACTION_LABEL_ID]" \ - '{title: $title, body: $body, labels: $labels}') +_rc=0 +file_action_issue "run-planner" "action: run-planner — periodic strategic planning" "$ISSUE_BODY" || _rc=$? +case "$_rc" in + 0) ;; + 1) log "poll: open run-planner action issue already exists — skipping" + log "--- Planner poll done ---" + exit 0 ;; + 2) log "ERROR: 'action' label not found — cannot file planner issue" + exit 1 ;; + *) log "ERROR: failed to create action issue for run-planner" + exit 1 ;; +esac -RESULT=$(codeberg_api POST "/issues" -d "$PAYLOAD" 2>/dev/null || true) -ISSUE_NUM=$(printf '%s' "$RESULT" | jq -r '.number // empty' 2>/dev/null || true) - -if [ -z "$ISSUE_NUM" ]; then - log "ERROR: failed to create action issue for run-planner" - exit 1 -fi - -log "Filed action issue #${ISSUE_NUM} for run-planner formula" -matrix_send "planner" "Filed action #${ISSUE_NUM}: run-planner — periodic strategic planning" 2>/dev/null || true +log "Filed action issue #${FILED_ISSUE_NUM} for run-planner formula" +matrix_send "planner" "Filed action #${FILED_ISSUE_NUM}: run-planner — periodic strategic planning" 2>/dev/null || true log "--- Planner poll done ---"