# SPDX-FileCopyrightText: Copyright © DuMux-Course contributors, see AUTHORS.md in root folder
# SPDX-License-Identifier: GPL-3.0-or-later

default:
  image: $IMAGE

stages:
  - solution checker
  - configure
  - build
  - test

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE=="parent_pipeline"

# variables that may be overwritten by the trigger
variables:
  DUMUX_REPO: "https://git.iws.uni-stuttgart.de/dumux-repositories/dumux.git"
  DUMUX_FOLDER: "DUMUX"
  MR_TARGET_BRANCH_NAME: ""
  DUMUX_MR_SOURCE_BRANCH: ""
  DUMUX_MR_TARGET_BRANCH: ""
  DUMUX_TRIGGER_COMMIT_SHA: ""
  DUMUX_TRIGGER_REF_SHA: ""

apply_patches:
  stage: solution checker
  script:
    - sh .patches/apply_patches.sh

select tests:
  stage: configure
  before_script:
    - |
      if [[ -n "$DUMUX_MR_SOURCE_BRANCH" ]]; then
          echo "Fetching upstream branches related to merge request."
          echo "Source branch: $DUMUX_MR_SOURCE_BRANCH"
          echo "Target branch: $DUMUX_MR_TARGET_BRANCH"

          git clone -b ${DUMUX_MR_SOURCE_BRANCH} --depth 1 $DUMUX_REPO $DUMUX_FOLDER
          pushd $DUMUX_FOLDER
              git fetch --depth=1 origin ${DUMUX_MR_TARGET_BRANCH}:${DUMUX_MR_TARGET_BRANCH}
          popd

      else
          git clone -b master --depth 1 $DUMUX_REPO $DUMUX_FOLDER

          pushd $DUMUX_FOLDER
              if [[ -n "$DUMUX_TRIGGER_COMMIT_SHA" ]]; then
                  echo "Fetching upstream commit $DUMUX_TRIGGER_COMMIT_SHA"
                  git fetch --depth=1 origin ${DUMUX_TRIGGER_COMMIT_SHA}
                  git checkout ${DUMUX_TRIGGER_COMMIT_SHA}
              fi

              if [[ -n "$DUMUX_TRIGGER_REF_SHA" ]]; then
                  echo "Fetching upstream reference commit $DUMUX_TRIGGER_REF_SHA"
                  git fetch --depth=1 origin ${DUMUX_TRIGGER_REF_SHA}
              fi
          popd
      fi
    - dunecontrol --opts=$DUNE_OPTS_FILE --only=dumux all
  script:
    - dunecontrol --opts=$DUNE_OPTS_FILE --current all
    - |
      if [[ -n "$DUMUX_MR_SOURCE_BRANCH" ]]; then
          CHECK_FOLDER=$DUMUX_FOLDER
          SOURCE_TREE=$DUMUX_MR_SOURCE_BRANCH
          TARGET_TREE=$DUMUX_MR_TARGET_BRANCH
      elif [[ -n "$DUMUX_TRIGGER_REF_SHA" ]]; then
          CHECK_FOLDER=$DUMUX_FOLDER
          SOURCE_TREE=$DUMUX_TRIGGER_COMMIT_SHA
          TARGET_TREE=$DUMUX_TRIGGER_REF_SHA
      elif [[ -n "$MR_TARGET_BRANCH_NAME" ]]; then
          CHECK_FOLDER="."
          SOURCE_TREE="HEAD"
          TARGET_TREE=origin/$MR_TARGET_BRANCH_NAME
      fi
    - |
      if [[ -n "$SOURCE_TREE" ]]; then
          echo "Detecting tests affected by changes in folder $CHECK_FOLDER,"
          echo "using source/target trees: $SOURCE_TREE / $TARGET_TREE"
          python3 $DUMUX_FOLDER/bin/testing/getchangedfiles.py --folder $CHECK_FOLDER \
                                                               --source-tree $SOURCE_TREE \
                                                               --target-tree $TARGET_TREE \
                                                               --outfile changedfiles.txt
          python3 $DUMUX_FOLDER/bin/testing/findtests.py --outfile affectedtests.json \
                                                         --file-list changedfiles.txt \
                                                         --build-dir build-cmake
      else
          echo "Skipping test selection, build/test stages will consider all tests!"
          touch affectedtests.json
      fi
  artifacts:
    paths:
      - $DUMUX_FOLDER
      - build-cmake
      - affectedtests.json
    expire_in: 3 hours

build course:
  stage: build
  script:
    - |
      pushd build-cmake
        make clean
        if [ -s ../affectedtests.json ]; then
          python3 ../$DUMUX_FOLDER/bin/testing/runselectedtests.py -c ../affectedtests.json -b
        else
          python3 ../$DUMUX_FOLDER/bin/testing/runselectedtests.py --all -b
        fi
      popd
  artifacts:
    paths:
      - build-cmake
      - $DUMUX_FOLDER
      - affectedtests.json
    expire_in: 3 hours

test course:
  stage: test
  variables:
    OMPI_ALLOW_RUN_AS_ROOT: 1
    OMPI_ALLOW_RUN_AS_ROOT_CONFIRM: 1
  script:
    - |
      pushd build-cmake
        if [ -s ../affectedtests.json ]; then
          python3 ../$DUMUX_FOLDER/bin/testing/runselectedtests.py -c ../affectedtests.json -t
        else
          python3 ../$DUMUX_FOLDER/bin/testing/runselectedtests.py --all -t
        fi
      popd
  needs:
    - job: build course
      artifacts: true
  artifacts:
    reports:
      junit: junit/dumux-course-cmake.xml
