name: brew

on:
  workflow_call:
    inputs:
      tag:
        description: The tag to release e.g., v0.1.0
        required: true
        type: string
    secrets:
      HOMEBREW_GITHUB_API_TOKEN:
        required: true
  workflow_dispatch:
    inputs:
      tag:
        description: The tag to release e.g., v0.1.0
        required: true
        type: string

defaults:
  run:
    shell: bash

env:
  BREW_FORMULA: akv
  BREW_TAP: heaths/tap

jobs:
  bump-formula:
    runs-on: ubuntu-latest
    environment:
      name: homebrew
      url: ${{ steps.bump.outputs.pullRequest }}
    steps:
    - name: Get user info
      id: user
      run: gh api 'users/${{ github.actor }}' --template 'name="{{ .name }}"{{"\n"}}email="{{ .email }}"{{"\n"}}' | tee -a "$GITHUB_OUTPUT"
      env:
        GH_TOKEN: ${{ github.token }}
    - name: Run brew bump-formula-pr
      id: bump
      # According to https://github.com/actions/runner-images?tab=readme-ov-file#available-images,
      # Linuxbrew is already installed on ubuntu-latest.
      run: |
        eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
        brew tap '${{ env.BREW_TAP }}'
        pullRequest=$(brew bump-formula-pr --no-browse --url '${{ env.TARBALL_URL }}' '${{ env.BREW_TAP }}/${{ env.BREW_FORMULA }}' | tail -1)
        echo "pullRequest=$pullRequest" >> "$GITHUB_OUTPUT"
      env:
        HOMEBREW_GIT_NAME: ${{ steps.user.outputs.name }}
        HOMEBREW_GIT_EMAIL: ${{ steps.user.outputs.email }}
        # https://github.com/settings/tokens/new?scopes=repo&description=Homebrew
        HOMEBREW_GITHUB_API_TOKEN: ${{ secrets.HOMEBREW_GITHUB_API_TOKEN }}
        TARBALL_URL: ${{ github.server_url }}/${{ github.repository }}/archive/${{ format('refs/tags/{0}', inputs.tag) || github.ref }}.tar.gz
    - name: Summarize
      run: |
        echo "### Review formula update 👀" >> "$GITHUB_STEP_SUMMARY"
        echo "${{ steps.bump.outputs.pullRequest }}" >> "$GITHUB_STEP_SUMMARY"
