---
kind: pipeline
type: docker
name: build

trigger:
  branch:
    include:
      - main
  event:
    include:
      - push
      - pull_request

services:
  - name: api
    image: vikunja/api:unstable
    environment:
      VIKUNJA_SERVICE_TESTINGTOKEN: averyLongSecretToSe33dtheDB
      VIKUNJA_LOG_LEVEL: DEBUG

steps:
# Disabled until we figure out why it is so slow
#  - name: restore-cache
#    image: meltwater/drone-cache:dev
#    pull: always
#    environment:
#      AWS_ACCESS_KEY_ID:
#        from_secret: cache_aws_access_key_id
#      AWS_SECRET_ACCESS_KEY:
#        from_secret: cache_aws_secret_access_key
#    settings:
#      debug: true
#      restore: true
#      bucket: kolaente.dev-drone-dependency-cache
#      endpoint: https://s3.fr-par.scw.cloud
#      region: fr-par
#      path_style: true
#      cache_key: '{{ .Repo.Name }}_{{ checksum "pnpm-lock.yaml" }}_{{ arch }}_{{ os }}'
#      mount:
#        - .cache

  - name: dependencies
    image: node:18-alpine
    pull: always
    environment:
      PNPM_CACHE_FOLDER: .cache/pnpm
      CYPRESS_CACHE_FOLDER: .cache/cypress
    commands:
      - corepack enable && pnpm config set store-dir .cache/pnpm
      - pnpm install --fetch-timeout 100000
#    depends_on:
#      - restore-cache

  - name: lint
    image: node:18-alpine
    pull: always
    environment:
      PNPM_CACHE_FOLDER: .cache/pnpm
    commands:
      - corepack enable && pnpm config set store-dir .cache/pnpm
      - pnpm run lint
    depends_on:
      - dependencies

  - name: build-prod
    image: node:18-alpine
    pull: always
    environment:
      PNPM_CACHE_FOLDER: .cache/pnpm
    commands:
      - corepack enable && pnpm config set store-dir .cache/pnpm
      - pnpm run build
    depends_on:
      - dependencies

  - name: test-unit
    image: node:18-alpine
    pull: always
    commands:
      - corepack enable && pnpm config set store-dir .cache/pnpm
      - pnpm run test:unit
    depends_on:
      - dependencies

  - name: typecheck
    failure: ignore
    image: node:18-alpine
    pull: always
    environment:
      PNPM_CACHE_FOLDER: .cache/pnpm
    commands:
      - corepack enable && pnpm config set store-dir .cache/pnpm
      - pnpm run typecheck
    depends_on:
      - dependencies

  - name: test-frontend
    image: cypress/browsers:node18.12.0-chrome106-ff106
    pull: always
    environment:
      CYPRESS_API_URL: http://api:3456/api/v1
      CYPRESS_TEST_SECRET: averyLongSecretToSe33dtheDB
      PNPM_CACHE_FOLDER: .cache/pnpm
      CYPRESS_CACHE_FOLDER: .cache/cypress
      CYPRESS_DEFAULT_COMMAND_TIMEOUT: 60000
      CYPRESS_RECORD_KEY:
        from_secret: cypress_project_key
    commands:
      - sed -i 's/localhost/api/g' dist/index.html
      - corepack enable && pnpm config set store-dir .cache/pnpm
      - pnpm cypress install
      - pnpm run serve:dist & npx wait-on http://localhost:4173
      - pnpm run test:frontend --browser chrome --record
    depends_on:
      - build-prod

#  - name: rebuild-cache
#    image: meltwater/drone-cache:dev
#    pull: always
#    environment:
#      AWS_ACCESS_KEY_ID:
#        from_secret: cache_aws_access_key_id
#      AWS_SECRET_ACCESS_KEY:
#        from_secret: cache_aws_secret_access_key
#    settings:
#      rebuild: true
#      bucket: kolaente.dev-drone-dependency-cache
#      endpoint: https://s3.fr-par.scw.cloud
#      region: fr-par
#      path_style: true
#      cache_key: '{{ .Repo.Name }}_{{ checksum "pnpm-lock.yaml" }}_{{ arch }}_{{ os }}'
#      mount:
#        - .cache
#    depends_on:
#      - dependencies

  - name: deploy-preview
    image: node:18-alpine
    pull: always
    environment:
      NETLIFY_AUTH_TOKEN:
        from_secret: netlify_auth_token
      NETLIFY_SITE_ID:
        from_secret: netlify_site_id
      GITEA_TOKEN:
        from_secret: gitea_token
    commands:
      - cp -r dist dist-preview
      # Override the default api url used for preview
      - sed -i 's|http://localhost:3456|https://try.vikunja.io|g' dist-preview/index.html
      - apk add --no-cache perl-utils
      - shasum -a 384 -c ./scripts/deploy-preview-netlify.js.sha384
      - node ./scripts/deploy-preview-netlify.js
    depends_on:
      - build-prod
    when:
      event:
        include:
          - pull_request

---
kind: pipeline
type: docker
name: release-latest

depends_on:
  - build

trigger:
  branch:
    - main
  event:
    - push

steps:
  - name: fetch-tags
    image: docker:git
    commands:
      - git fetch --tags

#  - name: restore-cache
#    image: meltwater/drone-cache:dev
#    pull: always
#    environment:
#      AWS_ACCESS_KEY_ID:
#        from_secret: cache_aws_access_key_id
#      AWS_SECRET_ACCESS_KEY:
#        from_secret: cache_aws_secret_access_key
#    settings:
#      restore: true
#      bucket: kolaente.dev-drone-dependency-cache
#      endpoint: https://s3.fr-par.scw.cloud
#      region: fr-par
#      path_style: true
#      cache_key: '{{ .Repo.Name }}_{{ checksum "pnpm-lock.yaml" }}_{{ arch }}_{{ os }}'
#      mount:
#        - .cache

  - name: build
    image: node:18-alpine
    pull: always
    environment:
      PNPM_CACHE_FOLDER: .cache/pnpm
    commands:
      - apk add git
      - corepack enable && pnpm config set store-dir .cache/.pnpm
      - pnpm install --fetch-timeout 100000 --frozen-lockfile
      - pnpm run lint
      - "echo '{\"VERSION\": \"'$(git describe --tags --always --abbrev=10 | sed 's/-/+/' | sed 's/^v//' | sed 's/-g/-/')'\"}' > src/version.json"
      - pnpm run build
      - sed -i 's/http\:\\/\\/localhost\\:3456\\/api\\/v1/\\/api\\/v1/g' dist/index.html # Override the default api url used for developing
#    depends_on:
#      - restore-cache

  - name: static
    image: kolaente/zip
    pull: always
    commands:
      - cd dist
      - zip -r ../vikunja-frontend-unstable.zip *
      - cd ..
    depends_on: [ build ]

  - name: release
    image: plugins/s3
    pull: always
    settings:
      bucket: vikunja-releases
      access_key:
        from_secret: aws_access_key_id
      secret_key:
        from_secret: aws_secret_access_key
      endpoint: https://s3.fr-par.scw.cloud
      region: fr-par
      path_style: true
      source: vikunja-frontend-unstable.zip
      target: /frontend/
    depends_on: [ static ]

---
kind: pipeline
type: docker
name: release-version

depends_on:
  - build

trigger:
  event:
    - tag

steps:
  - name: fetch-tags
    image: docker:git
    commands:
      - git fetch --tags

#  - name: restore-cache
#    image: meltwater/drone-cache:dev
#    pull: always
#    environment:
#      AWS_ACCESS_KEY_ID:
#        from_secret: cache_aws_access_key_id
#      AWS_SECRET_ACCESS_KEY:
#        from_secret: cache_aws_secret_access_key
#    settings:
#      restore: true
#      bucket: kolaente.dev-drone-dependency-cache
#      endpoint: https://s3.fr-par.scw.cloud
#      region: fr-par
#      path_style: true
#      cache_key: '{{ .Repo.Name }}_{{ checksum "pnpm-lock.yaml" }}_{{ arch }}_{{ os }}'
#      mount:
#        - .cache

  - name: build
    image: node:18-alpine
    pull: always
    environment:
      PNPM_CACHE_FOLDER: .cache/pnpm
    commands:
      - apk add git
      - corepack enable && pnpm config set store-dir .cache/pnpm
      - pnpm install --fetch-timeout 100000 --frozen-lockfile
      - pnpm run lint
      - "echo '{\"VERSION\": \"'$(git describe --tags --always --abbrev=10 | sed 's/-/+/' | sed 's/^v//' | sed 's/-g/-/')'\"}' > src/version.json"
      - pnpm run build
      - sed -i 's/http\:\\/\\/localhost\\:3456\\/api\\/v1/\\/api\\/v1/g' dist/index.html # Override the default api url used for developing
#    depends_on:
#      - restore-cache

  - name: static
    image: kolaente/zip
    pull: always
    commands:
      - cd dist
      - zip -r ../vikunja-frontend-${DRONE_TAG##v}.zip *
      - cd ..
    depends_on: [ build ]

  - name: release
    image: plugins/s3
    pull: always
    settings:
      bucket: vikunja-releases
      access_key:
        from_secret: aws_access_key_id
      secret_key:
        from_secret: aws_secret_access_key
      endpoint: https://s3.fr-par.scw.cloud
      region: fr-par
      path_style: true
      source: vikunja-frontend-${DRONE_TAG##v}.zip
      target: /frontend/
    depends_on: [ static ]

---
kind: pipeline
type: docker
name: trigger-desktop-update

trigger:
  branch:
    - main
  event:
    - push

depends_on:
 - release-latest

steps:
  - name: trigger
    image: plugins/downstream
    settings:
      server: https://drone.kolaente.de
      token:
        from_secret: drone_token
      repositories:
        - vikunja/desktop@main

---
kind: pipeline
type: docker
name: docker-release

depends_on:
  - release-latest
  - release-version

trigger:
  ref:
    - refs/heads/main
    - "refs/tags/**"
  event:
    exclude:
      - cron

steps:
  - name: fetch-tags
    image: docker:git
    commands:
      - git fetch --tags

  - name: docker-unstable
    image: thegeeklab/drone-docker-buildx
    privileged: true
    pull: true
    settings:
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password
      repo: vikunja/frontend
      tags: unstable
      build_args:
        - USE_RELEASE=true
        - RELEASE_VERSION=unstable
      platforms:
        - linux/386
        - linux/amd64
        - linux/arm/v6
        - linux/arm/v7
        - linux/arm64/v8
    depends_on: [ fetch-tags ]
    when:
      ref:
        - refs/heads/main

  - name: docker-release
    image: thegeeklab/drone-docker-buildx
    privileged: true
    pull: true
    settings:
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password
      repo: vikunja/frontend
      auto_tag: true
      build_args:
        - USE_RELEASE=true
        - RELEASE_VERSION=${DRONE_TAG##v}
      platforms:
        - linux/386
        - linux/amd64
        - linux/arm/v6
        - linux/arm/v7
        - linux/arm64/v8
    depends_on: [ fetch-tags ]
    when:
      ref:
        - "refs/tags/**"

---
kind: pipeline
type: docker
name: notify

trigger:
  ref:
    - refs/heads/main
    - "refs/tags/**"
  event:
    exclude:
      - cron

depends_on:
  - build
  - release-version
  - release-latest
  - trigger-desktop-update
  - docker-release

steps:
  - name: notify
    image: plugins/matrix
    settings:
      homeserver: https://matrix.org
      roomid: WqBDCxzghKcNflkErL:matrix.org
      username:
        from_secret: matrix_username
      password:
        from_secret: matrix_password
    when:
      status:
        - success
        - failure
---
kind: pipeline
type: docker
name: update-translations

depends_on:
  - build

trigger:
  branch:
    - main
  event:
    - cron
  cron:
    - update_translations

steps:
  - name: download
    pull: always
    image: jonasfranz/crowdin
    settings:
      download: true
      export_dir: src/i18n/lang/
      ignore_branch: true
      project_identifier: vikunja
    environment:
      CROWDIN_KEY:
        from_secret: crowdin_key

  - name: move-files
    pull: always
    image: bash
    depends_on:
      - download
    commands: 
      - mv src/i18n/lang/*/*.json src/i18n/lang

  - name: push
    pull: always
    image: appleboy/drone-git-push
    depends_on:
      - move-files
    settings:
      author_email: "frederik@vikunja.io"
      author_name: Frederick [Bot]
      branch: main
      commit: true
      commit_message: "[skip ci] Updated translations via Crowdin"
      remote: "ssh://git@kolaente.dev:9022/vikunja/frontend.git"
      ssh_key:
        from_secret: translation_git_push_ssh_key

  - name: upload
    pull: always
    image: jonasfranz/crowdin
    depends_on:
      - clone
    settings:
      files:
        en.json: src/i18n/lang/en.json
      ignore_branch: true
      project_identifier: vikunja
    environment:
      CROWDIN_KEY:
        from_secret: crowdin_key
---
kind: signature
hmac: 9f26b5af73e3464e9ee1b5fbcb96854ca8a7e5f8d6ee2d85fd8376aad951b446

...
