diff --git a/lib/pr-lifecycle.sh b/lib/pr-lifecycle.sh index 94cbac9..ad6f0de 100644 --- a/lib/pr-lifecycle.sh +++ b/lib/pr-lifecycle.sh @@ -110,17 +110,15 @@ pr_create() { # --------------------------------------------------------------------------- # pr_find_by_branch — Find an open PR by head branch name. -# Args: branch [api_url] +# Args: branch # Stdout: PR number # Returns: 0=found, 1=not found -# api_url defaults to FORGE_API if not provided # --------------------------------------------------------------------------- pr_find_by_branch() { local branch="$1" - local api_url="${2:-${FORGE_API}}" local pr_num pr_num=$(curl -sf -H "Authorization: token ${FORGE_TOKEN}" \ - "${api_url}/pulls?state=open&limit=20" | \ + "${FORGE_API}/pulls?state=open&limit=20" | \ jq -r --arg b "$branch" '.[] | select(.head.ref == $b) | .number' \ | head -1) || true if [ -n "$pr_num" ]; then diff --git a/lib/vault.sh b/lib/vault.sh index 584db17..ed42ffc 100644 --- a/lib/vault.sh +++ b/lib/vault.sh @@ -10,6 +10,7 @@ # # Functions: # vault_request — Create vault PR, return PR number +# vault_find_by_action — Find existing PR for action ID # # The function: # 1. Validates TOML content using validate_vault_action() from vault/vault-env.sh @@ -34,13 +35,6 @@ _vault_log() { fi } -# Get ops repo API URL (encodes hyphens for Forgejo API) -_vault_ops_api() { - local ops_repo_encoded - ops_repo_encoded=$(printf '%s' "$FORGE_OPS_REPO" | sed 's/-/%2D/g') - printf '%s' "${FORGE_URL}/api/v1/repos/${ops_repo_encoded}" -} - # ----------------------------------------------------------------------------- # vault_request — Create a vault PR or return existing one # Args: action_id toml_content @@ -63,7 +57,7 @@ vault_request() { # Check if PR already exists for this action local existing_pr - existing_pr=$(pr_find_by_branch "vault/${action_id}" "$(_vault_ops_api)") || true + existing_pr=$(vault_find_by_action "$action_id") || true if [ -n "$existing_pr" ]; then _vault_log "PR already exists for action $action_id: #${existing_pr}" printf '%s' "$existing_pr" @@ -112,8 +106,10 @@ This vault action has been created by an agent and requires admin approval before execution. See the TOML file for details." # Get ops repo API URL - local ops_api - ops_api="$(_vault_ops_api)" + # Forgejo encodes hyphens as %2D in URLs + local ops_repo_encoded + ops_repo_encoded=$(printf '%s' "$FORGE_OPS_REPO" | sed 's/-/%2D/g') + local ops_api="${FORGE_URL}/api/v1/repos/${ops_repo_encoded}" # Create branch local branch="vault/${action_id}" @@ -218,3 +214,39 @@ before execution. See the TOML file for details." printf '%s' "$pr_num" return 0 } + +# ----------------------------------------------------------------------------- +# vault_find_by_action — Find existing PR for an action ID +# Args: action_id +# Stdout: PR number (empty if not found) +# Returns: 0=found, 1=not found +# ----------------------------------------------------------------------------- +vault_find_by_action() { + local action_id="$1" + + if [ -z "$action_id" ]; then + echo "ERROR: action_id is required" >&2 + return 1 + fi + + local branch="vault/${action_id}" + + # Get ops repo API URL + local ops_repo_encoded + ops_repo_encoded=$(printf '%s' "$FORGE_OPS_REPO" | sed 's/-/%2D/g') + local ops_api="${FORGE_URL}/api/v1/repos/${ops_repo_encoded}" + + # Find open PRs by branch + local pr_num + pr_num=$(curl -sf -H "Authorization: token ${FORGE_TOKEN}" \ + "${ops_api}/pulls?state=open&limit=100" 2>/dev/null | \ + jq -r --arg b "$branch" '.[] | select(.head.ref == $b) | .number' \ + | head -1) || true + + if [ -n "$pr_num" ]; then + printf '%s' "$pr_num" + return 0 + fi + + return 1 +}