m`erge branch 'main' of https://github.com/UofT-CSC490-F2025/AIDoctors #36
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
| name: Backend Testing | |
| on: | |
| workflow_dispatch: | |
| pull_request: | |
| branches: "main" | |
| paths: | |
| - "src/application/backend/**" | |
| push: | |
| branches: "main" | |
| paths: | |
| - "src/application/backend/**" | |
| permissions: | |
| id-token: write | |
| contents: write | |
| pull-requests: write | |
| issues: write | |
| jobs: | |
| backend_test: | |
| runs-on: ubuntu-latest | |
| env: | |
| ENVIRONMENT: "testing" | |
| defaults: | |
| run: | |
| working-directory: src/application/backend | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: "3.12" | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install uv | |
| uv sync | |
| - name: Run pytest and capture output | |
| id: pytest | |
| shell: bash | |
| continue-on-error: true | |
| run: | | |
| set -o pipefail | |
| uv run -m pytest 2>&1 | tee pytest-output.txt | |
| - name: Extract Coverage Percentage | |
| id: covpct | |
| run: | | |
| # Extract total coverage percentage from pytest output | |
| total=$(grep -oP 'TOTAL\s+\d+\s+\d+\s+\K\d+' pytest-output.txt || echo "0") | |
| echo "pct=$total" >> $GITHUB_OUTPUT | |
| - name: Comment pytest results on PR | |
| if: github.event_name == 'pull_request' | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const outputPath = path.join('src', 'application', 'backend', 'pytest-output.txt'); | |
| const testOutput = fs.readFileSync(outputPath, 'utf8'); | |
| const output = `#### Backend Test Results | |
| <details><summary>Show Output</summary> | |
| \`\`\`text | |
| ${testOutput} | |
| \`\`\` | |
| </details> | |
| *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Workflow: \`${{ github.workflow }}\`*`; | |
| github.rest.issues.createComment({ | |
| issue_number: context.issue.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: output | |
| }); | |
| - name: Generate Badge | |
| if: github.ref == 'refs/heads/main' | |
| run: | | |
| pct="${{ steps.covpct.outputs.pct }}" | |
| color="red" | |
| if [ "$pct" -ge 80 ]; then color="orange"; fi | |
| if [ "$pct" -ge 90 ]; then color="green"; fi | |
| mkdir -p ../../../.github/badges | |
| cat > ../../../.github/badges/backend-coverage.svg <<EOF | |
| <svg xmlns="http://www.w3.org/2000/svg" width="200" height="20"> | |
| <rect width="140" height="20" fill="#555"/> | |
| <rect x="140" width="60" height="20" fill="$color"/> | |
| <text x="70" y="14" fill="#fff" font-family="DejaVu Sans" font-size="11" text-anchor="middle">Backend Coverage</text> | |
| <text x="170" y="14" fill="#fff" font-family="DejaVu Sans" font-size="11" text-anchor="middle">${pct}%</text> | |
| </svg> | |
| EOF | |
| - name: Commit badge | |
| if: github.ref == 'refs/heads/main' | |
| run: | | |
| git config user.name "github-actions" | |
| git config user.email "github-actions@github.com" | |
| git add ../../../.github/badges/backend-coverage.svg | |
| git commit -m "Update backend coverage badge" || exit 0 | |
| git push |