.pre-deploy-uber-space-setup: &pre-deploy-uber-space-setup
  - apk add rsync openssh-client
  - eval $(ssh-agent -s)
  - echo "$CI_DEPLOYMENT_PRIVATE_KEY" | tr -d '\r' | ssh-add -
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - echo "$UBERSPACE_KNOWN_HOST" >> ~/.ssh/known_hosts

workflow:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: '$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS'
      when: never
    - if: '$CI_COMMIT_BRANCH'
    - if: '$CI_COMMIT_TAG'

stages:
  - build
  - lint
  - upload
  - validate

build:
  stage: build
  image: $CI_REGISTRY/node:current-alpine
  before_script:
    - npm install -g @apidevtools/swagger-cli
    - mkdir dist
  script:
    - swagger-cli bundle -t yaml -o dist/submission-api.yaml spec/submission-api.yaml
  artifacts:
    expose_as: 'Built API spec'
    paths:
      - dist/
    expire_in: 1 day

lint:
  stage: lint
  image:
    name: $CI_REGISTRY/stoplight/spectral:5
    entrypoint: [ "" ]
  script:
    - spectral lint dist/submission-api.yaml --verbose

upload:preview:
  stage: upload
  image: $CI_REGISTRY/alpine: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:
    - *pre-deploy-uber-space-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/alpine:latest
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  before_script:
    - *pre-deploy-uber-space-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/alpine:latest
  rules:
    - if: $CI_COMMIT_TAG
  before_script:
    - *pre-deploy-uber-space-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/alpine:latest
  environment:
    name: review/$CI_COMMIT_REF_NAME
    action: stop
  needs:
    - upload:preview
  rules:
    - if: $CI_MERGE_REQUEST_ID
      when: manual
  before_script:
    - *pre-deploy-uber-space-setup
  script:
    - ssh fitko@dorado.uberspace.de "rm -rf preview.docs.fitko.dev/submission-api/$CI_COMMIT_REF_SLUG/"
