parameters:
  name: ''
  vmImage: ''
  matrix: []
  dependsOn: []
  condition: ''

jobs:
- job: ${{ parameters.name }}
  dependsOn: ${{ parameters.dependsOn }}
  condition: ${{ parameters.condition }}
  timeoutInMinutes: 120
  pool:
    vmImage: ${{ parameters.vmImage }}
  variables:
    TEST_DIR: '$(Agent.WorkFolder)/tmp_folder'
    VIRTUALENV: 'testvenv'
    JUNITXML: 'test-data.xml'
    XLEARN_SKIP_NETWORK_TESTS: '1'
    CCACHE_DIR: $(Pipeline.Workspace)/ccache
    CCACHE_COMPRESS: '1'
    PYTEST_XDIST_VERSION: 'latest'
    COVERAGE: 'true'
    CREATE_ISSUE_ON_TRACKER: 'true'
  strategy:
    matrix:
      ${{ insert }}: ${{ parameters.matrix }}

  steps:
    - task: UsePythonVersion@0
      inputs:
        versionSpec: '3.9'
        addToPath: false
      name: pyTools
      displayName: Select python version to run CI python scripts
    - bash: $(pyTools.pythonLocation)/bin/python build_tools/azure/get_selected_tests.py
      displayName: Check selected tests for all random seeds
      condition: eq(variables['Build.Reason'], 'PullRequest')
    - bash: build_tools/azure/install_setup_conda.sh
      displayName: Install conda if necessary and set it up
      condition: startsWith(variables['DISTRIB'], 'conda')
    - task: Cache@2
      inputs:
        key: '"ccache-v1" | "$(Agent.JobName)" | "$(Build.BuildNumber)"'
        restoreKeys: |
          "ccache-v1" | "$(Agent.JobName)"
        path: $(CCACHE_DIR)
      displayName: ccache
      continueOnError: true
    - script: |
        build_tools/azure/install.sh
      displayName: 'Install'
    - script: |
        build_tools/azure/test_script.sh
      displayName: 'Test Library'
    - script: |
        build_tools/azure/test_docs.sh
      displayName: 'Test Docs'
      condition: and(succeeded(), eq(variables['SELECTED_TESTS'], ''))
    - script: |
        build_tools/azure/test_pytest_soft_dependency.sh
      displayName: 'Test Soft Dependency'
      condition: and(succeeded(),
                     eq(variables['CHECK_PYTEST_SOFT_DEPENDENCY'], 'true'),
                     eq(variables['SELECTED_TESTS'], ''))
    - script: |
        build_tools/azure/combine_coverage_reports.sh
      condition: and(succeeded(), eq(variables['COVERAGE'], 'true'),
                     eq(variables['SELECTED_TESTS'], ''))
      displayName: 'Combine coverage'
    - task: PublishTestResults@2
      inputs:
        testResultsFiles: '$(TEST_DIR)/$(JUNITXML)'
        testRunTitle: ${{ format('{0}-$(Agent.JobName)', parameters.name) }}
      displayName: 'Publish Test Results'
      condition: succeededOrFailed()
    - bash: |
        set -ex
        if [[ $(BOT_GITHUB_TOKEN) == "" ]]; then
          echo "GitHub Token is not set. Issue tracker will not be updated."
          exit
        fi

        LINK_TO_RUN="https://dev.azure.com/$BUILD_REPOSITORY_NAME/_build/results?buildId=$BUILD_BUILDID&view=logs&j=$SYSTEM_JOBID"
        CI_NAME="$SYSTEM_JOBIDENTIFIER"
        ISSUE_REPO="$BUILD_REPOSITORY_NAME"

        $(pyTools.pythonLocation)/bin/pip install defusedxml PyGithub
        $(pyTools.pythonLocation)/bin/python maint_tools/update_tracking_issue.py \
          $(BOT_GITHUB_TOKEN) \
          $CI_NAME \
          $ISSUE_REPO \
          $LINK_TO_RUN \
          --junit-file $JUNIT_FILE \
          --auto-close false
      displayName: 'Update issue tracker'
      env:
        JUNIT_FILE: $(TEST_DIR)/$(JUNITXML)
      condition: and(succeededOrFailed(), eq(variables['CREATE_ISSUE_ON_TRACKER'], 'true'),
                     eq(variables['Build.Reason'], 'Schedule'))
    - script: |
        build_tools/azure/upload_codecov.sh
      condition: and(succeeded(), eq(variables['COVERAGE'], 'true'),
                     eq(variables['SELECTED_TESTS'], ''))
      displayName: 'Upload To Codecov'
      retryCountOnTaskFailure: 5
      env:
        CODECOV_TOKEN: $(CODECOV_TOKEN)
        JUNIT_FILE: $(TEST_DIR)/$(JUNITXML)
