.ssh-setup: &ssh-setup
  - eval $(ssh-agent -s)
  - echo "$CI_DEPLOYMENT_PRIVATE_KEY" | tr -d '\r' | ssh-add -
  - echo "$UBERSPACE_KNOWN_HOST" >> ~/.ssh/known_hosts

include:
  - project: 'fit-connect/pipeline'
    ref: main
    file: 'workflows/no-duplicate-pipelines.gitlab-ci.yml'

stages:
  - build
  - lint
  - upload
  - validate

build:
  stage: build
  image: ${CI_REGISTRY}/fit-connect/baseimage:latest
  before_script:
    - npm i -g @redocly/cli@latest
    - mkdir dist
  script:
    - redocly bundle spec/submission-api.yaml -o dist/submission-api.yaml
  artifacts:
    expose_as: 'Built API spec'
    paths:
      - dist/
    expire_in: 1 day

lint:
  stage: lint
  image:
    name: ${CI_REGISTRY}/stoplight/spectral:6
    entrypoint: [ "" ]
  script:
    - spectral lint spec/submission-api.yaml --verbose --display-only-failures
    - spectral lint dist/submission-api.yaml --verbose --display-only-failures

upload:preview:
  stage: upload
  image: ${CI_REGISTRY}/fit-connect/baseimage:latest
  environment:
    name: review/$CI_COMMIT_REF_NAME
    on_stop: stop:preview
    auto_stop_in: 2 week
    url: https://preview.docs.fitko.dev/submission-api/$CI_COMMIT_REF_SLUG/
  rules:
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
  before_script:
    - *ssh-setup
  script:
    - rsync -Lvz -e "ssh -o CheckHostIP=no" ./ci/index.html fitko@dorado.uberspace.de:preview.docs.fitko.dev/submission-api/$CI_COMMIT_REF_SLUG/
    - rsync -Lvz -e "ssh -o CheckHostIP=no" ./ci/rapidoc.js fitko@dorado.uberspace.de:preview.docs.fitko.dev/submission-api/$CI_COMMIT_REF_SLUG/
    - rsync -Lvz -e "ssh -o CheckHostIP=no" ./dist/submission-api.yaml fitko@dorado.uberspace.de:preview.docs.fitko.dev/submission-api/$CI_COMMIT_REF_SLUG/
    - rsync -Lvz -e "ssh -o CheckHostIP=no" ./dist/submission-api.yaml fitko@dorado.uberspace.de:schema.fitko.de/fit-connect/submission-api/latest-dev/

link-check:
  stage: validate
  variables:
    CRAWLER_URL: "https://schema.fitko.de/fit-connect/submission-api/"
    CRAWLER_DOMAIN: "schema.fitko.de"
    CRAWL_ONE_DOMAIN_ONLY: "yes"
  trigger:
    strategy: depend
    project: fit-connect/schema-link-checker
    branch: main

upload:latest:
  stage: upload
  image: ${CI_REGISTRY}/fit-connect/baseimage:latest
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  before_script:
    - *ssh-setup
  script:
    - rsync -Lvz -e "ssh -o CheckHostIP=no" ./dist/submission-api.yaml fitko@dorado.uberspace.de:schema.fitko.de/fit-connect/submission-api/latest/

upload:release:
  stage: upload
  image: ${CI_REGISTRY}/fit-connect/baseimage:latest
  rules:
    - if: $CI_COMMIT_TAG
  before_script:
    - *ssh-setup
  script:
    - sed -i "s,API_VERSION,$CI_COMMIT_TAG,g" dist/submission-api.yaml
    - rsync -Lvz -e "ssh -o CheckHostIP=no" ./dist/submission-api.yaml fitko@dorado.uberspace.de:schema.fitko.de/fit-connect/submission-api/$CI_COMMIT_TAG/

stop:preview:
  stage: .post
  image: ${CI_REGISTRY}/fit-connect/baseimage:latest
  environment:
    name: review/$CI_COMMIT_REF_NAME
    action: stop
  needs:
    - upload:preview
  rules:
    - if: $CI_MERGE_REQUEST_ID
      when: manual
  before_script:
    - *ssh-setup
  script:
    - ssh fitko@dorado.uberspace.de "rm -rf preview.docs.fitko.dev/submission-api/$CI_COMMIT_REF_SLUG/"
