Update Contributors.md #16
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); |