name: Release

on:
  workflow_dispatch:
    inputs:
      dry_run:
        description: 'Dry run release-plz'
        required: true
        default: true
        type: boolean
  push:
    branches:
      - main

jobs:
  # Release unpublished packages.
  release-plz-release:
    if: ${{ github.repository_owner == 'benniekiss' }}
    name: Release-plz release
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: read
      id-token: write

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Setup Rust
        uses: actions-rust-lang/setup-rust-toolchain@2b1f5e9b395427c92ee4e3331786ca3c37afe2d7 # v1

      - name: Generate GitHub token
        uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 # v3
        id: generate-token
        with:
          client-id: ${{ secrets.RELEASE_PLZ_APP_ID }}
          private-key: ${{ secrets.RELEASE_PLZ_APP_PRIVATE_KEY }}

      - name: Run release-plz
        uses: release-plz/action@1528104d2ca23787631a1c1f022abb64b34c1e11 # v0.5
        with:
          command: release
          dry_run: ${{ inputs.dry_run }}
        env:
          GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}

  # Create a PR with the new versions and changelog, preparing the next release.
  release-plz-pr:
    if: ${{ github.repository_owner == 'benniekiss' }}
    name: Release-plz PR
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
      id-token: write
    concurrency:
      group: release-plz-${{ github.ref }}
      cancel-in-progress: false

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Setup Rust
        uses: actions-rust-lang/setup-rust-toolchain@2b1f5e9b395427c92ee4e3331786ca3c37afe2d7 # v1

      - name: Generate GitHub token
        uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 # v3
        id: generate-token
        with:
          client-id: ${{ secrets.RELEASE_PLZ_APP_ID }}
          private-key: ${{ secrets.RELEASE_PLZ_APP_PRIVATE_KEY }}

      - name: Run release-plz
        uses: release-plz/action@1528104d2ca23787631a1c1f022abb64b34c1e11 # v0.5
        with:
          command: release-pr
          dry_run: ${{ inputs.dry_run }}
        env:
          GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
