# You can override the included template(s) by including variable overrides
# SAST customization: https://docs.gitlab.com/ee/user/application_security/sast/#customizing-the-sast-settings
# Secret Detection customization: https://docs.gitlab.com/ee/user/application_security/secret_detection/#customizing-settings
# Dependency Scanning customization: https://docs.gitlab.com/ee/user/application_security/dependency_scanning/#customizing-the-dependency-scanning-settings
# Container Scanning customization: https://docs.gitlab.com/ee/user/application_security/container_scanning/#customizing-the-container-scanning-settings
# Note that environment variables can be set in several places
# See https://docs.gitlab.com/ee/ci/variables/#cicd-variable-precedence

stages:
  - static
  - build
  - test

flawfinder-sast:
  stage: static
  needs: []
  dependencies: []

include:
- template: Security/SAST.gitlab-ci.yml

image: ubuntu:jammy

code_quality:
  stage: static
  allow_failure: true
  needs: []
  dependencies: []
  only:
    - main
    - merge_requests
  tags:
    - docker
  script:
    - DEBIAN_FRONTEND=noninteractive apt update && apt install -y python3 python3-pip cppcheck
    - python3 -m pip install -U cppcheck_codequality
    - cppcheck --xml --enable=warning,style,performance --std=c++14 --suppress=*:*catch.hpp --suppress=*:*loguru.cpp --suppress=*:*json.hpp ./ 2> cppcheck_out.xml
    - cppcheck-codequality --input-file=cppcheck_out.xml --output-file=cppcheck.json
  artifacts:
    reports:
      codequality: cppcheck.json

linux:build:
  only:
    - main
    - merge_requests
  
  stage: build
  tags:
    - docker

  script:
    - DEBIAN_FRONTEND=noninteractive apt update && apt install -y build-essential uuid-dev git libmsgpack-dev liburiparser-dev libgnutls28-dev cmake ninja-build cppcheck
    - mkdir build && cd build
    - cmake $CI_PROJECT_DIR -GNinja -DCMAKE_CXX_FLAGS="-fdiagnostics-color" -DUSE_CPPCHECK=TRUE -DCMAKE_BUILD_TYPE=Release -DCPACK_GENERATOR=DEB
    - ninja

  #cache:
  #  paths:
  #    - build/

  needs: []
  dependencies: []

  artifacts:
    name: ${CI_PROJECT_NAME}-${CI_COMMIT_SHORT_SHA}
    paths:
      - build/
    expire_in: 1 hour

linux:test:
  only:
    - main
    - merge_requests

  stage: test
  tags:
    - docker
  
  needs: ["linux:build"]
  script:
    - DEBIAN_FRONTEND=noninteractive apt update && apt install -y libmsgpackc2 liburiparser1 cmake
    - cd build
    - ctest -V --output-junit report.xml

  artifacts:
    when: always
    reports:
      junit: build/report.xml

linux:valgrind:
  only:
    - main
    - merge_requests

  stage: test
  tags:
    - docker
  
  needs: ["linux:build"]
  script:
    - DEBIAN_FRONTEND=noninteractive apt update && apt install -y libmsgpackc2 liburiparser1 valgrind
    - cd build
    - valgrind --error-exitcode=1 --leak-check=full --show-leak-kinds=all --track-origins=yes ./test/net_integration

  artifacts:
    when: always
    reports:
      junit: build/report.xml

### Windows

windows:build:
  only:
    - main
    - merge_requests

  stage: build
  tags:
    - windows

  needs: []
  dependencies: []

  cache: # use artifacts instead if multiple runners available
    key: $CI_COMMIT_SHORT_SHA
    paths:
      - build/

  script:
      - Import-Module "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/Tools/Microsoft.VisualStudio.DevShell.dll"
      - Enter-VsDevShell -VsInstallPath "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/"
      - cd $CI_PROJECT_DIR
      - if (Test-Path build) { Remove-Item build/ -Recurse }
      - mkdir build
      - cd build
      - cmake -DCMAKE_GENERATOR_PLATFORM=x64 -DWITH_GNUTLS=TRUE -DGNUTLS_INCLUDE_DIR="C:/Build/bin/gnutls/lib/includes/" -DGNUTLS_LIBRARY="C:/Build/bin/gnutls/lib/libgnutls.dll.a" ..
      - '& MSBuild.exe beyond-protocol.sln -property:Configuration=Release -nr:false -maxCpuCount'

windows:test:
  only:
    - main
    - merge_requests
  
  stage: test

  tags:
    - windows
  needs: ["windows:build"]

  cache: # use artifacts instead if multiple runners available
    key: $CI_COMMIT_SHORT_SHA
    paths:
      - build/

  script:
    - $env:PATH+=";C:/Shared/Deploy"
    - cd build
    - ctest -V --output-on-failure --timeout 60