language: php
sudo: required
addons:
  sonarcloud:
    organization: "helhum-github"
    branches:
      - latest
      - develop
jdk:
  - oraclejdk8
services:
  - mysql
cache:
  directories:
    - $HOME/.config/composer/cache
    - $HOME/.sonar/cache

# Do not build feature branches
branches:
  only:
    - latest
    - develop
    - /^v?([0-9]+\.){1,2}(x|[0-9]+)-?[a-z]*[1-9]*$/

# Inform me about failures
notifications:
  email:
    - typo3@helhum.io

# This is executed for all stages
before_install:
  - if php -i | grep -v TRAVIS_CMD | grep -q xdebug; then phpenv config-rm xdebug.ini; fi

install:
  - export COMPOSER_ROOT_VERSION=6.4.0
  - |
    composer require --no-update \
      typo3/cms-backend="$TYPO3_VERSION" \
      typo3/cms-core="$TYPO3_VERSION" \
      typo3/cms-extbase="$TYPO3_VERSION" \
      typo3/cms-extensionmanager="$TYPO3_VERSION" \
      typo3/cms-fluid="$TYPO3_VERSION" \
      typo3/cms-frontend="$TYPO3_VERSION" \
      typo3/cms-install="$TYPO3_VERSION" \
      typo3/cms-recordlist="$TYPO3_VERSION" \
      $PREFER_LOWEST
  - composer update
  - git checkout composer.json

# Test scripts
script:
  - .Build/bin/parallel-lint --exclude .Build .
  # This fails when command reference is not up to date
  - ./typo3cms commandreference:render > /dev/null 2>&1 && test -z "$(git diff --shortstat 2> /dev/null | tail -n1)"
  - git diff
  - .Build/bin/phpunit

# Configure build stages (with build matrix above, test stage is implicitly defined)
jobs:
  allow_failures:
    - env: TYPO3_VERSION="dev-master"
  include:
# Will be activated once 11.0.0 is released
#    - stage: test
#      php: 7.4
#      env: TYPO3_VERSION="~11.0.0"
    - stage: test
      php: 7.4
      env: TYPO3_VERSION="^10.4.1"
    - stage: test
      php: 7.3
      env: TYPO3_VERSION="^10.4.1"
    - stage: test
      php: 7.2
      env: TYPO3_VERSION="^10.4.1"
    - stage: test
      php: 7.2
      env: TYPO3_VERSION="^10.4.1" PREFER_LOWEST="--prefer-lowest"
    - stage: test
      php: 7.4
      env: TYPO3_VERSION="dev-master"
    - stage: test
      php: 7.4
      env: TYPO3_VERSION="^10.4.1"
      script:
        - git clean -dffx
        - composer update
        - .Build/bin/phpunit
    - stage: test
      php: 7.4
      env: Consistency checks
      before_install: skip
      install: skip
      before_script: skip
      script:
        - >
          if [ -n "$TRAVIS_TAG" ] && [[ "$TRAVIS_TAG" =~ ^v?([0-9]+\.)([0-9]+\.)([0-9]+)$ ]]; then
            composer set-version $(echo $TRAVIS_TAG | sed s/^v//g)
            test -z "$(git diff --shortstat 2> /dev/null | tail -n1)";
          fi
        - composer extension-verify-composer-json
        - composer extension-release

    - stage: sonar code scanner
      if: type = push AND branch IN (latest, develop)
      php: 7.4
      before_install: skip
      install: skip
      script:
        - git fetch --unshallow
        - export COMPOSER_ROOT_VERSION=6.4.0
        - composer install
        - .Build/bin/phpunit --whitelist Classes --coverage-clover .Build/clover.xml --log-junit .Build/junit.xml
        - >
          if [ -n "$SONAR_TOKEN" ]; then
            sonar-scanner
          fi

    - stage: update extension repo
      if: type = push
      php: 7.4
      before_install: skip
      install: skip
      before_script:
        - mkdir -p ~/.ssh
        - openssl aes-256-cbc -K $encrypted_85bf36ae99bb_key -iv $encrypted_85bf36ae99bb_iv -in Resources/Private/deploy_rsa.enc -out ~/.ssh/id_rsa -d
        - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
        - chmod 600 ~/.ssh/id_rsa
        - git clone git@github.com:TYPO3-Console/Extension.git ../extension -b template
        - cd ../extension && git checkout origin/$TRAVIS_BRANCH || true && cd -
        - composer extension-release
        - cd ../extension
        - git checkout README.md
        - git add .
      script:
        - |
          if [ -n "$TRAVIS_TAG" ]; then
              git commit -m "Release extension version $TRAVIS_TAG" --author "Helmut Hummel <typo3@helhum.io>"
              git tag $TRAVIS_TAG
              git push --tags
            else
              git commit -m "Update extension based on commit $TRAVIS_COMMIT" --author "Helmut Hummel <typo3@helhum.io>"
              git push origin HEAD:$TRAVIS_BRANCH
          fi;

    - stage: ship to TER
      if: tag IS present
      php: 7.4
      install: skip
      before_script: skip
      script:
        - |
          if [[ "$TRAVIS_TAG" =~ ^v?([0-9]+\.)([0-9]+\.)([0-9]+)$ ]] && [ -n "$TYPO3_ORG_USERNAME" ] && [ -n "$TYPO3_ORG_PASSWORD" ]; then
            echo -e "Preparing upload of release ${TRAVIS_TAG} to TER\n";
            # Install ter client
            composer global require helhum/ter-client

            # Build extension files
            composer extension-release

            # Upload
            TAG_MESSAGE=`git tag -n10 -l $TRAVIS_TAG | sed 's/^[v]*[0-9.]*[ ]*//g'`
            echo "Uploading release ${TRAVIS_TAG} to TER"
            $HOME/.config/composer/vendor/bin/ter-client upload typo3_console ../extension -u "$TYPO3_ORG_USERNAME" -p "$TYPO3_ORG_PASSWORD" -m "$TAG_MESSAGE"
          fi;
