stages:
  - lint
  - build
  - upload

lint:
  stage: lint
  image:
    name: $CI_REGISTRY/stoplight/spectral:latest
    entrypoint: [ "" ]
  script:
    - spectral lint spec/*.yaml

openapi4j-parser:
  stage: lint
  image: $CI_REGISTRY/node:current-buster
  before_script:
    - npm install -g @apidevtools/swagger-cli
    - export DEBIAN_FRONTEND=noninteractive
    - apt-get update && apt-get install -y openjdk-11-jre
  script:
    - swagger-cli bundle -t yaml -o "spec/submission-api-bundled.yaml" "spec/submission-api.yaml"
    - java -jar openapi4j-parser-cli-1.0.0.jar "spec/submission-api-bundled.yaml"

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: 30 days

upload:
  stage: upload
  image: $CI_REGISTRY/alpine:latest
  only:
    refs:
      - tags
    variables:
      - $CI_COMMIT_TAG
  before_script:
    - 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
  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/
