name: Bump unversioned casks

on:
  push:
    paths:
      - .github/workflows/bump-unversioned-casks.yml
  schedule:
    - cron: "*/30 * * * *"
  workflow_dispatch:
    inputs:
      clear_cache:
        description: Clear cache
        required: false
        default: false

concurrency:
  group: bump-unversioned-casks
  cancel-in-progress: true

env:
  HOMEBREW_DEVELOPER: 1
  HOMEBREW_NO_AUTO_UPDATE: 1

permissions:
  contents: read

jobs:
  bump-unversioned-casks:
    if: startsWith(github.repository, 'Homebrew/')
    runs-on: macos-11.0
    steps:
      - name: Set up Homebrew
        id: set-up-homebrew
        uses: Homebrew/actions/setup-homebrew@master
        with:
          core: false
          cask: true
          test-bot: false

      - name: Configure Git user
        uses: Homebrew/actions/git-user-config@master
        with:
          username: BrewTestBot

      - name: Set up commit signing
        id: set-up-commit-signing
        uses: Homebrew/actions/setup-commit-signing@master
        with:
          signing_key: ${{ secrets.BREWTESTBOT_GPG_SIGNING_SUBKEY }}

      - name: Cache Homebrew Gems
        uses: actions/cache@v4
        with:
          path: ${{ steps.set-up-homebrew.outputs.gems-path }}
          key: ${{ runner.os }}-rubygems-${{ steps.set-up-homebrew.outputs.gems-hash }}
          restore-keys: ${{ runner.os }}-rubygems-

      - name: Install Homebrew Gems
        run: brew install-bundler-gems

      - name: Generate cache ID
        id: cache-id
        run: |
          echo "time=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT

      - name: Cache state
        uses: actions/cache@v4
        with:
          path: ~/bump-unversioned-casks-state.json
          key: bump-unversioned-casks-${{ steps.cache-id.outputs.time }}
          restore-keys: bump-unversioned-casks-

      - name: Clear cache
        if: github.event.inputs.clear_cache == 'true'
        run: |
          rm -f ~/bump-unversioned-casks-state.json

      - name: Run brew bump-unversioned-casks ${{ github.repository }}
        run: |
          brew bump-unversioned-casks --state-file ~/bump-unversioned-casks-state.json --limit 15 '${{ github.repository }}'
        env:
          HOMEBREW_DEBUG: 1
          HOMEBREW_GITHUB_API_TOKEN: ${{ secrets.HOMEBREW_GITHUB_PUBLIC_REPO_TOKEN }}
          HOMEBREW_GPG_PASSPHRASE: ${{ secrets.BREWTESTBOT_GPG_SIGNING_SUBKEY_PASSPHRASE }}
        timeout-minutes: 20
