From 05aa387d18dbd7d671ac983585a8c5660d211f97 Mon Sep 17 00:00:00 2001 From: Dante Danelian Date: Tue, 17 Nov 2026 18:50:54 -0500 Subject: [PATCH 1/3] CD-2075: Wait for CI before auto-merging --- .github/workflows/stainless-auto-merge.yml | 44 +++++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/.github/workflows/stainless-auto-merge.yml b/.github/workflows/stainless-auto-merge.yml index c1ad23e..d9e68f5 100644 --- a/.github/workflows/stainless-auto-merge.yml +++ b/.github/workflows/stainless-auto-merge.yml @@ -7,9 +7,12 @@ on: jobs: auto-merge: runs-on: ubuntu-latest + timeout-minutes: 45 permissions: contents: write pull-requests: write + checks: read + statuses: read steps: - name: Check actor permission id: actor @@ -29,8 +32,45 @@ jobs: ACTOR: ${{ github.actor }} GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: Enable auto-merge + # The branch rules require a PR but no status checks, so GitHub's native + # auto-merge would merge before CI finishes. Wait for every other check + # on the PR to pass, and refuse to merge if any fails. + - name: Wait for CI if: steps.actor.outputs.can_merge == 'true' - run: gh pr merge "${{ github.event.pull_request.number }}" --auto --merge --repo "${{ github.repository }}" + run: | + stable=0 + for _ in $(seq 1 120); do + sleep 20 + checks="$(gh pr checks "$PR" --repo "$GITHUB_REPOSITORY" --json name,bucket \ + --jq "[.[] | select(.name != \"$GITHUB_JOB\")]" 2>/dev/null || echo '[]')" + total="$(jq 'length' <<<"$checks")" + pending="$(jq '[.[] | select(.bucket == "pending")] | length' <<<"$checks")" + failed="$(jq -r '.[] | select(.bucket == "fail" or .bucket == "cancel") | .name' <<<"$checks")" + + if [ -n "$failed" ]; then + echo "::error::Not merging; failed checks: $(echo "$failed" | paste -sd, -)" + exit 1 + fi + + # Require two quiet polls in a row so late-registering checks are not missed. + if [ "$total" -gt 0 ] && [ "$pending" -eq 0 ]; then + stable=$((stable + 1)) + [ "$stable" -ge 2 ] && { echo "All $total checks passed."; exit 0; } + else + stable=0 + fi + done + echo "::error::Timed out waiting for CI." + exit 1 + env: + PR: ${{ github.event.pull_request.number }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + # A PAT, not GITHUB_TOKEN, so the merge's push triggers downstream + # workflows (stlc-promote, release-please, publish). Staging has no + # RELEASE_PLEASE_TOKEN and falls back to PRODUCTION_REPO_TOKEN. + - name: Merge + if: steps.actor.outputs.can_merge == 'true' + run: gh pr merge "${{ github.event.pull_request.number }}" --merge --repo "${{ github.repository }}" env: GH_TOKEN: ${{ secrets.RELEASE_PLEASE_TOKEN || secrets.PRODUCTION_REPO_TOKEN }} From 9e36a412c14621f4c77dfcac4e001feca5a13068 Mon Sep 17 00:00:00 2001 From: Dante Danelian Date: Tue, 17 Nov 2026 19:01:52 -0500 Subject: [PATCH 2/3] CD-2075: Keep check output when gh reports pending checks --- .github/workflows/stainless-auto-merge.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/stainless-auto-merge.yml b/.github/workflows/stainless-auto-merge.yml index d9e68f5..00ddc65 100644 --- a/.github/workflows/stainless-auto-merge.yml +++ b/.github/workflows/stainless-auto-merge.yml @@ -41,8 +41,10 @@ jobs: stable=0 for _ in $(seq 1 120); do sleep 20 + # gh exits 8 while any check (including this job) is pending; keep its output. checks="$(gh pr checks "$PR" --repo "$GITHUB_REPOSITORY" --json name,bucket \ - --jq "[.[] | select(.name != \"$GITHUB_JOB\")]" 2>/dev/null || echo '[]')" + --jq "[.[] | select(.name != \"$GITHUB_JOB\")]" 2>/dev/null || true)" + [ -n "$checks" ] || checks='[]' total="$(jq 'length' <<<"$checks")" pending="$(jq '[.[] | select(.bucket == "pending")] | length' <<<"$checks")" failed="$(jq -r '.[] | select(.bucket == "fail" or .bucket == "cancel") | .name' <<<"$checks")" From 49f4086a83c36a5ab1c9c5e9a5814fd34d2fbe21 Mon Sep 17 00:00:00 2001 From: Dante Danelian Date: Tue, 17 Nov 2026 19:46:56 -0500 Subject: [PATCH 3/3] CD-2075: Poll check runs via REST and log each poll --- .github/workflows/stainless-auto-merge.yml | 23 +++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/.github/workflows/stainless-auto-merge.yml b/.github/workflows/stainless-auto-merge.yml index 00ddc65..8d34c3f 100644 --- a/.github/workflows/stainless-auto-merge.yml +++ b/.github/workflows/stainless-auto-merge.yml @@ -41,16 +41,21 @@ jobs: stable=0 for _ in $(seq 1 120); do sleep 20 - # gh exits 8 while any check (including this job) is pending; keep its output. - checks="$(gh pr checks "$PR" --repo "$GITHUB_REPOSITORY" --json name,bucket \ - --jq "[.[] | select(.name != \"$GITHUB_JOB\")]" 2>/dev/null || true)" - [ -n "$checks" ] || checks='[]' - total="$(jq 'length' <<<"$checks")" - pending="$(jq '[.[] | select(.bucket == "pending")] | length' <<<"$checks")" - failed="$(jq -r '.[] | select(.bucket == "fail" or .bucket == "cancel") | .name' <<<"$checks")" + runs="$(gh api "repos/$GITHUB_REPOSITORY/commits/$SHA/check-runs?per_page=100" \ + --jq "[.check_runs[] | select(.name != \"$GITHUB_JOB\") | {name, status, conclusion}]")" + statuses="$(gh api "repos/$GITHUB_REPOSITORY/commits/$SHA/status" \ + --jq '[.statuses[] | {name: .context, state}]')" + total=$(( $(jq 'length' <<<"$runs") + $(jq 'length' <<<"$statuses") )) + pending=$(( $(jq '[.[] | select(.status != "completed")] | length' <<<"$runs") \ + + $(jq '[.[] | select(.state == "pending")] | length' <<<"$statuses") )) + failed="$( { + jq -r '.[] | select(.status == "completed" and (.conclusion | IN("failure", "cancelled", "timed_out", "action_required", "startup_failure"))) | .name' <<<"$runs" + jq -r '.[] | select(.state | IN("failure", "error")) | .name' <<<"$statuses" + } | paste -sd, -)" + echo "checks=$total pending=$pending failed=${failed:-none}" if [ -n "$failed" ]; then - echo "::error::Not merging; failed checks: $(echo "$failed" | paste -sd, -)" + echo "::error::Not merging; failed checks: $failed" exit 1 fi @@ -65,7 +70,7 @@ jobs: echo "::error::Timed out waiting for CI." exit 1 env: - PR: ${{ github.event.pull_request.number }} + SHA: ${{ github.event.pull_request.head.sha }} GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # A PAT, not GITHUB_TOKEN, so the merge's push triggers downstream