name: Bump packages

permissions:
  contents: write

on:
  workflow_dispatch:
    inputs:
      action:
          description: 'bump or publish'
          required: true
          default: 'bump'
          type: choice
          options:
            - bump
            - publish

jobs:
  bump:
    permissions:
      contents: write
      pull-requests: write

    runs-on: ubuntu-latest
    if: github.event.inputs.action == 'bump'
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - name: configure git
        run: |
            git config --global user.email "actions@github.com"
            git config --global user.name "GitHub Actions"
      - run: rustc publish.rs
      - run: git checkout -b bump
      - run: printf "bump versions for release\n\n" > /tmp/bump
      - run: ./publish bump >> /tmp/bump
      - run: git add .
      - run: git commit -m "bump"
      - run: git push origin bump
      - run: gh pr create --fill --body-file /tmp/bump
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  publish:
    runs-on: ubuntu-latest
    if: github.event.inputs.action == 'publish'
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - run: rustc publish.rs
      - name: assert we're on bump branch
        run: test "$(git branch --show-current)" = "bump"
      - run: ./publish publish
        env:
          CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
