name: Release

on:
  push:
    tags:
      - 'release@*'

jobs:
  release:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Java 21
        uses: actions/setup-java@v3
        with:
          distribution: temurin
          java-version: 21

      - name: Cache Gradle
        uses: actions/cache@v4
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
          restore-keys: |
            ${{ runner.os }}-gradle-

      - name: Setup SSH key and rsync
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.JAVADOC_SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
          echo "${{ secrets.CI_KNOWN_HOSTS }}" > ~/.ssh/known_hosts
          chmod -R 600 ~/.ssh
          rm /etc/apt/sources.list.d/microsoft-prod.list
          apt-get update
          apt install -y rsync

      - name: Build and publish to Gitea Maven and JavaDoc to the website
        run: ./gradlew clean publish --no-daemon -PgiteaToken=${{ secrets.CI_PUBLISH_TOKEN }}
        # was run: ./gradlew clean publish uploadJavadoc --no-daemon -PgiteaToken=${{ secrets.CI_PUBLISH_TOKEN }} -PjavadocUser=${{ vars.JAVADOC_USER }} -PjavadocHost=${{ vars.JAVADOC_HOST }} -PjavadocPath=${{ vars.JAVADOC_PATH }} -PjavadocKeyPath=~/.ssh/id_rsa

      - name: Generate release notes
        id: notes
        run: |
          current_tag="${{ github.ref_name }}"

          # strip the prefix for sorting, keep prefix for matching
          prefix="release@"

          # get all matching tags, strip prefix, sort them
          all_versions=$(git tag --list "${prefix}*" | sed "s/^${prefix}//" | sort -V)

          # find previous version
          previous_tag=""
          for v in $all_versions; do
            if [[ "$prefix$v" == "$current_tag" ]]; then
              break
            fi
            previous_tag="$prefix$v"
          done

          if [[ -z "$previous_tag" ]]; then
            range=""
          else
            range="$previous_tag..$current_tag"
          fi

          echo "Comparing range: $range"

          body="## What's New"

          for category in "feat: Features" "fix: Bug Fixes" "docs: Documentation" "chore: Chores"; do
            prefix="${category%%:*}"
            title="${category##*: }"
            entries=$(git log $range --pretty=format:"%B" --no-merges | ( grep "^${prefix}" || true ) | sed "s/^${prefix}:/- /")
            if [[ -n "$entries" ]]; then
              body="$body\n\n### $title\n$entries"
            fi
          done

          echo -e "$body" > /tmp/release_notes.md

      - name: Create Gitea Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            app/build/distributions/*.tar
            app/build/distributions/*.zip
          body_path: /tmp/release_notes.md
