stages:
  - code_style
  - tests
  - update

variables:
  PYTHON_12_IMAGE: "docker.io/library/python:3.12-slim"
  PYTHON_13_IMAGE: "docker.io/library/python:3.13-slim"
  PYTHON_14_IMAGE: "docker.io/library/python:3.14-slim"
  ACCESS_TOKEN_NAME: "gitlab-ci-token"

check_coding_style:
  stage: code_style
  image: $PYTHON_12_IMAGE
  tags:
    - docker-wapiti
  rules:
    - if: |
        $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH ||
        $CI_PIPELINE_SOURCE == "merge_request_event"
      changes:
        - '**/*.py'
  before_script:
    - python -m pip install --upgrade pip
    - pip install -e .
    - pip install "pylint==3.3.6"
  script:
    - pylint --rcfile=.pylintrc wapitiCore

test-python:
  stage: tests
  tags:
    - docker-wapiti
  rules:
    - if: |
        $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH ||
        $CI_PIPELINE_SOURCE == "merge_request_event"
  parallel:
    matrix:
      - PYTHON_IMAGE: [$PYTHON_12_IMAGE, $PYTHON_13_IMAGE, $PYTHON_14_IMAGE]
  image: $PYTHON_IMAGE
  before_script:
    - python -c "import sys; print(sys.version)"
    - apt-get update
    - apt-get install php-cli php-xml sslscan curl bzip2 libdbus-glib-1-2 libasound2 libxt6 libxtst6 -y --no-install-recommends
    - python -m pip install --upgrade pip
    - pip3 install .[test] && playwright install firefox --with-deps
  script:
    - pytest --cov --cov-report term --cov-report xml:coverage.xml
  coverage: '/(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/'
  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml

test-build-docker:
  stage: tests
  tags:
    - shell-wapiti
  rules:
    - if: |
        $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH ||
        $CI_PIPELINE_SOURCE == "merge_request_event"
  script:
    - docker build .

update_hash :
    stage: update
    tags:
      - docker-wapiti
    rules:
        - if: '$UPDATE_HASH == "true"'
    image: $IMAGE_UPDATE_HASH
    before_script:
        - apt update && apt install -y git curl grep
    script:
        - git clone https://$ACCESS_TOKEN_NAME:$ACCESS_TOKEN@$PROJECT_URL/wapiti.git
        - cd ./wapiti/wapitiCore/data/attacks/
        - BRANCH_NAME=cms_update_`date +'%Y_%m_%d'`
        - git switch -c $BRANCH_NAME
        - curl -Lo "collected_data.db" https://github.com/Cyberwatch/HashThePlanet/releases/download/latest/hashtheplanet.db
        - python3 update_hash_files.py --db ./collected_data.db --source-file ./src_cms_files/
        - git add ./*.json
        - |
            if git diff --cached --quiet
            then
                exit 0
            else
                git config --global user.email "$COMMITER_MAIL"
                git config --global user.name "$COMMITER_NAME"
                git commit -m "AUTO-COMMIT: Update CMS hash `date +'%Y-%m-%d'`"
                git push --set-upstream origin $BRANCH_NAME
                curl -X POST --header "Private-Token: $ACCESS_TOKEN" \
                "https://$GITLAB_URL/api/v4/projects/$CI_PROJECT_ID/merge_requests" \
                --data "source_branch=$BRANCH_NAME" \
                --data "target_branch=master" \
                --data "title=Update hash files for CMS module" \
                --data "description=This MR is generated automatically by the CI JOB "update_hash". It allows us to update hash files for CMS module."\
                --data "remove_source_branch=true"
            fi
