# ### Continuous Integration
#
# Entrypoint for all CI related workflows
#
name: CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

env:
  NODE_OPTIONS: --max_old_space_size=6144

jobs:
  pre_check:
    name: Pre-Check
    runs-on: ubuntu-latest
    outputs:
      # Returns 'true' if the whole workflow is skippable for one of the following reasons:
      # - Exact same files have been successfully checked in older workflow run
      # - Only files that do not require checks ('paths_ignore') have been changed and
      #   workflow run on previous commit has been successful (backtracking)
      should_skip: ${{ steps.skip_check.outputs.should_skip }}
      # Returns information about the defined filter in 'paths_filter'
      paths_result: ${{ steps.skip_check.outputs.paths_result }}
    steps:
      - name: Check for skippable jobs
        id: skip_check
        # Switch back to fkirc/skip-duplicate-actions@master once https://github.com/fkirc/skip-duplicate-actions/pull/159 has been merged
        uses: paescuj/skip-duplicate-actions@path-filters
        # Don't skip any jobs if the pre-check should fail for any reason
        # (setting this on step-level makes sure the pre-check will still be marked as 'passed',
        # better solution pending at https://github.com/actions/toolkit/issues/399)
        continue-on-error: true
        with:
          # Cancel outdated workflow runs
          cancel_others: 'true'
          # Ignore changes in the following files globally
          paths_ignore: '["app/src/lang/translations/*.yaml"]'
          # Paths filter
          paths_filter: |
            e2e_tests:
              paths:
                - 'api/**/*'
                - 'tests/**/*'
                - 'docker-compose.yml'
                - '.github/workflows/{ci,e2e-tests}.yml'
            e2e_blackbox_tests:
              paths:
                - 'api/**/*'
                - 'tests-blackbox/**/*'
                - 'docker-compose.yml'
                - '.github/workflows/{ci,e2e-blackbox-tests}.yml'
            e2e_blackbox_tests_updated:
              paths:
                - 'tests-blackbox/**/*'

  # Workflows are called in every case and need to handle the value of should_skip themselves.
  # This is needed to pass required checks on pull requests.

  lint:
    name: Lint
    needs: pre_check
    uses: ./.github/workflows/lint.yml
    with:
      should_skip: ${{ needs.pre_check.outputs.should_skip }}

  codeql_analysis:
    name: CodeQL Analysis
    needs: pre_check
    uses: ./.github/workflows/codeql-analysis.yml
    with:
      should_skip: ${{ needs.pre_check.outputs.should_skip }}

  unit_tests:
    name: Unit Tests
    needs: pre_check
    uses: ./.github/workflows/unit-tests.yml
    with:
      should_skip: ${{ needs.pre_check.outputs.should_skip }}

  e2e_tests:
    name: End-to-End Tests
    needs: pre_check
    uses: ./.github/workflows/e2e-tests.yml
    with:
      should_skip:
        ${{ needs.pre_check.outputs.should_skip == 'true' ||
        fromJSON(needs.pre_check.outputs.paths_result).e2e_tests.should_skip }}

  e2e_blackbox_tests:
    name: End-to-End Blackbox Tests
    needs: pre_check
    uses: ./.github/workflows/e2e-blackbox-tests.yml
    with:
      should_skip: |
        ${{ (github.event_name == 'pull_request' &&
        fromJSON(needs.pre_check.outputs.paths_result).e2e_blackbox_tests_updated.should_skip) ||
        needs.pre_check.outputs.should_skip == 'true' ||
        fromJSON(needs.pre_check.outputs.paths_result).e2e_blackbox_tests.should_skip }}

  e2e_blackbox_tests_quick:
    name: End-to-End Blackbox Tests (Quick)
    needs: pre_check
    uses: ./.github/workflows/e2e-blackbox-tests-quick.yml
    with:
      should_skip: |
        ${{ (github.event_name == 'pull_request' &&
        !fromJSON(needs.pre_check.outputs.paths_result).e2e_blackbox_tests_updated.should_skip) ||
        github.event_name == 'push' ||
        needs.pre_check.outputs.should_skip == 'true' ||
        fromJSON(needs.pre_check.outputs.paths_result).e2e_blackbox_tests.should_skip }}
