# yad-managed: yad-checks
# verified-commits gate for the PRODUCT: every PR (including the Shape review/EP-* PRs)
# must contain only commits whose signature the platform marks Verified. Who may author them is who has
# write access to the repo — yadflow keeps no author list.
# Standalone workflow so it never collides with the Product-flavored yad-checks workflow.
#
# 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-verified-commits
on:
  pull_request:
    branches: ["**"]

# A newer push to the same pull request cancels this workflow's older run (#428): only the newest commit
# can be merged, and every gate here re-reads the whole PR range, so the older run checks nothing it misses.
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  verified-commits:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    env:
      GH_TOKEN: ${{ github.token }} # read-only: gh api commits/<sha> for the Verified badge
    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/verified-commits.sh" "origin/$YAD_BASE"
        env: { YAD_BASE: "${{ github.base_ref }}" }
