# Taken an adapted from gitlab's example repos:
# https://gitlab.com/gitlab-examples/julia

# An example .gitlab-ci.yml file to test (and optionally report the
# coverage results of) your [Julia][1] packages. Please refer to the
# [documentation][2] for more information about package development in
# Julia.
#
# Here, it is assumed that your Julia package is named
# `MyPackage`. Change it to whatever name you have given to your
# package.
#
# [1]: http://julialang.org/
# [2]: https://docs.julialang.org/en/v1/manual/documentation/index.html

variables:
  JULIALIB_REGISTRY_IMAGE: $CI_REGISTRY/caosdb/src/caosdb-julialib/testenv:$CI_COMMIT_REF_NAME

  JULIAINTTEST_PIPELINE: https://gitlab.indiscale.com/api/v4/projects/120/trigger/pipeline
  JULIAINTTEST_BRANCHES: https://gitlab.indiscale.com/api/v4/projects/120/repository/branches
  GIT_SUBMODULE_STRATEGY: normal

  TRIGGERED_BY_REPO: JULIALIB
  TRIGGERED_BY_REF: $CI_COMMIT_REF_NAME
  TRIGGERED_BY_HASH: $CI_COMMIT_SHORT_SHA

  # The defalt branch to use with caosdb-cpplib
  # TODO: Change back to dev once f-consolidate-c has been merged.
  CPP_DEFAULT_BRANCH: f-consolidate-c

image: $JULIALIB_REGISTRY_IMAGE

stages:
  - info
  - code-style
  - setup
  - test
  - deploy

# Print debugging info
info:
  tags: [cached-dind]
  image: docker:20.10
  stage: info
  needs: []
  script:
    - echo "Pipeline triggered by $TRIGGERED_BY_REPO@$TRIGGERED_BY_REF ($TRIGGERED_BY_HASH)"
    - echo "$JULIALIB_REGISTRY_IMAGE"
    - echo "$JULIAINTTEST_PIPELINE"
    - echo "$JULIAINTTEST_BRANCHES"
    - echo "$GIT_SUBMODULE_STRATEGY"

# Only check style for Julia 1.6 since support for 1.0 may be dropped anyway
code-style:
  stage: code-style
  image: julia:1.6
  tags: [ docker ]
  script:
    # install git
    - apt-get update && apt-get install -y git
    # install JuliaFormatter
    - julia  -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))'
    # try and format the files
    - julia  -e 'using JuliaFormatter; format(".", verbose=true)'
    # check the git diff for possible changes due to the formatting
    - julia -e '
      out = Cmd(`git diff --name-only`) |> read |> String;
      if out == ""
          exit(0);
      else
          @error "Some files have not been formatted !!!";
          write(stdout, out);
          exit(1);
      end'
  allow_failure: true

# Install libcaosdb in docker image
setup:
  tags: [ cached-dind ]
  image: docker:20.10
  stage: setup
  script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker pull $JULIALIB_REGISTRY_IMAGE || true
    - docker build
      --file .docker/Dockerfile
      --pull
      --tag $JULIALIB_REGISTRY_IMAGE .
    - docker push $JULIALIB_REGISTRY_IMAGE

test:
  stage: test
  # Use `docker` runners
  tags: [ docker ]
  # Uncomment below if you would like to run the tests on specific
  # references only, such as the branches `master`, `development`,
  # etc.
  # only:
  #   - master
  #   - development
  script:
    - .docker/install_cpplib.sh
    - export LD_LIBRARY_PATH=/root/.local/lib:$LD_LIBRARY_PATH
    - export CAOSDB_CLIENT_CONFIGURATION=$(pwd)/.docker/caosdb_client.json
    # Let's run the tests. Substitute `coverage = false` below, if you
    # do not want coverage results.
    - julia -e 'using Pkg; Pkg.add(path=pwd());
      Pkg.build("CaosDB");
      Pkg.test("CaosDB"; coverage = true)'
      # Comment out below if you do not want coverage results.
    - julia -e 'using Pkg; Pkg.add("Coverage");
      import CaosDB;
      cd(joinpath(dirname(pathof(CaosDB)), ".."));
      using Coverage; cl, tl = get_summary(process_folder());
      println("(", cl/tl*100, "%) covered")'


# REMARK: Do not forget to enable the coverage feature for your
# project, if you are using code coverage reporting above. This can be
# done by
#
# - Navigating to the `CI/CD Pipelines` settings of your project,
# - Copying and pasting the default `Simplecov` regex example
#   provided, i.e., `\(\d+.\d+\%\) covered` in the `test coverage
#   parsing` textfield.

# Example documentation deployment

# Prepare documentation for deployment but only deploy it when run on
# main branch

.pages_prepare: &pages_prepare
  tags: [ docker ]
  stage: deploy
  script:
    - julia -e 'using Pkg; Pkg.add(path=pwd()); Pkg.build("CaosDB");' # rebuild Julia (can be put somewhere else I'm sure)
    - julia -e 'using Pkg; import CaosDB; Pkg.add("Documenter")' # install Documenter
    - julia --color=yes docs/make.jl # make documentation
    - mv docs/build public # move to the directory picked up by Gitlab pages

pages:
  <<: *pages_prepare
  artifacts:
    paths:
      - public
  only:
    refs:
      - main

test_pages:
  <<: *pages_prepare
  except:
    refs:
      - main

# trigger the integration tests
trigger_inttest:
  tags: [ docker ]
  stage: deploy
  script:
    ## Determine the juliainttest branch...
    # ... use an f-branch if posible...
    - if echo "$CI_COMMIT_REF_NAME" | grep -c "^f-" ; then
        if curl -o /dev/null -s -w "%{http_code}" $JULIAINTTEST_BRANCHES/$CI_COMMIT_REF_NAME | grep "404"; then
          JULIAINT_REF=dev ;
        else
          JULIAINT_REF=$CI_COMMIT_REF_NAME;
        fi
      fi;
    # ... or use main if possible...
    - if [[ "$CI_COMMIT_REF_NAME" == "main" ]] ; then
        JULIAINT_REF=main ;
      fi
    # ... and fall-back to dev
    - JULIAINT_REF=${JULIAINT_REF:-dev}
    - F_BRANCH=$CI_COMMIT_REF_NAME

    - echo "Triggering caosdb-juliainttest@${JULIAINT_REF} (F_BRANCH=$F_BRANCH)"
    - curl -w "%{stderr}HTTPCODE=%{http_code}" -X POST
      -F token=$CI_JOB_TOKEN
      -F "variables[TRIGGERED_BY_REPO]=$TRIGGERED_BY_REPO"
      -F "variables[TRIGGERED_BY_REF]=$TRIGGERED_BY_REF"
      -F "variables[TRIGGERED_BY_HASH]=$TRIGGERED_BY_HASH"
      -F "variables[JULIALIB_REGISTRY_IMAGE]=$JULIALIB_REGISTRY_IMAGE"
      -F "variables[F_BRANCH]=${F_BRANCH}"
      -F ref=${JULIAINT_REF} $JULIAINTTEST_PIPELINE 2>HTTPCODE

    # fail if the request failed
    - grep -c "HTTPCODE=2" HTTPCODE


# WARNING: This template is using the `julia` images from [Docker
# Hub][3]. One can use custom Julia images and/or the official ones
# found in the same place. However, care must be taken to correctly
# locate the binary file (`/opt/julia/bin/julia` above), which is
# usually given on the image's description page.
#
# [3]: https://hub.docker.com/_/julia/