diff --git a/downstream/README.md b/downstream/README.md index a779765..2f0565e 100644 --- a/downstream/README.md +++ b/downstream/README.md @@ -9,6 +9,7 @@ Trigger a downstream workflow in another repository and wait for it to finish. 3. Triggers the downstream workflow 4. Waits for the workflow to complete 5. Fails if downstream workflow fails +6. In a merge queue, cancels the downstream run once the upstream `gh-readonly-queue/*` branch is deleted (the entry left the queue), so it doesn't fail at checkout. The token needs `actions: write` on the downstream repo. ## Inputs diff --git a/downstream/action.yml b/downstream/action.yml index c486884..7c33245 100644 --- a/downstream/action.yml +++ b/downstream/action.yml @@ -123,4 +123,5 @@ runs: UPSTREAM_TOKEN: ${{ inputs.upstream-token }} UPSTREAM_REPOSITORY: ${{ github.repository }} UPSTREAM_RUN_ID: ${{ github.run_id }} + UPSTREAM_REF: ${{ github.ref }} run: timeout "${{ inputs.timeout }}" ${GITHUB_ACTION_PATH}/wait.bash "$REPO" "${{ inputs.poll_interval }}" "${{ steps.trigger.outputs.run_url }}" diff --git a/downstream/wait.bash b/downstream/wait.bash index 6bafcd3..caf4642 100755 --- a/downstream/wait.bash +++ b/downstream/wait.bash @@ -3,6 +3,8 @@ REPO=${1} POLL_INTERVAL=${2} DOWNSTREAM_RUN_URL=${3} CANCEL_REQUESTED=0 +DOWNSTREAM_CANCEL_REQUESTED=0 +POLL_SECONDS=${WAIT_POLL_SECONDS:-60} DOWNSTREAM_RUN_ID=$(basename "$DOWNSTREAM_RUN_URL") @@ -45,6 +47,19 @@ cancel_upstream() { done } +# When a merge-queue entry is ejected, GitHub deletes its gh-readonly-queue branch. +# Downstream jobs that start afterwards fail at checkout, which shows up as a red +# downstream run although nothing in it broke. Only a 404 counts as gone. +upstream_ref_gone() { + [[ "${UPSTREAM_REF:-}" == refs/heads/gh-readonly-queue/* && -n "${UPSTREAM_REPOSITORY:-}" ]] || return 1 + + local output + if output=$(GH_TOKEN="${UPSTREAM_TOKEN:-${GH_TOKEN:-}}" gh api "repos/${UPSTREAM_REPOSITORY}/git/ref/${UPSTREAM_REF#refs/}" 2>&1); then + return 1 + fi + [[ "${output}" == *"HTTP 404"* ]] +} + echo "Downstream workflow: ${DOWNSTREAM_RUN_URL}" ATTEMPT=1 @@ -58,10 +73,17 @@ while true; do break fi - echo "Job is still running. Waiting 1 minute before retrying..." + if [[ "${DOWNSTREAM_CANCEL_REQUESTED}" == "0" ]] && upstream_ref_gone; then + echo "${UPSTREAM_REF#refs/heads/} no longer exists: the entry left the merge queue. Cancelling the downstream run." + if gh run cancel --repo "${REPO}" "${DOWNSTREAM_RUN_ID}"; then + DOWNSTREAM_CANCEL_REQUESTED=1 + else + echo "Could not cancel the downstream run." + fi + fi - # Only check every minute - sleep 60 + echo "Job is still running. Waiting ${POLL_SECONDS}s before retrying..." + sleep "${POLL_SECONDS}" done if [[ "${STATUS}" == "cancelled" ]]; then diff --git a/downstream/wait.test.bash b/downstream/wait.test.bash index 834d51a..799bd0c 100644 --- a/downstream/wait.test.bash +++ b/downstream/wait.test.bash @@ -61,3 +61,74 @@ if [[ "${code}" -ne 1 || "${output}" != *"Could not cancel upstream workflow run echo "${output}" exit 1 fi + +# Merge-queue cases: the fake keeps the downstream run in progress until it is +# cancelled or has been polled twice, and answers the ref lookup per GH_REF_STATUS. +STATE_DIR=$(mktemp -d) +QUEUE_BIN=$(mktemp -d) +trap 'rm -rf "${TMP_DIR}" "${STATE_DIR}" "${QUEUE_BIN}"' EXIT + +cat > "${QUEUE_BIN}/gh" <<'FAKE' +#!/usr/bin/env bash +if [[ "${1} ${2}" == "run view" ]]; then + polls=$(( $(cat "${STATE_DIR}/polls" 2>/dev/null || echo 0) + 1 )) + echo "${polls}" > "${STATE_DIR}/polls" + if [[ -e "${STATE_DIR}/cancelled" ]]; then + echo '{"status":"completed","conclusion":"cancelled"}' + elif (( polls > 2 )); then + echo '{"status":"completed","conclusion":"success"}' + else + echo '{"status":"in_progress","conclusion":""}' + fi + exit 0 +fi +if [[ "${1} ${2}" == "run cancel" ]]; then + touch "${STATE_DIR}/cancelled" + echo "downstream cancel called" + exit 0 +fi +if [[ "${1}" == "api" ]]; then + echo "${2}" >> "${STATE_DIR}/api_calls" + case "${GH_REF_STATUS}" in + exists) echo '{"ref":"x"}'; exit 0 ;; + gone) echo "gh: Not Found (HTTP 404)" >&2; exit 1 ;; + *) echo "gh: Server Error (HTTP 500)" >&2; exit 1 ;; + esac +fi +exit 1 +FAKE +chmod +x "${QUEUE_BIN}/gh" + +run_queue_case() { + local name=${1} ref=${2} ref_status=${3} expected_code=${4} expected_message=${5} unexpected_message=${6:-} + local output code + rm -f "${STATE_DIR}"/* + + set +e + output=$(PATH="${QUEUE_BIN}:${PATH}" STATE_DIR="${STATE_DIR}" GH_REF_STATUS="${ref_status}" WAIT_POLL_SECONDS=0 \ + UPSTREAM_REPOSITORY=shopware/shopware UPSTREAM_REF="${ref}" \ + bash "${SCRIPT_DIR}/wait.bash" shopware/example 1 https://github.com/shopware/example/actions/runs/1 2>&1) + code=$? + set -e + + if [[ "${code}" -ne "${expected_code}" || "${output}" != *"${expected_message}"* ]]; then + echo "Case '${name}': expected exit ${expected_code} and '${expected_message}', got exit ${code}" + echo "${output}" + exit 1 + fi + if [[ -n "${unexpected_message}" && "${output}" == *"${unexpected_message}"* ]]; then + echo "Case '${name}': output must not contain '${unexpected_message}'" + echo "${output}" + exit 1 + fi +} + +QUEUE_REF=refs/heads/gh-readonly-queue/trunk/pr-1-abc +run_queue_case "queue branch deleted" "${QUEUE_REF}" gone 1 "Downstream workflow was cancelled." +grep -q "git/ref/heads/gh-readonly-queue/trunk/pr-1-abc" "${STATE_DIR}/api_calls" || { echo "Case 'queue branch deleted': wrong ref looked up"; exit 1; } +run_queue_case "queue branch exists" "${QUEUE_REF}" exists 0 "Downstream workflow succeeded!" "downstream cancel called" +run_queue_case "ref lookup errors" "${QUEUE_REF}" error 0 "Downstream workflow succeeded!" "downstream cancel called" +run_queue_case "not a merge queue run" refs/heads/trunk gone 0 "Downstream workflow succeeded!" "downstream cancel called" +[[ ! -e "${STATE_DIR}/api_calls" ]] || { echo "Case 'not a merge queue run': must not look up the ref"; exit 1; } + +echo "All wait.bash cases passed."