# This workflow runs when a PR is first opened by an external contributor
# (author_association is FIRST_TIME_CONTRIBUTOR, FIRST_TIMER, or NONE).
# Org members with public membership are skipped (author_association = MEMBER).
# It checks that the PR body:
#   1. Contains a non-empty description (not just the template placeholder)
#   2. Includes the "## Before submitting" checklist
#   3. Has at least one checkbox checked
# If any check fails, the workflow posts a comment and closes the PR.

name: PR Template Check

on:
  pull_request_target:
    types: [opened]

permissions:
  pull-requests: write
  issues: write
  statuses: write

jobs:
  check-pr-template:
    runs-on: ubuntu-latest
    if: >
      github.event.pull_request.author_association == 'FIRST_TIME_CONTRIBUTOR' ||
      github.event.pull_request.author_association == 'FIRST_TIMER' ||
      github.event.pull_request.author_association == 'NONE'
    steps:
      - name: Check PR body follows template
        id: check
        uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3  # v9.0.0
        env:
          PR_BODY: ${{ github.event.pull_request.body }}
        with:
          script: |
            const body = process.env.PR_BODY || "";

            const errors = [];

            // 1. Must have a non-placeholder description (the "What does this PR do?" section)
            const descriptionSection = body.split("## Before submitting")[0] || "";
            const strippedDescription = descriptionSection
              .replace(/<!--[\s\S]*?-->/g, "")  // remove HTML comments
              .replace(/Fixes\s*#\s*\(issue\)/gi, "")  // remove placeholder issue line
              .trim();

            if (strippedDescription.length < 20) {
              errors.push("- Missing PR description: please explain what this PR does and why.");
            }

            // 2. Must have the "Before submitting" checklist
            if (!body.includes("## Before submitting")) {
              errors.push("- Missing '## Before submitting' checklist section.");
            }

            // 3. At least one checkbox must be checked
            const checkedBoxes = (body.match(/- \[x\]/gi) || []).length;
            if (checkedBoxes === 0) {
              errors.push("- No checkboxes are checked in the 'Before submitting' section. Please review and check the relevant items.");
            }

            if (errors.length > 0) {
              const comment = "Your PR has been closed because it does not follow the required [PR template](https://github.com/huggingface/trl/blob/main/.github/PULL_REQUEST_TEMPLATE.md). Please open a new PR following the template. See [CONTRIBUTING.md](https://github.com/huggingface/trl/blob/main/CONTRIBUTING.md#submitting-a-pull-request-pr) for guidance.";

              await github.rest.issues.createComment({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: context.payload.pull_request.number,
                body: comment,
              });

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

              core.setFailed("PR does not follow the required template.");
            } else {
              console.log("PR template check passed.");
            }
