variables:
  POSTGRES_DB: "MaarchParapheur"
  POSTGRES_USER: maarch
  POSTGRES_PASSWORD: ""

stages:
  - sync
  - test
  - deploy
  - after_deploy

before_script:
  - apt-get update -yqq > /dev/null
  - apt-get install cron -yqq > /dev/null
  - bash ci/docker_install_php.sh > /dev/null
  - bash ci/docker_install_database.sh > /dev/null
  - mkdir -p /opt/maarchparapheur/docservers/{documents,attachments,signatures,esigned_documents,original_documents}
  - ln -s $CI_PROJECT_DIR /var/www/html/MaarchParapheur
  - cd /var/www/html/MaarchParapheur
  - > 
    sed 's!"server": .*,!"server": "postgres",!; s!"name": .*,!"name": "MaarchParapheur",!; s!"user": .*,!"user": "maarch",!; s!"password": .*,!"password": "maarch",!;s!"enable": .*,!"enable": true,!' config/config.default.json > config/config.json
  - sed -i 's/rights="none" pattern="PDF"/rights="read | write" pattern="PDF"/' /etc/ImageMagick-6/policy.xml
  - php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
  - php composer-setup.php
  - php -r "unlink('composer-setup.php');"
  - mv composer.phar /usr/local/bin/composer

job_php-8.1:
  image: php:8.1.19-apache
  stage: test
  services:
    - name: postgres:10.1
      command: [ "-c", "datestyle=iso,dmy" ]
  script:
    - composer install
    - curl --location -s --output /usr/local/bin/phpunit https://phar.phpunit.de/phpunit-9.phar
    - chmod +x /usr/local/bin/phpunit
    - phpunit --coverage-text --colors=never
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'

  artifacts:
    paths:
      - test/unitTests/build/
    expire_in: 2h
  # coverage: '^\s*Lines:\s*\d+.\d+\%'

job_php-8.0:
  image: php:8.0-apache
  stage: test
  services:
    - name: postgres:10.1
      command: [ "-c", "datestyle=iso,dmy" ]
  script:
    - composer install
    - curl --location -s --output /usr/local/bin/phpunit https://phar.phpunit.de/phpunit-9.phar
    - chmod +x /usr/local/bin/phpunit
    - phpunit --coverage-text --colors=never
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
  artifacts:
    paths:
      - test/unitTests/build/
    expire_in: 2h
  # coverage: '^\s*Lines:\s*\d+.\d+\%'

job_php-7.4:
  image: php:7.4-apache
  stage: test
  services:
    - name: postgres:10.1
      command: [ "-c", "datestyle=iso,dmy" ]
  script:
    - composer install
    - curl --location -s --output /usr/local/bin/phpunit https://phar.phpunit.de/phpunit-9.phar
    - chmod +x /usr/local/bin/phpunit
    - phpunit --coverage-text --colors=never -c phpunit.xml
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
  artifacts:
    paths:
      - test/unitTests/build/
    expire_in: 2h
  # coverage: '^\s*Lines:\s*\d+.\d+\%'

create_merge_request:
  image: debian:10-slim
  stage: sync
  rules:
    - if: '$CI_COMMIT_BRANCH =~ /(feat|fix)\/[0-9]*/ && $CI_OPEN_MERGE_REQUESTS == null && $CI_COMMIT_BEFORE_SHA == "0000000000000000000000000000000000000000"'
  before_script:
    - apt-get update -yqq > /dev/null
    - apt install -y curl
    - apt install -y jq
  script:
    - chmod +x ./ci/create_mr.sh
    - ./ci/create_mr.sh

end_merge_request:
  image: debian:10-slim
  stage: sync
  rules:
    - if: '$CI_COMMIT_MESSAGE =~ /^Merge branch/ && $CI_PIPELINE_SOURCE != "schedule" && ($CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "2301_releases")'
  before_script:
    - apt-get update -yqq > /dev/null
    - apt install -y curl
    - apt install -y jq
  script:
    - chmod +x ./ci/end_mr.sh
    - ./ci/end_mr.sh

check_branch_releases:
  image: debian:10-slim
  stage: after_deploy
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule" && $TAG_MODE == "minor"'
  before_script:
    - apt-get update -yqq > /dev/null
    - apt install -y curl
    - apt install -y jq
    - apt install -y git
  script:
    - chmod +x ./ci/check_releases.sh
    - ./ci/check_releases.sh

create_minor_tag:
  image: php:8.1.14-cli
  stage: deploy
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule" && $TAG_MODE == "minor"'
  before_script:
    - apt-get update -yqq > /dev/null
    - apt install -y git
    - apt install -y curl
    - apt install -y jq
    - chmod +x ./ci/generate_app_dependencies.sh
    - ./ci/generate_app_dependencies.sh
  script:
    - chmod +x ./ci/new_minor_tag.sh
    - ./ci/new_minor_tag.sh

create_major_tag:
  image: php:8.1.14-cli
  stage: deploy
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule" && $TAG_MODE == "major"'
  before_script:
    - apt-get update -yqq > /dev/null
    - apt install -y git
    - apt install -y curl
    - apt install -y jq
    - chmod +x ./ci/generate_app_dependencies.sh
    - ./ci/generate_app_dependencies.sh
  script:
    - chmod +x ./ci/new_major_tag.sh
    - ./ci/new_major_tag.sh

reset_approvals:
  image: debian:10-slim
  stage: sync
  rules:
    - if: '$CI_OPEN_MERGE_REQUESTS != null && $CI_PIPELINE_SOURCE == "push"'
  before_script:
    - apt-get update -yqq > /dev/null
    - apt install -y curl
  script:
    - MR_ID=`echo $CI_OPEN_MERGE_REQUESTS | grep -oP "!(.)*" | tr -d "!"`
    - |
      curl --header "PRIVATE-TOKEN: $TOKEN_GITLAB" -X PUT "https://labs.maarch.org/api/v4/projects/$CI_PROJECT_ID/merge_requests/$MR_ID/reset_approvals"

