image: golang:latest

stages:
  - test
  - pre-release
  - release

run_tests:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH)'
  stage: test
  script:
    - go install gotest.tools/gotestsum@latest
    - gotestsum --format testname --junitfile report.xml
  artifacts:
    when: always
    reports:
      junit: report.xml

lint:
  image: golangci/golangci-lint:v1.56-alpine
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH)'
  stage: test
  script:
    - golangci-lint run --issues-exit-code 1 --print-issued-lines=false --new-from-rev $CI_MERGE_REQUEST_DIFF_BASE_SHA --out-format code-climate:gl-code-quality-report.json,line-number
  artifacts:
    reports:
      codequality: gl-code-quality-report.json
    paths:
      - gl-code-quality-report.json

# ----- prepare release -----

# Расчет тега и формирование Changelog
get_release_info:
  stage: pre-release
  image:
    name: orhunp/git-cliff:latest
    entrypoint: [ "" ]
  rules:
    - if: '$CI_PIPELINE_SOURCE == "pipeline" && $PREPARE_RELEASE == "true"' # Запуск при инициации из perxis
    - if: $CI_PIPELINE_SOURCE == "web"                                      # или при ручном запуске из GUI
      when: manual                    
  variables:
    GIT_STRATEGY: clone
    GIT_DEPTH: 0
  script:
    - echo "RELEASE_VERSION=$(git-cliff --bumped-version)" >> vars.env                # Расчет новой версии релиза через git-cliff
    - echo "$(git-cliff --unreleased | sed '1,6d' | sed '$d')" > current_changelog.md # удалить "лишние" строки для Changelog
  artifacts:
    reports:
      dotenv: vars.env # Use artifacts:reports:dotenv to expose the variables to other jobs
    paths:
      - current_changelog.md

# Получаем последнюю версию релиза
get_current_version:
  stage: pre-release
  image: bitnami/git:latest
  rules:
    - if: '$CI_PIPELINE_SOURCE == "pipeline" && $PREPARE_RELEASE == "true"'
    - if: $CI_PIPELINE_SOURCE == "web"
  needs:
    - job: get_release_info
  variables:
    GIT_STRATEGY: clone
    GIT_DEPTH: 0
  script:
    - echo CURRENT_VERSION=$(git describe --tags --abbrev=0) >> vars.env
  artifacts:
    reports:
      dotenv: vars.env

# Релиз и запись тега в артефакт для использования в perxis
prepare_release:
  stage: pre-release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  rules:
    - if: '$CI_PIPELINE_SOURCE == "pipeline" && $PREPARE_RELEASE == "true"'
    - if: $CI_PIPELINE_SOURCE == "web"
  needs:
    - job: get_release_info
      artifacts: true
    - job: get_current_version
      artifacts: true
  script:
    - echo "PERXIS_GO_RELEASE_VERSION=$RELEASE_VERSION" >> vars.env
    - echo "PERXIS_GO_CURRENT_VERSION=$CURRENT_VERSION" >> vars.env
    - |
      # Если новая версия совпадает со старой, значит изменений не было, и выпускать новый релиз не нужно
      NEEDS_RELEASE=false
      if [ "$RELEASE_VERSION" != "$CURRENT_VERSION" ]; then
        NEEDS_RELEASE=true
      fi
      
      echo "PERXIS_GO_NEEDS_RELEASE=$NEEDS_RELEASE" >> vars.env
  artifacts:
    when: always
    paths:
      - vars.env
      - current_changelog.md
    expire_in: 1 week

# ----- release -----

release:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  rules:
    - if: $CI_PIPELINE_SOURCE == "pipeline" && $NEEDS_RELEASE == "true"
    - if: $CI_PIPELINE_SOURCE == "web"
      when: manual
  script:
    - echo "Start release $VERSION"
  release:
    name: 'Release $VERSION'
    description: '$VERSION'
    tag_name: '$VERSION'
