name: Publish

on:
  workflow_dispatch:
    inputs:
      versionType:
        type: choice
        description: "<major|minor|patch>"
        required: true
        default: "patch"
        options:
          - major
          - minor
          - patch

jobs:
  publish:
    name: Publish ${{ inputs.versionType }} release
    permissions:
      # Give the default GITHUB_TOKEN write permission to commit and push the
      # added or changed files to the repository.
      contents: write
      discussions: write
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          fetch-depth: 0
      - name: Prepare Node
        uses: actions/setup-node@v4
        with:
          node-version: 20
      - uses: pnpm/action-setup@v4
        name: Install pnpm
        with:
          run_install: false
      - name: Get pnpm store directory
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
      - uses: actions/cache@v4
        name: Setup pnpm cache
        with:
          path: ${{ env.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-
      - name: Install
        run: pnpm i
      - name: Install git-cliff
        uses: taiki-e/install-action@git-cliff
      - id: update-version
        shell: bash
        name: Bump version
        # Use npm because yarn is for some reason not able to output only the version name
        run: |
          echo "version=$(pnpm run publish ${{ inputs.versionType }} | tail -n1)" >> $GITHUB_OUTPUT
          git add .
      - name: Generate a changelog for the new version
        shell: bash
        id: build-changelog
        run: |
          touch /tmp/changelog.md
          git-cliff --config  cliff.toml --verbose --strip header --unreleased --tag v${{ steps.update-version.outputs.version }} > /tmp/changelog.md
          if [ $? -eq 0 ]; then
            CONTENT=$(cat /tmp/changelog.md)
            cat /tmp/changelog.md | cat - ./CHANGELOG.md > temp && mv temp ./CHANGELOG.md
            {
              echo 'content<<EOF'
              echo "$CONTENT"
              echo EOF
            } >> $GITHUB_OUTPUT
            echo "version=${{ steps.update-version.outputs.version }}" >> $GITHUB_OUTPUT
          else
            echo "Failed to generate changelog"
            exit 1
          fi
        env:
          GITHUB_REPO: ${{ github.repository }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Commit changes
        uses: stefanzweifel/git-auto-commit-action@v5
        with:
          commit_message: "chore: bump version to v${{ steps.update-version.outputs.version }}"
          commit_user_name: "github-actions[bot]"
          commit_user_email: "41898282+github-actions[bot]@users.noreply.github.com"
          tagging_message: "v${{ steps.update-version.outputs.version }}"
      - name: Release
        uses: softprops/action-gh-release@v2
        with:
          draft: true
          body: ${{steps.build-changelog.outputs.content}}
          name: Clash Nyanpasu v${{steps.update-version.outputs.version}}
          tag_name: "v${{ steps.update-version.outputs.version }}"
          # target_commitish: ${{ steps.tag.outputs.sha }}
