name: Validate and publish blocklists

on:
  pull_request:
    branches: [main]
    paths:
      - "data/**"
      - "scripts/**"
      - "schemas/**"
      - "requirements.txt"
      - "blocklist.txt"
      - "dist/**"
      - ".github/workflows/blocklist.yml"
  push:
    branches: [main]
    paths:
      - "data/**"
      - "scripts/**"
      - "schemas/**"
      - "requirements.txt"
      - ".github/workflows/blocklist.yml"
  workflow_dispatch:

permissions:
  contents: read

jobs:
  validate:
    name: Validate, generate, and test
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository
        uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: "3.13"
          cache: pip

      - name: Install Python and format validators
        run: |
          python -m pip install --requirement requirements.txt
          sudo apt-get update
          sudo apt-get install --yes bind9-utils dnsmasq-base unbound

      - name: Run unit tests
        run: python -m unittest discover --start-directory scripts --pattern "test_*.py" --verbose

      - name: Validate canonical source and generate all formats
        env:
          OUTPUT_DIR: ${{ runner.temp }}/blocklist-dist
        run: |
          export SOURCE_DATE_EPOCH="$(git log -1 --format=%ct)"
          python scripts/generate_blocklist.py --validate-only
          python scripts/generate_blocklist.py --output-dir "$OUTPUT_DIR" --no-legacy
          python scripts/generate_blocklist.py --output-dir "$OUTPUT_DIR" --no-legacy --check

      - name: Run native configuration checks
        env:
          OUTPUT_DIR: ${{ runner.temp }}/blocklist-dist
        run: |
          named-checkzone rpz.hapara.fail "$OUTPUT_DIR/blocklist.rpz"
          dnsmasq --test --conf-file="$OUTPUT_DIR/dnsmasq.conf"
          unbound-checkconf "$OUTPUT_DIR/unbound.conf"

  publish:
    name: Update committed distributions
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    needs: validate
    runs-on: ubuntu-latest
    permissions:
      contents: write
    concurrency:
      group: blocklist-main-publish
      cancel-in-progress: false
    steps:
      - name: Check out repository
        uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: "3.13"
          cache: pip

      - name: Install dependencies
        run: python -m pip install --requirement requirements.txt

      - name: Generate committed distributions
        run: |
          git pull --rebase origin main
          export SOURCE_DATE_EPOCH="$(git log -1 --format=%ct)"
          python scripts/generate_blocklist.py

      - name: Commit generated files
        run: |
          if git diff --quiet -- blocklist.txt dist; then
            echo "Generated files are 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 blocklist.txt dist
          git commit -m "chore(dist): regenerate blocklist formats"
          git push origin HEAD:main
