name: Update star history

on:
  schedule:
    - cron: "17 0 * * *"
  workflow_dispatch:

permissions:
  contents: write

jobs:
  refresh:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Update star history cache key
        run: |
          today="$(date -u +%F)"
          perl -0pi -e "s/cache_bust=[0-9]{4}-[0-9]{2}-[0-9]{2}/cache_bust=$today/g" README.md

      - name: Commit README update
        run: |
          if git diff --quiet README.md; then
            echo "Star history cache key is already current."
            exit 0
          fi

          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git add README.md
          git commit -m "chore: refresh star history chart"
          git push
