From bda924026864cc18681cd688c2594dcd2e6c134a Mon Sep 17 00:00:00 2001 From: openhands Date: Sat, 21 Mar 2026 05:06:12 +0000 Subject: [PATCH] refactor: extract ensure_blocked_label_id to lib/ci-helpers.sh (#352) Move ensure_blocked_label_id() from dev/phase-handler.sh into lib/ci-helpers.sh to eliminate the duplicate blocked-label creation curl block that existed in both phase-handler.sh and dev-poll.sh. Both dev-agent.sh and action-agent.sh now source lib/ci-helpers.sh so the function is available when phase-handler.sh calls it. Co-Authored-By: Claude Opus 4.6 (1M context) --- action/action-agent.sh | 1 + dev/dev-agent.sh | 1 + dev/dev-poll.sh | 11 +---------- dev/phase-handler.sh | 19 ------------------- lib/ci-helpers.sh | 21 +++++++++++++++++++++ 5 files changed, 24 insertions(+), 29 deletions(-) diff --git a/action/action-agent.sh b/action/action-agent.sh index 1c6e3a1..1bc7536 100644 --- a/action/action-agent.sh +++ b/action/action-agent.sh @@ -23,6 +23,7 @@ ISSUE="${1:?Usage: action-agent.sh [project.toml]}" export PROJECT_TOML="${2:-${PROJECT_TOML:-}}" source "$(dirname "$0")/../lib/env.sh" +source "$(dirname "$0")/../lib/ci-helpers.sh" source "$(dirname "$0")/../lib/agent-session.sh" source "$(dirname "$0")/../lib/formula-session.sh" # shellcheck source=../dev/phase-handler.sh diff --git a/dev/dev-agent.sh b/dev/dev-agent.sh index ec8791e..b436482 100755 --- a/dev/dev-agent.sh +++ b/dev/dev-agent.sh @@ -22,6 +22,7 @@ set -euo pipefail # Load shared environment source "$(dirname "$0")/../lib/env.sh" +source "$(dirname "$0")/../lib/ci-helpers.sh" source "$(dirname "$0")/../lib/agent-session.sh" source "$(dirname "$0")/../lib/formula-session.sh" # shellcheck source=./phase-handler.sh diff --git a/dev/dev-poll.sh b/dev/dev-poll.sh index e09c49e..0112ff6 100755 --- a/dev/dev-poll.sh +++ b/dev/dev-poll.sh @@ -89,16 +89,7 @@ is_blocked() { _post_ci_blocked_comment() { local issue_num="$1" pr_num="$2" attempts="$3" local blocked_id - blocked_id=$(codeberg_api GET "/labels" 2>/dev/null \ - | jq -r '.[] | select(.name == "blocked") | .id' 2>/dev/null || true) - if [ -z "$blocked_id" ]; then - blocked_id=$(curl -sf -X POST \ - -H "Authorization: token ${CODEBERG_TOKEN}" \ - -H "Content-Type: application/json" \ - "${CODEBERG_API}/labels" \ - -d '{"name":"blocked","color":"#e11d48"}' 2>/dev/null \ - | jq -r '.id // empty' 2>/dev/null || true) - fi + blocked_id=$(ensure_blocked_label_id) [ -z "$blocked_id" ] && return 0 local comment diff --git a/dev/phase-handler.sh b/dev/phase-handler.sh index 6641184..07c4907 100644 --- a/dev/phase-handler.sh +++ b/dev/phase-handler.sh @@ -34,25 +34,6 @@ : "${CLAIMED:=false}" : "${PHASE_POLL_INTERVAL:=30}" -# --- Look up (or create) the "blocked" label ID --- -ensure_blocked_label_id() { - if [ -n "${_BLOCKED_LABEL_ID:-}" ]; then - printf '%s' "$_BLOCKED_LABEL_ID" - return 0 - fi - _BLOCKED_LABEL_ID=$(codeberg_api GET "/labels" 2>/dev/null \ - | jq -r '.[] | select(.name == "blocked") | .id' 2>/dev/null || true) - if [ -z "$_BLOCKED_LABEL_ID" ]; then - _BLOCKED_LABEL_ID=$(curl -sf -X POST \ - -H "Authorization: token ${CODEBERG_TOKEN}" \ - -H "Content-Type: application/json" \ - "${CODEBERG_API}/labels" \ - -d '{"name":"blocked","color":"#e11d48"}' 2>/dev/null \ - | jq -r '.id // empty' 2>/dev/null || true) - fi - printf '%s' "$_BLOCKED_LABEL_ID" -} - # --- Post diagnostic comment + label issue as blocked --- # Replaces the old escalation JSONL write path. # Captures tmux pane output, posts a structured comment on the issue, removes diff --git a/lib/ci-helpers.sh b/lib/ci-helpers.sh index 40f79e1..024cb49 100644 --- a/lib/ci-helpers.sh +++ b/lib/ci-helpers.sh @@ -5,6 +5,27 @@ # ci_passed() requires: WOODPECKER_REPO_ID (from env.sh / project config) # classify_pipeline_failure() requires: woodpecker_api() (defined in env.sh) +# ensure_blocked_label_id — look up (or create) the "blocked" label, print its ID. +# Caches the result in _BLOCKED_LABEL_ID to avoid repeated API calls. +# Requires: CODEBERG_TOKEN, CODEBERG_API (from env.sh), codeberg_api() +ensure_blocked_label_id() { + if [ -n "${_BLOCKED_LABEL_ID:-}" ]; then + printf '%s' "$_BLOCKED_LABEL_ID" + return 0 + fi + _BLOCKED_LABEL_ID=$(codeberg_api GET "/labels" 2>/dev/null \ + | jq -r '.[] | select(.name == "blocked") | .id' 2>/dev/null || true) + if [ -z "$_BLOCKED_LABEL_ID" ]; then + _BLOCKED_LABEL_ID=$(curl -sf -X POST \ + -H "Authorization: token ${CODEBERG_TOKEN}" \ + -H "Content-Type: application/json" \ + "${CODEBERG_API}/labels" \ + -d '{"name":"blocked","color":"#e11d48"}' 2>/dev/null \ + | jq -r '.id // empty' 2>/dev/null || true) + fi + printf '%s' "$_BLOCKED_LABEL_ID" +} + # diff_has_code_files — check if file list (stdin, one per line) contains code files # Non-code paths: docs/*, formulas/*, evidence/*, *.md # Returns 0 if any code file found, 1 if all files are non-code.