# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

name: Image Build
permissions: read-all
on:
  workflow_call:
    inputs:
      registry:
        description: Container Registry URL
        required: false
        default: ""
        type: string
      tag:
        description: Container Tag
        required: false
        default: "latest"
        type: string
      service:
        description: Example to test
        required: true
        type: string
jobs:
  get-test-case:
    runs-on: ubuntu-latest
    outputs:
      run_matrix: ${{ steps.test-case-matrix.outputs.run_matrix }}
      CHECKOUT_REF: ${{ steps.get-checkout-ref.outputs.CHECKOUT_REF }}
    steps:
      - name: Get checkout ref
        id: get-checkout-ref
        run: |
          if [ "${{ github.event_name }}" == "pull_request" ] || [ "${{ github.event_name }}" == "pull_request_target" ]; then
            CHECKOUT_REF=refs/pull/${{ github.event.number }}/merge
          else
            CHECKOUT_REF=${{ github.ref }}
          fi
          echo "CHECKOUT_REF=${CHECKOUT_REF}" >> $GITHUB_OUTPUT
          echo "checkout ref ${CHECKOUT_REF}"

      - name: Checkout out Repo
        uses: actions/checkout@v4
        with:
          ref: ${{ steps.get-checkout-ref.outputs.CHECKOUT_REF }}
          fetch-depth: 0

      - name: Get test matrix
        shell: bash
        id: test-case-matrix
        run: |
          set -x
          run_matrix=$(jq -n '{include: []}')
          service_l=$(echo ${{ inputs.service }} | tr '[:upper:]' '[:lower:]')
          cd ${{ github.workspace }}/tests
          test_cases=$(find . -type f -name "test_${service_l}*.sh")
          for script in $test_cases; do
            echo $script
            if echo "$script" | grep -q "on"; then
              hardware=$(echo $script | cut -d'/' -f3 | cut -d'.' -f1 | awk -F'on_' '{print $2}')
            else
              hardware="intel_cpu"
            fi
            run_matrix=$(echo $run_matrix | jq --arg script "$script" --arg hardware "$hardware" '.include += [{"test_case": $script, "hardware": $hardware}]')
          done
          run_matrix=$(echo $run_matrix | jq -c .)
          echo "run_matrix=${run_matrix}" >> $GITHUB_OUTPUT

  run-test:
    needs: [get-test-case]
    strategy:
      matrix:
        include: ${{ fromJSON(needs.get-test-case.outputs.run_matrix).include }}
    runs-on: ${{ matrix.hardware }}
    continue-on-error: true
    steps:
      - name: Clean up Working Directory
        run: |
          sudo rm -rf ${{github.workspace}}/*
          docker system prune -f
          docker rmi $(docker images --filter reference="*/*:comps" -q) || true

      - name: Checkout out Repo
        uses: actions/checkout@v4
        with:
          ref: ${{ needs.get-test-case.outputs.CHECKOUT_REF }}
          fetch-depth: 0

      - name: Run test
        shell: bash
        env:
          HUGGINGFACEHUB_API_TOKEN: ${{ secrets.HUGGINGFACEHUB_API_TOKEN }}
          HF_TOKEN: ${{ secrets.HF_TOKEN }}
          GOOGLE_CSE_ID: ${{ secrets.GOOGLE_CSE_ID }}
          GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }}
          PINECONE_KEY: ${{ secrets.PINECONE_KEY }}
          service: ${{ inputs.service }}
          test_case: ${{ matrix.test_case }}
          hardware: ${{ matrix.hardware }}
        run: |
          cd ${{ github.workspace }}/tests
          service=$(echo "${test_case}" | cut -d'_' -f2- |cut -d'.' -f1)
          echo "service=${service}" >> $GITHUB_ENV
          if [ -f ${test_case} ]; then timeout 30m bash ${test_case}; else echo "Test script {${test_case}} not found, skip test!"; fi

      - name: Clean up container
        if: cancelled() || failure()
        run: |
          cid=$(docker ps -aq --filter "name=test-comps-*")
          if [[ ! -z "$cid" ]]; then docker stop $cid && docker rm $cid && sleep 1s; fi
          docker system prune -f

      - name: Publish pipeline artifact
        if: ${{ !cancelled() }}
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.service }}
          path: ${{ github.workspace }}/tests/*.log
