# Adapted from https://github.com/pandas-dev/pandas/blob/master/azure-pipelines.yml
schedules:
- cron: "30 2 * * *"
  displayName: Run nightly build
  branches:
    include:
    - main
  always: true

jobs:
- job: git_commit
  displayName: Get Git Commit
  pool:
    vmImage: ubuntu-22.04
  steps:
    - bash: |
        set -ex
        if [[ $BUILD_REASON == "PullRequest" ]]; then
          # By default pull requests use refs/pull/PULL_ID/merge as the source branch
          # which has a "Merge ID into ID" as a commit message. The latest commit
          # message is the second to last commit
          COMMIT_ID=$(echo $BUILD_SOURCEVERSIONMESSAGE | awk '{print $2}')
          message=$(git log $COMMIT_ID -1 --pretty=%B)
        else
          message=$BUILD_SOURCEVERSIONMESSAGE
        fi
        echo "##vso[task.setvariable variable=message;isOutput=true]$message"
      name: commit
      displayName: Get source version message

- job: linting
  dependsOn: [git_commit]
  condition: |
    and(
      succeeded(),
      not(contains(dependencies['git_commit']['outputs']['commit.message'], '[lint skip]')),
      not(contains(dependencies['git_commit']['outputs']['commit.message'], '[ci skip]'))
    )
  displayName: Linting
  pool:
    vmImage: ubuntu-22.04
  steps:
    - task: UsePythonVersion@0
      inputs:
        versionSpec: '3.9'
    - bash: |
        # Include pytest compatibility with mypy
        pip install flake8 pytest mypy==1.3.0 black==23.3 ruff==0.0.272
      displayName: Install linters
    - bash: |
        black --check --diff .
      displayName: Run black
    - bash: |
        ruff check --show-source .
      displayName: Run ruff
    - bash: |
        ./build_tools/azure/linting.sh
      displayName: Run linting
    - bash: |
        mypy imblearn/
      displayName: Run mypy

- template: build_tools/azure/posix.yml
  parameters:
    name: Linux_Nightly
    vmImage: ubuntu-22.04
    dependsOn: [git_commit, linting]
    condition: |
      and(
        succeeded(),
        not(contains(dependencies['git_commit']['outputs']['commit.message'], '[ci skip]')),
        or(eq(variables['Build.Reason'], 'Schedule'),
           contains(dependencies['git_commit']['outputs']['commit.message'], '[scipy-dev]'
          )
        )
      )
    matrix:
      pylatest_pip_scipy_dev:
        DISTRIB: 'conda-pip-scipy-dev'
        PYTHON_VERSION: '*'
        CHECK_WARNINGS: 'true'
        TEST_DOCS: 'true'
        TEST_DOCSTRINGS: 'true'

# Will run all the time regardless of linting outcome.
- template: build_tools/azure/posix.yml
  parameters:
    name: Linux_Runs
    vmImage: ubuntu-22.04
    dependsOn: [git_commit]
    condition: |
      and(
        succeeded(),
        not(contains(dependencies['git_commit']['outputs']['commit.message'], '[ci skip]'))
      )
    matrix:
      pylatest_conda_forge_mkl:
        DISTRIB: 'conda'
        CONDA_CHANNEL: 'conda-forge'
        PYTHON_VERSION: '*'
        BLAS: 'mkl'
        COVERAGE: 'true'
        SHOW_SHORT_SUMMARY: 'true'

# Check compilation with Ubuntu bionic 18.04 LTS and scipy from conda-forge
- template: build_tools/azure/posix.yml
  parameters:
    name: Ubuntu_Jammy_Jellyfish
    vmImage: ubuntu-22.04
    dependsOn: [git_commit, linting]
    condition: |
      and(
        succeeded(),
        not(contains(dependencies['git_commit']['outputs']['commit.message'], '[ci skip]')),
        ne(variables['Build.Reason'], 'Schedule')
      )
    matrix:
      py39_conda_forge_openblas_ubuntu_1804:
        DISTRIB: 'conda'
        CONDA_CHANNEL: 'conda-forge'
        PYTHON_VERSION: '3.9'
        BLAS: 'openblas'
        COVERAGE: 'false'

- template: build_tools/azure/posix.yml
  parameters:
    name: Linux
    vmImage: ubuntu-22.04
    dependsOn: [linting, git_commit]
    condition: |
      and(
        succeeded(),
        not(contains(dependencies['git_commit']['outputs']['commit.message'], '[ci skip]')),
        ne(variables['Build.Reason'], 'Schedule')
      )
    matrix:
      # Linux environment to test that scikit-learn can be built against
      # versions of numpy, scipy with ATLAS that comes with Ubuntu Focal 20.04
      # i.e. numpy 1.17.4 and scipy 1.3.3
      ubuntu_atlas:
        DISTRIB: 'ubuntu'
        JOBLIB_VERSION: 'min'
        PANDAS_VERSION: 'none'
        THREADPOOLCTL_VERSION: 'min'
        COVERAGE: 'false'
      # Linux + Python 3.8 build with OpenBLAS and without SITE_JOBLIB
      py38_conda_conda_forge_openblas:
        DISTRIB: 'conda'
        CONDA_CHANNEL: 'conda-forge'
        PYTHON_VERSION: '3.8'
        BLAS: 'openblas'
        NUMPY_VERSION: '1.21.0'  # we cannot get an older version of the dependencies resolution
        SCIPY_VERSION: 'min'
        SKLEARN_VERSION: 'min'
        MATPLOTLIB_VERSION: 'none'
        PANDAS_VERSION: 'none'
        THREADPOOLCTL_VERSION: '2.2.0'
      # Linux environment to test the latest available dependencies and MKL.
      pylatest_pip_openblas_pandas:
        DISTRIB: 'conda-pip-latest'
        PYTHON_VERSION: '*'
        TEST_DOCS: 'true'
        TEST_DOCSTRINGS: 'true'
        CHECK_WARNINGS: 'true'
      # Test the intermediate version of scikit-learn
      pylatest_pip_openblas_sklearn_intermediate:
        DISTRIB: 'conda-pip-latest'
        PYTHON_VERSION: '3.10'
        TEST_DOCS: 'true'
        TEST_DOCSTRINGS: 'true'
        CHECK_WARNINGS: 'false'
        SKLEARN_VERSION: '1.1.3'
      pylatest_pip_openblas_sklearn_intermediate_bis:
        DISTRIB: 'conda-pip-latest'
        PYTHON_VERSION: '3.10'
        TEST_DOCS: 'true'
        TEST_DOCSTRINGS: 'true'
        CHECK_WARNINGS: 'false'
        SKLEARN_VERSION: '1.2.2'
      pylatest_pip_tensorflow:
        DISTRIB: 'conda-pip-latest-tensorflow'
        CONDA_CHANNEL: 'conda-forge'
        PYTHON_VERSION: '3.9'
        TEST_DOCS: 'true'
        TEST_DOCSTRINGS: 'true'
        CHECK_WARNINGS: 'true'
        COVERAGE: 'true'
      pylatest_conda_tensorflow:
        DISTRIB: 'conda-latest-tensorflow'
        CONDA_CHANNEL: 'conda-forge'
        PYTHON_VERSION: '3.9'
        TEST_DOCS: 'true'
        TEST_DOCSTRINGS: 'true'
        CHECK_WARNINGS: 'true'
      conda_tensorflow_minimum:
        DISTRIB: 'conda-minimum-tensorflow'
        CONDA_CHANNEL: 'conda-forge'
        PYTHON_VERSION: '3.8'
        NUMPY_VERSION: '1.19.5'  # This version is the minimum requrired by tensorflow
        SCIPY_VERSION: 'min'
        SKLEARN_VERSION: 'min'
        TENSORFLOW_VERSION: 'min'
        TEST_DOCS: 'true'
        TEST_DOCSTRINGS: 'false'  # it is going to fail because of scikit-learn inheritance
        CHECK_WARNINGS: 'false'  # in case the older version raise some FutureWarnings
      pylatest_pip_keras:
        DISTRIB: 'conda-pip-latest-keras'
        CONDA_CHANNEL: 'conda-forge'
        PYTHON_VERSION: '3.9'
        TEST_DOCS: 'true'
        TEST_DOCSTRINGS: 'true'
        CHECK_WARNINGS: 'true'
        COVERAGE: 'true'
      pylatest_conda_keras:
        DISTRIB: 'conda-latest-keras'
        CONDA_CHANNEL: 'conda-forge'
        PYTHON_VERSION: '3.9'
        TEST_DOCS: 'true'
        TEST_DOCSTRINGS: 'true'
        CHECK_WARNINGS: 'true'
      conda_keras_minimum:
        DISTRIB: 'conda-minimum-keras'
        CONDA_CHANNEL: 'conda-forge'
        PYTHON_VERSION: '3.8'
        NUMPY_VERSION: '1.19.5'  # This version is the minimum requrired by tensorflow
        SCIPY_VERSION: 'min'
        SKLEARN_VERSION: 'min'
        KERAS_VERSION: 'min'
        TEST_DOCS: 'true'
        TEST_DOCSTRINGS: 'false'  # it is going to fail because of scikit-learn inheritance
        CHECK_WARNINGS: 'false'  # in case the older version raise some FutureWarnings

# Currently runs on Python 3.8 while only Python 3.7 available
# - template: build_tools/azure/posix-docker.yml
#   parameters:
#     name: Linux_Docker
#     vmImage: ubuntu-20.04
#     dependsOn: [linting, git_commit]
#     condition: |
#       and(
#         succeeded(),
#         not(contains(dependencies['git_commit']['outputs']['commit.message'], '[ci skip]')),
#         ne(variables['Build.Reason'], 'Schedule')
#       )
#     matrix:
#       debian_atlas_32bit:
#         DISTRIB: 'debian-32'
#         DOCKER_CONTAINER: 'i386/debian:10.9'
#         JOBLIB_VERSION: 'min'
#         # disable pytest xdist due to unknown bug with 32-bit container
#         PYTEST_XDIST_VERSION: 'none'
#         PYTEST_VERSION: 'min'
#         THREADPOOLCTL_VERSION: '2.2.0'

- template: build_tools/azure/posix.yml
  parameters:
    name: macOS
    vmImage: macOS-11
    dependsOn: [linting, git_commit]
    condition: |
      and(
        succeeded(),
        not(contains(dependencies['git_commit']['outputs']['commit.message'], '[ci skip]')),
        ne(variables['Build.Reason'], 'Schedule')
      )
    matrix:
      pylatest_conda_forge_mkl:
        DISTRIB: 'conda'
        BLAS: 'mkl'
        CONDA_CHANNEL: 'conda-forge'
        CPU_COUNT: '3'
        TEST_DOCS: 'true'
      # TODO: re-enable when we find out why MKL on defaults segfaults
      # It seems that scikit-learn from defaults channel is built with LLVM/CLANG OMP
      # while we use MKL OMP. This could be the cause of the segfaults.
      # pylatest_conda_mkl_no_openmp:
      #   DISTRIB: 'conda'
      #   BLAS: 'mkl'
      #   SKLEARN_SKIP_OPENMP_TEST: 'true'
      #   CPU_COUNT: '3'
      #   TEST_DOCS: 'true'
      conda_conda_forge_openblas:
        DISTRIB: 'conda'
        CONDA_CHANNEL: 'conda-forge'
        BLAS: 'openblas'
        TEST_DOCS: 'true'
        CPU_COUNT: '3'

- template: build_tools/azure/windows.yml
  parameters:
    name: Windows
    vmImage: windows-latest
    dependsOn: [linting, git_commit]
    condition: |
      and(
        succeeded(),
        not(contains(dependencies['git_commit']['outputs']['commit.message'], '[ci skip]')),
        ne(variables['Build.Reason'], 'Schedule')
      )
    matrix:
      py38_conda_forge_mkl:
        DISTRIB: 'conda'
        CONDA_CHANNEL: 'conda-forge'
        PYTHON_VERSION: '3.10'
        CHECK_WARNINGS: 'true'
        PYTHON_ARCH: '64'
        PYTEST_VERSION: '*'
        COVERAGE: 'true'
