name: cereal validation

on:
  push:
    branches:
      - master
  pull_request:
    paths:
      - 'cereal/**'
  workflow_dispatch:
  workflow_call:
    inputs:
      run_number:
        default: '1'
        required: true
        type: string

concurrency:
  group: cereal-validation-ci-run-${{ inputs.run_number }}-${{ github.event_name == 'push' && github.ref == 'refs/heads/master' && github.run_id || github.head_ref || github.ref }}-${{ github.workflow }}-${{ github.event_name }}
  cancel-in-progress: true

env:
  CI: 1

jobs:
  generate_cereal_artifact:
    name: Generate cereal validation artifacts
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v6
        with:
          submodules: true
      - run: ./tools/op.sh setup
      - name: Build openpilot
        run: scons -j$(nproc) cereal
      - name: Generate the log file
        run: |
          export PYTHONPATH=${{ github.workspace }}
          python3 cereal/messaging/tests/validate_sp_cereal_upstream.py -g -f schema_instances.bin
      - name: 'Prepare artifact'
        run: |
          mkdir -p "cereal/messaging/tests/cereal_validations"
          cp cereal/messaging/tests/validate_sp_cereal_upstream.py "cereal/messaging/tests/cereal_validations/validate_sp_cereal_upstream.py"
          cp schema_instances.bin "cereal/messaging/tests/cereal_validations/schema_instances.bin"
      - name: 'Upload Artifact'
        uses: actions/upload-artifact@v4
        with:
          name: cereal_validations
          path: cereal/messaging/tests/cereal_validations

  validate_cereal_with_upstream:
    name: Validate cereal with Upstream
    runs-on: ubuntu-24.04
    needs: generate_cereal_artifact
    steps:
      - name: Checkout sunnypilot
        uses: actions/checkout@v6
      - name: Checkout upstream openpilot
        uses: actions/checkout@v6
        with:
          repository: 'commaai/openpilot'
          path: openpilot
          submodules: true
          ref: "refs/heads/master"
      - run: ./tools/op.sh setup
      - name: Build openpilot
        working-directory: openpilot
        run: scons -j$(nproc) cereal
      - name: Download build artifacts
        uses: actions/download-artifact@v4
        with:
          name: cereal_validations
          path: openpilot/cereal/messaging/tests/cereal_validations
      - name: 'Run the validation'
        run: |
          export PYTHONPATH=${{ github.workspace }}/openpilot
          chmod +x openpilot/cereal/messaging/tests/cereal_validations/validate_sp_cereal_upstream.py
          python3 openpilot/cereal/messaging/tests/cereal_validations/validate_sp_cereal_upstream.py -r -f openpilot/cereal/messaging/tests/cereal_validations/schema_instances.bin
