name: CI-3p-laravel-framework

on:
  workflow_dispatch:
  workflow_call:
    inputs:
      trigger:
        type: string
#      testtree:
#        type: string
      infradb:
        type: string
      connector:
        type: string

env:
  TESTTREE: ${{ inputs.testtree || inputs.trigger.event.inputs.testtree || 'main' }}
  TESTNAME: laravel-framework
  TESTDIST: debian12
  SHA: ${{ inputs.trigger && fromJson(inputs.trigger).event.workflow_run.head_sha || github.sha }}
  BRANCH: ${{ inputs.trigger && fromJson(inputs.trigger).event.workflow_run.head_branch || github.ref_name }}

jobs:
  test:
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: false
      matrix:
#        infradb: [ 'mariadb10.5', 'mariadb10.6', 'mariadb10.11', 'mariadb11.2', 'mariadb11.4', 'mariadb11.5' ]
#        infradb: [ 'mysql57', 'mysql80', 'mysql84', 'mysql90' ]
        infradb: ${{ fromJson( inputs.infradb ) }}
#        connector: [ 'mysql', 'mariadb' ]
        connector: ${{ fromJson( inputs.connector ) }}
    env:
      INFRADB: ${{ matrix.infradb }}
      CONNECTOR: ${{ matrix.connector }}
      BLDCACHE: ${{ inputs.trigger && fromJson(inputs.trigger).event.workflow_run.head_sha || github.sha }}_debian12-dbg_bin
      MATRIX: '(${{ matrix.infradb }},${{ matrix.connector }})'

    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: Checkout proxysql_3p_testing
      uses: actions/checkout@v4
      with:
        repository: 'proxysql/proxysql_3p_testing'
        ref: ${{ env.TESTTREE }}
        fetch-depth: 1
        path: 'proxysql_3p_testing'
        token: ${{ secrets.GH_TOKEN_PROXYSQL }}
        sparse-checkout: |
          common
          libs
          test_${{ env.TESTNAME }}
#          proxysql

#    - 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 bin
      if: ${{ env.BRANCH != 'none' }}
      uses: actions/cache/restore@v4
      with:
        key: ${{ env.BLDCACHE }}
        fail-on-cache-miss: true
        path: |
          proxysql/.git/
          proxysql/binaries/
        
    - name: Prepare
      run: |
        set +e
        
        cd proxysql_3p_testing/
        ./setup.sh test_${{ env.TESTNAME }}/${{ env.TESTNAME }}
        #git switch ${{ env.TESTTREE }}
        #git config user.email "GH-Actions@proxysql.com"
        #git config user.name "GH-Actions"
        #git rebase main -f -s recursive -X theirs

        sed -i 's|image: .*|image: ghcr.io/mrmohebi/docker-hoster|' common/infra-docker-hoster/docker-compose.yml
        
        # use cached build
        rm -rf ./proxysql && mv ../proxysql . || true
        ls -l proxysql/binaries/
        git -C ./proxysql describe --long --abbrev=7 

        # extend timout
        #cd test_${{ env.TESTNAME }}
        #sed -i 's/timeout -v -s1 800 //' run-tests.bash

        # fix permissions
        #mkdir laravel/cache
        #chmod -R 777 laravel/cache
        
        # set tested database infra
        #sed -i 's/^fn_test_proxysql m/#fn_test_proxysql m/' run-tests.bash
        #sed -i 's/^fn_test_proxysql_dev m/#fn_test_proxysql_dev m/' run-tests.bash
        #sed -i '/^fn_start_prep$/a fn_test_proxysql_dev ${{ matrix.infradb }}' run-tests.bash
        
        #sed -i '/EXPDATA=/i pwd' test_${{ env.TESTNAME }}/run-tests.bash
        #sed -i '/EXPDATA=/i ls -l' test_${{ env.TESTNAME }}/run-tests.bash

    - name: Run tests
      run: |
        set +e
        
        cd proxysql_3p_testing/test_${{ env.TESTNAME }}
        ./run-tests.bash
        RC=$?
        
        #sudo chmod -R 777 ${{ github.workspace }}/*
        exit $RC
        
    - name: Check baseline
      if: ${{ !cancelled() }}
      run: |
        set +e
        cd proxysql_3p_testing/test_${{ env.TESTNAME }}

        GHUSER="$(id -u):$(id -g)"
        sudo chown -R ${GHUSER} ./logs

        if ! ls ./logs/*/*_summary.log 1>/dev/null 2>&1; then
          echo "ERROR: No summary log found — tests likely did not run"
          exit 1
        fi

        cat ./logs/*/*_summary.log
        FAILS=$(cat ./logs/*/*_summary.log | grep -oP '(?<=^FAIL: )\d+')
        ERRORS=$(cat ./logs/*/*_summary.log | grep -oP '(?<=^ERROR: )\d+')

        if [[ -z "${FAILS}" ]]; then
          echo "ERROR: Could not parse FAIL count from summary log"
          exit 1
        fi
        if [[ ${FAILS} -ne 0 ]]; then
          echo "FAILED: ${FAILS} test failure(s)"
          exit 1
        fi
        if [[ -n "${ERRORS}" && ${ERRORS} -ne 0 ]]; then
          echo "FAILED: ${ERRORS} test error(s) (setup/fixture failures)"
          exit 1
        fi

        echo "All tests passed"

    - name: Archive artifacts logs
      if: ${{ !cancelled() }}
      uses: actions/upload-artifact@v4
      with:
        name: ${{ github.workflow }}-${{ env.SHA }}-${{ matrix.infradb }}-${{ matrix.connector }}-logs-run#${{ github.run_number }}
        path: |
          proxysql_3p_testing/test_${{ env.TESTNAME }}/logs
          
    - 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 }}'
