name: Vendor Gems

on:
  pull_request:
    paths:
      - .github/workflows/vendor-gems.yml
      - Library/Homebrew/dev-cmd/vendor-gems.rb
      - Library/Homebrew/Gemfile*
  push:
    paths:
      - .github/workflows/vendor-gems.yml
      - Library/Homebrew/dev-cmd/vendor-gems.rb
      - Library/Homebrew/Gemfile*
    branches-ignore:
      - main
      - master
  workflow_dispatch:
    inputs:
      pull_request:
        description: Pull request number
        required: true

permissions: {}

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

jobs:
  vendor-gems:
    if: github.repository_owner == 'Homebrew'
    runs-on: macos-26
    permissions:
      contents: write
      pull-requests: read
    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: github-actions[bot]

      - name: Prepare push target
        id: push-target
        if: github.event_name != 'pull_request'
        run: |
          if [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]]
          then
            gh pr checkout "${PR}"
          else
            git fetch origin "+refs/heads/${GITHUB_REF_NAME}:refs/remotes/origin/${GITHUB_REF_NAME}"
            git checkout --no-track -B "${GITHUB_REF_NAME}" "origin/${GITHUB_REF_NAME}"
          fi

          branch="$(git branch --show-current)"
          echo "branch=${branch}" >> "$GITHUB_OUTPUT"

          gem_name="$(echo "${branch}" | sed -E 's|.*/||;s|(.*)-.*$|\1|')"
          echo "gem_name=${gem_name}" >> "$GITHUB_OUTPUT"
        env:
          PR: ${{ github.event.pull_request.number || github.event.inputs.pull_request }}
          GITHUB_TOKEN: ${{ github.token }}
        working-directory: ${{ steps.set-up-homebrew.outputs.repository-path }}

      - name: Vendor Gems
        run: |
          if [[ "${GITHUB_EVENT_NAME}" == "pull_request" ]]
          then
            brew vendor-gems --non-bundler-gems --no-commit
          else
            brew vendor-gems --non-bundler-gems
          fi

      - name: Update RBI files
        run: brew typecheck --update

      - name: Commit RBI changes
        if: github.event_name != 'pull_request'
        env:
          GEM_NAME: ${{ steps.push-target.outputs.gem_name }}
        working-directory: ${{ steps.set-up-homebrew.outputs.repository-path }}
        run: |
          if [[ -n "$(git status --porcelain -- "Library/Homebrew/sorbet")" ]]
          then
            git add "Library/Homebrew/sorbet"
            git commit -m "Update RBI files for ${GEM_NAME}." \
                       -m "Autogenerated by the [vendor-gems](https://github.com/Homebrew/brew/blob/HEAD/.github/workflows/vendor-gems.yml) workflow."
          fi

      - name: Push to pull request
        if: github.event_name != 'pull_request'
        uses: Homebrew/actions/git-try-push@df4b09108a1de9d6f995fe68f302b3f68bd6d2ef # 2026.07.20.1
        with:
          token: ${{ github.token }}
          directory: ${{ steps.set-up-homebrew.outputs.repository-path }}
          branch: ${{ steps.push-target.outputs.branch }}
          force: true

      - name: Check vendor and RBI files are in sync
        if: github.event_name == 'pull_request'
        working-directory: ${{ steps.set-up-homebrew.outputs.repository-path }}
        run: |
          status="$(git status --porcelain -- Library/Homebrew/vendor/bundle Library/Homebrew/sorbet)"
          if [ -n "${status}" ]
          then
            {
              echo "::error::Vendored gems and/or RBI files are out of date."
              echo "Run 'brew vendor-gems && brew typecheck --update' (or 'brew update-portable-ruby' for portable-ruby bumps) and commit the changes."
              echo
              echo "${status}"
            } >&2
            exit 1
          fi
