Skip to content

Commit e31e7ce

Browse files
Fix Dependabot auto-merge without repository auto-merge setting.
The workflow failed because the repository has allow_auto_merge disabled and main requires linear history, so gh pr merge --auto --merge could never succeed. Approve patch and minor Dependabot PRs on pull_request_target, then squash merge them after CI completes via workflow_run once branch rules are satisfied. Co-authored-by: Jan Willem Kaper <kapersoft@users.noreply.github.com>
1 parent 8506dcd commit e31e7ce

1 file changed

Lines changed: 76 additions & 9 deletions

File tree

.github/workflows/dependabot-auto-merge.yml

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,101 @@ name: dependabot-auto-merge
22

33
on:
44
pull_request_target:
5+
types: [opened, synchronize, reopened]
56
branches:
67
- main
8+
workflow_run:
9+
workflows:
10+
- Pest tests
11+
- PHPStan
12+
- Pint
13+
- Rector
14+
- Composer Normalize
15+
types:
16+
- completed
17+
18+
concurrency:
19+
group: dependabot-auto-merge-${{ github.event_name == 'workflow_run' && github.event.workflow_run.head_sha || github.event.pull_request.head.sha }}
20+
cancel-in-progress: false
721

822
permissions:
923
contents: write
1024
pull-requests: write
25+
actions: read
1126

1227
jobs:
13-
dependabot:
28+
approve:
29+
name: Approve Dependabot PR
1430
runs-on: ubuntu-latest
15-
if: github.actor == 'dependabot[bot]'
31+
if: >
32+
github.event_name == 'pull_request_target' &&
33+
github.actor == 'dependabot[bot]'
1634
steps:
1735
- name: Dependabot metadata
1836
id: metadata
1937
uses: dependabot/fetch-metadata@25dd0e34f4fe68f24cc83900b1fe3fe149efef98 # v3.1.0
2038
with:
2139
github-token: ${{ github.token }}
2240

23-
- name: Auto-merge Dependabot PRs for semver-minor updates
24-
if: steps.metadata.outputs.update-type == 'version-update:semver-minor'
25-
run: gh pr merge --auto --merge "$PR_URL"
41+
- name: Approve patch and minor updates
42+
if: |
43+
steps.metadata.outputs.update-type == 'version-update:semver-patch' ||
44+
steps.metadata.outputs.update-type == 'version-update:semver-minor'
45+
run: gh pr review --approve "$PR_URL"
2646
env:
2747
PR_URL: ${{ github.event.pull_request.html_url }}
2848
GH_TOKEN: ${{ github.token }}
2949

30-
- name: Auto-merge Dependabot PRs for semver-patch updates
31-
if: steps.metadata.outputs.update-type == 'version-update:semver-patch'
32-
run: gh pr merge --auto --merge "$PR_URL"
50+
merge:
51+
name: Merge Dependabot PR
52+
runs-on: ubuntu-latest
53+
if: >
54+
github.event_name == 'workflow_run' &&
55+
github.event.workflow_run.conclusion == 'success' &&
56+
github.event.workflow_run.event == 'pull_request'
57+
steps:
58+
- name: Find pull request
59+
id: pr
3360
env:
34-
PR_URL: ${{ github.event.pull_request.html_url }}
3561
GH_TOKEN: ${{ github.token }}
62+
run: |
63+
PR_NUMBER=$(gh api \
64+
"repos/${{ github.repository }}/commits/${{ github.event.workflow_run.head_sha }}/pulls" \
65+
--jq 'first(.[].number) // empty')
66+
67+
if [ -z "$PR_NUMBER" ]; then
68+
echo "No pull request found for this workflow run."
69+
echo "skip=true" >> "$GITHUB_OUTPUT"
70+
exit 0
71+
fi
72+
73+
AUTHOR=$(gh pr view "$PR_NUMBER" --json author -q '.author.login')
74+
if [ "$AUTHOR" != "app/dependabot" ] && [ "$AUTHOR" != "dependabot[bot]" ]; then
75+
echo "Pull request #$PR_NUMBER is not from Dependabot."
76+
echo "skip=true" >> "$GITHUB_OUTPUT"
77+
exit 0
78+
fi
79+
80+
echo "skip=false" >> "$GITHUB_OUTPUT"
81+
echo "number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
82+
83+
- name: Squash merge when checks and review requirements are satisfied
84+
if: steps.pr.outputs.skip == 'false'
85+
env:
86+
GH_TOKEN: ${{ github.token }}
87+
PR_NUMBER: ${{ steps.pr.outputs.number }}
88+
run: |
89+
MERGE_STATE=$(gh pr view "$PR_NUMBER" --json mergeStateStatus -q '.mergeStateStatus')
90+
91+
if [ "$MERGE_STATE" != "CLEAN" ]; then
92+
echo "Pull request #$PR_NUMBER is not ready to merge (status: $MERGE_STATE)."
93+
exit 0
94+
fi
95+
96+
REVIEW=$(gh pr view "$PR_NUMBER" --json reviewDecision -q '.reviewDecision')
97+
if [ "$REVIEW" != "APPROVED" ]; then
98+
echo "Pull request #$PR_NUMBER is not approved (decision: $REVIEW)."
99+
exit 0
100+
fi
101+
102+
gh pr merge "$PR_NUMBER" --squash

0 commit comments

Comments
 (0)