# Vericull v4.0.0 Open-Source SARIF 2.1.0 Pull-Request Gate
# SPDX-License-Identifier: MIT OR Apache-2.0
#
# TEMPLATE REQUIRING INTEGRATION (V3-1). This file does not install or pin a scanner. It fails
# closed until a scan provenance check passes: the SARIF must be an artifact of a successful
# run of your scanner workflow for this exact commit, and it must carry a GitHub artifact
# attestation whose signer workflow, signer digest and source digest match values configured
# as repository variables (Settings > Variables), which a pull request cannot change:
#   VERICULL_SCANNER_RUN_WORKFLOW   workflow file name whose run uploads the SARIF artifact
#   VERICULL_SCANNER_ARTIFACT       artifact name holding vericull-scan.sarif
#   VERICULL_SCANNER_SIGNER_WORKFLOW  OWNER/REPO/.github/workflows/FILE.yml that signs the
#                                   attestation (keep it in a repository the PR cannot edit)
#   VERICULL_SCANNER_SIGNER_DIGEST  full commit SHA of that signer workflow
# Security invariant: NEVER read a PR-committed SARIF file from the untrusted checkout. The
# verified SARIF lives in $RUNNER_TEMP outside $GITHUB_WORKSPACE.
name: Vericull SARIF 2.1.0 Pull-Request Gate

on:
  pull_request:
    branches: [main]

permissions:
  contents: read
  actions: read
  attestations: read
  security-events: read

jobs:
  vericull-gate:
    name: Mechanized 7-Mode VCND + VCSF-36 64-Tree + Ed25519 Gate
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository bytes at exact PR commit
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          ref: ${{ github.sha }}
          persist-credentials: false

      - name: Fetch & verify pinned open-source CLI and router bundle outside workspace
        run: |
          set -euo pipefail
          curl -fsSL https://vericull.risconlabs.com/releases/vericull_cli.py -o "$RUNNER_TEMP/vericull_cli.py"
          curl -fsSL https://vericull.risconlabs.com/data/forensic-dossiers.json -o "$RUNNER_TEMP/forensic-dossiers.json"
          echo "8bcb4aee719c4fd944f9d4bae3bbfedd1a9e8567d2dcf945691d2e6afcceafe2  $RUNNER_TEMP/vericull_cli.py" | sha256sum -c -
          echo "ba7a934322897c40ea441fb17c9c1592d64c0dd9a9eaa53322739c5416298c3e  $RUNNER_TEMP/forensic-dossiers.json" | sha256sum -c -

      - name: Fetch scanner SARIF and verify its provenance (fails closed until integrated)
        id: scan
        env:
          GH_TOKEN: ${{ github.token }}
          TARGET_SHA: ${{ github.sha }}
          SCAN_DIR: ${{ runner.temp }}/vericull-scan
          SCAN_SARIF: ${{ runner.temp }}/vericull-scan.sarif
          RUN_WORKFLOW: ${{ vars.VERICULL_SCANNER_RUN_WORKFLOW }}
          ARTIFACT: ${{ vars.VERICULL_SCANNER_ARTIFACT }}
          SIGNER_WORKFLOW: ${{ vars.VERICULL_SCANNER_SIGNER_WORKFLOW }}
          SIGNER_DIGEST: ${{ vars.VERICULL_SCANNER_SIGNER_DIGEST }}
        run: |
          set -euo pipefail
          rm -rf "$SCAN_DIR" "$SCAN_SARIF"
          if [ -z "$RUN_WORKFLOW" ] || [ -z "$ARTIFACT" ] || [ -z "$SIGNER_WORKFLOW" ] || [ -z "$SIGNER_DIGEST" ]; then
            echo "ERROR: template requires integration: set the VERICULL_SCANNER_* repository variables (fail closed)." >&2
            exit 1
          fi
          if ! printf %s "$SIGNER_DIGEST" | grep -Eq '^[0-9a-f]{40}$'; then
            echo "ERROR: VERICULL_SCANNER_SIGNER_DIGEST must be a full 40-hex commit SHA (fail closed)." >&2
            exit 1
          fi
          RUN_ID="$(gh run list --repo "$GITHUB_REPOSITORY" --workflow "$RUN_WORKFLOW" --commit "$TARGET_SHA" \
            --status success --limit 1 --json databaseId --jq '.[0].databaseId // empty')"
          if [ -z "$RUN_ID" ]; then
            echo "ERROR: no successful $RUN_WORKFLOW run for commit $TARGET_SHA (fail closed)." >&2
            exit 1
          fi
          gh run download "$RUN_ID" --repo "$GITHUB_REPOSITORY" --name "$ARTIFACT" --dir "$SCAN_DIR"
          if [ ! -f "$SCAN_DIR/vericull-scan.sarif" ] || [ -L "$SCAN_DIR/vericull-scan.sarif" ]; then
            echo "ERROR: artifact $ARTIFACT lacks a regular vericull-scan.sarif (fail closed)." >&2
            exit 1
          fi
          cp "$SCAN_DIR/vericull-scan.sarif" "$SCAN_SARIF"
          gh attestation verify "$SCAN_SARIF" --repo "$GITHUB_REPOSITORY" \
            --signer-workflow "$SIGNER_WORKFLOW" --signer-digest "$SIGNER_DIGEST" \
            --source-digest "$TARGET_SHA" --deny-self-hosted-runners
          if [ ! -f "$SCAN_SARIF" ] || [ -L "$SCAN_SARIF" ] || [ ! -s "$SCAN_SARIF" ]; then
            echo "ERROR: trusted scan output $SCAN_SARIF is missing, empty, or a symlink (fail closed)." >&2
            exit 1
          fi

      - name: Offline zero-network VCND + 64-tree SARIF gate (default, free)
        if: ${{ env.VERICULL_TOKEN == '' }}
        env:
          VERICULL_TOKEN: ${{ secrets.VERICULL_TOKEN }}
        run: |
          set -euo pipefail
          python3 -I "$RUNNER_TEMP/vericull_cli.py" local-sarif \
            --sarif "$RUNNER_TEMP/vericull-scan.sarif" \
            --repo-root . \
            --model "$RUNNER_TEMP/forensic-dossiers.json" \
            --out "$RUNNER_TEMP/vericull-local-report.json"

      - name: Optional Edge Ed25519 receipt signing (when free vc_oss_ key is set)
        if: ${{ env.VERICULL_TOKEN != '' }}
        env:
          VERICULL_TOKEN: ${{ secrets.VERICULL_TOKEN }}
        run: |
          set -euo pipefail
          python3 -I "$RUNNER_TEMP/vericull_cli.py" remote-sarif \
            --sarif "$RUNNER_TEMP/vericull-scan.sarif" \
            --repo-root . \
            --model "$RUNNER_TEMP/forensic-dossiers.json" \
            --api-url https://vericull.risconlabs.com \
            --verify-key 418f3ba915d9efa93729d6db323f307b65ffdf476c7228247320542a2b17e03e \
            --out "$RUNNER_TEMP/vericull-signed-receipt.json"
