name: Post-Release Homebrew Tap

permissions:
  contents: read

on:
  workflow_call:
    inputs:
      ref:
        required: true
        description: "GitHub ref to use"
        type: string
      version:
        required: true
        description: "Version to produce"
        type: string
      dry-run:
        required: false
        default: true
        description: "Whether to run in dry-run mode and skip pushing the commit"
        type: boolean
  workflow_dispatch:
    inputs:
      ref:
        required: true
        description: "GitHub ref to use"
        type: string
      version:
        required: true
        description: "Version to produce"
        type: string
      dry-run:
        required: false
        default: true
        description: "Whether to run in dry-run mode and skip pushing the commit"
        type: boolean

env:
  PULUMI_VERSION: ${{ inputs.version }}
  GITHUB_TOKEN: ${{ secrets.PULUMI_BOT_TOKEN }}

jobs:
  update-homebrew-tap:
    name: Update Homebrew Tap
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.ref }}
          path: pulumi
      - name: Checkout tap repo
        uses: actions/checkout@v4
        with:
          repository: pulumi/homebrew-tap
          path: homebrew-tap
          token: ${{ secrets.PULUMI_BOT_TOKEN }}
      - name: Update Homebrew Tap
        run: |
          set -euo pipefail

          # Can simulate this by cloning pulumi/pulumi & pulumi/homebrew-tap to adacent directories
          # and running from their parent:

          ./pulumi/.github/scripts/generate-homebrew-tap \
            "${PULUMI_VERSION}" ./pulumi/.github/scripts/pulumi-tap-template.rb \
            > ./homebrew-tap/Formula/pulumi.rb
      - name: Commit updated formula
        working-directory: homebrew-tap
        run: |
          set -euo pipefail

          git config user.name pulumi-bot
          git config user.email bot@pulumi.com
          git add Formula/pulumi.rb
          echo "::group::git diff"
          git  --no-pager diff
          echo "::endgroup::"
          git commit -m "Brew formula update for Pulumi version ${PULUMI_VERSION}"
      - name: Push formula
        working-directory: homebrew-tap
        if: ${{ !inputs.dry-run }}
        run: |
          set -euo pipefail

          git push origin HEAD:master