# yad-managed: yad-checks
# SDLC pattern gates on the PR text (profile: code): the PR title and the PR body follow the convention.
# Its own workflow (#425) so that an edit of the title or body re-runs these two checks and nothing else.
# A gate job skipped by `if:` counts as passed, so a run of yad-checks started by an edit would turn a
# failed gate green. This file is OWNED by yad-checks (the marker on line 1 identifies it).
#
# Gate scripts (#426): every job runs the BASE branch's copy of checks/, never the PR's own, so a PR that
# changes a gate script is still judged by the script already merged. A script the base does not have yet
# (the PR that first installs the gates, or a `yad update` that adds one) runs from the PR, with a note.
# The base's script runs with THIS file's call: keep every call one the previous release's script accepts.
# This file itself still comes from the PR: protect checks/ and the CI files with a CODEOWNERS rule and
# a required Code Owner review (docs/branch-protection.md, "Who may change the gates").
name: yad-pr-text
on:
  pull_request:
    # `edited` (beyond the opened/synchronize/reopened defaults) so a PR title/body correction
    # re-runs these checks without a close/reopen — e.g. fixing a body the pr-template gate held.
    types: [opened, synchronize, reopened, edited]
    branches: ["**"]

# A newer edit of the PR text cancels this workflow's older run on the same commit (#428): both checks read the
# PR's current title and body, so the newest run is the one that counts. The group names the commit too, so a
# late-delivered edit of an older commit can never cancel the run a newer push started.
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number }}-${{ github.event.pull_request.head.sha }}
  cancel-in-progress: true

# Read-only: no check here writes to the repo.
permissions:
  contents: read

jobs:
  # Pass the title via env (never interpolate untrusted ${{ }} into a run line — injection-safe).
  pr-title:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    env:
      PR_TITLE: ${{ github.event.pull_request.title }}
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with: { fetch-depth: 0, persist-credentials: false }
      - name: Gate scripts from the base branch (#426)
        env: { YAD_BASE: "${{ github.base_ref }}" }
        run: |
          YAD_CHECKS="$RUNNER_TEMP/yad-base-checks/checks"
          (
            set -euo pipefail
            b="origin/${YAD_BASE}"; d="${YAD_CHECKS%/checks}"
            rm -rf "$d"; mkdir -p "$YAD_CHECKS"
            git rev-parse --verify --quiet "${b}^{commit}" > /dev/null || { echo "FAIL [base-checks]: ${b} is not in this checkout, so the gate scripts cannot be read from the base branch. Check the code out with its full history." >&2; exit 1; }
            t="$(git ls-tree "$b" checks)"
            case "$t" in
              "") on_base=0 ;;
              "040000 tree "*) git archive "$b" checks | tar -x -C "$d"; on_base=1 ;;
              *) echo "FAIL [base-checks]: checks on ${b} is not a folder, so the gate scripts cannot be read from it. Make checks/ a plain folder on the base branch." >&2; exit 1 ;;
            esac
            git ls-tree -z HEAD checks/ > "$d/.pr"
            n=0
            while IFS= read -r -d '' e; do
              m="${e%% *}"; p="${e#*$'\t'}"; o="${e#* * }"; o="${o%%$'\t'*}"; s="${p//[[:cntrl:]]/?}"
              if [ -e "$d/$p" ] || [ -L "$d/$p" ]; then continue; fi
              case "$m" in
                100644 | 100755) git cat-file blob "$o" > "$d/$p"; if [ "$m" = 100755 ]; then chmod +x "$d/$p"; fi ;;
                *) printf 'note: %s in this PR is not a plain file, so it is not run (#426)\n' "$s"; continue ;;
              esac
              n=$((n + 1))
              if [ "$on_base" = 1 ]; then printf 'note: %s is not on %s yet, so the copy from this PR runs (#426)\n' "$s" "$YAD_BASE"; fi
            done < "$d/.pr"
            rm -f "$d/.pr"
            if [ "$n" = 1 ]; then w="1 file"; else w="$n files"; fi
            if [ "$on_base" = 0 ]; then printf 'note: %s has no checks/ folder yet, so the gate scripts from this PR run: %s (#426)\n' "$YAD_BASE" "$w"; fi
          )
          echo "YAD_CHECKS=$YAD_CHECKS" >> "$GITHUB_ENV"
      - run: bash "$YAD_CHECKS/pr-title.sh" --profile code "$PR_TITLE"

  pr-template:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    env:
      PR_BODY: ${{ github.event.pull_request.body }}
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with: { fetch-depth: 0, persist-credentials: false }
      - name: Gate scripts from the base branch (#426)
        env: { YAD_BASE: "${{ github.base_ref }}" }
        run: |
          YAD_CHECKS="$RUNNER_TEMP/yad-base-checks/checks"
          (
            set -euo pipefail
            b="origin/${YAD_BASE}"; d="${YAD_CHECKS%/checks}"
            rm -rf "$d"; mkdir -p "$YAD_CHECKS"
            git rev-parse --verify --quiet "${b}^{commit}" > /dev/null || { echo "FAIL [base-checks]: ${b} is not in this checkout, so the gate scripts cannot be read from the base branch. Check the code out with its full history." >&2; exit 1; }
            t="$(git ls-tree "$b" checks)"
            case "$t" in
              "") on_base=0 ;;
              "040000 tree "*) git archive "$b" checks | tar -x -C "$d"; on_base=1 ;;
              *) echo "FAIL [base-checks]: checks on ${b} is not a folder, so the gate scripts cannot be read from it. Make checks/ a plain folder on the base branch." >&2; exit 1 ;;
            esac
            git ls-tree -z HEAD checks/ > "$d/.pr"
            n=0
            while IFS= read -r -d '' e; do
              m="${e%% *}"; p="${e#*$'\t'}"; o="${e#* * }"; o="${o%%$'\t'*}"; s="${p//[[:cntrl:]]/?}"
              if [ -e "$d/$p" ] || [ -L "$d/$p" ]; then continue; fi
              case "$m" in
                100644 | 100755) git cat-file blob "$o" > "$d/$p"; if [ "$m" = 100755 ]; then chmod +x "$d/$p"; fi ;;
                *) printf 'note: %s in this PR is not a plain file, so it is not run (#426)\n' "$s"; continue ;;
              esac
              n=$((n + 1))
              if [ "$on_base" = 1 ]; then printf 'note: %s is not on %s yet, so the copy from this PR runs (#426)\n' "$s" "$YAD_BASE"; fi
            done < "$d/.pr"
            rm -f "$d/.pr"
            if [ "$n" = 1 ]; then w="1 file"; else w="$n files"; fi
            if [ "$on_base" = 0 ]; then printf 'note: %s has no checks/ folder yet, so the gate scripts from this PR run: %s (#426)\n' "$YAD_BASE" "$w"; fi
          )
          echo "YAD_CHECKS=$YAD_CHECKS" >> "$GITHUB_ENV"
      - run: |
          body="$(mktemp)"; printf '%s' "$PR_BODY" > "$body"
          bash "$YAD_CHECKS/pr-template.sh" --profile code "$body"
