# This job is the main jobs will dispatch build and test for every modules of our mono repo.
name: Main

on:
  pull_request:
  push:
    branches:
      - 'main'
      - 'release/*'
    tags:
      - 'v[0-9]+.*'
  schedule:
    # Nightly Release @ 3AM after each work day
    - cron: "0 3 * * 2-6"

jobs:
  # This jobs outputs for each modules of our mono-repo if it changed,
  # in order to launch jobs only for the changed modules
  file-change:
    if: ${{ github.event_name != 'schedule' }}
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    outputs:
      compiler: ${{ steps.compiler.outputs.any_changed }}
      optimizer: ${{ steps.optimizer.outputs.any_changed }}
      concrete-cpu: ${{ steps.concrete-cpu.outputs.any_changed }}
      concrete-cpu-api: ${{ steps.concrete-cpu-api.outputs.any_changed }}
      concrete-cuda: ${{ steps.concrete-cuda.outputs.any_changed }}
      concrete-cuda-api: ${{ steps.concrete-cuda-api.outputs.any_changed }}
      concrete-python: ${{ steps.concrete-python.outputs.any_changed }}
      concrete-compiler-cpu-workflow: ${{ steps.concrete-compiler-cpu-workflow.outputs.any_changed }}
      concrete-compiler-gpu-workflow: ${{ steps.concrete-compiler-gpu-workflow.outputs.any_changed }}
      concrete-compiler-format-and-linting-workflow: ${{ steps.concrete-compiler-format-and-linting-workflow.outputs.any_changed }}
      concrete-compiler-macos-workflow: ${{ steps.concrete-compiler-macos-workflow.outputs.any_changed }}
      concrete-compiler-docker-images-workflow: ${{ steps.concrete-compiler-docker-images-workflow.outputs.any_changed }}
      concrete-cpu-workflow: ${{ steps.concrete-cpu-workflow.outputs.any_changed }}
      concrete-cuda-workflow: ${{ steps.concrete-cuda-workflow.outputs.any_changed }}
      concrete-python-workflow: ${{ steps.concrete-python-workflow.outputs.any_changed }}
      concrete-optimizer-workflow: ${{ steps.concrete-optimizer-workflow.outputs.any_changed }}
      push-main: ${{ steps.github.outputs.push-main }}
    steps:
      - name: Checkout the repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
          token: ${{ secrets.CONCRETE_ACTIONS_TOKEN }}

      - name: Get changed files in the concrete-compiler directory
        id: compiler
        uses: tj-actions/changed-files@7a453ffa2eb31a7e84f3281f88ef6d774c4d807d
        with:
          files: compilers/concrete-compiler

      - name: Get changed files for concrete-optimizer
        id: optimizer
        uses: tj-actions/changed-files@7a453ffa2eb31a7e84f3281f88ef6d774c4d807d
        with:
          files: |
            compilers/concrete-optimizer
            .github/workflows/optimizer.yml

      - name: Get changed files in the concrete-cpu directory
        id: concrete-cpu
        uses: tj-actions/changed-files@7a453ffa2eb31a7e84f3281f88ef6d774c4d807d
        with:
          files: backends/concrete-cpu/implementation
      
      - name: Get changed files in the concrete-cuda directory
        id: concrete-cuda
        uses: tj-actions/changed-files@7a453ffa2eb31a7e84f3281f88ef6d774c4d807d
        with:
          files: backends/concrete-cuda

      - name: Get changed files in the concrete-python directory
        id: concrete-python
        uses: tj-actions/changed-files@7a453ffa2eb31a7e84f3281f88ef6d774c4d807d
        with:
          files: frontends/concrete-python

      - name: Check if compiler_build_and_test_cpu workflow has changed
        id: concrete-compiler-cpu-workflow
        uses: tj-actions/changed-files@7a453ffa2eb31a7e84f3281f88ef6d774c4d807d
        with:
          files: ./.github/workflows/compiler_build_and_test_cpu.yml

      - name: Check if compiler_build_and_test_gpu workflow has changed
        id: concrete-compiler-gpu-workflow
        uses: tj-actions/changed-files@7a453ffa2eb31a7e84f3281f88ef6d774c4d807d
        with:
          files: ./.github/workflows/compiler_build_and_test_gpu.yml

      - name: Check if compiler_format_and_linting.yml workflow has changed
        id: concrete-compiler-format-and-linting-workflow
        uses: tj-actions/changed-files@7a453ffa2eb31a7e84f3281f88ef6d774c4d807d
        with:
          files: ./.github/workflows/compiler_format_and_linting.yml

      - name: Check if compiler_macos_build_and_test workflow has changed
        id: concrete-compiler-macos-workflow
        uses: tj-actions/changed-files@7a453ffa2eb31a7e84f3281f88ef6d774c4d807d
        with:
          files: ./.github/workflows/compiler_macos_build_and_test.yml

      - name: Check if compiler_publish_docker_images workflow has changed
        id: concrete-compiler-docker-images-workflow
        uses: tj-actions/changed-files@7a453ffa2eb31a7e84f3281f88ef6d774c4d807d
        with:
          files: ./.github/workflows/compiler_publish_docker_images.yml

      - name: Check if concrete_cpu_test workflow has changed
        id: concrete-cpu-workflow
        uses: tj-actions/changed-files@7a453ffa2eb31a7e84f3281f88ef6d774c4d807d
        with:
          files: ./.github/workflows/concrete_cpu_test.yml

      - name: Check if concrete_cuda_test workflow has changed
        id: concrete-cuda-workflow
        uses: tj-actions/changed-files@7a453ffa2eb31a7e84f3281f88ef6d774c4d807d
        with:
          files: ./.github/workflows/concrete_cuda_test.yml

      - name: Check if concrete_python_checks workflow has changed
        id: concrete-python-workflow
        uses: tj-actions/changed-files@7a453ffa2eb31a7e84f3281f88ef6d774c4d807d
        with:
          files: ./.github/workflows/concrete_python_checks.yml

      - name: Check if optimizer workflow has changed
        id: concrete-optimizer-workflow
        uses: tj-actions/changed-files@7a453ffa2eb31a7e84f3281f88ef6d774c4d807d
        with:
          files: ./.github/workflows/optimizer.yml
      
      - name: Get changed files in the concrete-cpu directory
        id: concrete-cpu-api
        uses: tj-actions/changed-files@7a453ffa2eb31a7e84f3281f88ef6d774c4d807d
        with:
          files: backends/concrete-cpu/implementation/include/
      
      - name: Get changed files in the concrete-cuda directory
        id: concrete-cuda-api
        uses: tj-actions/changed-files@7a453ffa2eb31a7e84f3281f88ef6d774c4d807d
        with:
          files: backends/concrete-cuda/implementation/include/
      
      - name: Set some github event outputs
        id: github
        if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'release'))
        run: echo "push-main=true" >> "$GITHUB_OUTPUT"

#################################################
# Compiler jobs #################################
  compiler-compliance:
    needs: file-change
    if: needs.file-change.outputs.compiler == 'true' || needs.file-change.outputs.concrete-compiler-format-and-linting-workflow == 'true' || needs.file-change.outputs.push-main == 'true' || contains(github.ref, 'refs/tags/')
    uses: ./.github/workflows/compiler_format_and_linting.yml

  compiler-cpu-build:
    needs: file-change
    if: needs.file-change.outputs.compiler == 'true' || needs.file-change.outputs.concrete-cpu-api == 'true'|| needs.file-change.outputs.concrete-compiler-cpu-workflow == 'true' || needs.file-change.outputs.push-main == 'true' || contains(github.ref, 'refs/tags/')
    uses: ./.github/workflows/start_slab.yml
    secrets: inherit
    with:
      command: compiler-cpu-build

  compiler-gpu-build:
    needs: file-change
    if: needs.file-change.outputs.compiler == 'true' || needs.file-change.outputs.concrete-cuda-api == 'true' || needs.file-change.outputs.concrete-compiler-gpu-workflow == 'true' || needs.file-change.outputs.push-main == 'true' || contains(github.ref, 'refs/tags/')
    uses: ./.github/workflows/start_slab.yml
    secrets: inherit
    with:
      command: compiler-gpu-build

  compiler-macos-tests:
    needs: file-change
    if: needs.file-change.outputs.compiler == 'true' || needs.file-change.outputs.concrete-compiler-macos-workflow == 'true' || needs.file-change.outputs.push-main == 'true' || contains(github.ref, 'refs/tags/')
    uses: ./.github/workflows/compiler_macos_build_and_test.yml
    secrets: inherit

  compiler-publish-docker-images:
    needs: file-change
    if: (needs.file-change.outputs.compiler == 'true' || needs.file-change.outputs.concrete-compiler-docker-images-workflow == 'true') && (needs.file-change.outputs.push-main == 'true' || contains(github.ref, 'refs/tags/'))
    uses: ./.github/workflows/start_slab.yml
    secrets: inherit
    with:
      command: compiler-publish-docker-images

  compiler-cpu-benchmark:
    needs: file-change
    if: needs.file-change.outputs.push-main == 'true'
    uses: ./.github/workflows/start_slab.yml
    secrets: inherit
    with:
      command: compiler-cpu-benchmark

  compiler-gpu-benchmark:
    needs: file-change
    if: needs.file-change.outputs.push-main == 'true'
    uses: ./.github/workflows/start_slab.yml
    secrets: inherit
    with:
      command: compiler-gpu-benchmark

#################################################
# Optimizer jobs ################################
  optimizer:
    needs: file-change
    if: |
      needs.file-change.outputs.parameters-curves == 'true' ||
      needs.file-change.outputs.concrete-cpu == 'true' ||
      needs.file-change.outputs.optimizer == 'true'||
      needs.file-change.outputs.push-main
    uses: ./.github/workflows/optimizer.yml
    secrets: inherit

#################################################
# ConcreteCPU jobs ##############################
  concrete-cpu:
    needs: file-change
    if: needs.file-change.outputs.concrete-cpu == 'true' || needs.file-change.outputs.concrete-cpu-workflow == 'true' || needs.file-change.outputs.push-main
    uses: ./.github/workflows/concrete_cpu_test.yml
    secrets: inherit

#################################################
# Concrete-cuda jobs ############################
  concrete-cuda-tests:
    needs: file-change
    if:  needs.file-change.outputs.concrete-cuda == 'true' || needs.file-change.outputs.concrete-cuda-workflow == 'true' || needs.file-change.outputs.push-main
    uses: ./.github/workflows/start_slab.yml
    secrets: inherit
    with:
      command: concrete-cuda-tests

  concrete-cuda-benchmark:
    needs: file-change
    if:  needs.file-change.outputs.concrete-cuda == 'true' || needs.file-change.outputs.concrete-cuda-workflow == 'true' || needs.file-change.outputs.push-main
    uses: ./.github/workflows/start_slab.yml
    secrets: inherit
    with:
      command: concrete-cuda-benchmark

#################################################
# Concrete Python jobs ##########################
  concrete-python:
    needs: file-change
    if: needs.file-change.outputs.concrete-python == 'true' || needs.file-change.outputs.concrete-python-workflow == 'true' || needs.file-change.outputs.push-main || contains(github.ref, 'refs/tags/')
    uses: ./.github/workflows/concrete_python_checks.yml
    secrets: inherit

  concrete-python-tests-linux:
    needs: file-change
    if: needs.file-change.outputs.concrete-python == 'true' || needs.file-change.outputs.push-main || contains(github.ref, 'refs/tags/')
    uses: ./.github/workflows/start_slab.yml
    secrets: inherit
    with:
      command: concrete-python-tests-linux

  concrete-python-tests-macos:
    needs: file-change
    if: needs.file-change.outputs.concrete-python == 'true' || needs.file-change.outputs.push-main || contains(github.ref, 'refs/tags/')
    uses: ./.github/workflows/concrete_python_test_macos.yml
    secrets: inherit

#################################################
# Release jobs #################################
  concrete-python-nightly-release:
    if: ${{ github.event_name == 'schedule' }}
    uses: ./.github/workflows/start_slab.yml
    secrets: inherit
    with:
      command: concrete-python-release
      user_inputs: 'nightly'

  concrete-python-public-release:
#    needs: [compiler-cpu-build, compiler-macos-tests, compiler-publish-docker-images, concrete-python-tests-linux, concrete-python-tests-macos]
    if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
    uses: ./.github/workflows/start_slab.yml
    secrets: inherit
    with:
      command: concrete-python-release
      user_inputs: 'public'

  concrete-python-docker-release:
    needs: [concrete-python-public-release]
    if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
    uses: ./.github/workflows/concrete_python_push_docker_image.yml
    secrets: inherit
    with:
      tag: ${{ github.ref_name }}
