-
Notifications
You must be signed in to change notification settings - Fork 168
98 lines (85 loc) Β· 3.72 KB
/
auto-pr-merge.yml
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
name: Auto-merge PRs to Contributors.md
on:
pull_request_target:
types: [opened, synchronize]
paths:
- 'Contributors.md' # Only trigger if Contributors.md is changed
jobs:
auto-merge:
runs-on: ubuntu-latest
permissions:
contents: write # Allow merging
pull-requests: write # Allow PR actions
issues: write # Allow posting comments
steps:
# Checkout the repository code
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 2
# Get list of files changed in the pull request
- name: Check if only Contributors.md was changed
id: check_files
run: |
PR_FILES=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files" | jq -r '.[].filename')
echo "Files changed in this PR: $PR_FILES"
# Check if the only file changed is Contributors.md
if [[ "$PR_FILES" == "Contributors.md" ]]; then
echo "only_contributors=true" >> $GITHUB_ENV
else
echo "only_contributors=false" >> $GITHUB_ENV
fi
# Merge the PR if only Contributors.md was changed
- name: Merge PR
id: merge_pr
if: env.only_contributors == 'true'
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const pull_number = context.payload.pull_request.number;
// Try to merge the pull request using the squash method
const response = await github.rest.pulls.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pull_number,
merge_method: "squash"
});
if (response.status === 200) {
console.log(`Pull request #${pull_number} merged successfully.`);
} else {
throw new Error(`Failed to merge pull request #${pull_number}.`);
}
# Post a congratulatory comment after the PR is merged
- name: Post comment after merge
if: steps.merge_pr.conclusion == 'success'
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const user = context.payload.pull_request.user.login;
const message = `π Hello @${user}, thank you for your contribution! Your pull request has been successfully merged! π\n\nWe appreciate your effort in improving the project. Feel free to contribute again in the future!\n π`;
// Post the comment on the PR
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
body: message
});
# Post a comment if the PR was not merged because other files were changed
- name: Post comment if not merged
if: env.only_contributors != 'true'
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const pull_number = context.payload.pull_request.number;
const user = context.payload.pull_request.user.login;
const message = `Hello @${user}, thank you for your contribution. This pull request contains changes to files other than Contributors.md and requires manual review.`;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pull_number,
body: message
});