name: Update sponsors, maintainers, manpage and completions

on:
  push:
    branches:
      - main
      - master
    paths:
      - .github/workflows/sponsors-maintainers-man-completions.yml
      - README.md
      - Library/Homebrew/cmd/**
      - Library/Homebrew/dev-cmd/**
      - Library/Homebrew/completions/**
      - Library/Homebrew/manpages/**
      - Library/Homebrew/cli/parser.rb
      - Library/Homebrew/completions.rb
      - Library/Homebrew/env_config.rb
  schedule:
    - cron: "0 0 * * *"
  workflow_dispatch:

permissions:
  contents: read

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

jobs:
  updates:
    runs-on: ubuntu-latest
    if: github.repository == 'Homebrew/brew'
    steps:
      - name: Setup Homebrew
        id: set-up-homebrew
        uses: Homebrew/actions/setup-homebrew@fd832223f9f99ebf0244dd20658680e5d4aca049 # 2026.08.03.2
        with:
          core: false
          cask: false

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

      - name: Set up commit signing
        uses: Homebrew/actions/setup-commit-signing@fd832223f9f99ebf0244dd20658680e5d4aca049 # 2026.08.03.2
        with:
          signing_key: ${{ secrets.BREWTESTBOT_SSH_SIGNING_KEY }}

      - name: Cache Bundler RubyGems
        uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
        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: Update sponsors, maintainers, manpage and completions
        id: update
        run: |
          git fetch origin

          if [[ -n "$GITHUB_REF_NAME" && "$GITHUB_REF_NAME" != "master" && "$GITHUB_REF_NAME" != "main" ]]
          then
            BRANCH="$GITHUB_REF_NAME"
          else
            BRANCH=sponsors-maintainers-man-completions
          fi
          echo "branch=${BRANCH}" >> "$GITHUB_OUTPUT"

          if git ls-remote --exit-code --heads origin "${BRANCH}"
          then
            git checkout --force "${BRANCH}"
            git checkout "README.md" \
                         "docs/Manpage.md" \
                         "manpages/brew.1" \
                         "completions"
          else
            git checkout --force --no-track -B "${BRANCH}" origin/HEAD
          fi

          if brew update-sponsors
          then
            git add "README.md"
            git commit -m "Update sponsors." \
                       -m "Autogenerated by the [sponsors-maintainers-man-completions](https://github.com/Homebrew/brew/blob/HEAD/.github/workflows/sponsors-maintainers-man-completions.yml) workflow."
            COMMITTED=true
          fi

          if brew update-maintainers
          then
            git add "README.md" \
                    "docs/Manpage.md" \
                    "manpages/brew.1"
            git commit -m "Update maintainers." \
                       -m "Autogenerated by the [sponsors-maintainers-man-completions](https://github.com/Homebrew/brew/blob/HEAD/.github/workflows/sponsors-maintainers-man-completions.yml) workflow."
            COMMITTED=true
          fi

          if brew generate-man-completions
          then
            git add "README.md" \
                    "docs/Manpage.md" \
                    "manpages/brew.1" \
                    "completions"
            git commit -m "Update manpage and completions." \
                       -m "Autogenerated by the [sponsors-maintainers-man-completions](https://github.com/Homebrew/brew/blob/HEAD/.github/workflows/sponsors-maintainers-man-completions.yml) workflow."
            COMMITTED=true
          fi

          if [[ -n "${COMMITTED-}" ]]
          then
            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
        env:
          GITHUB_TOKEN: ${{ secrets.HOMEBREW_GITHUB_PUBLIC_REPO_TOKEN }}
          HOMEBREW_GITHUB_API_TOKEN: ${{ secrets.HOMEBREW_BREW_UPDATE_SPONSORS_MAINTAINERS_TOKEN }}
        working-directory: ${{ steps.set-up-homebrew.outputs.repository-path }}

      - name: Push commits
        if: steps.update.outputs.committed == 'true'
        uses: Homebrew/actions/git-try-push@fd832223f9f99ebf0244dd20658680e5d4aca049 # 2026.08.03.2
        with:
          token: ${{ secrets.HOMEBREW_GITHUB_PUBLIC_REPO_TOKEN }}
          directory: ${{ steps.set-up-homebrew.outputs.repository-path }}
          branch: ${{ steps.update.outputs.branch }}
          force: true

      - name: Open a pull request
        if: steps.update.outputs.pull_request == 'true'
        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: updates
    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 sponsors, maintainers, manpage and completions issue
        uses: Homebrew/actions/create-or-update-issue@fd832223f9f99ebf0244dd20658680e5d4aca049 # 2026.08.03.2
        with:
          title: Failed to update sponsors, maintainers, manpage and completions
          body: >
            The sponsors, maintainers, manpage and completions workflow [failed](${{ env.RUN_URL }}). No sponsors, maintainers, manpage and completions were updated.
          labels: bug
          update-existing: ${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') || contains(needs.*.result, 'skipped') }}
          close-existing: ${{ needs.updates.result == 'success' }}
          close-from-author: github-actions[bot]
          close-comment: >
            The sponsors, maintainers, manpage and completions workflow [succeeded](${{ env.RUN_URL }}). Closing this issue.
