name: CI-maketest

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

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

jobs:
  builds:
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: false
      matrix:
        testdist: [ 'ubuntu22' ]
        target: [ 'testaurora','testgalera','testgrouprep','testreadonly','testreplicationlag','testall' ]
    env:
      TESTDIST: ${{ matrix.testdist }}
      TARGET: ${{ matrix.target }}
      MATRIX: '(${{ matrix.target }})'

    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 repository
      uses: actions/checkout@v4
      with:
        repository: ${{ github.repository }}
        ref: ${{ env.SHA }}
        fetch-depth: 0
        path: 'proxysql'
        
#    - name: hot-fixes
#      run: |
#        sed -i 's/-I$(CURL_IDIR) -I$(IDIR)/-I$(CURL_IDIR) -I${SQLITE3_DIR} -I$(IDIR)/' proxysql/test/tap/tests/Makefile
        
    - name: Make-test
      run: |
        cd proxysql/
        sed -i 's/docker-compose/docker compose/g' Makefile
        #git fetch --tags --force
        GIT_VERSION=$(git describe --long --abbrev=7)
        echo "GIT_VERSION=${GIT_VERSION}"
        echo "GIT_VERSION=${GIT_VERSION}" >> $GITHUB_ENV
        
        echo "TARGET: ${{ matrix.target }}"
        sed -i "/command/c \    command: bash -l -c 'cd /opt/proxysql && make cleanall && make -j$(nproc) ${{ matrix.target }}'" docker-compose.yml
        make ${TESTDIST}-dbg | tee ../build.log
        
    - name: Check build
      run: |
        grep 'exited with code 0' build.log
        
    - name: Archive artifacts logs
      if: ${{ failure() && !cancelled() }}
      uses: actions/upload-artifact@v4
      with:
        name: ${{ github.workflow }}-${{ env.SHA }}-logs-run#${{ github.run_number }}-${{ matrix.target }}
        path: |
          ./build-*.log
          
    - 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 }}'
