Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions internal/scaffold/fullsend-repo/scripts/lib/github-api-csma.sh
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,25 @@ github_csma_run() {

: >"${outfile}"
: >"${errfile}"
if gh "$@" >"${outfile}" 2>"${errfile}"; then
cat "${outfile}"
return 0
fi
local rc=0
gh "$@" >"${outfile}" 2>"${errfile}" || rc=$?

combined=$(cat "${outfile}" "${errfile}")
if github_csma_is_rate_limit "${combined}"; then
if (( attempt < max_attempts - 1 )); then
_github_csma_sleep_after_rate_limit "${attempt}" "${resource}"
continue
fi
_github_csma_emit_failure "${combined}"
return 1
fi

_github_csma_emit_failure "${combined}"
return 1
if (( rc != 0 )); then
_github_csma_emit_failure "${combined}"
return 1
fi
cat "${outfile}"
return 0
done

return 1
Expand All @@ -223,21 +227,25 @@ github_csma_run_pipe() {

: >"${outfile}"
: >"${errfile}"
if gh "$@" <"${infile}" >"${outfile}" 2>"${errfile}"; then
cat "${outfile}"
return 0
fi
local rc=0
gh "$@" <"${infile}" >"${outfile}" 2>"${errfile}" || rc=$?

combined=$(cat "${outfile}" "${errfile}")
if github_csma_is_rate_limit "${combined}"; then
if (( attempt < max_attempts - 1 )); then
_github_csma_sleep_after_rate_limit "${attempt}" "${resource}"
continue
fi
_github_csma_emit_failure "${combined}"
return 1
fi

_github_csma_emit_failure "${combined}"
return 1
if (( rc != 0 )); then
_github_csma_emit_failure "${combined}"
return 1
fi
cat "${outfile}"
return 0
done

return 1
Expand All @@ -264,21 +272,25 @@ github_csma_run_cmd() {

: >"${outfile}"
: >"${errfile}"
if "$@" <"${infile}" >"${outfile}" 2>"${errfile}"; then
cat "${outfile}"
return 0
fi
local rc=0
"$@" <"${infile}" >"${outfile}" 2>"${errfile}" || rc=$?

combined=$(cat "${outfile}" "${errfile}")
if github_csma_is_rate_limit "${combined}"; then
if (( attempt < max_attempts - 1 )); then
_github_csma_sleep_after_rate_limit "${attempt}" "${resource}"
continue
fi
_github_csma_emit_failure "${combined}"
return 1
fi

_github_csma_emit_failure "${combined}"
return 1
if (( rc != 0 )); then
_github_csma_emit_failure "${combined}"
return 1
fi
cat "${outfile}"
return 0
done

return 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ if [[ "\${GH_CSMA_FAIL_MODE:-}" == "auth" ]] && [[ "\$1" != "api" || "\$2" != "r
exit 1
fi

# Simulate gh exiting 0 but printing a rate limit error to stdout.
# This is how "gh project view" behaves on GraphQL rate limits.
if [[ "\${GH_CSMA_FAIL_MODE:-}" == "exit0-ratelimit" ]] && [[ "\$1" != "api" || "\$2" != "rate_limit" ]]; then
if (( fail_count <= GH_CSMA_FAIL_UNTIL )); then
echo "GraphQL: API rate limit exceeded for installation ID 131739396." >&2
exit 0
fi
fi

if [[ -n "\${GH_CSMA_FAIL_UNTIL:-}" ]] && (( fail_count <= GH_CSMA_FAIL_UNTIL )); then
echo "You have exceeded a secondary rate limit. Please retry again later." >&2
exit 1
Expand Down Expand Up @@ -146,17 +155,21 @@ run_test() {
local fail_until="${2:-}"
local min_gh_calls="${3:-1}"
local expect_failure="${4:-false}"
local fail_mode="${5:-}"

local run_dir="${TEST_TMPDIR}/run-${test_name}"
mkdir -p "${run_dir}/iteration-1/output"
echo "${FIXTURE_JSON}" > "${run_dir}/iteration-1/output/agent-result.json"

> "${GH_LOG}"
rm -f "${GH_FAIL_COUNT}"
unset GH_CSMA_FAIL_UNTIL
unset GH_CSMA_FAIL_UNTIL GH_CSMA_FAIL_MODE
if [[ -n "${fail_until}" ]]; then
export GH_CSMA_FAIL_UNTIL="${fail_until}"
fi
if [[ -n "${fail_mode}" ]]; then
export GH_CSMA_FAIL_MODE="${fail_mode}"
fi

local exit_code=0
(cd "${run_dir}" && bash "${POST_SCRIPT}") > "${TEST_TMPDIR}/stdout-${test_name}.log" 2>&1 || exit_code=$?
Expand Down Expand Up @@ -294,6 +307,15 @@ export GITHUB_CSMA_MAX_ATTEMPTS=3
run_test_failure_stderr "exhausted-retries" "100" "secondary rate limit"
unset GITHUB_CSMA_MAX_ATTEMPTS

# gh exits 0 but output contains rate limit error (gh project view behavior).
# First 2 calls fail with exit-0 rate limit, then succeed.
run_test "exit0-rate-limit-retry" "2" 10 "false" "exit0-ratelimit"

# Exhausted retries when gh keeps exiting 0 with rate limit errors.
export GITHUB_CSMA_MAX_ATTEMPTS=3
run_test_failure_stderr "exit0-rate-limit-exhausted" "100" "rate limit exceeded" "exit0-ratelimit"
unset GITHUB_CSMA_MAX_ATTEMPTS

if [[ ${FAILURES} -gt 0 ]]; then
echo ""
echo "${FAILURES} test(s) failed."
Expand Down
Loading