---
include:
  - remote: https://jobs.just-ci.dev/85-feat-prevent-semantic-release-from-releasing-an-initial-release/project-automation/generic.yaml

semantic-version-tag-checker:
  stage: .pre
  image:
    name: docker.io/docker:git
    entrypoint: [""]
  script:
    - echo "[*] Validating that a semantic version tag is already available on
      'origin/${CI_DEFAULT_BRANCH}' to prevent semantic-release from creating
      the initial release (v1.0.0). You can disable this check by setting the
      variable ALLOW_INITIAL_SEMANTIC_RELEASE."
    - |
      if [ "$(git rev-parse --is-shallow-repository)" != "false" ]; then
        echo "[*] Repository is shallow. Unshallowing."
        git fetch --unshallow -q
      fi
    - |
      if ! $( (git tag --merged origin/${CI_DEFAULT_BRANCH} ||:) | grep -qE '^v?[0-9]+\.[0-9]+\.[0-9]+$'); then # prevent SIGPIPE (exit 141) if grep exits before piped input is finished, see https://unix.stackexchange.com/a/582850/558271
        echo "[!] No semantic version tag is available in the history of 'origin/${CI_DEFAULT_BRANCH}'. Please first create a tag (e.g. 'v0.0.1') on 'origin/${CI_DEFAULT_BRANCH}' and rebase (intermediate branches and) the current branch on that."
        exit 1
      else
        echo "[+] Semantic version tags are available in the history of 'origin/${CI_DEFAULT_BRANCH}'."
      fi
  rules:
    - if: $ALLOW_INITIAL_SEMANTIC_RELEASE != null
      when: never
    - !reference [.release:rules, rules]
    - allow_failure: true

find-next-release:
  extends: .release:rules
  stage: build
  image: registry.gitlab.com/just-ci/images/node:latest
  variables:
    NPM_PACKAGES: semantic-release @semantic-release/exec
  script:
    - !reference [".release:install", script]
    - wget -O .releaserc
      https://jobs.just-ci.dev/85-feat-prevent-semantic-release-from-releasing-an-initial-release/project-automation/release/find-next-release.json
    - semantic-release --dry-run --no-ci
    - echo "TBUMP_NEXT_VERSION=$(cat .VERSION 2>/dev/null || echo "[!] No new
      release" 1>&2)" > TBUMP_NEXT_VERSION.env
  artifacts:
    reports:
      dotenv: TBUMP_NEXT_VERSION.env
