stages:
  - execute
  - buildprod
  - build
  - prepare
  - postprocess
  - validate
  - test
  - deploy
  - finalize
  - release

.prepareaccess:
  before_script:
    - if [[ "x$(command -v ssh-agent)" != "x" && -n $SSH_PRIVATE_KEY ]]; then eval $(ssh-agent -s); fi
    - if [[ "x$(command -v ssh-add)" != "x" && -n $SSH_PRIVATE_KEY ]]; then echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null; fi
    - if [[ "x$(command -v ssh-add)" != "x" && -n $SSH_PRIVATE_KEY ]]; then mkdir -p ~/.ssh; fi
    - if [[ "x$(command -v ssh-add)" != "x" && -n $SSH_PRIVATE_KEY ]]; then chmod 700 ~/.ssh; fi
    - if [[ "x$(command -v ssh-keyscan)" != "x" && -n $SSH_PRIVATE_KEY ]]; then ssh-keyscan ${CI_SERVER_HOST} >> ~/.ssh/known_hosts; fi
    - if [[ "x$(command -v ssh-keyscan)" != "x" && -n $SSH_PRIVATE_KEY ]]; then chmod 644 ~/.ssh/known_hosts; fi
    - if [[ "x$(command -v ssh)" != "x" && -n $SSH_PRIVATE_KEY ]]; then ssh git@${CI_SERVER_HOST}; fi
    - if [[ "x$(command -v glab)" != "x" && -n $GITLAB_ACCESS_TOKEN ]]; then glab auth login --hostname ${CI_SERVER_HOST} --token $GITLAB_ACCESS_TOKEN; fi
    - if [[ "x$(command -v glab)" != "x" && -n $GITLAB_PRIVATE_TOKEN ]]; then glab auth login --hostname ${CI_SERVER_HOST} --token $GITLAB_PRIVATE_TOKEN; fi
    - if [[ -n $GITLAB_ACCESS_TOKEN && -f composer.json ]]; then composer config -g --no-plugins --no-interaction gitlab-token.${CI_SERVER_HOST} $GITLAB_ACCESS_TOKEN; fi
    - if [[ -n $GITLAB_PRIVATE_TOKEN && -f composer.json ]]; then composer config -g --no-plugins --no-interaction gitlab-token.${CI_SERVER_HOST} $GITLAB_PRIVATE_TOKEN; fi
    - if [[ -n $DOCKER_AUTH_CONFIG ]]; then mkdir -p ~/.docker; echo $DOCKER_AUTH_CONFIG > ~/.docker/config.json; fi

.preparecomposerplugins:
  before_script:
    # Add the Drupal Vendor Hardening Composer Plugin.
    # see https://github.com/drupal/core-vendor-hardening
    - composer config --no-plugins --no-interaction allow-plugins.drupal/core-vendor-hardening true
    # Disallowing plugin script.
    - composer config --no-plugins --no-interaction allow-plugins.php-http/discovery false
    # Allow PHPStan Extension Installer
    - composer config --no-plugins --no-interaction allow-plugins.phpstan/extension-installer true
    # Allow Drupal Dependency Quality Gate Composer Audit plugin
    - if [[ "$IGNORE_COMPOSER_EXTENDED_AUDIT" == "0" ]]; then composer config --no-plugins --no-interaction allow-plugins.mxr576/ddqg-composer-audit true; fi
    # Disallow Drupal Dependency Quality Gate Composer Audit plugin
    - if [[ "$IGNORE_COMPOSER_EXTENDED_AUDIT" == "1" ]]; then composer config --no-plugins --no-interaction allow-plugins.mxr576/ddqg-composer-audit false; fi
    # Composer audit should fail on abandonded packages found
    - if [[ "$IGNORE_COMPOSER_ABANDONED_AUDIT" == "0" ]]; then composer config --no-plugins --no-interaction audit.abandoned fail; fi
    # Composer audit should only report found abandonded packages
    - if [[ "$IGNORE_COMPOSER_ABANDONED_AUDIT" == "1" ]]; then composer config --no-plugins --no-interaction audit.abandoned report; fi

.customreferences:
  LanguageFiles: []

.merge:
  stage: release
  tags:
    - default
  variables:
    GIT_STRATEGY: none
  image: registry.lakedrops.com/docker/gitlab-drupal-ci/php-8.2
  cache: {}
  before_script: !reference [.prepareaccess, before_script]
  script:
    - rm -rf /tmp/release || true
    - mkdir -p /tmp/release
    - cd /tmp/release
    - git clone -b $SOURCE_BRANCH git@${CI_SERVER_HOST}:$CI_PROJECT_PATH.git .
    - merge
    - cd -
    - rm -rf /tmp/release || true
