fix: extract file_action_issue helper to eliminate duplicate code blocks

CI duplicate-detection flagged shared action-issue filing pattern between
gardener-run.sh and planner-poll.sh. Extract into lib/file-action-issue.sh
and refactor both scripts to use it.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
openhands 2026-03-20 12:49:08 +00:00
parent eb90a42095
commit 59b6d76afa
4 changed files with 87 additions and 78 deletions

View file

@ -91,7 +91,7 @@ echo "=== 2/2 Function resolution ==="
# Functions provided by shared lib files (available to all agent scripts via source) # Functions provided by shared lib files (available to all agent scripts via source)
LIB_FUNS=$( 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 if [ -f "$f" ]; then get_fns "$f"; fi
done | sort -u done | sort -u
) )
@ -159,6 +159,7 @@ check_script dev/dev-poll.sh
check_script dev/phase-test.sh check_script dev/phase-test.sh
check_script gardener/gardener-agent.sh lib/agent-session.sh check_script gardener/gardener-agent.sh lib/agent-session.sh
check_script gardener/gardener-poll.sh check_script gardener/gardener-poll.sh
check_script gardener/gardener-run.sh
check_script review/review-pr.sh check_script review/review-pr.sh
check_script review/review-poll.sh check_script review/review-poll.sh
check_script planner/planner-poll.sh check_script planner/planner-poll.sh

View file

@ -8,16 +8,17 @@
# ============================================================================= # =============================================================================
set -euo pipefail set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" FACTORY_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
FACTORY_ROOT="$(dirname "$SCRIPT_DIR")"
# Load shared environment (with optional project TOML override) # Load shared environment (with optional project TOML override)
# Usage: gardener-run.sh [projects/harb.toml] # Usage: gardener-run.sh [projects/harb.toml]
export PROJECT_TOML="${1:-}" export PROJECT_TOML="${1:-}"
# shellcheck source=../lib/env.sh # shellcheck source=../lib/env.sh
source "$FACTORY_ROOT/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" LOCK_FILE="/tmp/gardener-run.lock"
log() { echo "[$(date -u +%Y-%m-%dT%H:%M:%S)Z] $*" >> "$LOG_FILE"; } log() { echo "[$(date -u +%Y-%m-%dT%H:%M:%S)Z] $*" >> "$LOG_FILE"; }
@ -43,28 +44,7 @@ fi
log "--- Gardener run start ---" log "--- Gardener run start ---"
# ── Dedup: skip if an open run-gardener action issue already exists ─────── # ── File action issue for run-gardener formula ────────────────────────────
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 ─────────────────────────────────────────────────────
ISSUE_BODY="--- ISSUE_BODY="---
formula: run-gardener formula: run-gardener
model: opus model: opus
@ -76,21 +56,20 @@ AGENTS.md update, and commit-and-pr.
Filed automatically by \`gardener-run.sh\`." Filed automatically by \`gardener-run.sh\`."
PAYLOAD=$(jq -nc \ _rc=0
--arg title "action: run-gardener — periodic housekeeping" \ file_action_issue "run-gardener" "action: run-gardener — periodic housekeeping" "$ISSUE_BODY" || _rc=$?
--arg body "$ISSUE_BODY" \ case "$_rc" in
--argjson labels "[$ACTION_LABEL_ID]" \ 0) ;;
'{title: $title, body: $body, labels: $labels}') 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) log "Filed action issue #${FILED_ISSUE_NUM} for run-gardener formula"
ISSUE_NUM=$(printf '%s' "$RESULT" | jq -r '.number // empty' 2>/dev/null || true) matrix_send "gardener" "Filed action #${FILED_ISSUE_NUM}: run-gardener — periodic housekeeping" 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 "--- Gardener run done ---" log "--- Gardener run done ---"

49
lib/file-action-issue.sh Normal file
View file

@ -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 <formula_name> <title> <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
}

View file

@ -13,6 +13,8 @@ FACTORY_ROOT="$(dirname "$SCRIPT_DIR")"
# shellcheck source=../lib/env.sh # shellcheck source=../lib/env.sh
source "$FACTORY_ROOT/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" LOG_FILE="$SCRIPT_DIR/planner.log"
LOCK_FILE="/tmp/planner-poll.lock" LOCK_FILE="/tmp/planner-poll.lock"
@ -40,28 +42,7 @@ fi
log "--- Planner poll start ---" log "--- Planner poll start ---"
# ── Dedup: skip if an open run-planner action issue already exists ──────── # ── File action issue for run-planner formula ─────────────────────────────
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 ─────────────────────────────────────────────────────
ISSUE_BODY="--- ISSUE_BODY="---
formula: run-planner formula: run-planner
model: opus model: opus
@ -73,21 +54,20 @@ strategic planning (resource+leverage gap analysis), and memory update.
Filed automatically by \`planner-poll.sh\`." Filed automatically by \`planner-poll.sh\`."
PAYLOAD=$(jq -nc \ _rc=0
--arg title "action: run-planner — periodic strategic planning" \ file_action_issue "run-planner" "action: run-planner — periodic strategic planning" "$ISSUE_BODY" || _rc=$?
--arg body "$ISSUE_BODY" \ case "$_rc" in
--argjson labels "[$ACTION_LABEL_ID]" \ 0) ;;
'{title: $title, body: $body, labels: $labels}') 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) log "Filed action issue #${FILED_ISSUE_NUM} for run-planner formula"
ISSUE_NUM=$(printf '%s' "$RESULT" | jq -r '.number // empty' 2>/dev/null || true) matrix_send "planner" "Filed action #${FILED_ISSUE_NUM}: run-planner — periodic strategic planning" 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 "--- Planner poll done ---" log "--- Planner poll done ---"