From d648024b3a5993464aa492a1e1e787149d792d94 Mon Sep 17 00:00:00 2001 From: codejeet Date: Wed, 1 Apr 2026 12:31:17 +0200 Subject: [PATCH] Fix #377 redispatch revalidation fallback --- sgt | 47 +++++++++++++++++++++++++++-- test_rig_repo_canonical_resolver.sh | 45 +++++++++++++++++++++------ 2 files changed, 81 insertions(+), 11 deletions(-) diff --git a/sgt b/sgt index 355af74..ee5e9e7 100755 --- a/sgt +++ b/sgt @@ -4331,6 +4331,49 @@ _pr_head_sha() { gh pr view "$pr" --repo "$repo" --json headRefOid --jq '.headRefOid // ""' 2>/dev/null || true } +_resling_live_issue_state() { + local repo="${1:-}" issue_number="${2:-}" + local owner_repo issue_state + [[ -n "$repo" && -n "$issue_number" ]] || return 1 + + issue_state="$(gh issue view "$issue_number" --repo "$repo" --json state --jq '.state' 2>/dev/null || true)" + if [[ -n "$issue_state" ]]; then + printf '%s\n' "$issue_state" + return 0 + fi + + owner_repo="$(_repo_owner_repo "$repo")" + [[ -n "$owner_repo" ]] || return 1 + issue_state="$(gh api "repos/$owner_repo/issues/$issue_number" --jq '(.state // "") | ascii_upcase' 2>/dev/null || true)" + [[ -n "$issue_state" ]] || return 1 + printf '%s\n' "$issue_state" +} + +_resling_live_pr_state_mergeable() { + local repo="${1:-}" source_pr="${2:-}" + local owner_repo pr_snapshot + [[ -n "$repo" && -n "$source_pr" ]] || return 1 + + pr_snapshot="$(gh pr view "$source_pr" --repo "$repo" --json state,mergeable --jq '.state + "|" + (.mergeable // "")' 2>/dev/null || true)" + if [[ -n "$pr_snapshot" && "$pr_snapshot" == *"|"* ]]; then + printf '%s\n' "$pr_snapshot" + return 0 + fi + + owner_repo="$(_repo_owner_repo "$repo")" + [[ -n "$owner_repo" ]] || return 1 + pr_snapshot="$( + gh api "repos/$owner_repo/pulls/$source_pr" \ + --jq '[ + (if (.state // "") == "closed" and (.merged // false) then "MERGED" else ((.state // "") | ascii_upcase) end), + (if .mergeable == true then "MERGEABLE" elif .mergeable == false then "CONFLICTING" else "" end) + ] | join("|")' \ + 2>/dev/null || true + )" + [[ -n "$pr_snapshot" && "$pr_snapshot" == *"|"* ]] || return 1 + printf '%s\n' "$pr_snapshot" +} + _merge_queue_set_field() { local queue_file="${1:-}" field="${2:-}" value="${3:-}" local safe_value tmp_file line replaced @@ -5399,7 +5442,7 @@ _resling_pre_dispatch_revalidate() { return 1 fi - issue_state=$(gh issue view "$issue_number" --repo "$canonical_repo" --json state --jq '.state' 2>/dev/null || true) + issue_state="$(_resling_live_issue_state "$canonical_repo" "$issue_number" || true)" if [[ -z "$issue_state" ]]; then echo "unable to query live issue state for #$issue_number" return 1 @@ -5410,7 +5453,7 @@ _resling_pre_dispatch_revalidate() { fi if [[ -n "$source_pr" && "$source_pr" != "0" ]]; then - pr_snapshot=$(gh pr view "$source_pr" --repo "$canonical_repo" --json state,mergeable --jq '.state + "|" + (.mergeable // "")' 2>/dev/null || true) + pr_snapshot="$(_resling_live_pr_state_mergeable "$canonical_repo" "$source_pr" || true)" if [[ -z "$pr_snapshot" || "$pr_snapshot" != *"|"* ]]; then return 0 fi diff --git a/test_rig_repo_canonical_resolver.sh b/test_rig_repo_canonical_resolver.sh index 6bc583e..bb0479f 100755 --- a/test_rig_repo_canonical_resolver.sh +++ b/test_rig_repo_canonical_resolver.sh @@ -19,11 +19,14 @@ extract_fn() { eval "$(extract_fn _one_line)" eval "$(extract_fn _escape_quotes)" +eval "$(extract_fn _repo_owner_repo)" eval "$(extract_fn _repo_owner_repo_strict)" eval "$(extract_fn _repo_owner_repo_url)" eval "$(extract_fn _rig_repo_resolve_error)" eval "$(extract_fn _rig_repo_resolve_error_unpack)" eval "$(extract_fn _resolve_rig_repo_canonical)" +eval "$(extract_fn _resling_live_issue_state)" +eval "$(extract_fn _resling_live_pr_state_mergeable)" eval "$(extract_fn _resling_pre_dispatch_revalidate)" export SGT_ROOT="$TMP_ROOT/root" @@ -43,30 +46,51 @@ rig_repo() { } GH_CALLS_FILE="$TMP_ROOT/gh-calls" +GH_MODE_FILE="$TMP_ROOT/gh-mode" printf '0\n' > "$GH_CALLS_FILE" +printf 'direct\n' > "$GH_MODE_FILE" gh() { local calls + local mode + mode="$(cat "$GH_MODE_FILE")" + calls="$(cat "$GH_CALLS_FILE")" + calls=$((calls + 1)) + printf '%s\n' "$calls" > "$GH_CALLS_FILE" + if [[ "${1:-}" == "issue" && "${2:-}" == "view" ]]; then - calls="$(cat "$GH_CALLS_FILE")" - calls=$((calls + 1)) - printf '%s\n' "$calls" > "$GH_CALLS_FILE" + if [[ "$mode" == "issue-rest-fallback" || "$mode" == "mixed-rest-fallback" ]]; then + return 1 + fi echo "OPEN" return 0 fi if [[ "${1:-}" == "pr" && "${2:-}" == "view" ]]; then - calls="$(cat "$GH_CALLS_FILE")" - calls=$((calls + 1)) - printf '%s\n' "$calls" > "$GH_CALLS_FILE" + if [[ "$mode" == "pr-rest-fallback" || "$mode" == "mixed-rest-fallback" ]]; then + return 1 + fi echo "OPEN|MERGEABLE" return 0 fi + if [[ "${1:-}" == "api" ]]; then + case "${2:-}" in + repos/acme/openclaw-agent-dm/issues/12) + echo "OPEN" + return 0 + ;; + repos/acme/openclaw-agent-dm/pulls/34) + echo "OPEN|MERGEABLE" + return 0 + ;; + esac + fi echo "mock gh unsupported: $*" >&2 return 1 } assert_ok() { - local name="$1" rig="$2" repo="$3" issue="$4" source_pr="$5" expect_calls="$6" + local name="$1" mode="$2" rig="$3" repo="$4" issue="$5" source_pr="$6" expect_calls="$7" local out before after + printf '%s\n' "$mode" > "$GH_MODE_FILE" before="$(cat "$GH_CALLS_FILE")" out="$(_resling_pre_dispatch_revalidate "$rig" "$repo" "$issue" "$source_pr" 2>&1 || true)" after="$(cat "$GH_CALLS_FILE")" @@ -106,8 +130,11 @@ assert_fail() { } echo "=== rig repo canonical resolver regression ===" -assert_ok "canonical repo" "oadm" "https://github.com/acme/openclaw-agent-dm" "12" "34" "2" -assert_ok "alias owner/repo" "oadm" "acme/openclaw-agent-dm" "12" "34" "2" +assert_ok "canonical repo" "direct" "oadm" "https://github.com/acme/openclaw-agent-dm" "12" "34" "2" +assert_ok "alias owner/repo" "direct" "oadm" "acme/openclaw-agent-dm" "12" "34" "2" +assert_ok "issue state REST fallback" "issue-rest-fallback" "oadm" "https://github.com/acme/openclaw-agent-dm" "12" "34" "3" +assert_ok "source PR REST fallback" "pr-rest-fallback" "oadm" "https://github.com/acme/openclaw-agent-dm" "12" "34" "3" +assert_ok "issue and PR REST fallback" "mixed-rest-fallback" "oadm" "https://github.com/acme/openclaw-agent-dm" "12" "34" "4" assert_fail "missing repo rejected" "oadm" "" "12" "34" "missing repo" assert_fail "mismatched repo rejected" "oadm" "acme/other-repo" "12" "34" "repo mismatch"