name: Update Sorbet files

on:
  pull_request:
    paths:
      - Library/Homebrew/dev-cmd/typecheck.rb
      - Library/Homebrew/sorbet/**
      - "!Library/Homebrew/sorbet/rbi/**"
  push:
    paths:
      - .github/workflows/sorbet.yml
    branches-ignore:
      - main
      - master
  schedule:
    - cron: "0 0 * * *"
  workflow_dispatch:

permissions:
  contents: read

defaults:
  run:
    shell: bash -xeuo pipefail {0}

jobs:
  tapioca:
    if: github.repository == 'Homebrew/brew'
    runs-on: macos-26
    steps:
      - name: Set up Homebrew
        id: set-up-homebrew
        uses: Homebrew/actions/setup-homebrew@df4b09108a1de9d6f995fe68f302b3f68bd6d2ef # 2026.07.20.1
        with:
          core: false
          cask: false

      - name: Configure Git user
        if: github.event_name != 'pull_request'
        uses: Homebrew/actions/git-user-config@df4b09108a1de9d6f995fe68f302b3f68bd6d2ef # 2026.07.20.1
        with:
          username: BrewTestBot

      - name: Set up commit signing
        if: github.event_name != 'pull_request' && github.actor != 'dependabot[bot]'
        uses: Homebrew/actions/setup-commit-signing@df4b09108a1de9d6f995fe68f302b3f68bd6d2ef # 2026.07.20.1
        with:
          signing_key: ${{ secrets.BREWTESTBOT_SSH_SIGNING_KEY }}

      - name: Update RBI files
        id: update
        working-directory: ${{ steps.set-up-homebrew.outputs.repository-path }}
        run: |
          if [[ "${GITHUB_EVENT_NAME}" != "pull_request" ]]
          then
            git fetch origin

            BRANCH="sorbet-files-update"
            echo "branch=${BRANCH}" >> "$GITHUB_OUTPUT"

            if git ls-remote --exit-code --heads origin "${BRANCH}"
            then
              git checkout "${BRANCH}"
              git checkout "Library/Homebrew/sorbet"
            else
              git checkout --no-track -B "${BRANCH}" origin/HEAD
            fi
          fi

          brew typecheck --update --suggest-typed

      - name: Commit changes
        id: commit
        if: github.event_name != 'pull_request'
        env:
          GITHUB_TOKEN: ${{ secrets.HOMEBREW_GITHUB_PUBLIC_REPO_TOKEN }}
        working-directory: ${{ steps.set-up-homebrew.outputs.repository-path }}
        run: |
          if ! git diff --stat --exit-code "Library/Homebrew/sorbet"
          then
            git add "Library/Homebrew/sorbet"
            git commit -m "sorbet: Update RBI files." \
                       -m "Autogenerated by the [sorbet](https://github.com/Homebrew/brew/blob/HEAD/.github/workflows/sorbet.yml) workflow."

            if ! git diff --stat --exit-code "Library/Homebrew"
            then
              git add "Library/Homebrew/"
              git commit -m "sorbet: Autobump sigils via Spoom" \
                         -m "Autogenerated by the [sorbet](https://github.com/Homebrew/brew/blob/HEAD/.github/workflows/sorbet.yml) workflow."
            fi

            echo "committed=true" >> "$GITHUB_OUTPUT"
            PULL_REQUEST_STATE="$(gh pr view --json=state | jq -r ".state" || true)"
            if [[ "${PULL_REQUEST_STATE}" != "OPEN" ]]
            then
              echo "pull_request=true" >> "$GITHUB_OUTPUT"
            fi
          fi

      - name: Push commits
        if: steps.commit.outputs.committed == 'true' && github.actor != 'dependabot[bot]'
        uses: Homebrew/actions/git-try-push@df4b09108a1de9d6f995fe68f302b3f68bd6d2ef # 2026.07.20.1
        with:
          token: ${{ secrets.HOMEBREW_GITHUB_PUBLIC_REPO_TOKEN }}
          directory: ${{ steps.set-up-homebrew.outputs.repository-path }}
          branch: ${{ steps.update.outputs.branch }}
          force: true
          origin_branch: "HEAD"

      - name: Open a pull request
        if: steps.commit.outputs.pull_request == 'true' && github.actor != 'dependabot[bot]'
        run: gh pr create --fill
        env:
          GITHUB_TOKEN: ${{ secrets.HOMEBREW_GITHUB_PUBLIC_REPO_TOKEN }}
        working-directory: ${{ steps.set-up-homebrew.outputs.repository-path }}

  issue:
    needs: tapioca
    if: always() && github.event_name == 'schedule' && github.repository == 'Homebrew/brew'
    runs-on: ubuntu-slim
    env:
      RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
    permissions:
      # To create or update issues
      issues: write
    steps:
      - name: Open, update, or close Sorbet issue
        uses: Homebrew/actions/create-or-update-issue@df4b09108a1de9d6f995fe68f302b3f68bd6d2ef # 2026.07.20.1
        with:
          title: Failed to update RBI files
          body: >
            The Sorbet workflow [failed](${{ env.RUN_URL }}). No RBI files were updated.
          labels: bug
          update-existing: ${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') || contains(needs.*.result, 'skipped') }}
          close-existing: ${{ needs.tapioca.result == 'success' }}
          close-from-author: github-actions[bot]
          close-comment: >
            The Sorbet workflow [succeeded](${{ env.RUN_URL }}). Closing this issue.
