Skip to content

Commit 30bc131

Browse files
feat: auto-merge Speakeasy SDK generation PRs (#332)
2 parents b0eda78 + 1b0118c commit 30bc131

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)