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
17 changes: 17 additions & 0 deletions .github/actions/pr-require-checks/aggregate-results/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,22 @@ inputs:
the runner, or no single matching step.
The writer step of the full run is matched by the same name.
default: ''
rate-limit-wait-seconds:
description: >-
Ceiling, in seconds, on the time this step spends waiting out GitHub API
rate limits, summed over its calls. A 403 or 429 is a rate limit when it
carries `retry-after`, `x-ratelimit-remaining: 0`, or a rate-limit
message; the step then waits `retry-after`, else until
`x-ratelimit-reset`, else 60 seconds, and retries once. A wait that does
not fit in what is left fails the call at once and prints the reset
time, and a second rate limit fails it too: the call fails closed, as any
other failed read does, and never ends neutral. Every 403 or 429 prints
its status, message and `x-ratelimit-*`, `retry-after` and
`x-accepted-github-permissions` headers. Keep this, plus
`rerun-wait-seconds` and any other step's wait, under the calling job's
`timeout-minutes`, and under 3600, the life of a GitHub App installation
token. `0` never waits.
default: '60'
token:
description: >-
Token used to write and read the commit status. The calling job needs
Expand Down Expand Up @@ -235,6 +251,7 @@ runs:
CARRY_FORWARD_WAIT_SECONDS: ${{ inputs.carry-forward-wait-seconds }}
RERUN_CONTRACT_ONLY_SIBLINGS: ${{ inputs.rerun-contract-only-siblings }}
RERUN_WAIT_SECONDS: ${{ inputs.rerun-wait-seconds }}
RATE_LIMIT_WAIT_SECONDS: ${{ inputs.rate-limit-wait-seconds }}
RECORD_PENDING: ${{ inputs.record-pending }}
YIELD_TO_FULL_RUN: ${{ inputs.yield-to-full-run }}
STEP_NAME: ${{ inputs.step-name }}
Expand Down
58 changes: 31 additions & 27 deletions .github/actions/pr-require-checks/aggregate-results/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ CARRY_FORWARD_POLL_SECONDS=15
# Ceiling, in seconds, on how long a full run that records `success` waits for
# in-flight contract-only siblings to finish before re-running the failed ones;
# see `rerun_failed_contract_only_siblings`. `0` disables the wait. The default
# fits inside the 3-minute `timeout-minutes` the README recommends for the
# fits inside the 5-minute `timeout-minutes` the README recommends for the
# ci-status job. Validated below. The re-list interval is fixed, like the poll
# above.
RERUN_WAIT_SECONDS="${RERUN_WAIT_SECONDS:-90}"
Expand Down Expand Up @@ -152,23 +152,9 @@ trap 'rm -rf -- "$scratch"' EXIT
gh_stdout="$scratch/gh-stdout"
gh_stderr="$scratch/gh-stderr"

GH_HTTP_STATUS=""
gh_api() {
local method="$1" path="$2"
shift 2
local status=0
: >"$gh_stdout"
: >"$gh_stderr"
GH_HTTP_STATUS=""
set +e
gh api -X "$method" "$path" "$@" >"$gh_stdout" 2>"$gh_stderr"
status=$?
set -e
if [[ "$status" -ne 0 ]]; then
GH_HTTP_STATUS="$(sed -n 's/.*(HTTP \([0-9][0-9]*\)).*/\1/p' "$gh_stderr" | head -n1)"
fi
return "$status"
}
gh_headers="$scratch/gh-headers"
# shellcheck source=../gh-api.sh
source "${BASH_SOURCE[0]%/*}/../gh-api.sh"

# A GitHub expression renders as the literal `true`/`false`. Empty means the
# caller left the input unset, which takes the safer reading of each flag:
Expand Down Expand Up @@ -241,6 +227,8 @@ require_pattern sha "$SHA" '^[0-9a-f]{40}$' 'a full 40-character lowercase commi
# a no-op, either of which silently changes the branch the caller asked for.
require_pattern carry-forward-wait-seconds "$CARRY_FORWARD_WAIT_SECONDS" '^[0-9]+$' 'a non-negative integer number of seconds'
require_pattern rerun-wait-seconds "$RERUN_WAIT_SECONDS" '^[0-9]+$' 'a non-negative integer number of seconds'
# At most an hour, the life of a GitHub App installation token.
require_pattern rate-limit-wait-seconds "$RATE_LIMIT_WAIT_SECONDS" '^(0|[1-9][0-9]{0,2}|[1-2][0-9]{3}|3[0-5][0-9]{2}|3600)$' 'an integer number of seconds from 0 to 3600'

# POST one `status-context` entry on `sha` naming this run, retrying after 1s,
# 2s and 4s. Every write is load-bearing, not best-effort: the carry-forward
Expand All @@ -261,14 +249,22 @@ write_status() {
if gh_api POST "repos/${REPOSITORY}/statuses/${SHA}" --input "$scratch/status-payload.json"; then
return 0
fi
# gh_api already took the one bounded wait a rate limit allows.
if [[ "$GH_FAILURE" == rate-limit ]]; then
break
fi
if [[ "$attempt" -lt 4 ]]; then
delay=$((STATUS_RETRY_BASE_DELAY * (1 << (attempt - 1))))
echo "::warning::could not record ${STATUS_CONTEXT} on ${SHA} (HTTP ${GH_HTTP_STATUS:-unknown}); retrying in ${delay}s"
sleep "$delay"
fi
done
cat "$gh_stderr" >&2
echo "::error::could not record ${STATUS_CONTEXT} on ${SHA} (${GH_HTTP_STATUS:-unknown}); the calling job needs statuses: write"
if [[ "$GH_FAILURE" == rate-limit ]]; then
echo "::error::could not record ${STATUS_CONTEXT} on ${SHA} (HTTP ${GH_HTTP_STATUS}): rate limited past rate-limit-wait-seconds; re-run this job after the limit resets"
else
echo "::error::could not record ${STATUS_CONTEXT} on ${SHA} (${GH_HTTP_STATUS:-unknown}); the calling job needs statuses: write"
fi
return 1
}

Expand Down Expand Up @@ -498,8 +494,7 @@ own_job_identity() {

# Reads between the retries. The jobs API can list this job or this step a
# moment after the runner started it, and lists a sibling job before its
# `skipped`, so a miss is read again. The sleeps sum to 15 seconds, well inside
# the 3-minute ci-status job.
# `skipped`, so a miss is read again. The sleeps sum to 15 seconds.
OWN_GATE_RETRY_DELAYS='1 2 4 8'

# resolve_own_gate <job|step> <consequence>
Expand All @@ -526,7 +521,7 @@ resolve_own_gate() {
reads=$((reads + 1))
# shellcheck disable=SC2310 # gh_api handles its own errexit; the retry loop classifies the status.
if ! gh_api GET "${endpoint}${own_job_query}"; then
if [[ "$GH_HTTP_STATUS" == 403 || "$delay" == "${OWN_GATE_RETRY_DELAYS##* }" ]]; then
if [[ "$GH_HTTP_STATUS" == 403 || "$GH_FAILURE" == rate-limit || "$delay" == "${OWN_GATE_RETRY_DELAYS##* }" ]]; then
warn_actions_failed "$endpoint" 'actions: read' "$consequence"
return 1
fi
Expand Down Expand Up @@ -765,12 +760,17 @@ verify_no_newer_verdict() {

# The Actions calls need a scope an explicit `permissions:` block does not grant
# by default: `actions: read` to wait, `actions: write` to re-run siblings. Name
# the scope rather than printing a bare 403, and say what the run does instead.
# the scope only for a permission failure, not for a rate limit or an
# unclassified 403 (see gh-api.sh), and say what the run does instead.
# Neither caller treats a refusal as permission to pass.
warn_actions_failed() {
local endpoint="$1" scope="$2" consequence="$3"
if [[ "$GH_HTTP_STATUS" == 403 ]]; then
echo "::warning::${endpoint} returned HTTP 403; the ci-status job needs '${scope}'. ${consequence}"
if [[ "$GH_FAILURE" == permission ]]; then
echo "::warning::${endpoint} returned HTTP ${GH_HTTP_STATUS}; the ci-status job needs '${scope}'. ${consequence}"
elif [[ "$GH_FAILURE" == rate-limit ]]; then
echo "::warning::${endpoint} is rate limited (HTTP ${GH_HTTP_STATUS}). ${consequence}"
elif [[ -n "$GH_FAILURE" ]]; then
echo "::warning::${endpoint} returned HTTP ${GH_HTTP_STATUS}: $(escape_annotation "$GH_MESSAGE"). ${consequence}"
else
echo "::warning::could not read ${endpoint} (HTTP ${GH_HTTP_STATUS:-unknown}). ${consequence}"
fi
Expand Down Expand Up @@ -1355,8 +1355,12 @@ rerun_failed_contract_only_siblings() {
fi
# shellcheck disable=SC2310 # gh_api handles its own errexit; the caller classifies the status.
if ! gh_api POST "repos/${REPOSITORY}/actions/runs/${id}/rerun-failed-jobs"; then
if [[ "$GH_HTTP_STATUS" == 403 ]]; then
echo "::warning::re-running run ${id} returned HTTP 403; rerun-contract-only-siblings needs 'actions: write' on the ci-status job."
if [[ "$GH_FAILURE" == permission ]]; then
echo "::warning::re-running run ${id} returned HTTP ${GH_HTTP_STATUS}; rerun-contract-only-siblings needs 'actions: write' on the ci-status job."
elif [[ "$GH_FAILURE" == rate-limit ]]; then
echo "::warning::re-running run ${id} is rate limited (HTTP ${GH_HTTP_STATUS}); not re-running it."
elif [[ -n "$GH_FAILURE" ]]; then
echo "::warning::re-running run ${id} returned HTTP ${GH_HTTP_STATUS}: $(escape_annotation "$GH_MESSAGE")."
else
echo "::warning::could not re-run run ${id} (HTTP ${GH_HTTP_STATUS:-unknown})."
fi
Expand Down
Loading
Loading