stages:
  - test
  - doc
  - build
  - build_check

before_script:
  - echo 'sudo --preserve-env=PYTHONPATH -u www-data python3-coverage run -a --source se,sosse ./sosse/sosse_admin.py "$@"' > /tmp/sudo_sosse
  - chmod 755 /tmp/sudo_sosse
  - test -z "$APT_PROXY" || (echo "Acquire::http::Proxy \"$APT_PROXY\";" > /etc/apt/apt.conf.d/proxy.conf)

unit_tests:
  image: biolds/sosse:debian-test
  stage: test
  coverage: '/(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/'
  artifacts:
    paths:
      - se/migrations/*.py
      - htmlcov/*
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml
  script:
    - 'grep ^Depends: debian/control | sed -e "s/.*},//" -e "s/,//g" | xargs apt install -y'
    - /etc/init.d/postgresql start
    - mkdir -p /var/lib/sosse/screenshots /var/lib/sosse/html /var/lib/sosse/downloads/0 && chown -R www-data:www-data /var/lib/sosse/
    - /usr/bin/python3 /root/httpbin/httpbin/manage.py runserver 0.0.0.0:8000 &
    - export PYTHONPATH="$CI_PROJECT_DIR"
    - /tmp/sudo_sosse default_conf | sed -e "s/^#debug=.*/debug=true/" -e "s/^#browser_options=\(.*\)/browser_options=\1 --no-sandbox --disable-dev-shm-usage/" -e "s/#dl_check_time=.*/dl_check_time=1/" > /etc/sosse/sosse.conf
    - /tmp/sudo_sosse test -v3 --failfast
    - /tmp/sudo_sosse load_se tests/opensearch.xml
    - /tmp/sudo_sosse update_se
    - /tmp/sudo_sosse shell -c 'from django.contrib.auth.models import User; User.objects.all().delete()'
    - /tmp/sudo_sosse default_admin
    - /tmp/sudo_sosse generate_secret
    - /tmp/sudo_sosse extract_doc cli > /dev/null
    - /tmp/sudo_sosse extract_doc conf > /dev/null
    - /tmp/sudo_sosse extract_doc se > /dev/null
    - python3 ./sosse/sosse_admin.py makemigrations
    - python3-coverage report
    - python3-coverage html
    - python3-coverage xml

functional_tests:
  image: biolds/sosse:pip-test
  stage: test
  needs:
    - doc_code_extract
  artifacts:
    when: always
    paths:
      - tests/robotframework/screenshots/*
      - tests/robotframework/*.html
      - log/*
  script:
    - make _pip_functional_tests
    - (make _rf_functional_tests ; echo -n $? > /tmp/ret_code) || true
    - mv /var/log/sosse log
    - test "$(cat /tmp/ret_code)" -eq 0

migrations:
  image: biolds/sosse:debian-test
  stage: test
  script:
    - 'grep ^Depends: debian/control | sed -e "s/.*},//" -e "s/,//g" | xargs apt install -y'
    - /etc/init.d/postgresql start
    - export PYTHONPATH="$CI_PROJECT_DIR"
    - /tmp/sudo_sosse default_conf | sed -e "s/^#db_pass=.*/db_pass=sosse/" -e "s/^#debug=.*/debug=true/" > /etc/sosse/sosse.conf
    - MIGRATION_COUNT="$(ls se/migrations/*.py|grep '/[0-9]'|wc -l)"
    - python3 ./sosse/sosse_admin.py makemigrations
    - MIGRATION_COUNT2="$(ls se/migrations/*.py|grep '/[0-9]'|wc -l)"
    - test "$MIGRATION_COUNT" -eq "$MIGRATION_COUNT2"

static_checks:
  image: biolds/sosse:debian-test
  stage: test
  script:
    - make static_checks

doc_gen:
  image: biolds/sosse:debian-test
  stage: test
  artifacts:
    paths:
      - doc/source/*_generated.rst
      - doc/source/*/*_generated.rst
      - doc/source/CHANGELOG.md
  script:
    - make doc_gen

doc_code_extract:
  image: biolds/sosse:doc
  stage: test
  needs:
    - doc_gen
  artifacts:
    paths:
      - doc/code_blocks.json
  script:
    - sed -e 's/|[^|]*| //g' -e 's/^.. \(image\|figure\)::.*//' -i $(find doc -name \*.rst)
    - make _build_doc

doc:
  image: biolds/sosse:doc
  stage: doc
  artifacts:
    paths:
      - doc/build/*
  needs:
    - doc_gen
    - functional_tests
  script:
    - make _build_doc
    - test -n "$RTD_TOKEN" && curl -X POST -d "branches=main" -d "token=$RTD_TOKEN" -d "default_branch=main" https://readthedocs.org/api/v2/webhook/sosse/236935/ || true

pip_pkg:
  image: biolds/sosse:pip-test
  artifacts:
    paths:
      - dist/*
  stage: build
  needs:
    - doc
    - migrations
    - unit_tests
    - functional_tests
    - static_checks
  script:
    - sed -e "s/^SOSSE_VERSION_TAG = .*/SOSSE_VERSION_TAG = '${CI_COMMIT_TAG/v/}'/" -i sosse/settings.py
    - sed -e "s/^SOSSE_VERSION_COMMIT = .*/SOSSE_VERSION_COMMIT = '${CI_COMMIT_SHORT_SHA}'/" -i sosse/settings.py
    - make _pip_pkg

debian_pkg:
  image: biolds/sosse:debian-pkg
  #image: registry.gitlab.com/biolds1/sosse/debian-pkg:latest
  stage: build
  needs:
    - doc
    - migrations
    - unit_tests
    - functional_tests
    - static_checks
  artifacts:
    paths:
      - deb/*.deb
  script:
    - cd "$CI_PROJECT_DIR"
    - rm -rf htmlcov
    - sed -e "s/^SOSSE_VERSION_TAG = .*/SOSSE_VERSION_TAG = '${CI_COMMIT_TAG/v/}'/" -i sosse/settings.py
    - sed -e "s/^SOSSE_VERSION_COMMIT = .*/SOSSE_VERSION_COMMIT = '${CI_COMMIT_SHORT_SHA}'/" -i sosse/settings.py
    - mkdir /deb
    - make _deb
    - mv /deb .

changelog:
  image: biolds/sosse:debian-test
  stage: build
  needs: []
  artifacts:
    paths:
      - CHANGELOG.md
  variables:
    GIT_DEPTH: 0 # disable sahllow cloning since the step requires all tags
  script:
    - 'last_ver="$(git tag -l | sort -V | tail -n 1 | sed -e "s/^v//")"'
    - 'new_ver="${last_ver%.*}.$((${last_ver/*./} + 1))"'
    - echo "$last_ver / $new_ver"
    - 'curl -X GET -H "PRIVATE-TOKEN: $GITLAB_REST_API_CHANGELOG" "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/repository/changelog?version=$new_ver" | jq -r .notes | tee /tmp/new'
    - cat /tmp/new CHANGELOG.md > new.md && mv new.md CHANGELOG.md

doc_test_debian:
  image: debian:bookworm
  stage: build_check
  needs:
    - doc_code_extract
  script:
    - apt update
    - apt install -y make jq
    - make _doc_test_debian

doc_test_pip:
  image: debian:bookworm
  stage: build_check
  needs:
    - doc_code_extract
  script:
    - apt update
    - apt install -y make jq
    - make _doc_test_pip

debian_pkg_check:
  image: debian:bookworm
  stage: build_check
  artifacts:
    when: always
    paths:
      - tests/robotframework/screenshots/*
      - tests/robotframework/*.html
      - log/*
  needs:
    - doc_code_extract
    - debian_pkg
  script:
    - apt update
    - apt install -y jq make virtualenv
    - make _deb_pkg_functional_tests
    - (make _rf_functional_tests ; echo -n $? > /tmp/ret_code) || true
    - mv /var/log/sosse log
    - test "$(cat /tmp/ret_code)" -eq 0

pip_pkg_check:
  image: biolds/sosse:pip-test
  stage: build_check
  artifacts:
    when: always
    paths:
      - tests/robotframework/screenshots/*
      - tests/robotframework/*.html
      - log/*
  needs:
    - doc_code_extract
    - pip_pkg
  script:
    - make _pip_pkg_functional_tests
    - (make _rf_functional_tests ; echo -n $? > /tmp/ret_code) || true
    - mv /var/log/sosse log
    - test "$(cat /tmp/ret_code)" -eq 0
