|
| 1 | +name: Auto-merge Speakeasy PR |
| 2 | + |
| 3 | +# Speakeasy mode:pr opens PRs (speakeasy-sdk-regen-*) and labels them |
| 4 | +# patch/minor/major. Merge automatically so sdk_publish.yaml can run. |
| 5 | + |
| 6 | +on: |
| 7 | + pull_request: |
| 8 | + types: [labeled] |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: write |
| 12 | + pull-requests: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + auto-merge: |
| 16 | + if: | |
| 17 | + github.event.sender.login == 'github-actions[bot]' && |
| 18 | + github.event.pull_request.user.login == 'github-actions[bot]' && |
| 19 | + startsWith(github.event.pull_request.head.ref, 'speakeasy-sdk-regen-') && |
| 20 | + contains(github.event.pull_request.title, '🐝 Update SDK') && |
| 21 | + contains(fromJSON('["patch", "minor", "major"]'), github.event.label.name) |
| 22 | + runs-on: ubuntu-latest |
| 23 | + steps: |
| 24 | + - name: Close superseded Speakeasy PRs |
| 25 | + env: |
| 26 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 27 | + run: | |
| 28 | + set -euo pipefail |
| 29 | + CURRENT_PR="${{ github.event.pull_request.number }}" |
| 30 | +
|
| 31 | + PRIOR_JSON=$(gh pr list \ |
| 32 | + --repo "${{ github.repository }}" \ |
| 33 | + --state open \ |
| 34 | + --search "head:speakeasy-sdk-regen-" \ |
| 35 | + --limit 500 \ |
| 36 | + --json number \ |
| 37 | + --jq --arg current "$CURRENT_PR" '[.[].number | select(tostring != $current)] | .[]') |
| 38 | +
|
| 39 | + if [ -z "$PRIOR_JSON" ]; then |
| 40 | + echo "No superseded Speakeasy PRs to close" |
| 41 | + exit 0 |
| 42 | + fi |
| 43 | +
|
| 44 | + mapfile -t PRIOR <<< "$PRIOR_JSON" |
| 45 | +
|
| 46 | + echo "Closing ${#PRIOR[@]} superseded Speakeasy PR(s)" |
| 47 | + for N in "${PRIOR[@]}"; do |
| 48 | + gh pr close "$N" --repo "${{ github.repository }}" --delete-branch \ |
| 49 | + --comment "Superseded by newer Speakeasy SDK generation PR #${CURRENT_PR}" \ |
| 50 | + || echo "::warning::Failed to close PR #$N (continuing)" |
| 51 | + sleep 1 |
| 52 | + done |
| 53 | +
|
| 54 | + - name: Auto-merge Speakeasy PR |
| 55 | + env: |
| 56 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 57 | + run: | |
| 58 | + set -euo pipefail |
| 59 | + PR_NUM="${{ github.event.pull_request.number }}" |
| 60 | + REPO="${{ github.repository }}" |
| 61 | +
|
| 62 | + STATE=$(gh pr view "$PR_NUM" --repo "$REPO" --json state --jq .state) |
| 63 | + if [ "$STATE" != "OPEN" ]; then |
| 64 | + echo "PR #$PR_NUM is $STATE — skipping merge" |
| 65 | + exit 0 |
| 66 | + fi |
| 67 | +
|
| 68 | + # Prefer GitHub auto-merge when required checks exist; otherwise |
| 69 | + # squash-merge directly (same pattern as sdk-release-prs.yaml). |
| 70 | + AUTO_MERGE_NOOP_PATTERN='is in clean status|protected branch rules|Branch does not have required protected branch rules' |
| 71 | + if gh pr merge "$PR_NUM" --repo "$REPO" --squash --auto --delete-branch 2> /tmp/gh-merge.err; then |
| 72 | + echo "Auto-merge enabled for Speakeasy PR #$PR_NUM" |
| 73 | + elif grep -qiE "$AUTO_MERGE_NOOP_PATTERN" /tmp/gh-merge.err; then |
| 74 | + echo "Auto-merge not applicable — merging PR #$PR_NUM directly" |
| 75 | + cat /tmp/gh-merge.err >&2 |
| 76 | + gh pr merge "$PR_NUM" --repo "$REPO" --squash --delete-branch |
| 77 | + else |
| 78 | + echo "::error::Failed to enable auto-merge on Speakeasy PR #$PR_NUM" |
| 79 | + cat /tmp/gh-merge.err >&2 |
| 80 | + exit 1 |
| 81 | + fi |
0 commit comments