name: Update Fancyss Rules 3.0

on:
  push:
    branches: ['3.0']
    paths:
      - '.github/workflows/fancyss-rules-3.0.yml'
      - 'fancyss_rules/*'
      - 'fancyss_rules_ng/*'
  workflow_dispatch:
    inputs:
      FORCE_PUSH:
        description: 'Force to push'
        required: true
        default: 'false'
  schedule:
    - cron: '45 19 * * *'

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v7

      - name: Run update script
        run: |
          sudo apt-get update
          sudo apt-get install moreutils iprange -y
          cd fancyss_rules
          bash update_rules.sh update
          cd ../fancyss_rules_ng
          bash update_rules.sh update

      - name: Commit to repo
        if: env.is_changed == '1' || github.event.inputs.FORCE_PUSH == 'true'
        run: |
          cd $GITHUB_WORKSPACE
          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git add fancyss_rules/.
          git add fancyss_rules_ng/.
          git commit -m "Actions Auto Update at $(date +'%Y-%m-%d %H:%M:%S')"
          git push

      - name: Delete old workflow runs
        uses: GitRML/delete-workflow-runs@main
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          repository: ${{ github.repository }}
          keep_minimum_runs: 7
          retain_days: 7