name: Integration

on:  # yamllint disable-line rule:truthy
  push:
    branches: ["master"]
  pull_request:
    branches: ["master"]

permissions:
  contents: read

jobs:
  python:
    name: Python ${{ matrix.python-version }}
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        os: [ubuntu-20.04]
        python-version: ["3.9", "3.10", "3.11", "3.12"]
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Install Ubuntu packages
        run: |
          sudo ./utils/searxng.sh install packages
          sudo apt install firefox
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
          architecture: 'x64'
      - name: Cache Python dependencies
        id: cache-python
        uses: actions/cache@v4
        with:
          path: |
            ./local
            ./.nvm
            ./node_modules
          key: python-${{ matrix.os }}-${{ matrix.python-version }}-${{ hashFiles('requirements*.txt', 'setup.py') }}
      - name: Install Python dependencies
        if: steps.cache-python.outputs.cache-hit != 'true'
        run: |
          make V=1 install
          make V=1 gecko.driver
      - name: Run tests
        run: make V=1 ci.test

  themes:
    name: Themes
    runs-on: ubuntu-20.04
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Install Ubuntu packages
        run: sudo ./utils/searxng.sh install buildhost
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'
          architecture: 'x64'
      - name: Cache Python dependencies
        id: cache-python
        uses: actions/cache@v4
        with:
          path: |
            ./local
            ./.nvm
            ./node_modules
          key: python-ubuntu-20.04-3.12-${{ hashFiles('requirements*.txt', 'setup.py','.nvmrc', 'package.json') }}
      - name: Install node dependencies
        run: make V=1 node.env
      - name: Build themes
        run: make V=1 themes.all

  documentation:
    name: Documentation
    runs-on: ubuntu-20.04
    permissions:
      contents: write  # for JamesIves/github-pages-deploy-action to push changes in repo
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: '0'
          persist-credentials: false
      - name: Install Ubuntu packages
        run: sudo ./utils/searxng.sh install buildhost
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'
          architecture: 'x64'
      - name: Cache Python dependencies
        id: cache-python
        uses: actions/cache@v4
        with:
          path: |
            ./local
            ./.nvm
            ./node_modules
          key: python-ubuntu-20.04-3.12-${{ hashFiles('requirements*.txt', 'setup.py','.nvmrc', 'package.json') }}
      - name: Build documentation
        run: |
          make V=1 docs.clean docs.html
      - name: Deploy
        if: github.ref == 'refs/heads/master'
        uses: JamesIves/github-pages-deploy-action@3.7.1
        with:
          GITHUB_TOKEN: ${{ github.token }}
          BRANCH: gh-pages
          FOLDER: dist/docs
          CLEAN: true  # Automatically remove deleted files from the deploy branch
          SINGLE_COMMIT: true
          COMMIT_MESSAGE: '[doc] build from commit ${{ github.sha }}'

  babel:
    name: Update translations branch
    runs-on: ubuntu-20.04
    if: ${{ github.repository_owner == 'searxng' && github.ref == 'refs/heads/master' }}
    needs:
      - python
      - themes
      - documentation
    permissions:
      contents: write  # for make V=1 weblate.push.translations
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: '0'
          token: ${{ secrets.WEBLATE_GITHUB_TOKEN }}
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'
          architecture: 'x64'
      - name: Cache Python dependencies
        id: cache-python
        uses: actions/cache@v4
        with:
          path: |
            ./local
            ./.nvm
            ./node_modules
          key: python-ubuntu-20.04-3.12-${{ hashFiles('requirements*.txt', 'setup.py','.nvmrc', 'package.json') }}
      - name: weblate & git setup
        env:
          WEBLATE_CONFIG: ${{ secrets.WEBLATE_CONFIG }}
        run: |
          mkdir -p ~/.config
          echo "${WEBLATE_CONFIG}" > ~/.config/weblate
          git config --global user.email "searxng-bot@users.noreply.github.com"
          git config --global user.name "searxng-bot"
      - name: Update transations
        id: update
        run: |
          make V=1 weblate.push.translations

  dockers:
    name: Docker
    if: github.ref == 'refs/heads/master'
    needs:
      - python
      - themes
      - documentation
    env:
      DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
    runs-on: ubuntu-20.04
    steps:
      - name: Checkout
        if: env.DOCKERHUB_USERNAME != null
        uses: actions/checkout@v4
        with:
          # make sure "make docker.push" can get the git history
          fetch-depth: '0'
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'
          architecture: 'x64'
      - name: Cache Python dependencies
        id: cache-python
        uses: actions/cache@v4
        with:
          path: |
            ./local
            ./.nvm
            ./node_modules
          key: python-ubuntu-20.04-3.12-${{ hashFiles('requirements*.txt', 'setup.py','.nvmrc', 'package.json') }}
      - name: Set up QEMU
        if: env.DOCKERHUB_USERNAME != null
        uses: docker/setup-qemu-action@v1
      - name: Set up Docker Buildx
        if: env.DOCKERHUB_USERNAME != null
        uses: docker/setup-buildx-action@v1
      - name: Login to DockerHub
        if: env.DOCKERHUB_USERNAME != null
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Build and push
        if: env.DOCKERHUB_USERNAME != null
        run: make -e GIT_URL=$(git remote get-url origin) docker.buildx
