################
# GitLab template for the s3fs module.
# Utilizes the Drupal gitlab_templates base
#
# Additional features:
#   * PHPStan analysis.
#   * PCOV Code Coverage reporting.
################

include:
  ################
  # DrupalCI includes:
  # As long as you include this, any future includes added by the Drupal Association will be accessible to your pipelines automatically.
  # View these include files at https://git.drupalcode.org/project/gitlab_templates/
  ################
  - project: $_GITLAB_TEMPLATES_REPO
    ref: $_GITLAB_TEMPLATES_REF
    file:
      - '/includes/include.drupalci.main.yml'
      - '/includes/include.drupalci.variables.yml'
      - '/includes/include.drupalci.workflows.yml'

################
# Pipeline configuration variables
#
# Docs at https://git.drupalcode.org/project/gitlab_templates/-/blob/1.0.x/includes/include.drupalci.variables.yml
################
variables:
  _TARGET_CORE:
    value: "$CORE_STABLE"
  _TARGET_PHP:
    value: "$CORE_PHP_MAX"
  # Collect code coverage data.
  _PHPUNIT_EXTRA:
    value: --coverage-cobertura $CI_PROJECT_DIR/coverage.xml --coverage-filter $CI_PROJECT_DIR/$_WEB_ROOT/modules/custom/ --coverage-text --colors=never

################
# Customize composer stage to not symlink the module as it interferes with
# the PCOV extension tracking coverage.
#
# During archive assets stage trying to overwrite symlinks with actual
# files/directives causes errors so we must perform these actions
# during the initial script phase instead of during after_script.
################
.composer-local:
  extends: .composer-base
  script:
    # Run the normal commands first.
    - !reference [.composer-base, script]
    # Remove the module directory and all the symlinks in it.
    - rm -Rf web/modules/custom/$CI_PROJECT_NAME
    # Duplicate the code into the modules folder without using symlinks.
    - git clone ./ web/modules/custom/$CI_PROJECT_NAME
    # Remove the build root copy to avoid duplicate classnames.
    - rm -Rf src tests modules

composer:
  extends: .composer-local

################
# Ensure baseline inclues ALL entries not just new.
################
phpstan:
  after_script:
    - echo '' > web/modules/custom/$CI_PROJECT_NAME/phpstan-baseline.neon
    - vendor/bin/phpstan analyse -c web/modules/custom/$CI_PROJECT_NAME/phpstan.neon --no-progress --generate-baseline phpstan-baseline.neon web/modules/custom/$CI_PROJECT_NAME
    - sed -i "s#$_WEB_ROOT/modules/custom/$CI_PROJECT_NAME/##" phpstan-baseline.neon|| true

################
# Customize phpunit stage to:
#   * Enable PCOV Coverage Reporting.
#   * Collect code coverage data.
################
phpunit:
  extends: .phpunit-base
  services:
    # Include the normal containers.
    - !reference [.with-database]
    - !reference [.with-chrome]
    # Add a container for LocalStack
    - name: localstack/localstack:2.1.0
      variables:
        SERVICES: s3
        EDGE_PORT: 4566
        HOSTNAME_EXTERNAL: s3fslocalstack
  before_script:
    # Install and enable PCOV.
    - echo -e "\e[0Ksection_start:`date +%s`:php_ext_install[collapsed=true]\r\e[0KInstall PHP Extensions"
    - apt-get update && apt-get install -qy --no-install-recommends $PHPIZE_DEPS
    - echo -e '[pcov]\npcov.directory=.' > /usr/local/etc/php/conf.d/pcov.ini
    - pecl install pcov && docker-php-ext-enable pcov
    - echo -e "\e[0Ksection_end:`date +%s`:php_ext_install\r\e[0K"
    # GitLab doesn't support aliases when setting up the container.
    # Get the IP for the localstack container.
    - localstack_ipaddr=$(getent hosts localstack-localstack | cut -d " " -f 1)
    # write the aliases to the hosts file.
    - echo "$localstack_ipaddr s3.localhost.localstack.cloud s3fs-test-bucket.s3.localhost.localstack.cloud" >> /etc/hosts

  after_script:
    # Ensure paths for coverage are git-relative.
    - sed -i "s#<source>$CI_PROJECT_DIR/web/modules/custom/$CI_PROJECT_NAME#<source>$CI_PROJECT_DIR#" coverage.xml
  # Capture line coverage counts for GitLab UI display.
  coverage: /^\s*Lines:\s*\d+.\d+\%/
  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml
    paths:
      - junit.xml
      - $CI_PROJECT_DIR/$_WEB_ROOT/sites/default/files/simpletest
      - $BROWSERTEST_OUTPUT_DIRECTORY
      - apache.access.log.txt
      - apache.error.log.txt
      - coverage.xml
