# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: 'Test Report'
on:
  workflow_run:
    workflows: ['NVIDIA Test Github Validation']
    types:
      - completed
jobs:
  test-results:
    name: Test Results
    runs-on: ubuntu-latest
    if: github.event.workflow_run.conclusion != 'skipped'

    permissions:
      checks: write

      # required by download step to access artifacts API
      actions: read
      # needed unless run with comment_mode: off
      pull-requests: write

      # only needed for private repository
      contents: read
      issues: read

    steps:
      - name: Download and Extract Artifacts
        uses: dawidd6/action-download-artifact@20319c5641d495c8a52e688b7dc5fada6c3a9fbc # v8
        with:
           run_id: ${{ github.event.workflow_run.id }}
           path: artifacts

      - name: Set Test Run URL
        id: set_test_run_url
        run: echo "test_run_url=${{ github.event.workflow_run.html_url }}" >> $GITHUB_OUTPUT

      - name: Summarize Test Results as Markdown Table
        id: summarize_test_results
        run: |
          sudo apt-get update -y
          sudo apt-get install -y libxml2-utils
          SUMMARY_FILE="${GITHUB_WORKSPACE}/summary.md"
          echo "### This test report is a summary of the test run in workflow run: [${{ steps.set_test_run_url.outputs.test_run_url }}](${{ steps.set_test_run_url.outputs.test_run_url }})" >> $SUMMARY_FILE
          echo "| Test Suite | Tests | Passed | Failed | Skipped | Duration |" >> $SUMMARY_FILE
          echo "|------------|-------|--------|--------|---------|----------|" >> $SUMMARY_FILE
          total_tests=0
          total_passed=0
          total_failed=0
          total_skipped=0
          total_duration=0
          for file in artifacts/**/*.xml; do
            if [ -f "$file" ]; then
              suite_name=$(basename "$(dirname "$file")")
              tests=$(xmllint --xpath "count(//testcase)" "$file")
              passed=$(xmllint --xpath "count(//testcase[not(failure) and not(error) and not(skipped)])" "$file")
              failed=$(xmllint --xpath "count(//testcase[failure or error])" "$file")
              skipped=$(xmllint --xpath "count(//testcase[skipped])" "$file")
              duration=$(xmllint --xpath "sum(//testcase/@time)" "$file")
              # Debugging: Output values for each file
              echo "Processing file: $file"
              echo "Suite: $suite_name, Tests: $tests, Passed: $passed, Failed: $failed, Skipped: $skipped, Duration: $duration"
              echo "| $suite_name | $tests | $passed | $failed | $skipped | ${duration}s |" >> $SUMMARY_FILE
              total_tests=$((total_tests + tests))
              total_passed=$((total_passed + passed))
              total_failed=$((total_failed + failed))
              total_skipped=$((total_skipped + skipped))
              total_duration=$(echo "$total_duration + $duration" | bc)
            fi
          done
          echo "Total tests: $total_tests"
          echo "Total passed: $total_passed"
          echo "Total failed: $total_failed"
          echo "Total skipped: $total_skipped"
          echo "Total duration: $total_duration"
          echo "|------------|-------|--------|--------|---------|----------|" >> $SUMMARY_FILE
          echo "| **Total** | $total_tests | $total_passed | $total_failed | $total_skipped | ${total_duration}s |" >> $SUMMARY_FILE

          echo "SUMMARY_FILE_PATH=$SUMMARY_FILE" >> $GITHUB_ENV
          echo "summary_file=$SUMMARY_FILE" >> $GITHUB_OUTPUT
          echo "Current directory: $(pwd)"

      - name: Complete report test status
        if: always()
        id: report_test_status
        uses: actions/github-script@v7
        with:
          script: |
            const fs = require('fs');
            const path = process.env.SUMMARY_FILE_PATH;
            const summaryContent = fs.readFileSync(path, 'utf-8');
            console.log("Contents of the summary file:");
            console.log(summaryContent);
            const check_run = await github.rest.checks.create({
              owner: context.repo.owner,
              repo: context.repo.repo,
              name: "Summarize test report",
              head_sha: "${{ github.event.workflow_run.head_sha }}",
              status: "completed",
              conclusion: "${{ job.status }}",
              output: {
                title: "Test Summary",
                summary: summaryContent
              }
            });
            return check_run.data.id;
        env:
          SUMMARY_FILE_PATH: ${{ steps.summarize_test_results.outputs.summary_file }}
