name: Release

on:
  workflow_call:

jobs:
  docker:
    runs-on: blacksmith-4vcpu-ubuntu-2204
    steps:
      - name: Git describe
        id: ghd
        uses: proudust/gh-describe@v2
      - name: Login to GHCR
        uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 # v3
        with:
          username: ${{ secrets.DOCKER_HUB_USERNAME }}
          password: ${{ secrets.DOCKER_HUB_PASSWORD }}
      - name: Set up QEMU
        uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392 # v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3
        with:
          version: latest
      - name: Docker meta version
        if: ${{ github.ref_type == 'tag' }}
        id: meta
        uses: docker/metadata-action@902fa8ec7d6ecbf8d84d538b9b233a880e428804 # v5
        with:
          images: |
            vikunja/vikunja
          tags: |
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
            type=raw,value=latest
      - name: Build and push unstable
        if: ${{ github.ref_type != 'tag' }}
        uses: useblacksmith/build-push-action@574eb0ee0b59c6a687ace24192f0727dfb65d6d7 # v1
        with:
          platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8
          push: true
          tags: vikunja/vikunja:unstable
          build-args: |
            RELEASE_VERSION=${{ steps.ghd.outputs.describe }}
      - name: Build and push version
        if: ${{ github.ref_type == 'tag' }}
        uses: useblacksmith/build-push-action@574eb0ee0b59c6a687ace24192f0727dfb65d6d7 # v1
        with:
          platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            RELEASE_VERSION=${{ steps.ghd.outputs.describe }}

  binaries:
    runs-on: blacksmith-8vcpu-ubuntu-2204
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
      - name: Git describe
        id: ghd
        uses: proudust/gh-describe@v2
      - uses: useblacksmith/setup-go@647ac649bd5b480f2a262e3e3e5f4d150ed452ad # v6
        with:
          go-version: stable
      - name: Download Mage Binary
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
        with:
          name: mage_bin
      - name: get frontend
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
        with:
          name: frontend_dist
          path: frontend/dist
      - run: chmod +x ./mage-static
      - name: install upx
        run: |
          wget https://github.com/upx/upx/releases/download/v5.0.0/upx-5.0.0-amd64_linux.tar.xz
          echo 'b32abf118d721358a50f1aa60eacdbf3298df379c431c3a86f139173ab8289a1  upx-5.0.0-amd64_linux.tar.xz' > upx-5.0.0-amd64_linux.tar.xz.sha256
          sha256sum -c upx-5.0.0-amd64_linux.tar.xz.sha256
          tar xf upx-5.0.0-amd64_linux.tar.xz
          mv upx-5.0.0-amd64_linux/upx /usr/local/bin
      - name: GPG setup
        uses: kolaente/action-gpg@main
        with:
          gpg-passphrase: "${{ secrets.RELEASE_GPG_PASSPHRASE }}"
          gpg-sign-key: "${{ secrets.RELEASE_GPG_SIGN_KEY }}"
      - name: setup xgo cache
        uses: useblacksmith/cache@c5fe29eb0efdf1cf4186b9f7fcbbcbc0cf025662 # v5
        with:
          path: /home/runner/.xgo-cache
          key: ${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-
      - name: build and release
        env:
          RELEASE_VERSION: ${{ steps.ghd.outputs.describe }}
          XGO_OUT_NAME: vikunja-${{ github.ref_type == 'tag' && steps.ghd.outputs.describe || 'unstable' }}
        run: |
          export PATH=$PATH:$GOPATH/bin
          ./mage-static release
      - name: sign
        run: |
          ls -hal dist/zip/*
          for file in dist/zip/*; do
            gpg -v --default-key 7D061A4AA61436B40713D42EFF054DACD908493A -b --batch --yes --passphrase "${{ secrets.RELEASE_GPG_PASSPHRASE }}" --pinentry-mode loopback --sign "$file"
          done
      - name: Upload
        uses: kolaente/s3-action@41963184b524ccac734ea4d8c964ac74b5b1af89 # v1.2.1
        with:
          s3-access-key-id: ${{ secrets.HETZNER_S3_ACCESS_KEY }}
          s3-secret-access-key: ${{ secrets.HETZNER_S3_SECRET_KEY }}
          s3-endpoint: "https://fsn1.your-objectstorage.com"
          s3-bucket: "vikunja"
          s3-region: "fsn1"
          target-path: /vikunja/${{ github.ref_type == 'tag' && steps.ghd.outputs.describe || 'unstable' }}
          files: "dist/zip/*"
          strip-path-prefix: dist/zip/
      - name: Store Binaries
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
        with:
          name: vikunja_bins
          path: ./dist/binaries/*

  os-package:
    runs-on: ubuntu-latest
    needs:
      - binaries
    strategy:
      matrix:
        package:
          - rpm
          - deb
          - apk
          - archlinux

    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
      - name: Download Vikunja Binary
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
        with:
          name: vikunja_bins
          pattern: vikunja-*-linux-amd64
      - name: Git describe
        id: ghd
        uses: proudust/gh-describe@v2
      - name: Download Mage Binary
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
        with:
          name: mage_bin
      - name: Prepare
        env:
          RELEASE_VERSION: ${{ steps.ghd.outputs.describe }}
        run: |
          chmod +x ./mage-static
          ./mage-static release:prepare-nfpm-config
          mkdir -p ./dist/os-packages
          mv ./vikunja-*-linux-amd64 ./vikunja
          chmod +x ./vikunja
      - name: Create package
        id: nfpm
        uses: kolaente/action-gh-nfpm@master
        with:
          packager: ${{ matrix.package }}
          target: ./dist/os-packages/vikunja-${{ github.ref_type == 'tag' && steps.ghd.outputs.describe || 'unstable' }}-x86_64.${{ matrix.package }}
          config: ./nfpm.yaml
      - name: Upload
        uses: kolaente/s3-action@41963184b524ccac734ea4d8c964ac74b5b1af89 # v1.2.1
        with:
          s3-access-key-id: ${{ secrets.HETZNER_S3_ACCESS_KEY }}
          s3-secret-access-key: ${{ secrets.HETZNER_S3_SECRET_KEY }}
          s3-endpoint: "https://fsn1.your-objectstorage.com"
          s3-bucket: "vikunja"
          s3-region: "fsn1"
          target-path: /vikunja/${{ github.ref_type == 'tag' && steps.ghd.outputs.describe || 'unstable' }}
          files: "dist/os-packages/*"
          strip-path-prefix: dist/os-packages/

  config-yaml:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
      - name: Git describe
        id: ghd
        uses: proudust/gh-describe@v2
      - name: Download Mage Binary
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
        with:
          name: mage_bin
      - name: generate
        run: |
          chmod +x ./mage-static
          ./mage-static generate:config-yaml 1
      - name: Upload to S3
        uses: kolaente/s3-action@41963184b524ccac734ea4d8c964ac74b5b1af89 # v1.2.1
        with:
          s3-access-key-id: ${{ secrets.HETZNER_S3_ACCESS_KEY }}
          s3-secret-access-key: ${{ secrets.HETZNER_S3_SECRET_KEY }}
          s3-endpoint: "https://fsn1.your-objectstorage.com"
          s3-bucket: "vikunja"
          s3-region: "fsn1"
          target-path: /vikunja/${{ github.ref_type == 'tag' && steps.ghd.outputs.describe || 'unstable' }}
          files: "config.yml.sample"

  desktop:
    strategy:
      matrix:
        os:
          - ubuntu-latest
          - windows-latest
          - macos-latest
    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
      - name: Git describe
        id: ghd
        uses: proudust/gh-describe@v2
      - name: Install pnpm
        uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4
        with:
          package_json_file: desktop/package.json
      - name: Setup Node
        uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
        with:
          node-version-file: frontend/.nvmrc
          cache: pnpm
          cache-dependency-path: desktop/pnpm-lock.yaml
      - name: Install Linux dependencies
        if: ${{ runner.os == 'Linux' }}
        run: |
          sudo apt-get update
          sudo apt-get install --no-install-recommends -y libopenjp2-tools rpm libarchive-tools
      - name: get frontend
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
        with:
          name: frontend_dist
          path: frontend/dist
      - name: Build desktop app
        working-directory: desktop
        run: |
          pnpm install  --frozen-lockfile --prefer-offline --fetch-timeout 100000
          node build.js "${{ steps.ghd.outputs.describe }}" ${{ github.ref_type == 'tag' }}
      - name: Upload to S3
        uses: kolaente/s3-action@41963184b524ccac734ea4d8c964ac74b5b1af89 # v1.2.1
        with:
          s3-access-key-id: ${{ secrets.HETZNER_S3_ACCESS_KEY }}
          s3-secret-access-key: ${{ secrets.HETZNER_S3_SECRET_KEY }}
          s3-endpoint: "https://fsn1.your-objectstorage.com"
          s3-bucket: "vikunja"
          s3-region: "fsn1"
          files: "desktop/dist/Vikunja*"
          target-path: /desktop/${{ github.ref_type == 'tag' && steps.ghd.outputs.describe || 'unstable' }}
          strip-path-prefix: desktop/dist/
          exclude: "desktop/dist/*.blockmap"

  generate-swagger-docs:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
        with:
          ssh-key: ${{ secrets.SSH_PRIVATE_KEY }}
          persist-credentials: true
      - name: Download Mage Binary
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
        with:
          name: mage_bin
      - name: Set up Go
        uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5
        with:
          go-version: stable
      - name: generate
        run: |
          export PATH=$PATH:$GOPATH/bin
          go install github.com/swaggo/swag/cmd/swag
          chmod +x ./mage-static
          ./mage-static generate:swagger-docs
      - name: Check for changes
        id: check_changes
        run: |
          if git diff --quiet; then
            echo "changes_exist=0" >> "$GITHUB_OUTPUT"
          else
            echo "changes_exist=1" >> "$GITHUB_OUTPUT"
          fi
      - name: Commit files
        if: steps.check_changes.outputs.changes_exist != '0'
        run: |
          git config --local user.email "bot@vikunja.io"
          git config --local user.name "Frederick [Bot]"
          git commit -am "[skip ci] Updated swagger docs"
      - name: Push changes
        if: steps.check_changes.outputs.changes_exist != '0'
        uses: ad-m/github-push-action@master
        with:
          ssh: true
          branch: ${{ github.ref }}
