name: Prepare Publish

on:
  workflow_dispatch:
    inputs:
      target_branch:
        description: Release target branch.
        required: true
        default: main
        type: string
      release:
        description: Bumpp release type.
        required: true
        default: next
        type: choice
        options:
          - next
          - patch
          - minor
          - major
          - prepatch
          - preminor
          - premajor
      version:
        description: Specify exact version instead of bumpp release type
        required: false
        type: string

concurrency:
  group: ${{ github.workflow }}-${{ inputs.target_branch }}-${{ inputs.version || inputs.release }}
  cancel-in-progress: false

permissions: {}

jobs:
  prepare:
    if: github.repository == 'vitest-dev/vitest'
    name: Prepare release PR
    runs-on: ubuntu-latest
    permissions:
      contents: read # checkout target branch
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          ref: ${{ inputs.target_branch }}
          fetch-depth: 0
          persist-credentials: false

      - name: Install pnpm
        uses: pnpm/action-setup@41ff72655975bd51cab0327fa583b6e92b6d3061 # v4.2.0

      - name: Set node version to 24
        uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          node-version: 24
          package-manager-cache: false

      - name: Install
        run: pnpm install --frozen-lockfile --prefer-offline
        env:
          PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: '1'

      - name: Create branch and update version
        env:
          TARGET_BRANCH: ${{ inputs.target_branch }}
          RELEASE_TYPE: ${{ inputs.release }}
          RELEASE_VERSION: ${{ inputs.version }}
          RUN_ID: ${{ github.run_id }}
        run: |
          RELEASE_INPUT="${RELEASE_VERSION:-$RELEASE_TYPE}"
          PREPARE_BRANCH="prepare-$TARGET_BRANCH-$RELEASE_INPUT-$RUN_ID"
          git switch -c "$PREPARE_BRANCH"
          # The numeric prefix comes from `gh api /users/vitest-release-bot%5Bbot%5D --jq .id`.
          # This is just to make the avatar in the commit look pretty.
          git config user.name "vitest-release-bot[bot]"
          git config user.email "292707936+vitest-release-bot[bot]@users.noreply.github.com"
          pnpm run release

      - id: generate-token
        uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
        with:
          client-id: ${{ secrets.RELEASE_GITHUB_APP_ID }}
          private-key: ${{ secrets.RELEASE_GITHUB_APP_PRIVATE_KEY }}
          owner: ${{ github.repository_owner }}
          repositories: ${{ github.event.repository.name }}
          permission-contents: write
          permission-pull-requests: write

      - name: Open release PR
        env:
          TARGET_BRANCH: ${{ inputs.target_branch }}
          GH_TOKEN: ${{ steps.generate-token.outputs.token }}
        run: |
          PREPARE_BRANCH="$(git branch --show-current)"
          VERSION="$(jq -r .version package.json)"
          git push -u "https://x-access-token:$GH_TOKEN@github.com/$GITHUB_REPOSITORY.git" HEAD
          gh pr create \
            --base "$TARGET_BRANCH" \
            --head "$PREPARE_BRANCH" \
            --title "chore: release v$VERSION" \
            --body "Release PR generated by the Prepare Publish workflow: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"
