|
| 1 | +name: PR Workflow Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: ~ |
| 5 | + |
| 6 | +jobs: |
| 7 | + check: |
| 8 | + runs-on: ubuntu-latest |
| 9 | + steps: |
| 10 | + - name: Checkout merge commit |
| 11 | + uses: actions/checkout@v4 |
| 12 | + with: |
| 13 | + ref: "refs/pull/${{ github.event.number }}/merge" |
| 14 | + fetch-depth: 2 |
| 15 | + |
| 16 | + - name: Check changes to .github |
| 17 | + if: ${{ github.event.pull_request.head.repo.fork }} |
| 18 | + id: check |
| 19 | + run: | |
| 20 | + echo "==> Changed files:" |
| 21 | + git diff --name-only HEAD^1 HEAD |
| 22 | + count=$(git diff --name-only HEAD^1 HEAD | grep -c '^\.github/') || count=0 |
| 23 | +
|
| 24 | + if [[ $count -gt 0 ]]; then |
| 25 | + echo "==> Found $count violations!" |
| 26 | + echo "==> Violating files" |
| 27 | + git diff --name-only HEAD^1 HEAD | grep '^\.github/' || echo "--None--" |
| 28 | + echo "::error::PR is trying to change a workflow!" |
| 29 | + echo "bad=true" >> "$GITHUB_OUTPUT" |
| 30 | + exit 1 |
| 31 | + else |
| 32 | + echo "All OK" |
| 33 | + echo "bad=false" >> "$GITHUB_OUTPUT" |
| 34 | + fi |
| 35 | +
|
| 36 | + - name: Comment PR |
| 37 | + if: ${{ always() && github.event.pull_request.head.repo.fork && steps.check.outputs.bad == 'true' }} |
| 38 | + uses: actions/github-script@v7 |
| 39 | + with: |
| 40 | + script: | |
| 41 | + github.rest.issues.createComment({ |
| 42 | + issue_number: context.issue.number, |
| 43 | + owner: context.repo.owner, |
| 44 | + repo: context.repo.repo, |
| 45 | + body: `> [!CAUTION] |
| 46 | + > This pull request contains changes to GitHub workflows! |
| 47 | + > Proceed with caution and if not sure, contact your GitHub admin.` |
| 48 | + }) |
| 49 | +
|
| 50 | + - if: ${{ !github.event.pull_request.head.repo.fork }} |
| 51 | + run: | |
| 52 | + echo "Not a PR from fork." |
0 commit comments