name: VeeR-EL2 CI

on:
  push:
    branches: ["main"]
  pull_request:
  workflow_dispatch:
  schedule:
    - cron: '0 2 * * *' # run daily at 02:00 am (UTC)

jobs:

  Check-PR-Origin:
    name: Check PR origin
    runs-on: ubuntu-24.04
    outputs:
      is_internal: ${{ steps.check.outputs.is_internal }}
    steps:
      - name: Determine if PR is from chipsalliance organization
        id: check
        env:
          EVENT_NAME: ${{ github.event_name }}
          HEAD_REPO_OWNER: ${{ github.event.pull_request.head.repo.owner.login }}
        run: |
          if [ "$EVENT_NAME" != "pull_request" ]; then
            echo "is_internal=true" >> $GITHUB_OUTPUT
            echo "Not a pull request event. Custom CI checks will run."
          elif [ "$HEAD_REPO_OWNER" = "chipsalliance" ]; then
            echo "is_internal=true" >> $GITHUB_OUTPUT
            echo "PR from chipsalliance organization. Custom CI checks will run."
          else
            echo "is_internal=false" >> $GITHUB_OUTPUT
            echo "PR from external fork ($HEAD_REPO_OWNER). Custom CI checks will be skipped."
          fi

  Detect-Changes:
    name: Detect changed paths
    runs-on: ubuntu-24.04
    outputs:
      docs_changed: ${{ github.event_name != 'pull_request' || steps.filter.outputs.docs == 'true' }}
      code_changed: ${{ github.event_name != 'pull_request' || steps.filter.outputs.code == 'true' }}
    steps:
      - name: Filter changed paths
        if: github.event_name == 'pull_request'
        uses: dorny/paths-filter@v3
        id: filter
        with:
          filters: |
            docs:
              - 'docs/**'
            code:
              - '!docs/**'

  Test-DCLS:
    name: Test-DCLS-Regression
    needs: [Check-PR-Origin, Detect-Changes]
    if: needs.Detect-Changes.outputs.code_changed == 'true'
    uses: ./.github/workflows/test-regression-dcls.yml
    with:
      run_custom: ${{ needs.Check-PR-Origin.outputs.is_internal == 'true' }}

  Test-Regression-Cache-Waypack-0-num-ways-2:
    name: Test-Regression Cache Waypack 0 Num Ways 2
    needs: [Check-PR-Origin, Detect-Changes]
    if: needs.Detect-Changes.outputs.code_changed == 'true'
    uses: ./.github/workflows/test-regression-cache-waypack.yml
    with:
      waypack: 0
      num_ways: 2
      run_custom: ${{ needs.Check-PR-Origin.outputs.is_internal == 'true' }}

  Test-Regression-Cache-Waypack-0-num-ways-4:
    name: Test-Regression Cache Waypack 0 Num Ways 4
    needs: [Check-PR-Origin, Detect-Changes]
    if: needs.Detect-Changes.outputs.code_changed == 'true'
    uses: ./.github/workflows/test-regression-cache-waypack.yml
    with:
      waypack: 0
      num_ways: 4
      run_custom: ${{ needs.Check-PR-Origin.outputs.is_internal == 'true' }}

  Test-Regression-Cache-Waypack-1-num-ways-2:
    name: Test-Regression Cache Waypack 1 Num Ways 2
    needs: [Check-PR-Origin, Detect-Changes]
    if: needs.Detect-Changes.outputs.code_changed == 'true'
    uses: ./.github/workflows/test-regression-cache-waypack.yml
    with:
      waypack: 1
      num_ways: 2
      run_custom: ${{ needs.Check-PR-Origin.outputs.is_internal == 'true' }}

  Test-Regression-Cache-Waypack-1-num-ways-4:
    name: Test-Regression Cache Waypack 1 Num Ways 4
    needs: [Check-PR-Origin, Detect-Changes]
    if: needs.Detect-Changes.outputs.code_changed == 'true'
    uses: ./.github/workflows/test-regression-cache-waypack.yml
    with:
      waypack: 1
      num_ways: 4
      run_custom: ${{ needs.Check-PR-Origin.outputs.is_internal == 'true' }}

  Test-Exceptions-Regression:
    name: Test-Exceptions-Regression
    needs: [Check-PR-Origin, Detect-Changes]
    if: needs.Detect-Changes.outputs.code_changed == 'true'
    uses: ./.github/workflows/test-regression-exceptions.yml
    with:
      run_custom: ${{ needs.Check-PR-Origin.outputs.is_internal == 'true' }}

  Test-Verification:
    name: Test-Verification
    needs: [Check-PR-Origin, Detect-Changes]
    if: needs.Detect-Changes.outputs.code_changed == 'true'
    uses: ./.github/workflows/test-verification.yml
    with:
      run_custom: ${{ needs.Check-PR-Origin.outputs.is_internal == 'true' }}

  Test-Microarchitectural:
    name: Test-Microarchitectural
    needs: [Check-PR-Origin, Detect-Changes]
    if: needs.Detect-Changes.outputs.code_changed == 'true'
    uses: ./.github/workflows/test-uarch.yml
    with:
      run_custom: ${{ needs.Check-PR-Origin.outputs.is_internal == 'true' }}

  Test-RISCV-DV:
    name: Test-RISCV-DV
    needs: [Check-PR-Origin, Detect-Changes]
    if: needs.Detect-Changes.outputs.code_changed == 'true'
    uses: ./.github/workflows/test-riscv-dv.yml
    with:
      run_custom: ${{ needs.Check-PR-Origin.outputs.is_internal == 'true' }}

  Test-RISCOF:
    name: Test-RISCOF
    needs: [Detect-Changes]
    if: needs.Detect-Changes.outputs.code_changed == 'true'
    uses: ./.github/workflows/test-riscof.yml

  Test-UVM:
    name: Test-UVM
    needs: [Detect-Changes]
    if: needs.Detect-Changes.outputs.code_changed == 'true'
    uses: ./.github/workflows/test-uvm.yml

  Test-Renode:
    name: Test-Renode
    needs: [Detect-Changes]
    if: needs.Detect-Changes.outputs.code_changed == 'true'
    uses: ./.github/workflows/test-renode.yml

  Test-OpenOCD:
    name: Test-OpenOCD
    needs: [Check-PR-Origin, Detect-Changes]
    if: needs.Detect-Changes.outputs.code_changed == 'true'
    uses: ./.github/workflows/test-openocd.yml
    with:
      run_custom: ${{ needs.Check-PR-Origin.outputs.is_internal == 'true' }}

  Report-Coverage:
    name: Report-Coverage
    if: github.event_name != 'pull_request'
    needs: [
      Check-PR-Origin,
      Test-DCLS,
      Test-Regression-Cache-Waypack-0-num-ways-2,
      Test-Regression-Cache-Waypack-0-num-ways-4,
      Test-Regression-Cache-Waypack-1-num-ways-2,
      Test-Regression-Cache-Waypack-1-num-ways-4,
      Test-Exceptions-Regression,
      Test-Verification,
      Test-Microarchitectural,
      Test-RISCV-DV,
      Test-RISCOF,
      Test-OpenOCD
    ]
    uses: ./.github/workflows/report-coverage.yml
    with:
      run_custom: ${{ needs.Check-PR-Origin.outputs.is_internal == 'true' }}

  Upload-PR-Info:
    name: Upload PR info
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-24.04
    needs: [Check-PR-Origin]
    steps:
      - name: Write PR info
        run: |
          echo "number=${{ github.event.number }}" >> pr_number.txt
      - name: Upload PR info
        uses: actions/upload-artifact@v4
        with:
          name: pr_number
          path: pr_number.txt

  Build-Docs:
    name: Build-Docs
    needs: [Detect-Changes]
    if: needs.Detect-Changes.outputs.docs_changed == 'true'
    uses: ./.github/workflows/build-docs.yml

  Publish-to-GH-Pages:
    concurrency:
      group: concurrency-group-${{ github.repository }}-publish
      cancel-in-progress: false
    permissions:
      actions: write
      contents: write
    name: Publish-to-GH-Pages
    needs: [Check-PR-Origin, Report-Coverage, Build-Docs]
    uses: ./.github/workflows/publish-webpage.yml
    with:
      run_custom: ${{ needs.Check-PR-Origin.outputs.is_internal == 'true' }}
