# yad-managed: yad-checks
# yad-update-guard — integrity gate for DIRECT pushes to the default branch.
#
# `yad update --push` commits the applied SDLC drift (skills, gate scripts, CI wiring) and pushes it
# straight to the default branch with NO pull request — so the pull_request
# gate suite never fires. This workflow is the "skipped from CI EXCEPT verified-commits + the pattern
# gate" contract: on every push to the default branch it runs ONLY those two gates over the pushed
# range. It is not scoped to yad-update commits on purpose — ANY direct-to-main commit (a hotfix, a
# force-push) then gets signature + subject-format checked, which is a strictly-good invariant. A
# normal PR merge sails through: a GitHub merge/squash commit is platform-Verified, and commit-message.sh
# skips merges. CAVEAT: a *rebase-merge* recreates the PR commits WITHOUT GitHub's signature, so they
# lose the Verified badge and fail the signature check on the direct push — teams using rebase-merge
# should sign their commits or not wire this guard.
name: yad-update-guard
on:
  push:
    # Every branch but the private `yad/wip/*` capture branches (E43): `yad capture` pushes one person's
    # draft snapshots there every few minutes, and a run per push — even one whose job is skipped below —
    # is noise in the Actions list. (`branches-ignore` means "all others"; GitHub allows only one of the two.)
    branches-ignore: ["yad/wip/**"]

permissions:
  contents: read

jobs:
  update-guard:
    # Only the default branch — pushes to feature branches are reviewed via their PR gate suite — and the
    # trunk named by the YAD_TRUNK repository variable, for a team whose trunk (the branch named in the
    # Product's registry) is not the default branch (`yad ci check` names the value). Unset, it is ''.
    if: github.ref_name == github.event.repository.default_branch || (vars.YAD_TRUNK != '' && github.ref_name == vars.YAD_TRUNK)
    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 }
      - name: Resolve the pushed range base
        id: base
        env: { YAD_BEFORE: "${{ github.event.before }}" }
        run: |
          before="$YAD_BEFORE"
          # First push / branch create / an unfetched before-SHA: fall back to the commit's parent.
          # This checks only the tip commit (HEAD~1..HEAD) — a rare fail-open for the multi-commit
          # first-push case, preferred over failing closed on a missing ref.
          if [ -z "$before" ] || [ "$before" = "0000000000000000000000000000000000000000" ] \
             || ! git rev-parse --verify --quiet "${before}^{commit}" >/dev/null; then
            before="$(git rev-parse --verify --quiet HEAD~1 || git rev-parse HEAD)"
          fi
          echo "base=$before" >> "$GITHUB_OUTPUT"
      - name: verified-commits
        env: { YAD_RANGE_BASE: "${{ steps.base.outputs.base }}" }
        run: bash checks/verified-commits.sh "$YAD_RANGE_BASE"
      - name: commit-message
        env: { YAD_RANGE_BASE: "${{ steps.base.outputs.base }}" }
        run: bash checks/commit-message.sh "$YAD_RANGE_BASE"
