# This file is a template, and might need editing before it works on your project.
# This is a sample GitLab CI/CD configuration file that should run without any modifications.
# It demonstrates a basic 3 stage CI/CD pipeline. Instead of real tests or scripts,
# it uses echo commands to simulate the pipeline execution.
#
# A pipeline is composed of independent jobs that run scripts, grouped into stages.
# Stages run in sequential order, but jobs within stages run in parallel.
#
# For more information, see: https://docs.gitlab.com/ee/ci/yaml/index.html#stages
#
# You can copy and paste this template into a new `.gitlab-ci.yml` file.
# You should not add this template to an existing `.gitlab-ci.yml` file by using the `include:` keyword.
#
# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Getting-Started.gitlab-ci.yml

# Build JAVA applications using Apache Maven (http://maven.apache.org)
# For docker image tags see https://hub.docker.com/_/maven/
#
# For general lifecycle information see https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

# This template will build and test your projects
# * Caches downloaded dependencies and plugins between invocation.
# * Verify but don't deploy merge requests.
# * Deploy built artifacts from master branch only.

variables:
  # CI_TARGET_BRANCH: $CI_DEFAULT_BRANCH
  CI_TARGET_BRANCH: "ttf-022"
  DEV_CI_TARGET_BRANCH: "dev"
  MAIN_CI_TARGET_BRANCH: "master"
  SITE_LOCATION: "updatesite/org.etsi.mts.tdl.updatesite/target"
  ARCHIVE_NAME: "org.etsi.mts.tdl.updatesite-1.0.0-SNAPSHOT.zip"
  SITE_ARCHIVE: "${SITE_LOCATION}/${ARCHIVE_NAME}"
  SITE_PATH: "${SITE_LOCATION}/repository/"
  REPO_PREFIX: "https://labs.etsi.org/rep/top/ide/-/jobs"
  REPO_PATH: "artifacts/raw/updatesite/org.etsi.mts.tdl.updatesite/target/repository/"
  PACKAGE_NAME: "TOP IDE Interim Build"
  PACKAGE_NAME_MAIN: "TOP IDE Build"
  BETA_KEY: "beta"
  MAIN_KEY: "stable"
  RELEASE_KEY: "beta"
  RELEASE_PATH: "public/updatesite/${RELEASE_KEY}"
  RELEASE_PAGE: "https://top.etsi.org/ide/updatesite/${RELEASE_KEY}"
  STANDALONE_LOCATION: "plugins/org.etsi.mts.tdl.standalone/target"
  STANDALONE_NAME: "org.etsi.mts.tdl.standalone-1.0.0-SNAPSHOT-shadow.jar"
  STANDALONE_PATH: "${STANDALONE_LOCATION}/${STANDALONE_NAME}"

  # `showDateTime` will show the passed time in milliseconds. You need to specify `--batch-mode` to make this work.
  MAVEN_OPTS: >-
    -Xmx2048m
    -Dhttps.protocols=TLSv1.2
    -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository
    -Dorg.slf4j.simpleLogger.showDateTime=true
    -Djava.awt.headless=true

  # As of Maven 3.3.0 instead of this you MAY define these options in `.mvn/maven.config` so the same config is used
  # when running from the command line.
  # As of Maven 3.6.1, the use of `--no-tranfer-progress` (or `-ntp`) suppresses download and upload messages. The use
  # of the `Slf4jMavenTransferListener` is no longer necessary.
  # `installAtEnd` and `deployAtEnd` are only effective with recent version of the corresponding plugins.
  MAVEN_CLI_OPTS: >-
    --batch-mode
    --errors
    --fail-at-end
    --show-version
    --no-transfer-progress
    -DinstallAtEnd=true
    -DdeployAtEnd=true

.default_rules:
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule"
      when: never
    - if: $CI_COMMIT_BRANCH == $CI_TARGET_BRANCH
    - if: $CI_PIPELINE_SOURCE == "web"
      when: always
    - if: $CI_COMMIT_TAG
      when: never
    # - when: never



# This template uses the latest Maven 3 release, e.g., 3.8.6, and OpenJDK 8 (LTS)
# for verifying and deploying images
# Maven 3.8.x REQUIRES HTTPS repositories.
# See https://maven.apache.org/docs/3.8.1/release-notes.html#how-to-fix-when-i-get-a-http-repository-blocked for more.
image: maven:3-openjdk-11

# Cache downloaded dependencies and plugins between builds.
# To keep cache across branches add 'key: "$CI_JOB_NAME"'
# Be aware that `mvn deploy` will install the built jar into this repository. If you notice your cache size
# increasing, consider adding `-Dmaven.install.skip=true` to `MAVEN_OPTS` or in `.mvn/maven.config`
cache:
  paths:
    - .m2/repository

# For merge requests do not `deploy` but only run `verify`.
# See https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
# .verify:
  # stage: test
  # script:
    # - cd org.etsi.mts.tdl.parent
    # - 'mvn $MAVEN_CLI_OPTS verify'
  # except:
    # variables:
      # - $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

# Verify merge requests using JDK8
# verify:jdk11:
  # extends:
    # - .verify

# To deploy packages from CI, create a `ci_settings.xml` file
# For deploying packages to GitLab's Maven Repository: See https://docs.gitlab.com/ee/user/packages/maven_repository/index.html#create-maven-packages-with-gitlab-cicd for more details.
# Please note: The GitLab Maven Repository is currently only available in GitLab Premium / Ultimate.
# For `master` or `main` branch run `mvn deploy` automatically.
#deploy:jdk11:
#  stage: deploy
#  script:
#    - if [ ! -f ci_settings.xml ]; then
#        echo "CI settings missing\! If deploying to GitLab Maven Repository, please see https://docs.gitlab.com/ee/user/packages/maven_repository/index.html#create-maven-packages-with-gitlab-cicd for instructions.";
#      fi
#    - 'mvn $MAVEN_CLI_OPTS deploy --settings ci_settings.xml'
#  only:
#    variables:
#      - $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH


stages:          # List of stages for jobs, and their order of execution
  - config
  - build
  - test
  - deploy
  - prepare
  - upload
  - release

config-job:       # This job runs in the build stage, which runs first.
  stage: config
  extends: 
    - main-branch-key
    - dev-branch-key
    - ttf-branch-key
  script:
    - echo $RELEASE_KEY
    - echo $RELEASE_PATH
    - echo $RELEASE_PAGE
    - exit 1

build-job:       # This job runs in the build stage, which runs first.
  stage: build
  rules:
    - !reference [.default_rules, rules]
  script:
    - echo "Compiling the code..."
    - echo "BUILD_JOB_ID=$CI_JOB_ID" >> build.env
    - cat build.env
    - cd org.etsi.mts.tdl.parent
    - mvn clean install
    - echo "Compile complete."
  artifacts:
    expire_in: 7 days
    reports:
      dotenv: build.env
    paths:
      - $SITE_ARCHIVE
      - $SITE_PATH
      # - $STANDALONE_PATH 

# unit-test-job:   # This job runs in the test stage.
  # stage: test    # It only starts when the job in the build stage completes successfully.
  # script:
    # - echo "Running unit tests... This will take about 6 seconds."
    # - sleep 6
    # - echo "Code coverage is 0%"

# lint-test-job:   # This job also runs in the test stage.
  # stage: test    # It can run at the same time as unit-test-job (in parallel).
  # script:
    # - echo "Linting code... This will take about 1 seconds."
    # - sleep 1
    # - echo "No lint issues found."

# deploy-job:      # This job runs in the deploy stage.
  # stage: deploy  # It only runs when *both* jobs in the test stage complete successfully.
  # environment: production
  # script:
    # - echo "Deploying application..."
    # - echo "Application successfully deployed."


# Adapted from OpenAPI2TDL converter
prepare-job:
  stage: prepare
  rules:
    - !reference [.default_rules, rules]
  script:
    - VERSION=`date +%Y%m%d.%H.%M`
    - echo "EXTRA_DESCRIPTION=automatic-release" >> variables.env
    - echo "VERSION=$VERSION" >> variables.env
    - echo "PACKAGE_REGISTRY_URL=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/ide/${VERSION}" >> variables.env
    - echo "PACKAGE_LATEST_URL=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/ide/latest" >> variables.env
    - cat variables.env
  artifacts:
    reports:
      dotenv: variables.env   # Use artifacts:reports:dotenv to expose the variables to other jobs

# https://gitlab.com/gitlab-org/release-cli/-/tree/master/docs/examples/release-assets-as-generic-package/
upload-job:
  stage: upload
  image: curlimages/curl:latest
  needs:
    - job: prepare
      artifacts: true
    - job: build-job
      artifacts: true
  rules:
    - !reference [.default_rules, rules]
  script:
    - echo $BUILD_JOB_ID    
    - echo "${PACKAGE_REGISTRY_URL}/${ARCHIVE_NAME}"
    - |
      curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file ${SITE_ARCHIVE} ${PACKAGE_REGISTRY_URL}/${ARCHIVE_NAME}
    # - |
      # curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file ${STANDALONE_LOCATION} ${PACKAGE_REGISTRY_URL}/${STANDALONE_NAME}
    # - |
      # curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file ${SITE_ARCHIVE} ${PACKAGE_LATEST_URL}/${ARCHIVE_NAME}
    # - |
      # cd ${SITE_LOCATION} && find repository/ -type f -exec echo curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file {} ${PACKAGE_LATEST_URL}/{} \; && cd ~
    # - |
      # cd ${SITE_LOCATION} && find repository/ -type f -exec curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file {} ${PACKAGE_LATEST_URL}/{} \; 
    # - |
      # curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file bin/${MACOS_GUI_JAR} ${PACKAGE_REGISTRY_URL}/${MACOS_GUI_JAR}
    # - |
      # curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file bin/${WINDOWS_GUI_JAR} ${PACKAGE_REGISTRY_URL}/${WINDOWS_GUI_JAR}
    # - |
      # curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file bin/${CLI_JAR} ${PACKAGE_REGISTRY_URL}/${CLI_JAR}
    # - |
      # curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file bin/${LIB_JAR} ${PACKAGE_REGISTRY_URL}/${LIB_JAR}

pages-job:
  stage: upload
  # image: curlimages/curl:latest
  needs:
    - job: prepare
      artifacts: true
    - job: build-job
      artifacts: true
  script:
    # Get the previous GitLab Pages content
    # - set +e
    # - curl --fail "$CI_PAGES_URL/content.zip" -o "content.zip"
    # - if [ -e "content.zip" ]; then unzip "content.zip"; rm "content.zip"; fi
    # - set -e
    # Add/update content
    # TODO: differentiate on branch?
    - echo $CI_PAGES_URL
    - mkdir -p $RELEASE_PATH
    - cp -r -f $SITE_PATH/* $RELEASE_PATH
    # Zip the content and publish the zip again
    # - zip -r "content.zip" "public"
    # - mv "content.zip" "public/"
    # - mv $SITE_ARCHIVE $RELEASE_PATH/content.zip
  artifacts:
    paths:
      - public
  rules:
    - !reference [.default_rules, rules]
  # only:
    # - tags  

release-job:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  needs:
    - job: prepare
      artifacts: true
    - job: build-job
      artifacts: true
  extends: 
    - main-branch-key
    - dev-branch-key
    - ttf-branch-key
  rules:
    - !reference [.default_rules, rules]
  script:
    # unpack latest?
    # TODO: permalink for latest release? or manual job?
    # --description "Published automatically, permalink for installation (valid for 7 days) at https://labs.etsi.org/rep/top/ide/-/releases/permalink/latest/downloads/repository." 
    - echo $BUILD_JOB_ID    
    - |
      release-cli create --name "Release $VERSION" \
        --tag-name $VERSION \
        --ref $CI_COMMIT_SHA \
        --assets-link "{\"name\":\"${PACKAGE_NAME}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${ARCHIVE_NAME}\",\"filepath\":\"/repository-archive\"}" \
        --assets-link "{\"name\":\"${PACKAGE_NAME} (latest ${RELEASE_KEY} repository (exipres in 7 days))\",\"url\":\"${REPO_PREFIX}/${BUILD_JOB_ID}/${REPO_PATH}\",\"filepath\":\"/repository\"}" \
        --assets-link "{\"name\":\"${PACKAGE_NAME} (latest ${RELEASE_KEY} release (permalink))\",\"url\":\"${RELEASE_PAGE}\"}" \
        # --assets-link "{\"name\":\"${PACKAGE_NAME} (latest standalone (exipres in 7 days))\",\"url\":\"${PACKAGE_REGISTRY_URL}/${STANDALONE_NAME}\"}" \
        # --assets-link "{\"name\":\"${PACKAGE_NAME} (latest)\",\"url\":\"${PACKAGE_LATEST_URL}/${ARCHIVE_NAME}\"}" \
        # --assets-link "{\"name\":\"${MACOS_GUI_JAR}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${MACOS_GUI_JAR}\"}" \
        # --assets-link "{\"name\":\"${WINDOWS_GUI_JAR}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${WINDOWS_GUI_JAR}\"}" \
        # --assets-link "{\"name\":\"${CLI_JAR}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${CLI_JAR}\"}" \
        # --assets-link "{\"name\":\"${LIB_JAR}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${LIB_JAR}\"}"

# assign variables depending on the branch
main-branch-key:
  variables:
    RELEASE_KEY: $MAIN_KEY
  rules:
    - if: $CI_COMMIT_BRANCH == $MAIN_CI_TARGET_BRANCH
  
dev-branch-key:
  variables:
    RELEASE_KEY: $BETA_KEY
  rules:
    - if: $CI_COMMIT_BRANCH == $DEV_CI_TARGET_BRANCH

ttf-branch-key:
  variables:
    RELEASE_KEY: $BETA_KEY
  rules:
    - if: $CI_COMMIT_BRANCH == "ttf-022"
