name: Build Authors Cache

on:
  schedule:
    - cron: '0 16 * * *'
  workflow_dispatch:

env:
  TZ: Asia/Shanghai

jobs:
  build:
    name: Build Authors Cache
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20.x
      - name: Cache node_modules
        uses: actions/cache@v4
        id: cache
        with:
          path: node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('yarn.lock') }}
          restore-keys: ${{ runner.os }}-node-
      - name: Install Dependencies
        if: steps.cache.outputs.cache-hit != 'true'
        run: yarn --frozen-lockfile --production
      - name: Fetch Authors
        run: |
          rm -rf authors.json

          # Fetch authors list with 10 concurrency requests
          node --loader ts-node/esm scripts/post-build/commits-info/fetch-authors-cache.ts 10
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Commit Authors Cache
        run: |
          git switch --orphan authors-cache
          git add -f authors.json

          git config user.name github-actions
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git commit -m "Update authors cache on $(date '+%Y-%m-%d %H:%M:%S')"

          git push -f origin authors-cache
