name: PR Checklist Check

on:
  pull_request_target:
    types: [opened, edited, reopened, synchronize]

jobs:
  check:
    runs-on: ubuntu-latest

    permissions:
      pull-requests: write
      issues: write

    steps:
      - name: Check checklist
        id: check
        uses: actions/github-script@v7
        with:
          script: |
            const body = context.payload.pull_request.body || "";
            const regex = /-\s*\[\s*x\s*\].*没有.*认真阅读/i;
            const bad = regex.test(body);
            core.setOutput("bad", bad);

      - name: Close PR
        if: steps.check.outputs.bad == 'true'
        uses: actions/github-script@v7
        with:
          script: |
            const pr = context.payload.pull_request;

            await github.rest.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: pr.number,
              body: `检测到你勾选了“我没有认真阅读”，PR 已关闭。`
            });

            await github.rest.pulls.update({
              owner: context.repo.owner,
              repo: context.repo.repo,
              pull_number: pr.number,
              state: "closed"
            });