name: ci

on:
  push:
    branches:
      - develop
      - releases/**
  pull_request:
    branches:
      - develop
      - releases/**
  merge_group:

concurrency:
  group: ci-${{github.ref}}-${{github.event.pull_request.number || github.run_number}}
  cancel-in-progress: true

jobs:
  # Check which files have been updated by the PR
  changes:
    runs-on: ubuntu-latest
      # Set job outputs to values from filter step
    outputs:
      bootstrap: ${{ steps.filter.outputs.bootstrap }}
      core: ${{ steps.filter.outputs.core }}
      packages: ${{ steps.filter.outputs.packages }}
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
        if: ${{ github.event_name == 'push' || github.event_name == 'merge_group' }}
        with:
          fetch-depth: 0
            # For pull requests it's not necessary to checkout the code
      - uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36
        id: filter
        with:
          # For merge group events, compare against the target branch (main)
          base: ${{ github.event_name == 'merge_group' && github.event.merge_group.base_ref || '' }}
          # For merge group events, use the merge group head ref
          ref: ${{ github.event_name == 'merge_group' && github.event.merge_group.head_sha || github.ref }}
          # See https://github.com/dorny/paths-filter/issues/56 for the syntax used below
          # Don't run if we only modified packages in the
          # built-in repository or documentation
          filters: |
            bootstrap:
            - 'lib/spack/**'
            - 'share/spack/**'
            - '.github/workflows/bootstrap.yml'
            - '.github/workflows/ci.yaml'
            core:
            - './!(var/**)/**'
            - 'var/spack/test_repos/**'
            packages:
            - 'var/**'
      # Some links for easier reference:
      #
      # "github" context: https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#github-context
      # job outputs: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idoutputs
      # setting environment variables from earlier steps: https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
      #
  bootstrap:
    if: ${{ github.repository == 'spack/spack' && needs.changes.outputs.bootstrap == 'true' }}
    needs: [ prechecks, changes ]
    uses: ./.github/workflows/bootstrap.yml
    secrets: inherit

  unit-tests:
    if: ${{ github.repository == 'spack/spack' && needs.changes.outputs.core == 'true' }}
    needs: [ prechecks, changes ]
    uses: ./.github/workflows/unit_tests.yaml
    secrets: inherit

  prechecks:
    needs: [ changes ]
    uses: ./.github/workflows/prechecks.yml
    secrets: inherit
    with:
      with_coverage: ${{ needs.changes.outputs.core }}
      with_packages: ${{ needs.changes.outputs.packages }}

  import-check:
    needs: [ changes ]
    uses: ./.github/workflows/import-check.yaml

  all-prechecks:
    needs: [ prechecks ]
    if: ${{ always() }}
    runs-on: ubuntu-latest
    steps:
    - name: Success
      run: |
        if [ "${{ needs.prechecks.result }}" == "failure" ] || [ "${{ needs.prechecks.result }}" == "canceled" ]; then
          echo "Unit tests failed."
          exit 1
        else
          exit 0
        fi

  coverage:
    needs: [ unit-tests, prechecks ]
    if: ${{ needs.changes.outputs.core }}
    uses: ./.github/workflows/coverage.yml
    secrets: inherit

  all:
    needs: [ unit-tests, coverage, bootstrap ]
    if: ${{ always() }}
    runs-on: ubuntu-latest
    # See https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/accessing-contextual-information-about-workflow-runs#needs-context
    steps:
    - name: Status summary
      run: |
        if [ "${{ needs.unit-tests.result }}" == "failure" ] || [ "${{ needs.unit-tests.result }}" == "canceled" ]; then
          echo "Unit tests failed."
          exit 1
        elif [ "${{ needs.bootstrap.result }}" == "failure" ] || [ "${{ needs.bootstrap.result }}" == "canceled" ]; then
          echo "Bootstrap tests failed."
          exit 1
        else
          exit 0
        fi
