variables:
  # python dependencies
  CONDA_ENVS: "$CI_PROJECT_DIR/.conda_envs"
  CLI_CONDA_ENV: "$CONDA_ENVS/cli.env"
  SCORER_CONDA_ENV: "$CONDA_ENVS/scorer.env"
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.pip"
  # CLIs install paths
  MASTER_CLI: "/tmp/master"
  PERF_CLI: "$CI_PROJECT_DIR"
  # secrets files from nist
  NIST_FILES: "$CI_PROJECT_DIR/.ncache"
  FILE_INDEX: "$NIST_FILES/file-index.json"
  ACTIVITY_INDEX: "$NIST_FILES/activity-index.json"
  CHUNKS_FILE: "$NIST_FILES/chunks-file.json"
  # system cache dir for simulation pipeline
  SYSTEM_CACHE_DIR: "$CI_PROJECT_DIR/.scache"
  # get-system needs
  CLI_URL: "https://gitlab.kitware.com/actev/diva_evaluation_cli.git"

default:
  cache:
    key: "$CI_COMMIT_REF_SLUG"
    paths:
      - "$SYSTEM_CACHE_DIR"
      - "$NIST_FILES"
      - "$PIP_CACHE_DIR"
      - "$CONDA_ENVS"
  image: continuumio/anaconda3


# DEV-MERGE-TEST:
#   This pipeline is used for NIST's development branches. The mergeablity with `development` and `master` branches is tested.
# SETUP:
#   This pipeline will try to install the pushed CLI and merge it with NIST's.
# BASIC-TESTS:
#
# SIMULATION
#
stages:
  - dev-merge-test
  - setup
  - basic-tests
  - simulation

# STAGE DEV-MERGE-TEST
test-merge:
    stage: dev-merge-test
    rules:
      - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH =~ /^dev.*/'
    script:
        - if [[ "$CI_COMMIT_BRANCH" -ne "development" ]]; then git checkout development ; git merge $CI_COMMIT_BRANCH; fi
        - git checkout master && git merge development

# STAGE SETUP
install:
  stage: setup
  rules:
    - if: '$CI_COMMIT_BRANCH =~ /^dev.*/'
      when: never
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH != "master"'
  script:
    # need to pull master cli and install this one into the pulled one
    - python3 -m pip install pycodestyle
    - export PATH=$PATH:~/.local/bin
    - if [[ -f environment.yml ]]; then args="-c $CLI_CONDA_ENV -s $SCORER_CONDA_ENV"; fi
    - export PATH=$PATH:~/.local/bin
    - bash diva_evaluation_cli/bin/install.sh --all $args

# Linting, unit tests, and system validation
test-code:
  stage: basic-tests
  dependencies: [install]
  rules:
    - if: '$CI_COMMIT_BRANCH =~ /^baseline.*/'
      when: never
    - if: '$CI_PIPELINE_SOURCE == "push"'
  script:
    - python3 -m pycodestyle --filename=*.py diva_evaluation_cli/bin/ test/
    - python3 -m unittest discover -s test/
    - if [[ -f environment.yml ]]; then conda activate $CLI_CONDA_ENV; fi
    - actev -h
    - actev validate-system --strict

# making sure baseline and placebo are valid
test-valid-systems:
  stage: basic-tests
  dependencies: [install]
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH =~ /^pipeline_testing_system.*/'
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "baseline_system_development"'
  script:
    - if [[ -f environment.yml ]]; then conda activate $CLI_CONDA_ENV; fi
    - actev -h
    - actev validate-system

# downloading some files for simulation
simulation-setup:
  stage: simulation
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH =~ /^pipeline_testing_system.*/'
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "baseline_system_development"'
  script:
    - echo TODO url ... -o $FILE-INDEX
    - echo TODO curl ... -o $ACTIVITY-INDEX

# testing placebos, baseline updates and performers CLI
# it emulates a few pipeline steps
test-get-system:
  stage: simulation
  #dependencies: [test-valid-systems, simulation-setup]
  dependencies: [test-valid-systems]
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH =~ /^pipeline_testing_system.*/'
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "baseline_system_development"'
  script:
    - git clone https://oauth2:a38YdwbfNrksDQxo5kff@gitlab.kitware.com/actev/diva_evaluation_cli.git --recursive $MASTER_CLI
    - export PATH=$PATH:~/.local/bin
    - bash $MASTER_CLI/diva_evaluation_cli/bin/install.sh --all -c $CLI_CONDA_ENV -s $SCORER_CONDA_ENV
    - actev -h &> /dev/null
    - actev get-system git -u $CLI_URL -l $PERF_CLI -s $CI_COMMIT_BRANCH
    - exit 0
    - rm -rf $MASTER_CLI/diva_evaluation_cli/src
    - ln -s $PERF_CLI/diva_evaluation_cli/src $MASTER_CLI/diva_evaluation_cli/src
    - rm -rf $MASTER_CLI/diva_evaluation_cli/container_output
    - cp -R $PERF_CLI/diva_evaluation_cli/container_output $MASTER_CLI/diva_evaluation_cli/container_output
    - if [[ -f requirements.txt ]]; then python3 -m pip install -r $PERF_CLI/diva_evaluation_cli/requirements.txt; fi
    - if [[ -f environment.yml ]]; then bash $PERF_CLI/diva_evaluation_cli/bin/install.sh --all; fi

test-main-steps:
  stage: simulation
  #dependencies: [test-get-system]
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH =~ /^pipeline_testing_system.*/'
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "baseline_system_development"'
  script:
    - actev experiment-init -f $FILE_INDEX -a $ACTIVITIY_INDEX -c $CHUNKS_FILE -v $VIDEO_LOCATION -s $SYSTEM_CACHE_DIR
    - echo todo next steps
