name: Release

on:
  workflow_dispatch:
    inputs:
      bump:
        description: "Version bump type"
        required: true
        default: "patch"
        type: choice
        options:
          - patch
          - minor
          - major

jobs:
  ci:
    runs-on: docker
    container:
      image: oven/bun:1
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install dependencies
        run: bun install --frozen-lockfile --ignore-scripts

      - name: Lint
        run: bun run lint

      - name: Test
        run: bun test

      - name: Build
        run: bun run build

  release:
    needs: ci
    runs-on: dind
    steps:
      - name: Clone repository
        run: |
          apk add --no-cache git curl jq docker-cli
          git clone https://${{ secrets.GITEA_TOKEN }}@gitea.jeanlucmakiola.de/${{ gitea.repository }}.git repo
          cd repo
          git checkout ${{ gitea.ref_name }}

      - name: Compute version
        working-directory: repo
        run: |
          LATEST_TAG=$(git tag -l 'v*' --sort=-v:refname | head -n1)
          if [ -z "$LATEST_TAG" ]; then
            LATEST_TAG="v0.0.0"
          fi
          MAJOR=$(echo "$LATEST_TAG" | sed 's/v//' | cut -d. -f1)
          MINOR=$(echo "$LATEST_TAG" | sed 's/v//' | cut -d. -f2)
          PATCH=$(echo "$LATEST_TAG" | sed 's/v//' | cut -d. -f3)
          case "${{ gitea.event.inputs.bump }}" in
            major) MAJOR=$((MAJOR+1)); MINOR=0; PATCH=0 ;;
            minor) MINOR=$((MINOR+1)); PATCH=0 ;;
            patch) PATCH=$((PATCH+1)) ;;
          esac
          NEW_VERSION="v${MAJOR}.${MINOR}.${PATCH}"
          echo "VERSION=$NEW_VERSION" >> "$GITHUB_ENV"
          echo "PREV_TAG=$LATEST_TAG" >> "$GITHUB_ENV"
          echo "New version: $NEW_VERSION"

      - name: Generate changelog
        working-directory: repo
        run: |
          if [ "$PREV_TAG" = "v0.0.0" ]; then
            CHANGELOG=$(git log --pretty=format:"- %s" HEAD)
          else
            CHANGELOG=$(git log --pretty=format:"- %s" "${PREV_TAG}..HEAD")
          fi
          echo "CHANGELOG<<CHANGELOG_EOF" >> "$GITHUB_ENV"
          echo "$CHANGELOG" >> "$GITHUB_ENV"
          echo "CHANGELOG_EOF" >> "$GITHUB_ENV"

      - name: Create and push tag
        working-directory: repo
        run: |
          git config user.name "Gitea Actions"
          git config user.email "actions@gitea.jeanlucmakiola.de"
          git tag -a "$VERSION" -m "Release $VERSION"
          git push origin "$VERSION"

      - name: Build and push Docker image
        working-directory: repo
        run: |
          REGISTRY="gitea.jeanlucmakiola.de"
          IMAGE="${REGISTRY}/${{ gitea.repository_owner }}/gearbox"
          docker build -t "${IMAGE}:${VERSION}" -t "${IMAGE}:latest" .
          echo "${{ secrets.REGISTRY_TOKEN }}" | docker login "$REGISTRY" -u "${{ gitea.repository_owner }}" --password-stdin
          docker push "${IMAGE}:${VERSION}"
          docker push "${IMAGE}:latest"

      - name: Create Gitea release
        run: |
          API_URL="${GITHUB_SERVER_URL}/api/v1/repos/${{ gitea.repository }}/releases"
          curl -s -X POST "$API_URL" \
            -H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \
            -H "Content-Type: application/json" \
            -d "$(jq -n \
              --arg tag "$VERSION" \
              --arg name "$VERSION" \
              --arg body "$CHANGELOG" \
              '{tag_name: $tag, name: $name, body: $body, draft: false, prerelease: false}')"
