Skip to content
Merged
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
51 changes: 49 additions & 2 deletions .github/workflows/stainless-auto-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,8 +32,52 @@ 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
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: $failed"
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:
SHA: ${{ github.event.pull_request.head.sha }}
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 }}
Loading