# This workflow will install Python dependencies, fetch external sources and generate blocklists with a single version of Python

name: Fetch updates

on:
  schedule:
    - cron: '17 11 * * 1-5'
  workflow_dispatch:

permissions:
  contents: write

jobs:
  fetch-updates:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v7
      with:
        ref: ${{ github.head_ref }}
    - name: Set up Python 3.14
      uses: actions/setup-python@v7
      with:
        python-version: "3.14"
        # cache: "pip"
        check-latest: false
    - name: Install dependencies
      run: |
        if [ -f requirements.txt ]; then
          python -m pip install -r requirements.txt --quiet
        else
          echo "No requirements.txt, skipping."
        fi
    - name: Update external sources
      run: |
        python "./scripts/import_from_wiki_gg.py"
        python "./scripts/import_from_indie_wiki.py"
    - name: Generate blocklists from sources
      run: |
        python "./scripts/generate_blocklists.py"
    - name: Check for Changes
      id: check_changes
      run: |
        if [[ -z "$(git status --porcelain)" ]]; then
          echo "No changes detected."
          echo "has_changes=false" >> "$GITHUB_OUTPUT"
        else
          echo "Changes detected."
          echo "has_changes=true" >> "$GITHUB_OUTPUT"
        fi
    - name: Commit and Push Changes
      if: steps.check_changes.outputs.has_changes == 'true'
      run: |
        # configure user
        git config --global user.name "${{ github.actor }}"
        git config --global user.email "${{ github.actor }}@users.noreply.github.com"

        # stage any file changes to be committed
        git add .

        # make commit with staged changes
        git commit -m 'build(lists): auto update blocklists'

        # push the commit back up to source GitHub repository
        git push
