|
| 1 | +name: Auto-merge PRs |
| 2 | +on: |
| 3 | + pull_request_target: |
| 4 | + types: [opened, synchronize, reopened] |
| 5 | + paths: |
| 6 | + - "contributors/**" # Run if only contributors dir changed |
| 7 | + |
| 8 | +jobs: |
| 9 | + auto-merge: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + permissions: |
| 12 | + contents: write |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | + steps: |
| 16 | + - name: Checkout repository |
| 17 | + uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + ref: ${{ github.event.pull_request.head.sha }} |
| 20 | + fetch-depth: 2 |
| 21 | + |
| 22 | + - name: Get a list of files changed in the pull request |
| 23 | + run: | |
| 24 | + PR_FILES=$(gh pr view 2615 --json files --jq '.[].filename' ${{ github.event.pull_request.html_url }}) |
| 25 | + FILES_CHANGED=$(echo $PR_FILES | tr '\n' ' ') |
| 26 | + echo "FILES_CHANGED=$FILES_CHANGED" >> $GITHUB_ENV |
| 27 | + env: |
| 28 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 29 | + |
| 30 | + - name: Check if PR modifies only contributors/{username}.html |
| 31 | + id: only_contributors_check |
| 32 | + uses: actions/github-script@v6 |
| 33 | + with: |
| 34 | + script: | |
| 35 | + const username = context.payload.pull_request.user.login; |
| 36 | + const expected = `contributors/${username}.html`; |
| 37 | + const filesChanged = process.env.files_changed.trim(); |
| 38 | + console.log(`Comparing "${filesChanged}" to "${expected}"`); |
| 39 | + const onlyContributors = filesChanged === expected; |
| 40 | + core.setOutput('only_contributors', onlyContributors); |
| 41 | +
|
| 42 | + - name: Set up Python |
| 43 | + uses: actions/setup-python@v3 |
| 44 | + with: |
| 45 | + python-version: "3.x" |
| 46 | + |
| 47 | + - name: Install dependencies |
| 48 | + run: pip install bs4 groq |
| 49 | + |
| 50 | + - name: Check PR contents |
| 51 | + env: |
| 52 | + GROQ_API_KEY: ${{ secrets.GROQ_API_KEY }} |
| 53 | + run: python scripts/check_pr.py |
| 54 | + |
| 55 | + - name: Auto-approve PR if not flagged |
| 56 | + if: success() && steps.only_contributors_check.outputs.only_contributors == 'true' |
| 57 | + run: gh pr merge --merge "${{ github.event.pull_request.html_url }}" |
| 58 | + env: |
| 59 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 60 | + |
| 61 | + - name: Create comment if not flagged |
| 62 | + if: success() && steps.only_contributors_check.outputs.only_contributors == 'true' |
| 63 | + run: echo "👏 You've successfully submitted a PR, and it's been automatically merged!" | tee comment.txt |
| 64 | + |
| 65 | + - name: Create comment if flagged |
| 66 | + if: failure() |
| 67 | + run: echo "🚩 Your PR was flagged. Please review and appropriately modify your PR; if this flag is in error, wait for the maintainer to review it." | tee comment.txt |
| 68 | + |
| 69 | + - name: Create comment if something other than contributors/{username}.html was modified |
| 70 | + if: success() && steps.only_contributors_check.outputs.only_contributors != 'true' |
| 71 | + run: echo "👏 You've successfully submitted a PR! It contains changes that require review by the maintainer before merging.\nFiles changed:\n$FILES_CHANGED" | tee comment.txt |
| 72 | + |
| 73 | + - name: Post comment on PR |
| 74 | + if: always() |
| 75 | + uses: thollander/actions-comment-pull-request@v2 |
| 76 | + with: |
| 77 | + filePath: comment.txt |
0 commit comments