Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions .github/workflows/post-coverage-comment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
name: Post Coverage Comment

on:
workflow_run:
workflows: ["PR Code Coverage"]
types:
- completed

jobs:
post-comment:
runs-on: ubuntu-latest
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
Comment on lines +12 to +14
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition only checks if the workflow_run concluded successfully, but it doesn't verify that the coverage artifact was actually uploaded. If the pr-code-coverage workflow succeeds but skips the artifact upload step (e.g., due to the conditional on line 446 of pr-code-coverage.yml), this workflow will still run and fail when trying to download a non-existent artifact. Consider adding a check for the specific job or step status that uploads the artifact.

Copilot uses AI. Check for mistakes.
permissions:
pull-requests: write
contents: read

steps:
- name: Download coverage data
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh run download ${{ github.event.workflow_run.id }} \
--repo ${{ github.repository }} \
--name coverage-comment-data

- name: Read coverage data
id: coverage
run: |
if [[ ! -f pr-info.json ]]; then
echo "❌ pr-info.json not found"
exit 1
fi

cat pr-info.json

# Extract values from JSON with proper quoting
PR_NUMBER="$(jq -r '.pr_number' pr-info.json)"
COVERAGE_PCT="$(jq -r '.coverage_percentage' pr-info.json)"
COVERED_LINES="$(jq -r '.covered_lines' pr-info.json)"
TOTAL_LINES="$(jq -r '.total_lines' pr-info.json)"
PATCH_PCT="$(jq -r '.patch_coverage_pct' pr-info.json)"
LOW_COV_FILES="$(jq -r '.low_coverage_files' pr-info.json)"
PATCH_SUMMARY="$(jq -r '.patch_coverage_summary' pr-info.json)"
ADO_URL="$(jq -r '.ado_url' pr-info.json)"

# Export to env for next step (single-line values)
echo "PR_NUMBER=${PR_NUMBER}" >> $GITHUB_ENV
echo "COVERAGE_PERCENTAGE=${COVERAGE_PCT}" >> $GITHUB_ENV
echo "COVERED_LINES=${COVERED_LINES}" >> $GITHUB_ENV
echo "TOTAL_LINES=${TOTAL_LINES}" >> $GITHUB_ENV
echo "PATCH_COVERAGE_PCT=${PATCH_PCT}" >> $GITHUB_ENV
echo "ADO_URL=${ADO_URL}" >> $GITHUB_ENV

# Handle multiline values with proper quoting
{
echo "LOW_COVERAGE_FILES<<EOF"
echo "$LOW_COV_FILES"
echo "EOF"
} >> $GITHUB_ENV

{
echo "PATCH_COVERAGE_SUMMARY<<EOF"
echo "$PATCH_SUMMARY"
echo "EOF"
} >> $GITHUB_ENV

- name: Comment coverage summary on PR
uses: marocchino/sticky-pull-request-comment@v2
with:
number: ${{ env.PR_NUMBER }}
header: Code Coverage Report
message: |
# 📊 Code Coverage Report

<table>
<tr>
<td align="center" width="200">

### 🔥 Diff Coverage
### **${{ env.PATCH_COVERAGE_PCT }}**
<br>
</td>
<td align="center" width="200">

### 🎯 Overall Coverage
### **${{ env.COVERAGE_PERCENTAGE }}**
<br>
</td>
<td>

**📈 Total Lines Covered:** `${{ env.COVERED_LINES }}` out of `${{ env.TOTAL_LINES }}`
**📁 Project:** `mssql-python`

</td>
</tr>
</table>

---

${{ env.PATCH_COVERAGE_SUMMARY }}

---
### 📋 Files Needing Attention

<details>
<summary>📉 <strong>Files with overall lowest coverage</strong> (click to expand)</summary>
<br>

```diff
${{ env.LOW_COVERAGE_FILES }}
```

</details>

---
### 🔗 Quick Links

<table>
<tr>
<td align="left" width="200">
<b>⚙️ Build Summary</b>
</td>
<td align="left">
<b>📋 Coverage Details</b>
</td>
</tr>
<tr>
<td align="left" width="200">

[View Azure DevOps Build](${{ env.ADO_URL }})

</td>
<td align="left">

[Browse Full Coverage Report](${{ env.ADO_URL }}&view=codecoverage-tab)

</td>
</tr>
</table>
42 changes: 42 additions & 0 deletions .github/workflows/pr-code-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,49 @@ jobs:
echo "PATCH_COVERAGE_SUMMARY=Patch coverage report could not be generated." >> $GITHUB_ENV
fi

- name: Save coverage data for comment
run: |
mkdir -p coverage-comment-data
jq -n \
--arg pr_number "${{ github.event.pull_request.number }}" \
--arg coverage_percentage "${{ env.COVERAGE_PERCENTAGE }}" \
--arg covered_lines "${{ env.COVERED_LINES }}" \
--arg total_lines "${{ env.TOTAL_LINES }}" \
--arg patch_coverage_pct "${{ env.PATCH_COVERAGE_PCT }}" \
--arg low_coverage_files "${{ env.LOW_COVERAGE_FILES }}" \
--arg patch_coverage_summary "${{ env.PATCH_COVERAGE_SUMMARY }}" \
--arg ado_url "${{ env.ADO_URL }}" \
'{
pr_number: $pr_number,
coverage_percentage: $coverage_percentage,
covered_lines: $covered_lines,
total_lines: $total_lines,
patch_coverage_pct: $patch_coverage_pct,
low_coverage_files: $low_coverage_files,
patch_coverage_summary: $patch_coverage_summary,
ado_url: $ado_url
}' > coverage-comment-data/pr-info.json

# Validate JSON before uploading
echo "Validating generated JSON..."
jq . coverage-comment-data/pr-info.json > /dev/null || {
echo "❌ Invalid JSON generated"
cat coverage-comment-data/pr-info.json
exit 1
}
echo "✅ JSON validation successful"
cat coverage-comment-data/pr-info.json

Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The step does not validate the JSON output before uploading it as an artifact. If the jq command fails or produces invalid JSON due to special characters in the environment variables, the artifact will contain malformed JSON that will cause the downstream workflow to fail. Consider adding a validation step using 'jq . coverage-comment-data/pr-info.json' after generating the file to ensure it's valid JSON before uploading.

Suggested change
- name: Validate coverage comment JSON
run: jq . coverage-comment-data/pr-info.json

Copilot uses AI. Check for mistakes.
- name: Upload coverage comment data
uses: actions/upload-artifact@v4
with:
name: coverage-comment-data
path: coverage-comment-data/
retention-days: 1

- name: Comment coverage summary on PR
# Skip for forked PRs due to token permission restrictions
if: github.event.pull_request.head.repo.full_name == github.repository
uses: marocchino/sticky-pull-request-comment@v2
with:
header: Code Coverage Report
Expand Down
5 changes: 3 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

# Clean one-liner: set level and output mode together
setup_logging(output="both")

print("Logging is set up.")
print("This is a test PR for mssql-python.")
conn_str = os.getenv("DB_CONNECTION_STRING")
conn = connect(conn_str)
cursor = conn.cursor()
Expand All @@ -15,4 +16,4 @@
print(f"Database ID: {row[0]}, Name: {row[1]}")

cursor.close()
conn.close()
conn.close()
Loading