name: Sync robots.json

on:
  schedule:
    # Runs every day at midnight
    - cron: '0 0 * * *'
  workflow_dispatch:

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

      - name: Set up Node.js and Yarn
        uses: actions/setup-node@v4
        with:
          node-version: '22'
          cache: 'yarn'

      - run: yarn workspaces focus hono-middleware @hono/ua-blocker

      - name: Fetch latest robots.json
        run: yarn workspace @hono/ua-blocker getrobotstxt

      - name: Generate data
        run: yarn workspace @hono/ua-blocker prebuild

      - name: Create Pull Request if changes exist
        uses: peter-evans/create-pull-request@v6
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: 'chore(ua-blocker): update robots.json from upstream'
          title: 'chore(ua-blocker): update robots.json from upstream'
          body: 'This PR was automatically created after detecting changes in the upstream `robots.json` file.'
          branch: 'chore/sync-robots-json'
          delete-branch: true
          # Assignee and labels
          assignees: finxol
          reviewers: finxol
          labels: robots.json
