image: framasoft/framadate-ci:7.3-pdo_mysql
stages:
  - test
  - deploy

# Run php-cs-fixer and phpunit on all branches
test:
  stage: test
  script:
    - composer install -o  --no-interaction --no-progress --prefer-dist
    - php vendor/bin/php-cs-fixer fix --verbose --dry-run
    - vendor/bin/phpunit --bootstrap app/tests/bootstrap.php --debug app/tests
  image: framasoft/framadate-ci:${PHP_VERSION}-pdo_mysql
  parallel:
      matrix:
          - PHP_VERSION:
                - "7.3"
                - "7.4"
                - "8.0"
                - "8.1"
  cache:
    paths:
      - vendor/

# Create artifacts on master
pages:
  stage: deploy
  script:
  - latesttag=$(git describe --tags)
  - git checkout ${latesttag}
  - composer install -o  --no-interaction --no-progress --prefer-dist --no-dev
  - composer dump-autoload --optimize --no-dev --classmap-authoritative
  - mkdir framadate
  - mv `ls -A | grep -v framadate` ./framadate
  - echo $latesttag > framadate/VERSION
  - find framadate/ -type d -exec chmod 750 {} \;
  - find framadate/ -type f -exec chmod 640 {} \;
  - rm -rf framadate/.git
  - export RELEASE_ZIP="framadate-${CI_COMMIT_TAG}.zip"
  - zip -r $RELEASE_ZIP framadate
  - mkdir .public
  - cp $RELEASE_ZIP .public/latest.zip
  - mv .public public
  - if [[ -z $GITLAB_API_TOKEN ]]; then exit; fi
  - export PROJECT_API_URL="https://framagit.org/api/v4/projects/${CI_PROJECT_ID}"
  - export DESCRIPTION_URL="${PROJECT_API_URL}/repository/tags/${CI_COMMIT_TAG}"
  - export RELEASE_URL="${DESCRIPTION_URL}/release"
  - 'export HEADER="Private-Token: ${GITLAB_API_TOKEN}"'
  - export artifactUrl=$(curl -s --request POST --header "${HEADER}" --form "file=@${RELEASE_ZIP}" "${PROJECT_API_URL}/uploads" | jq .url)
  - export artifactAbsoluteUrl="${CI_PROJECT_URL}${artifactUrl}"
  - export description=$(curl -s --header "${HEADER}" "${DESCRIPTION_URL}" | jq .release.description | sed -e 's@"@@g')
  - if [[ $description == 'null' ]]; then export METHOD="POST"; echo -e 'You can download the release zip here:'" [${RELEASE_ZIP}](${artifactAbsoluteUrl})" > /tmp/text; fi
  - if [[ $description != 'null' ]]; then export METHOD="PUT";  echo -e "${description}\n\n"'You can download the release zip here:'" [${RELEASE_ZIP}](${artifactAbsoluteUrl})" > /tmp/text; fi
  - curl -s --request $METHOD --data-urlencode "description@/tmp/text" --header "${HEADER}" "${RELEASE_URL}"
  - curl -s --request POST --header "${HEADER}" --data name="${RELEASE_ZIP}" --data url="${artifactAbsoluteUrl}" "${PROJECT_API_URL}/releases/${CI_COMMIT_TAG}/assets/links"
  artifacts:
    paths:
    - public
  only:
    - tags
  except:
    - (beta|alpha)
