# (C) Copyright IBM 2024.
#
# This code is part of Qiskit.
#
# This code is licensed under the Apache License, Version 2.0 with LLVM
# Exceptions. You may obtain a copy of this license in the LICENSE.txt
# file in the root directory of this source tree.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
name: Continuous Integration
on: [push, pull_request]
jobs:
  Build:
    runs-on: ubuntu-latest
    env:
      CONAN_USER_HOME: ${{ github.workspace }}
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.10.9'
      - name: Install pip packages
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements-dev.txt
      - name: Static checks
        id: static_checks
        if: github.event_name == 'pull_request'
        run: pre-commit run --all-files --show-diff-on-failure
      - name: Load Conan cache
        id: cache
        uses: actions/cache/restore@v3
        with:
          path: .conan
          key: conan-${{ runner.os }}
          restore-keys: conan-${{ runner.os }}
      - name: Create Conan default profile
        run: |
          conan profile new default --detect || true
          conan profile update settings.compiler.libcxx=libstdc++11 default
      # Check if all conan packages are within the cache. If not
      # we will need to build packages (and if on main flush the cache)
      - name : Check Conan dependencies are cached
        id: check_conan_cache
        run: |
          export CONAN_LLVM_GIT_CACHE="${{ runner.temp }}/llvm-project"
          conan info . -pr:h default -pr:b default --build=outdated --json build_requires.json
          build_requires=$(cat build_requires.json)
          if [[ "$build_requires" == *"[]"* ]];
          then
              all_in_cache=true
          else
              all_in_cache=false
          fi
          echo "all_in_cache=$all_in_cache" >> $GITHUB_OUTPUT
          echo "Conan build requires: $build_requires"
          echo "Conan cache is complete: $all_in_cache"
      # If we have a cache miss on 'main', clear the cache.
      # A dependency was updated, so we need to drop the old one
      # to prevent unbounded cache growth over time.
      - name : Clear Conan cache
        if: github.event_name == 'push' && github.ref == 'refs/heads/main' && steps.check_conan_cache.outputs.all_in_cache != 'true'
        run: |
          rm -rf ./.conan
          # Re-add conan deps after flushing the conan cache before building packages.
          conan profile new default --detect || true
          conan profile update settings.compiler.libcxx=libstdc++11 default
      # Installs dependencies, builds, tests and packages
      - name: Create Conan Package
        id: conan_create
        run: |
          conan create . openqasm/stable --build=outdated -pr:h default -pr:b default
      # On 'main' branch, always save the cache if Conan install succeeded.
      # Note: we only update the cache from 'main' to avoid "cache thrashing", which would result in the 'main'
      #       cache getting LRU-evicted for every PR, since a single run uses most of the 10GB repo limit.
      - uses: actions/cache/save@v3
        if: github.event_name == 'push' && github.ref == 'refs/heads/main' && steps.conan_install.outcome == 'success' && steps.cache.outputs.cache-hit != 'true'
        with:
          path: .conan
          key: conan-${{ runner.os }}-${{ hashFiles('conandata.yml', 'conanfile.py') }}
