-
Notifications
You must be signed in to change notification settings - Fork 46
117 lines (97 loc) · 4 KB
/
auto-merge.yml
File metadata and controls
117 lines (97 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Auto Merge Workflow
# Automatically merges pull requests that meet all requirements
# Only runs for PRs with 'auto-merge' label or from dependabot
name: Auto Merge
on:
pull_request:
types: [opened, synchronize, reopened, labeled]
pull_request_review:
types: [submitted]
check_suite:
types: [completed]
jobs:
auto-merge:
runs-on: ubuntu-latest
# Only run for PRs with 'auto-merge' label or from dependabot
if: |
github.event.pull_request.user.login == 'dependabot[bot]' ||
contains(github.event.pull_request.labels.*.name, 'auto-merge')
# Permissions required for merging PRs
permissions:
contents: write # Write to merge commits
pull-requests: write # Update PR status
steps:
# Step 1: Checkout repository code
- name: 🔑 Checkout
uses: actions/checkout@v4
# Step 2: Display auto-merge configuration
- name: 🤖 Auto Merge Configuration
run: |
echo "Auto-merge workflow triggered"
echo "PR #${{ github.event.pull_request.number }}"
echo "Author: ${{ github.event.pull_request.user.login }}"
echo ""
echo "Requirements for auto-merge:"
echo "- All required checks must pass"
echo "- PR must be approved by maintainer"
echo "- No merge conflicts"
echo "- Branch is up to date with base"
# Step 3: Check conditions and attempt to merge
- name: ✅ Enable Auto-Merge
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const pr = context.payload.pull_request;
// Verify this is a pull request event
if (!pr) {
console.log('Not a pull request event');
return;
}
// Check if PR qualifies for auto-merge
const shouldAutoMerge =
pr.user.login === 'dependabot[bot]' ||
pr.labels.some(label => label.name === 'auto-merge');
if (!shouldAutoMerge) {
console.log('PR does not qualify for auto-merge');
return;
}
console.log('PR qualifies for auto-merge');
// Verify all required checks have passed
const { data: checks } = await github.rest.checks.listForRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: pr.head.sha
});
const allChecksPassed = checks.check_runs.every(check =>
check.conclusion === 'success'
);
if (!allChecksPassed) {
console.log('Not all checks have passed yet, waiting...');
return;
}
// Verify PR has been approved (skip for dependabot)
const { data: reviews } = await github.rest.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number
});
const isApproved = reviews.some(review => review.state === 'APPROVED');
if (!isApproved && pr.user.login !== 'dependabot[bot]') {
console.log('PR not approved yet, waiting for review...');
return;
}
console.log('All conditions met, attempting to merge...');
// Attempt to merge the PR
try {
await github.rest.pulls.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
merge_method: 'squash'
});
console.log('✅ PR merged successfully');
} catch (error) {
console.log('Could not merge PR:', error.message);
console.log('This may be due to merge conflicts or other restrictions');
}