PR Check Unit Tests - Changed the delete of Wazuh indexes process for OVA and AMI - Branch change/779-update-the-deleted-indexes-in-ova-and-ami-builds #137
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| run-name: PR Check Unit Tests - ${{ github.event.pull_request.title }} - Branch ${{ github.head_ref }} | |
| name: PR Check - Unit Tests | |
| on: | |
| pull_request: | |
| types: [opened, synchronize, reopened, ready_for_review] | |
| permissions: | |
| contents: read | |
| pull-requests: write | |
| issues: write | |
| jobs: | |
| unit_tests: | |
| if: ${{ !github.event.pull_request.draft }} | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.12' | |
| - name: Install hatch | |
| run: pip install hatch | |
| - name: Run unit tests with coverage | |
| id: run_tests | |
| continue-on-error: true | |
| shell: bash | |
| run: | | |
| set -o pipefail | |
| hatch run dev:test-cov 2>&1 | tee test_output.txt | |
| TEST_EXIT_CODE=${PIPESTATUS[0]} | |
| if [ -z "$TEST_EXIT_CODE" ]; then | |
| TEST_EXIT_CODE=1 | |
| fi | |
| echo "exit_code=$TEST_EXIT_CODE" >> $GITHUB_OUTPUT | |
| - name: Parse test results | |
| if: always() | |
| id: parse_results | |
| run: | | |
| SUMMARY=$(grep -oP '\d+ failed, \d+ passed in [\d.]+s' test_output.txt || echo "") | |
| if [ -z "$SUMMARY" ]; then | |
| SUMMARY=$(grep -oP '\d+ passed in [\d.]+s' test_output.txt || echo "No test summary found") | |
| fi | |
| echo "summary=${SUMMARY}" >> $GITHUB_OUTPUT | |
| COVERAGE=$(grep -A1 "^TOTAL" test_output.txt | tail -1 | awk '{print $NF}' || echo "N/A") | |
| echo "coverage=${COVERAGE}" >> $GITHUB_OUTPUT | |
| FAILED=$(echo "$SUMMARY" | grep -oP '^\d+(?= failed)' || echo "0") | |
| echo "failed=${FAILED}" >> $GITHUB_OUTPUT | |
| PASSED=$(echo "$SUMMARY" | grep -oP '\d+(?= passed)' || echo "0") | |
| echo "passed=${PASSED}" >> $GITHUB_OUTPUT | |
| grep "^FAILED" test_output.txt > failed_tests.txt || true | |
| - name: Create test summary | |
| if: always() | |
| run: | | |
| echo "## π§ͺ Unit Tests Results" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| if [ "${{ steps.run_tests.outputs.exit_code }}" == "0" ]; then | |
| echo "β **All tests passed!**" >> $GITHUB_STEP_SUMMARY | |
| status="success :green_circle:" | |
| else | |
| echo "β **Some tests failed**" >> $GITHUB_STEP_SUMMARY | |
| status="failure :red_circle:" | |
| fi | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "### Summary" >> $GITHUB_STEP_SUMMARY | |
| echo "- **Result:** ${status}" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| # Show table if available | |
| if grep -q "Name.*Stmts.*Miss.*Cover" test_output.txt; then | |
| echo "### π Details" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo '```' >> $GITHUB_STEP_SUMMARY | |
| sed -n '/Name.*Stmts.*Miss.*Cover/,/^TOTAL/p' test_output.txt >> $GITHUB_STEP_SUMMARY | |
| echo '```' >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| fi | |
| # Show failed tests if any | |
| if [ -s failed_tests.txt ]; then | |
| echo "### β Failed Tests" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo '```' >> $GITHUB_STEP_SUMMARY | |
| cat failed_tests.txt >> $GITHUB_STEP_SUMMARY | |
| echo '```' >> $GITHUB_STEP_SUMMARY | |
| fi | |
| - name: Comment PR with results | |
| if: always() | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const fs = require('fs'); | |
| const exitCode = '${{ steps.run_tests.outputs.exit_code }}'; | |
| let body = '## π§ͺ Unit Tests Results\n\n'; | |
| let status = ''; | |
| if (exitCode === '0') { | |
| body += 'β **All tests passed!**\n\n'; | |
| status="success :green_circle:" | |
| } else { | |
| body += 'β **Some tests failed**\n\n'; | |
| status="failure :red_circle:" | |
| } | |
| body += '### Summary\n'; | |
| body += `- **Result:** ${status}\n`; | |
| body += `- **Workflow:** [View Details](${context.payload.repository.html_url}/actions/runs/${context.runId})\n\n`; | |
| // Add coverage table if available | |
| try { | |
| const testOutput = fs.readFileSync('test_output.txt', 'utf8'); | |
| const coverageMatch = testOutput.match(/Name\s+Stmts\s+Miss\s+Cover[\s\S]*?^TOTAL[\s\S]*?$/m); | |
| if (coverageMatch) { | |
| body += '### π Coverage Details\n\n'; | |
| body += '```\n' + coverageMatch[0] + '\n```\n\n'; | |
| } | |
| } catch (error) { | |
| console.log('Could not read test output:', error.message); | |
| } | |
| // Add failed tests if any | |
| try { | |
| if (fs.existsSync('failed_tests.txt')) { | |
| const failedTests = fs.readFileSync('failed_tests.txt', 'utf8').trim(); | |
| if (failedTests) { | |
| body += '### β Failed Tests\n\n'; | |
| body += '```\n' + failedTests + '\n```\n'; | |
| } | |
| } | |
| } catch (error) { | |
| console.log('Could not read failed tests:', error.message); | |
| } | |
| // Find and update or create comment | |
| const { data: comments } = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| }); | |
| const botComment = comments.find(comment => | |
| comment.user.type === 'Bot' && | |
| comment.body.includes('π§ͺ Unit Tests Results') | |
| ); | |
| if (botComment) { | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: botComment.id, | |
| body: body | |
| }); | |
| console.log('Updated existing comment'); | |
| } else { | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| body: body | |
| }); | |
| console.log('Created new comment'); | |
| } | |
| - name: Upload test output | |
| if: always() | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: test-output | |
| path: | | |
| test_output.txt | |
| failed_tests.txt | |
| retention-days: 7 | |
| - name: Check final status | |
| run: | | |
| if [ "${{ steps.run_tests.outputs.exit_code }}" != "0" ]; then | |
| echo "::error::Unit tests failed" | |
| exit 1 | |
| fi | |
| echo "β All tests passed" |