name: Deploy new rules
on:
  push:
    branches:
      - 'master'
  pull_request:
    branches:
      - 'master'
  schedule:
    - cron: '57 * * * *'

env:
  GIT_NAME: '${{ secrets.GIT_NAME }}'
  GIT_EMAIL: '${{ secrets.GIT_EMAIL }}'
  GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'

jobs:
  buildrules:
    name: Trigger action
    runs-on: '${{ matrix.os }}'

    strategy:
      fail-fast: false
      matrix:
        python_version:
          - '3.11'
        os:
          - ubuntu-latest

    steps:
      - uses: actions/checkout@main
        name: Clone repository
        with:
          token: '${{ secrets.GITHUB_TOKEN }}'
          fetch-depth: 5

      - name: Set up Python ${{ matrix.python_version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python_version }}

      - name: Install dependencies
        run: |
          sudo apt install aptitude -y -f
          # sudo aptitude install nodejs npm -y -f
          sudo apt-get update && sudo apt-get install -y ca-certificates curl gnupg
          curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
          NODE_MAJOR=20
          echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
          sudo apt-get update && sudo aptitude install nodejs -y
          python -m pip install --upgrade pip
          if [ -f requirements.txt ]; then pip install --no-cache -I -r requirements.txt; fi

      - name: Install hostlist-compiler
        run: |
          npm i -g @adguard/hostlist-compiler

      - name: compile
        run: |
          hostlist-compiler -c tools/hostscompilerconf.json -o _public/blockrules.txt
          hostlist-compiler -c tools/noise.json -o _public/noise.txt

      # - name: Render the rules
      #   run: |
      #     BUILD="$(git rev-parse --short HEAD)"
      #     flrender -v -i ublockorigin-rules=. adblocker-rules.template _public/blockrules.txt
      #     sed -i -e "s/\! BuildID:/\! BuildID: $BUILD/g" _public/blockrules.txt
      #     head -n 5 _public/blockrules.txt

      - name: Deploy
        run: |
          cd _public
          # As we have no parts of the third world country (USA) domestic
          # Racism issues, we keep the original branch refs 'master'
          git config --global init.defaultBranch master
          git init
          git add -A
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git commit -m "Release $(git --git-dir ../.git rev-parse --short HEAD)"

      - name: Force push to destination branch
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: gh-pages
          force: true
          directory: _public

  # qodana:
  #   needs: buildrules
  #   runs-on: ubuntu-latest
  #   permissions:
  #     contents: write
  #     pull-requests: write
  #     checks: write
  #   steps:
  #     - uses: actions/checkout@main
  #       with:
  #         ref: ${{ github.event.pull_request.head.sha }} # to check out the actual pull request commit, not the merge commit
  #         fetch-depth: 0 # a full history is required for pull request analysis
  #     - name: 'Qodana Scan'
  #       uses: JetBrains/qodana-action@v2023.2
  #       env:
  #         QODANA_TOKEN: ${{ secrets.QODANA_TOKEN }} # read the steps about it below
