name: Update readme sponsor block

on:
  workflow_dispatch:
  repository_dispatch:
    types:
      - webhook
  schedule:
    - cron: '0 1 * * *'

permissions:
  contents: write
  pull-requests: write

jobs:
  sponsors:
    if: github.repository == 'axios/axios'
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0
          persist-credentials: false
      - name: git config
        run: |
          git config user.name "${GITHUB_ACTOR}"
          git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
      - name: Setup node
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: 24.x
          cache: npm
      - name: Install dependencies
        run: npm ci --ignore-scripts
      - name: Check if sponsors require updates
        id: sponsors-requires-update
        run: node ./scripts/update-readme-sponsors.mjs
      - name: Check tracked README sponsor diff
        id: readme-tracked-change
        run: |
          if git diff --quiet -- README.md; then
            echo "readme_changed=false" >> "$GITHUB_OUTPUT"
          else
            echo "readme_changed=true" >> "$GITHUB_OUTPUT"
          fi
      - name: Read sponsors.md file content
        run: |
          echo 'CONTENT<<EOF' >> $GITHUB_ENV
          cat ./temp/sponsors.md >> $GITHUB_ENV
          echo 'EOF' >> $GITHUB_ENV
        shell: bash
        if: steps.sponsors-requires-update.outputs.changed == 'true' && steps.readme-tracked-change.outputs.readme_changed == 'true'
      - name: Echo sponsors content
        run: |
          echo "$CONTENT"
        if: steps.sponsors-requires-update.outputs.changed == 'true' && steps.readme-tracked-change.outputs.readme_changed == 'true'
      - name: Create pull request
        uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1
        with:
          branch: sponsors
          delete-branch: true
          commit-message: 'chore(sponsor): update sponsor block'
          title: 'chore(docs): update sponsor block'
          body: |
            **New sponsor block update:**
            ${{ env.CONTENT }}
          labels: |
            commit::docs
            priority::high
            type::automated-pr
          signoff: false
          draft: false
        if: steps.sponsors-requires-update.outputs.changed == 'true' && steps.readme-tracked-change.outputs.readme_changed == 'true'
