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

trigger:
  event:
    exclude:
      - cron

workspace:
  base: /go
  path: src/code.vikunja.io/api

volumes:
  - name: tmp-sqlite-unit
    temp:
      medium: memory
  - name: tmp-sqlite-integration
    temp:
      medium: memory
  - name: tmp-sqlite-migration
    temp:
      medium: memory
  - name: tmp-mysql-unit
    temp:
      medium: memory
  - name: tmp-mysql-integration
    temp:
      medium: memory
  - name: tmp-mysql-migration
    temp:
      medium: memory
  - name: tmp-postgres-unit
    temp:
      medium: memory
  - name: tmp-postgres-integration
    temp:
      medium: memory
  - name: tmp-postgres-migration
    temp:
      medium: memory
      

services:
  - name: test-mysql-unit
    image: mariadb:11
    environment:
      MYSQL_ROOT_PASSWORD: vikunjatest
      MYSQL_DATABASE: vikunjatest
    volumes:
      - name: tmp-mysql-unit
        path: /var/lib/mysql
  - name: test-mysql-integration
    image: mariadb:11
    environment:
      MYSQL_ROOT_PASSWORD: vikunjatest
      MYSQL_DATABASE: vikunjatest
    volumes:
      - name: tmp-mysql-integration
        path: /var/lib/mysql
  - name: test-mysql-migration
    image: mariadb:11
    environment:
      MYSQL_ROOT_PASSWORD: vikunjatest
      MYSQL_DATABASE: vikunjatest
    volumes:
      - name: tmp-mysql-migration
        path: /var/lib/mysql
  - name: test-postgres-unit
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: vikunjatest
      POSTGRES_DB: vikunjatest
    volumes:
      - name: tmp-postgres-unit
        path: /var/lib/postgresql/data
    commands:
      - docker-entrypoint.sh -c fsync=off -c full_page_writes=off # turns of wal
  - name: test-postgres-integration
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: vikunjatest
      POSTGRES_DB: vikunjatest
    volumes:
      - name: tmp-postgres-integration
        path: /var/lib/postgresql/data
    commands:
      - docker-entrypoint.sh -c fsync=off -c full_page_writes=off # turns of wal
  - name: test-postgres-migration
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: vikunjatest
      POSTGRES_DB: vikunjatest
    volumes:
      - name: tmp-postgres-migration
        path: /var/lib/postgresql/data
    commands:
      - docker-entrypoint.sh -c fsync=off -c full_page_writes=off # turns of wal

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

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

  # We're statically compiling the magefile to avoid race condition issues caused by multiple pipeline steps
  # compiling the same magefile at the same time. It's also faster if each step does not need to compile it first.
  - name: mage
    image: vikunja/golang-build:latest
    pull: always
    environment:
      GOPROXY: 'https://goproxy.kolaente.de'
    commands:
      - mage -compile ./mage-static
      - mkdir -p frontend/dist
      - touch frontend/dist/index.html
    when:
      event: [ push, tag, pull_request ]

  - name: api-build
    image: vikunja/golang-build:latest
    pull: always
    environment:
      GOPROXY: 'https://goproxy.kolaente.de'
    depends_on: [ mage ]
    commands:
      - ./mage-static build:build
    when:
      event: [ push, tag, pull_request ]

  - name: api-lint
    image: golangci/golangci-lint:v1.56.2
    pull: always
    environment:
      GOPROXY: 'https://goproxy.kolaente.de'
    depends_on: [ mage ]
    commands:
      - export "GOROOT=$(go env GOROOT)"
      - ./mage-static check:golangci
    when:
      event: [ push, tag, pull_request ]

  - name: test-migration-prepare
    image: kolaente/toolbox:latest
    pull: always
    commands:
      # Get the latest version
      - wget https://dl.vikunja.io/api/unstable/vikunja-unstable-linux-amd64-full.zip -q -O vikunja-latest.zip
      - unzip vikunja-latest.zip vikunja-unstable-linux-amd64

  - name: test-migration-sqlite
    image: vikunja/golang-build:latest
    pull: always
    depends_on: 
      - test-migration-prepare
      - api-build
    environment:
      VIKUNJA_DATABASE_TYPE: sqlite
      VIKUNJA_DATABASE_PATH: /db/vikunja-migration-test.db
      VIKUNJA_LOG_DATABASE: stdout
      VIKUNJA_LOG_DATABASELEVEL: debug
    volumes:
      - name: tmp-sqlite-migration
        path: /db
    commands:
      - ./vikunja-unstable-linux-amd64 migrate
      # Run the migrations from the binary build in the step before
      - ./vikunja migrate
    when:
      event: [ push, tag, pull_request ]

  - name: test-migration-mysql
    image: vikunja/golang-build:latest
    pull: always
    depends_on:
      - test-migration-prepare
      - api-build
    environment:
      VIKUNJA_DATABASE_TYPE: mysql
      VIKUNJA_DATABASE_HOST: test-mysql-migration
      VIKUNJA_DATABASE_USER: root
      VIKUNJA_DATABASE_PASSWORD: vikunjatest
      VIKUNJA_DATABASE_DATABASE: vikunjatest
      VIKUNJA_LOG_DATABASE: stdout
      VIKUNJA_LOG_DATABASELEVEL: debug
    commands:
      - ./vikunja-unstable-linux-amd64 migrate
      # Run the migrations from the binary build in the step before
      - ./vikunja migrate
    when:
      event: [ push, tag, pull_request ]

  - name: test-migration-psql
    image: vikunja/golang-build:latest
    pull: always
    depends_on:
      - test-migration-prepare
      - api-build
    environment:
      VIKUNJA_DATABASE_TYPE: postgres
      VIKUNJA_DATABASE_HOST: test-postgres-migration
      VIKUNJA_DATABASE_USER: postgres
      VIKUNJA_DATABASE_PASSWORD: vikunjatest
      VIKUNJA_DATABASE_DATABASE: vikunjatest
      VIKUNJA_DATABASE_SSLMODE: disable
      VIKUNJA_LOG_DATABASE: stdout
      VIKUNJA_LOG_DATABASELEVEL: debug
    commands:
      - ./vikunja-unstable-linux-amd64 migrate
      # Run the migrations from the binary build in the step before
      - ./vikunja migrate
    when:
      event: [ push, tag, pull_request ]

  - name: api-test-unit
    image: vikunja/golang-build:latest
    pull: always
    environment:
      GOPROXY: 'https://goproxy.kolaente.de'
    commands:
      - ./mage-static test:unit
    depends_on: [ fetch-tags, mage ]
    when:
      event: [ push, tag, pull_request ]

  - name: api-test-unit-sqlite
    image: vikunja/golang-build:latest
    pull: always
    environment:
      GOPROXY: 'https://goproxy.kolaente.de'
      VIKUNJA_TESTS_USE_CONFIG: 1
      VIKUNJA_DATABASE_TYPE: sqlite
      VIKUNJA_DATABASE_PATH: /db/vikunja-test.db
    volumes:
      - name: tmp-sqlite-unit
        path: /db
    commands:
      - ./mage-static test:unit
    depends_on: [ fetch-tags, mage ]
    when:
      event: [ push, tag, pull_request ]

  - name: api-test-unit-mysql
    image: vikunja/golang-build:latest
    pull: always
    environment:
      GOPROXY: 'https://goproxy.kolaente.de'
      VIKUNJA_TESTS_USE_CONFIG: 1
      VIKUNJA_DATABASE_TYPE: mysql
      VIKUNJA_DATABASE_HOST: test-mysql-unit
      VIKUNJA_DATABASE_USER: root
      VIKUNJA_DATABASE_PASSWORD: vikunjatest
      VIKUNJA_DATABASE_DATABASE: vikunjatest
    commands:
      - ./mage-static test:unit
    depends_on: [ fetch-tags, mage ]
    when:
      event: [ push, tag, pull_request ]

  - name: api-test-unit-postgres
    image: vikunja/golang-build:latest
    pull: always
    environment:
      GOPROXY: 'https://goproxy.kolaente.de'
      VIKUNJA_TESTS_USE_CONFIG: 1
      VIKUNJA_DATABASE_TYPE: postgres
      VIKUNJA_DATABASE_HOST: test-postgres-unit
      VIKUNJA_DATABASE_USER: postgres
      VIKUNJA_DATABASE_PASSWORD: vikunjatest
      VIKUNJA_DATABASE_DATABASE: vikunjatest
      VIKUNJA_DATABASE_SSLMODE: disable
    commands:
      - ./mage-static test:unit
    depends_on: [ fetch-tags, mage ]
    when:
      event: [ push, tag, pull_request ]

  - name: integration-test
    image: vikunja/golang-build:latest
    pull: always
    environment:
      GOPROXY: 'https://goproxy.kolaente.de'
    commands:
      - ./mage-static test:integration
    depends_on: [ fetch-tags, mage ]
    when:
      event: [ push, tag, pull_request ]

  - name: integration-test-sqlite
    image: vikunja/golang-build:latest
    pull: always
    environment:
      GOPROXY: 'https://goproxy.kolaente.de'
      VIKUNJA_TESTS_USE_CONFIG: 1
      VIKUNJA_DATABASE_TYPE: sqlite
      VIKUNJA_DATABASE_PATH: /db/vikunja-test.db
    volumes:
      - name: tmp-sqlite-integration
        path: /db
    commands:
      - ./mage-static test:integration
    depends_on: [ fetch-tags, mage ]
    when:
      event: [ push, tag, pull_request ]

  - name: integration-test-mysql
    image: vikunja/golang-build:latest
    pull: always
    environment:
      GOPROXY: 'https://goproxy.kolaente.de'
      VIKUNJA_TESTS_USE_CONFIG: 1
      VIKUNJA_DATABASE_TYPE: mysql
      VIKUNJA_DATABASE_HOST: test-mysql-integration
      VIKUNJA_DATABASE_USER: root
      VIKUNJA_DATABASE_PASSWORD: vikunjatest
      VIKUNJA_DATABASE_DATABASE: vikunjatest
    commands:
      - ./mage-static test:integration
    depends_on: [ fetch-tags, mage ]
    when:
      event: [ push, tag, pull_request ]

  - name: integration-test-postgres
    image: vikunja/golang-build:latest
    pull: always
    environment:
      GOPROXY: 'https://goproxy.kolaente.de'
      VIKUNJA_TESTS_USE_CONFIG: 1
      VIKUNJA_DATABASE_TYPE: postgres
      VIKUNJA_DATABASE_HOST: test-postgres-integration
      VIKUNJA_DATABASE_USER: postgres
      VIKUNJA_DATABASE_PASSWORD: vikunjatest
      VIKUNJA_DATABASE_DATABASE: vikunjatest
      VIKUNJA_DATABASE_SSLMODE: disable
    commands:
      - ./mage-static test:integration
    depends_on: [ fetch-tags, mage ]
    when:
      event: [ push, tag, pull_request ]

  - name: test-api-run
    image: vikunja/golang-build:latest
    pull: always
    environment:
      VIKUNJA_SERVICE_TESTINGTOKEN: averyLongSecretToSe33dtheDB
      VIKUNJA_LOG_LEVEL: DEBUG
      VIKUNJA_CORS_ENABLE: 1
      VIKUNJA_DATABASE_PATH: memory
      VIKUNJA_DATABASE_TYPE: sqlite
    commands:
      - ./vikunja
    detach: true
    depends_on:
      - api-build

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

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

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

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

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

  - name: frontend-test
    image: cypress/browsers:node18.12.0-chrome107
    pull: always
    environment:
      CYPRESS_API_URL: http://test-api-run: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:
      - cd frontend
      - sed -i 's/localhost/test-api-run/g' dist-test/index.html
      - corepack enable && pnpm config set store-dir .cache/pnpm
      - pnpm cypress install
      - pnpm run test:e2e-record-test
    depends_on:
      - frontend-build-prod
      - test-api-run
  
  - name: frontend-deploy-preview
    image: williamjackson/netlify-cli
    pull: always
    user: root # The rest runs as root and thus the permissions wouldn't work
    environment:
      NETLIFY_AUTH_TOKEN:
        from_secret: netlify_auth_token
      NETLIFY_SITE_ID:
        from_secret: netlify_site_id
      GITEA_TOKEN:
        from_secret: gitea_token
    commands:
      - cd frontend
      - cp -r dist-test 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
      # create via:
      # `shasum -a 384 ./scripts/deploy-preview-netlify.mjs > ./scripts/deploy-preview-netlify.mjs.sha384`
      - shasum -a 384 -c ./scripts/deploy-preview-netlify.mjs.sha384
      - node ./scripts/deploy-preview-netlify.mjs
    depends_on:
      - frontend-build-prod
    when:
      event:
        include:
          - pull_request

---
kind: pipeline
type: docker
name: generate-swagger-docs

depends_on:
  - build-and-test

workspace:
  base: /go
  path: src/code.vikunja.io/api

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

steps:
  - name: generate-swagger-docs
    image: vikunja/golang-build:latest
    pull: always
    environment:
      GOPROXY: 'https://goproxy.kolaente.de'
    commands:
      - mage do-the-swag

  - name: push
    pull: always
    image: appleboy/drone-git-push
    depends_on:
      - generate-swagger-docs
    settings:
      author_email: "frederik@vikunja.io"
      author_name: Frederick [Bot]
      branch: main
      commit: true
      commit_message: "[skip ci] Updated swagger docs"
      remote: "ssh://git@kolaente.dev:9022/vikunja/vikunja.git"
      ssh_key:
        from_secret: git_push_ssh_key

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

depends_on:
  - build-and-test

workspace:
  base: /source
  path: /

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

steps:
  # Needed to get the versions right as they depend on tags
  - name: fetch-tags
    image: docker:git
    commands:
      - git fetch --tags

  - name: frontend-dependencies
    image: node:20.11.1-alpine
    pull: always
    environment:
      PNPM_CACHE_FOLDER: .cache/pnpm
      CYPRESS_CACHE_FOLDER: .cache/cypress
      PUPPETEER_SKIP_DOWNLOAD: true
    commands:
      - cd frontend
      - corepack enable && pnpm config set store-dir .cache/pnpm
      - pnpm install --fetch-timeout 100000

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

  # We're statically compiling the magefile to avoid race condition issues caused by multiple pipeline steps
  # compiling the same magefile at the same time. It's also faster if each step does not need to compile it first.
  - name: mage
    image: vikunja/golang-build:latest
    pull: always
    environment:
      GOPROXY: 'https://goproxy.kolaente.de'
    commands:
      - mage -compile ./mage-static
    when:
      event: [ push, tag, pull_request ]

  - name: before-static-build
    image: techknowlogick/xgo:latest
    pull: always
    commands:
      - export PATH=$PATH:$GOPATH/bin
      - go install github.com/magefile/mage
      - ./mage-static release:dirs
    depends_on: [ fetch-tags, mage ]

  - name: static-build-windows
    image: techknowlogick/xgo:latest
    pull: always
    environment:
      # This path does not exist. However, when we set the gopath to /go, the build fails. Not sure why.
      # Leaving this here until we know how to resolve this properly.
      GOPATH: /srv/app
      GOPROXY: https://goproxy.kolaente.de
    commands:
      - export PATH=$PATH:$GOPATH/bin
      - go install github.com/magefile/mage
      - ./mage-static release:windows
    depends_on:
      - before-static-build
      - frontend-build

  - name: static-build-linux
    image: techknowlogick/xgo:latest
    pull: always
    environment:
      # This path does not exist. However, when we set the gopath to /go, the build fails. Not sure why.
      # Leaving this here until we know how to resolve this properly.
      GOPATH: /srv/app
      GOPROXY: https://goproxy.kolaente.de
    commands:
      - export PATH=$PATH:$GOPATH/bin
      - go install github.com/magefile/mage
      - ./mage-static release:linux
    depends_on:
      - before-static-build
      - frontend-build

  - name: static-build-darwin
    image: techknowlogick/xgo:latest
    pull: always
    environment:
      # This path does not exist. However, when we set the gopath to /go, the build fails. Not sure why.
      # Leaving this here until we know how to resolve this properly.
      GOPATH: /srv/app
      GOPROXY: https://goproxy.kolaente.de
    commands:
      - export PATH=$PATH:$GOPATH/bin
      - go install github.com/magefile/mage
      - ./mage-static release:darwin
    depends_on:
      - before-static-build
      - frontend-build

  - name: after-build-compress
    image: kolaente/upx
    pull: always
    depends_on:
      - static-build-windows
      - static-build-linux
      - static-build-darwin
    commands:
      - ./mage-static release:compress

  - name: after-build-static
    image: techknowlogick/xgo:latest
    pull: always
    depends_on:
      - after-build-compress
    commands:
      - go install github.com/magefile/mage
      - ./mage-static release:copy
      - ./mage-static release:check
      - ./mage-static release:os-package
      - ./mage-static release:zip

  - name: sign-release
    image: plugins/gpgsign:1
    pull: always
    depends_on: [ after-build-static ]
    settings:
      key:
        from_secret: gpg_privkey
      passphrase:
        from_secret: gpg_password
      files:
        - dist/zip/*
      detach_sign: true

  # Push the releases to our pseudo-s3-bucket
  - name: release-unstable
    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
      strip_prefix: dist/zip/
      source: dist/zip/*
      target: /vikunja/unstable/
    when:
      branch:
        - main
      event:
        - push
    depends_on: [ sign-release ]

  - name: release-version
    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
      strip_prefix: dist/zip/
      source: dist/zip/*
      target: /vikunja/${DRONE_TAG##v}/
    when:
      event:
        - tag
    depends_on: [ sign-release ]

  # Build os packages and push it to our bucket
  - name: build-os-packages-unstable
    image: goreleaser/nfpm:v2.35.3
    pull: always
    commands:
      - apk add git go
      - ./mage-static release:packages
      - mv dist/os-packages/vikunja*.x86_64.rpm dist/os-packages/vikunja-unstable-x86_64.rpm
      - mv dist/os-packages/vikunja*_amd64.deb dist/os-packages/vikunja-unstable-amd64.deb
      - mv dist/os-packages/vikunja*_x86_64.apk dist/os-packages/vikunja-unstable-x86_64.apk
    when:
      branch:
        - main
      event:
        - push
    depends_on: [ after-build-compress ]

  - name: build-os-packages-version
    image: goreleaser/nfpm:v2.35.3
    pull: always
    commands:
      - apk add git go
      - ./mage-static release:packages
      - mv dist/os-packages/vikunja*.x86_64.rpm dist/os-packages/vikunja-${DRONE_TAG##v}-x86_64.rpm
      - mv dist/os-packages/vikunja*_amd64.deb dist/os-packages/vikunja-${DRONE_TAG##v}-amd64.deb
      - mv dist/os-packages/vikunja*_x86_64.apk dist/os-packages/vikunja-${DRONE_TAG##v}-x86_64.apk
    when:
      event:
        - tag
    depends_on: [ after-build-compress ]

  # Push the os releases to our pseudo-s3-bucket
  - name: release-os-latest
    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
      strip_prefix: dist/os-packages/
      source: dist/os-packages/*
      target: /vikunja/unstable/
    when:
      branch:
        - main
      event:
        - push
    depends_on: [ build-os-packages-unstable ]

  - name: release-os-version
    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
      strip_prefix: dist/os-packages/
      source: dist/os-packages/*
      target: /vikunja/${DRONE_TAG##v}/
    when:
      event:
        - tag
    depends_on: [ build-os-packages-version ]

  - name: gitea-release
    image: plugins/gitea-release
    pull: true
    settings:
      api_key:
        from_secret: gitea_token
      base_url: https://kolaente.dev
      files: dist/zip/*
      prerelease: true
      title: ${DRONE_TAG##v} 
    when:
      event:
        - tag
    depends_on: [ sign-release ]

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

depends_on:
  - build-and-test

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: always
    settings:
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password
      repo: vikunja/vikunja
      tags: 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: generate-tags
    image: thegeeklab/docker-autotag
    environment:
      DOCKER_AUTOTAG_VERSION: ${DRONE_TAG}
      DOCKER_AUTOTAG_EXTRA_TAGS: latest
      DOCKER_AUTOTAG_OUTPUT_FILE: .tags
    depends_on: [ fetch-tags ]
    when:
      ref:
        - "refs/tags/**"

  - name: docker-release
    image: thegeeklab/drone-docker-buildx
    privileged: true
    pull: always
    settings:
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password
      repo: vikunja/vikunja
      platforms:
        - linux/386
        - linux/amd64
        - linux/arm/v6
        - linux/arm/v7
        - linux/arm64/v8
    depends_on: [ generate-tags ]
    when:
      ref:
        - "refs/tags/**"


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

depends_on:
  - build-and-test

trigger:
  branch:
    - main
  event:
    - push

steps:
  - name: fetch-tags
    image: docker:git
    commands:
      - git fetch --tags
  
  - name: build
    image: node:20.11.1-alpine
    pull: always
    environment:
      PNPM_CACHE_FOLDER: .cache/pnpm
      SENTRY_AUTH_TOKEN:
        from_secret: sentry_auth_token
      SENTRY_ORG: vikunja
      SENTRY_PROJECT: frontend-oss
      PUPPETEER_SKIP_DOWNLOAD: true
    commands:
      - cd frontend
      - corepack enable && pnpm config set store-dir .cache/pnpm
      - pnpm install --fetch-timeout 100000 --frozen-lockfile
      - pnpm run lint
      - 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

  - name: static
    image: kolaente/zip
    pull: always
    commands:
      - cd frontend
      - cd dist
      - zip -r ../vikunja-frontend-unstable.zip *
    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: frontend/vikunja-frontend-unstable.zip
      target: /
    depends_on: [ static ]

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

depends_on:
  - build-and-test

trigger:
  event:
    - tag

steps:
  - name: fetch-tags
    image: docker:git
    commands:
      - git fetch --tags
  
  - name: build
    image: node:20.11.1-alpine
    pull: always
    environment:
      PNPM_CACHE_FOLDER: .cache/pnpm
      SENTRY_AUTH_TOKEN:
        from_secret: sentry_auth_token
      SENTRY_ORG: vikunja
      SENTRY_PROJECT: frontend-oss
    commands:
      - cd frontend
      - corepack enable && pnpm config set store-dir .cache/pnpm
      - pnpm install --fetch-timeout 100000 --frozen-lockfile
      - 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

  - name: static
    image: kolaente/zip
    pull: always
    commands:
      - cd frontend
      - cd dist
      - zip -r ../vikunja-frontend-${DRONE_TAG##v}.zip *
    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: frontend/vikunja-frontend-${DRONE_TAG##v}.zip
      target: /
    depends_on: [ static ]

---
kind: pipeline
type: docker
name: update-translations

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

steps:
  - name: download
    pull: always
    image: ghcr.io/kolaente/kolaente/drone-crowdin-v2:latest
    settings:
      crowdin_key:
        from_secret: crowdin_key
      project_id: 462614
      target: download
      download_to: frontend/src/i18n/lang/
      download_export_approved_only: true

  - name: move-files
    pull: always
    image: bash
    depends_on:
      - download
    commands:
      - mv frontend/src/i18n/lang/*/*.json frontend/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: "chore(i18n): update translations via Crowdin"
      remote: "ssh://git@kolaente.dev:9022/vikunja/vikunja.git"
      ssh_key:
        from_secret: git_push_ssh_key

  - name: upload
    pull: always
    image: ghcr.io/kolaente/kolaente/drone-crowdin-v2:latest
    depends_on:
      - clone
    settings:
      crowdin_key:
        from_secret: crowdin_key
      project_id: 462614
      target: upload
      upload_files:
        frontend/src/i18n/lang/en.json: en.json

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

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

steps:
  - name: fetch-tags
    image: docker:git
    commands:
      - git fetch --tags
#
#  - name: restore-cache
#    image: meltwater/drone-cache:dev
#    pull: true
#    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 "desktop/yarn.lock" }}_{{ arch }}_{{ os }}'
#      mount:
#        - '.cache'

  - name: build
    image: electronuserland/builder:wine-mono
    pull: true
    environment:
      YARN_CACHE_FOLDER: .cache/yarn/
    depends_on:
      - fetch-tags
#      - restore-cache
    commands:
      - cd desktop
      - export VERSION=${DRONE_TAG##v}
      - if [ -z "$$VERSION" ]; then export VERSION=unstable; fi
      # TODO: Move to a yarn script
      - wget https://dl.vikunja.io/frontend/vikunja-frontend-$$VERSION.zip
      - unzip vikunja-frontend-$$VERSION.zip -d frontend
      - sed -i 's/\\/api\\/v1//g' frontend/index.html
      - ./bumpp.sh
      - yarn install
      - yarn dist --linux --windows

#  - name: rebuild-cache
#    image: meltwater/drone-cache:dev
#    pull: true
#    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 "desktop/yarn.lock" }}_{{ arch }}_{{ os }}'
#      mount:
#        - '.cache'
#    depends_on:
#      - build

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

depends_on:
  - frontend-release-unstable
  - frontend-release-version

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

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

#  - name: restore-cache
#    image: meltwater/drone-cache:dev
#    pull: true
#    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 "desktop/yarn.lock" }}_{{ arch }}_{{ os }}'
#      mount:
#        - '.cache'

  - name: build
    image: electronuserland/builder:wine-mono
    pull: true
    environment:
      YARN_CACHE_FOLDER: .cache/yarn/
    depends_on:
      - fetch-tags
#      - restore-cache
    commands:
      - cd desktop
      - export VERSION=${DRONE_TAG##v}
      - if [ -z "$$VERSION" ]; then export VERSION=unstable; fi
      # TODO: Move to a yarn script
      - wget https://dl.vikunja.io/frontend/vikunja-frontend-$$VERSION.zip
      - unzip vikunja-frontend-$$VERSION.zip -d frontend
      - sed -i 's/\\/api\\/v1//g' frontend/index.html
      - ./bumpp.sh
      - yarn install
      - cat package.json
      - yarn dist --linux --windows

#  - name: rebuild-cache
#    image: meltwater/drone-cache:dev
#    pull: true
#    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 "desktop/yarn.lock" }}_{{ arch }}_{{ os }}'
#      mount:
#        - '.cache'
#    depends_on:
#      - build
  
  - name: rename-unstable
    image: bash
    pull: true
    commands:
      - cd desktop/dist
      - bash -c 'for file in Vikunja*; do suffix=".$${file##*.}"; if [[ ! -d $file ]]; then mv "$file" "Vikunja-Desktop-unstable$${suffix}"; fi; done'
    depends_on:
      - build
    when:
      event:
        exclude:
          - tag

  - name: release-unstable
    image: plugins/s3
    pull: true
    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
      strip_prefix: desktop/dist/
      source: desktop/dist/Vikunja-Desktop*
      target: /desktop/unstable/
    when:
      event:
        exclude:
          - tag
    depends_on:
      - rename-unstable

  - name: release-version
    image: plugins/s3
    pull: true
    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
      strip_prefix: desktop/dist/
      source: desktop/dist/*
      target: /desktop/${DRONE_TAG##v}/
    when:
      event:
        - tag
    depends_on: [ build ]

# Disabled until we have a working mac runner
#---
#kind: pipeline
#type: exec
#name: desktop-release-macos
#
#trigger:
#  ref:
#    - refs/heads/main
#    - "refs/tags/**"
#
#platform:
#  os: darwin
#  arch: amd64
#
#steps:
#  - name: build
#    environment:
#      ACCESS_KEY:
#        from_secret: aws_access_key_id
#      SECRET_KEY:
#        from_secret: aws_secret_access_key
#    commands:
#      - git fetch --tags
#      - export VERSION=${DRONE_TAG##v}
#      - if [ -z "$$VERSION" ]; then export VERSION=unstable; fi
#      # TODO: Move to a yarn script
#      - wget https://dl.vikunja.io/frontend/vikunja-frontend-$$VERSION.zip
#      - unzip vikunja-frontend-$$VERSION.zip -d frontend
#      - sed -i '' 's/\\/api\\/v1//g' frontend/index.html
#      # Make sure that the -unstable suffix is added to release files
#      - sed -i '' "s/\$${version}/$$VERSION/g" package.json
#      - yarn install
#      - yarn dist --mac
#      - mc config host add scw-fr-par https://s3.fr-par.scw.cloud $ACCESS_KEY $SECRET_KEY --api S3v4
#      - mc cp ./dist/*.dmg scw-fr-par/vikunja-releases/desktop/$VERSION/
#      - mc cp ./dist/*.dmg.blockmap scw-fr-par/vikunja-releases/desktop/$VERSION/

---
kind: pipeline
type: docker
name: deploy-docs

workspace:
  base: /go
  path: src/code.vikunja.io/api

clone:
  depth: 50

trigger:
  event:
    - push
  branch:
    - main

steps:
  - name: theme
    image: kolaente/toolbox
    pull: always
    commands:
      - mkdir docs/themes/vikunja -p
      - cd docs/themes/vikunja
      - wget https://dl.vikunja.io/theme/vikunja-theme.tar.gz
      - tar -xzf vikunja-theme.tar.gz

  - name: build
    image: klakegg/hugo:0.111.3
    pull: always
    commands:
      - cd docs
      - hugo
      - mv public/docs/* public # Hugo seems to be not capable of setting a different theme for a home page, so we do this ugly hack to fix it.

  - name: docker
    image: plugins/docker
    pull: always
    settings:
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password
      repo: vikunja/docs
      context: docs/
      dockerfile: docs/Dockerfile
---
kind: pipeline
type: docker
name: notify

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

depends_on:
  - build-and-test
  - release
  - deploy-docs
  - docker-release
  - desktop-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: signature
hmac: a5d31a6cb5eb6482e72bea619ee391ff2b8118b9865e3896a607b8a7e874a797

...
