name: Run release

on:
  workflow_dispatch:
    inputs:
      kind:
        description: 'Release kind (major, minor, patch)'
        required: true
        default: 'auto'
        type: choice
        options:
          - auto
          - major
          - minor
          - patch
      branch:
        description: 'Branch to release from'
        required: true
        default: 'master'
        type: string

jobs:
  release:
    runs-on: node22
    permissions:
      contents: write
      packages: write
      id-token: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
          token: ${{ secrets.BOT_PAT }}
      - if: ${{ github.event.inputs.branch != 'master' }}
        run: git checkout ${{ github.event.inputs.branch }}
      - uses: ./.forgejo/actions/init
      - uses: https://github.com/denoland/setup-deno@v1
        with:
          deno-version: '2.0'

      - name: Initialize configs
        env:
          GH_PRIVKEY: ${{ secrets.GH_PRIVKEY }}
        run: |
          git config user.name "mtcute-bot"
          git config user.email mtcute-bot@tei.su

          echo "$GH_PRIVKEY" > /tmp/bot-key
          mkdir -p ~/.ssh
          ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
          echo "Host github.com" >> ~/.ssh/config
          echo "  IdentityFile /tmp/bot-key" >> ~/.ssh/config
          chmod 600 /tmp/bot-key
          git remote add github git@github.com:mtcute/mtcute.git
      
      - name: Run release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          pnpm exec fuman-build release \
            --kind=${{ github.event.inputs.kind }} \
            --with-npm \
            --npm-token=${{ secrets.NPM_TOKEN }} \
            --with-jsr \
            --jsr-token=${{ secrets.JSR_TOKEN }} \
            --git-extra-origins=github \
            --with-github-release \
            --github-repo=mtcute/mtcute \
            --github-token=${{ secrets.GH_PAT }}
