name: Version

on:
  workflow_run:
    workflows: ["CI"]
    types:
      - completed
    branches: [main]
  workflow_dispatch:
    inputs:
      skip_version_bump:
        description: 'Skip version bumping for testing workflow logic'
        required: false
        default: 'false'
        type: choice
        options:
        - 'true'
        - 'false'

jobs:
  version:
    runs-on: ubuntu-latest
    if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }}
    permissions:
      contents: write      # Required for pushing commits/tags
      pull-requests: read  # Required for PR info
    steps:
    - uses: actions/checkout@v4
      with:
        ref: ${{ github.event.workflow_run.head_branch || github.ref }}
        fetch-depth: 0

    - name: Setup Rust
      uses: actions-rust-lang/setup-rust-toolchain@v1
      with:
        toolchain: stable

    - name: Configure Git
      run: |
        git config user.name "github-actions[bot]"
        git config user.email "github-actions[bot]@users.noreply.github.com"

    - name: Build grubble
      run: cargo build --release

    - name: Run tests
      run: cargo test

    - name: Bump version
      if: github.event_name != 'workflow_dispatch' || github.event.inputs.skip_version_bump != 'true'
      run: ./target/release/grubble --push --tag --release-notes
