name: Check Vendor Version

on:
  pull_request:
    paths:
      - .github/workflows/vendor-version.yml
      - Library/Homebrew/vendor/bundle/ruby/**

permissions:
  contents: read

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

jobs:
  check-vendor-version:
    runs-on: ubuntu-latest
    steps:
      - name: Set up Homebrew
        id: set-up-homebrew
        uses: Homebrew/actions/setup-homebrew@master
        with:
          core: false
          cask: false
          test-bot: false

      - name: Install Bundler RubyGems
        run: brew install-bundler-gems --groups=all

      - name: Get Ruby ABI version
        id: ruby-abi
        run: echo "version=$(brew ruby -e "puts Gem.ruby_api_version")" >> "${GITHUB_OUTPUT}"

      - name: Get gem info
        id: gem-info
        working-directory: ${{ steps.set-up-homebrew.outputs.gems-path }}/${{ steps.ruby-abi.outputs.version }}/gems
        run: |
          {
            echo "vendor-version=$(<../.homebrew_vendor_version)"
            echo "ignored<<EOS"
            git check-ignore -- *
            echo "EOS"
          } >> "${GITHUB_OUTPUT}"

      - name: Compare to base ref
        working-directory: ${{ steps.set-up-homebrew.outputs.gems-path }}/${{ steps.ruby-abi.outputs.version }}
        env:
          ABI_VERSION: ${{ steps.ruby-abi.outputs.version }}
          VENDOR_VERSION: ${{ steps.gem-info.outputs.vendor-version }}
          IGNORED_GEMS: ${{ steps.gem-info.outputs.ignored }}
        run: |
          git checkout "origin/${GITHUB_BASE_REF}"
          rm .homebrew_vendor_version
          brew install-bundler-gems --groups=all
          if [[ "$(brew ruby -e "puts Gem.ruby_api_version")" == "${ABI_VERSION}" && \
                "$(<.homebrew_vendor_version)" == "${VENDOR_VERSION}" ]]
          then
            while IFS= read -r gem; do
              gem_dir="./gems/${gem}"
              [[ -d "${gem_dir}" ]] || continue
              exit_code=0
              git check-ignore --quiet "${gem_dir}" || exit_code=$?
              if (( exit_code != 0 )); then
                if (( exit_code == 1 )); then
                  echo "::error::VENDOR_VERSION needs bumping in utils/gems.rb" >&2
                else
                  echo "::error::git check-ignore failed" >&2
                fi
                exit "${exit_code}"
              fi
            done <<< "${IGNORED_GEMS}"
          fi
