name: CI-repltests

on:
  workflow_dispatch:
  workflow_call:
    inputs:
      trigger:
        type: string

env:
  SHA: ${{ inputs.trigger && fromJson(inputs.trigger).event.workflow_run.head_sha || github.sha }}

jobs:
  tests:
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: false
      matrix:
        testdist: [ 'ubuntu22-tap' ]
#        infradb: [ 'mysql57', 'mysql80', 'mysql81', 'mysql82', 'mariadb10', 'mariadb11' ]
        infradb: [ 'mysql57' ]
    env:
      TESTDIST: ${{ matrix.testdist }}
      INFRADB:  ${{ matrix.infradb }}
      BLDCACHE: ${{ inputs.trigger && fromJson(inputs.trigger).event.workflow_run.head_sha || github.sha }}_${{ matrix.testdist }}_src
      MATRIX: '(${{ matrix.infradb }})'

    steps:
    
    - uses: LouisBrunner/checks-action@v2.0.0
      id: checks
      if: always()
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        name: '${{ github.workflow }} / ${{ github.job }} ${{ env.MATRIX }}'
        repo: ${{ github.repository }}
        sha: ${{ env.SHA }}
        status: 'in_progress'
#        action_url: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
        details_url: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
    
    - name: Install dependencies
      run: |
        sudo apt-get update -y
        sudo apt-get install -y sysbench gettext
      
#    - name: Wait for cache
#      if: ${{ env.BRANCH != 'none' }}
#      env:
#        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
#      run: |
#        echo "Wating for cache '${BLDCACHE}' ..."
#        sleep $(( $RANDOM / 1024 ))
#        PROBE="$(gh cache list --limit 300 --repo ${{ github.repository }} | grep -i ${BLDCACHE} || echo '')"
#        while [[ -z ${PROBE} ]]; do
#          echo "Wating for cache '${BLDCACHE}' ..."
#          sleep 30
#          PROBE="$(gh cache list --limit 300 --repo ${{ github.repository }} | grep -i ${BLDCACHE} || echo '')"
#        done
#        echo "Cache available '${BLDCACHE}'"

    - name: Cache restore src
      id: cache-src
      uses: actions/cache/restore@v4
      with:
        key: ${{ env.BLDCACHE }}
        fail-on-cache-miss: true
        path: |
          proxysql/src/

    - name: Checkout jenkins_build_scripts
      uses: actions/checkout@v4
      with:
        repository: 'proxysql/jenkins-build-scripts'
        ref: 'proxysql_repl_tests'
        fetch-depth: 0
        path: 'jenkins-build-scripts'
        token: ${{ secrets.GH_TOKEN_PROXYSQL }}

    - name: Docker-hoster
      run: |
        cd jenkins-build-scripts/infra-docker-hoster
        sed -i 's|image: .*|image: ghcr.io/mrmohebi/docker-hoster|' docker-compose.yml
        docker compose up -d

    - name: Replication-tests
      run: |
        set +e
        
        cd jenkins-build-scripts
        grep -rl 'docker-compose ' | xargs -r -n1 sed -i 's/docker-compose /docker compose /g'
        sed -i "s|#!/usr/bin/bash|#!/usr/bin/bash +e|" proxysql_repl_tests/bin/debezium-check.bash
        sed -i "s|#!/usr/bin/bash|#!/usr/bin/bash +e|" proxysql_repl_tests/exec_repl_test.sh
        sed -i "s|JENKINS_SCRIPTS_PATH=.*|JENKINS_SCRIPTS_PATH=${{ github.workspace }}/jenkins-build-scripts|" env.sh
        sed -i "s|WORKSPACE=.*|WORKSPACE=${{ github.workspace }}/proxysql|" env.sh
        
        cd proxysql_repl_tests
        #./exec_repl_test.sh 5.7 no-ssl debezium
        RCS=0
        for N in {1..1}; do
          ./exec_all_repl_tests.sh
          RC=$?
          RCS=$(( $RCS + $RC ))
        done
        
        #./docker-compose-destroy.bash
        sudo chmod -R 777 ${{ github.workspace }}/*
        exit $RCS
        
    - name: Fix artifact permissions
      if: ${{ failure() && !cancelled() }}
      run: |
        # actions/upload-artifact dies with EACCES when it scandirs into
        # directories under ci_*_logs/ that were created inside docker
        # build containers (root-owned). Make everything readable by the
        # runner user before upload. sudo required because files are
        # root-owned; 2>/dev/null + || true because the path may not
        # exist on all failure paths (e.g. a cache-restore failure before
        # any test even runs).
        sudo chmod -R a+rX proxysql/ci_*_logs/ 2>/dev/null || true

    - name: Archive artifacts logs
      if: ${{ failure() && !cancelled() }}
      uses: actions/upload-artifact@v4
      with:
        name: ${{ github.workflow }}-${{ env.SHA }}-logs-run#${{ github.run_number }}
        path: |
          proxysql/ci_*_logs/
          
    - name: Archive artifacts bin
      if: ${{ failure() && !cancelled() }}
      uses: actions/upload-artifact@v4
      with:
        name: ${{ github.workflow }}-${{ env.SHA }}-bin-run#${{ github.run_number }}
        path: |
          proxysql/binaries/
          proxysql/src/
          proxysql/libs/
#          proxysql/deps/
    
    - uses: LouisBrunner/checks-action@v2.0.0
      if: always()
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        check_id: ${{ steps.checks.outputs.check_id }}
        repo: ${{ github.repository }}
        sha: ${{ env.SHA }}
        conclusion: ${{ job.status }}
#        action_url: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
        details_url: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
