Skip to content

Commit 739a718

Browse files
authored
Merge branch 'main' into jahnvi/pyformat_param_style
2 parents 9947af8 + c666f6c commit 739a718

File tree

3 files changed

+270
-70
lines changed

3 files changed

+270
-70
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
name: Post Coverage Comment
2+
description: Posts a standardized code coverage comment on a pull request
3+
4+
inputs:
5+
pr_number:
6+
description: 'Pull request number'
7+
required: true
8+
coverage_percentage:
9+
description: 'Overall coverage percentage'
10+
required: true
11+
covered_lines:
12+
description: 'Number of covered lines'
13+
required: true
14+
total_lines:
15+
description: 'Total number of lines'
16+
required: true
17+
patch_coverage_pct:
18+
description: 'Patch/diff coverage percentage'
19+
required: true
20+
low_coverage_files:
21+
description: 'Files with lowest coverage (multiline)'
22+
required: true
23+
patch_coverage_summary:
24+
description: 'Patch coverage summary markdown (multiline)'
25+
required: true
26+
ado_url:
27+
description: 'Azure DevOps build URL'
28+
required: true
29+
30+
runs:
31+
using: composite
32+
steps:
33+
- name: Post coverage comment
34+
uses: marocchino/sticky-pull-request-comment@v2
35+
with:
36+
header: Code Coverage Report
37+
number: ${{ inputs.pr_number }}
38+
message: |
39+
# 📊 Code Coverage Report
40+
41+
<table>
42+
<tr>
43+
<td align="center" width="200">
44+
45+
### 🔥 Diff Coverage
46+
### **${{ inputs.patch_coverage_pct }}**
47+
<br>
48+
</td>
49+
<td align="center" width="200">
50+
51+
### 🎯 Overall Coverage
52+
### **${{ inputs.coverage_percentage }}**
53+
<br>
54+
</td>
55+
<td>
56+
57+
**📈 Total Lines Covered:** `${{ inputs.covered_lines }}` out of `${{ inputs.total_lines }}`
58+
**📁 Project:** `mssql-python`
59+
60+
</td>
61+
</tr>
62+
</table>
63+
64+
---
65+
66+
${{ inputs.patch_coverage_summary }}
67+
68+
---
69+
### 📋 Files Needing Attention
70+
71+
<details>
72+
<summary>📉 <strong>Files with overall lowest coverage</strong> (click to expand)</summary>
73+
<br>
74+
75+
```diff
76+
${{ inputs.low_coverage_files }}
77+
```
78+
79+
</details>
80+
81+
---
82+
### 🔗 Quick Links
83+
84+
<table>
85+
<tr>
86+
<td align="left" width="200">
87+
<b>⚙️ Build Summary</b>
88+
</td>
89+
<td align="left">
90+
<b>📋 Coverage Details</b>
91+
</td>
92+
</tr>
93+
<tr>
94+
<td align="left" width="200">
95+
96+
[View Azure DevOps Build](${{ inputs.ado_url }})
97+
98+
</td>
99+
<td align="left">
100+
101+
[Browse Full Coverage Report](${{ inputs.ado_url }}&view=codecoverage-tab)
102+
103+
</td>
104+
</tr>
105+
</table>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
name: Post Coverage Comment
2+
3+
# This workflow handles posting coverage comments for FORKED PRs.
4+
#
5+
# Why a separate workflow?
6+
# - Forked PRs have restricted GITHUB_TOKEN permissions for security
7+
# - They cannot write comments directly to the base repository's PRs
8+
# - workflow_run triggers run in the BASE repository context with full permissions
9+
# - This allows us to safely post comments on forked PRs
10+
#
11+
# How it works:
12+
# 1. PR Code Coverage workflow uploads coverage data as an artifact (forked PRs only)
13+
# 2. This workflow triggers when PR Code Coverage completes successfully
14+
# 3. Downloads the artifact and posts the comment with full write permissions
15+
#
16+
# Same-repo PRs post comments directly in pr-code-coverage.yml (faster)
17+
# Forked PRs use this workflow (required for permissions)
18+
19+
on:
20+
workflow_run:
21+
workflows: ["PR Code Coverage"]
22+
types:
23+
- completed
24+
25+
jobs:
26+
post-comment:
27+
runs-on: ubuntu-latest
28+
if: >
29+
github.event.workflow_run.event == 'pull_request' &&
30+
github.event.workflow_run.conclusion == 'success'
31+
permissions:
32+
pull-requests: write
33+
contents: read
34+
35+
steps:
36+
- name: Checkout repo
37+
uses: actions/checkout@v4
38+
39+
- name: Download coverage data
40+
env:
41+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
42+
run: |
43+
# Download artifact with error handling for non-existent artifacts
44+
if ! gh run download ${{ github.event.workflow_run.id }} \
45+
--repo ${{ github.repository }} \
46+
--name coverage-comment-data 2>&1; then
47+
echo "⚠️ No coverage-comment-data artifact found"
48+
echo "This is expected for same-repo PRs (they post comments directly)"
49+
echo "Exiting gracefully..."
50+
exit 0
51+
fi
52+
53+
# Verify artifact was downloaded
54+
if [[ ! -f pr-info.json ]]; then
55+
echo "⚠️ Artifact downloaded but pr-info.json not found"
56+
echo "This may indicate an issue with artifact upload"
57+
exit 1
58+
fi
59+
60+
- name: Read coverage data
61+
id: coverage
62+
run: |
63+
if [[ ! -f pr-info.json ]]; then
64+
echo "❌ pr-info.json not found"
65+
exit 1
66+
fi
67+
68+
cat pr-info.json
69+
70+
# Extract values from JSON with proper quoting
71+
PR_NUMBER="$(jq -r '.pr_number' pr-info.json)"
72+
COVERAGE_PCT="$(jq -r '.coverage_percentage' pr-info.json)"
73+
COVERED_LINES="$(jq -r '.covered_lines' pr-info.json)"
74+
TOTAL_LINES="$(jq -r '.total_lines' pr-info.json)"
75+
PATCH_PCT="$(jq -r '.patch_coverage_pct' pr-info.json)"
76+
LOW_COV_FILES="$(jq -r '.low_coverage_files' pr-info.json)"
77+
PATCH_SUMMARY="$(jq -r '.patch_coverage_summary' pr-info.json)"
78+
ADO_URL="$(jq -r '.ado_url' pr-info.json)"
79+
80+
# Export to env for next step (single-line values)
81+
echo "PR_NUMBER=${PR_NUMBER}" >> $GITHUB_ENV
82+
echo "COVERAGE_PERCENTAGE=${COVERAGE_PCT}" >> $GITHUB_ENV
83+
echo "COVERED_LINES=${COVERED_LINES}" >> $GITHUB_ENV
84+
echo "TOTAL_LINES=${TOTAL_LINES}" >> $GITHUB_ENV
85+
echo "PATCH_COVERAGE_PCT=${PATCH_PCT}" >> $GITHUB_ENV
86+
echo "ADO_URL=${ADO_URL}" >> $GITHUB_ENV
87+
88+
# Handle multiline values with proper quoting
89+
{
90+
echo "LOW_COVERAGE_FILES<<EOF"
91+
echo "$LOW_COV_FILES"
92+
echo "EOF"
93+
} >> $GITHUB_ENV
94+
95+
{
96+
echo "PATCH_COVERAGE_SUMMARY<<EOF"
97+
echo "$PATCH_SUMMARY"
98+
echo "EOF"
99+
} >> $GITHUB_ENV
100+
101+
- name: Comment coverage summary on PR
102+
uses: ./.github/actions/post-coverage-comment
103+
with:
104+
pr_number: ${{ env.PR_NUMBER }}
105+
coverage_percentage: ${{ env.COVERAGE_PERCENTAGE }}
106+
covered_lines: ${{ env.COVERED_LINES }}
107+
total_lines: ${{ env.TOTAL_LINES }}
108+
patch_coverage_pct: ${{ env.PATCH_COVERAGE_PCT }}
109+
low_coverage_files: ${{ env.LOW_COVERAGE_FILES }}
110+
patch_coverage_summary: ${{ env.PATCH_COVERAGE_SUMMARY }}
111+
ado_url: ${{ env.ADO_URL }}

.github/workflows/pr-code-coverage.yml

Lines changed: 54 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -417,75 +417,59 @@ jobs:
417417
echo "PATCH_COVERAGE_SUMMARY=Patch coverage report could not be generated." >> $GITHUB_ENV
418418
fi
419419
420-
- name: Comment coverage summary on PR
421-
uses: marocchino/sticky-pull-request-comment@v2
422-
with:
423-
header: Code Coverage Report
424-
message: |
425-
# 📊 Code Coverage Report
426-
427-
<table>
428-
<tr>
429-
<td align="center" width="200">
430-
431-
### 🔥 Diff Coverage
432-
### **${{ env.PATCH_COVERAGE_PCT }}**
433-
<br>
434-
</td>
435-
<td align="center" width="200">
436-
437-
### 🎯 Overall Coverage
438-
### **${{ env.COVERAGE_PERCENTAGE }}**
439-
<br>
440-
</td>
441-
<td>
442-
443-
**📈 Total Lines Covered:** `${{ env.COVERED_LINES }}` out of `${{ env.TOTAL_LINES }}`
444-
**📁 Project:** `mssql-python`
445-
446-
</td>
447-
</tr>
448-
</table>
449-
450-
---
451-
452-
${{ env.PATCH_COVERAGE_SUMMARY }}
453-
454-
---
455-
### 📋 Files Needing Attention
456-
457-
<details>
458-
<summary>📉 <strong>Files with overall lowest coverage</strong> (click to expand)</summary>
459-
<br>
460-
461-
```diff
462-
${{ env.LOW_COVERAGE_FILES }}
463-
```
464-
465-
</details>
466-
467-
---
468-
### 🔗 Quick Links
469-
470-
<table>
471-
<tr>
472-
<td align="left" width="200">
473-
<b>⚙️ Build Summary</b>
474-
</td>
475-
<td align="left">
476-
<b>📋 Coverage Details</b>
477-
</td>
478-
</tr>
479-
<tr>
480-
<td align="left" width="200">
481-
482-
[View Azure DevOps Build](${{ env.ADO_URL }})
483-
484-
</td>
485-
<td align="left">
420+
- name: Save coverage data for comment
421+
run: |
422+
mkdir -p coverage-comment-data
423+
jq -n \
424+
--arg pr_number "${{ github.event.pull_request.number }}" \
425+
--arg coverage_percentage "${{ env.COVERAGE_PERCENTAGE }}" \
426+
--arg covered_lines "${{ env.COVERED_LINES }}" \
427+
--arg total_lines "${{ env.TOTAL_LINES }}" \
428+
--arg patch_coverage_pct "${{ env.PATCH_COVERAGE_PCT }}" \
429+
--arg low_coverage_files "${{ env.LOW_COVERAGE_FILES }}" \
430+
--arg patch_coverage_summary "${{ env.PATCH_COVERAGE_SUMMARY }}" \
431+
--arg ado_url "${{ env.ADO_URL }}" \
432+
'{
433+
pr_number: $pr_number,
434+
coverage_percentage: $coverage_percentage,
435+
covered_lines: $covered_lines,
436+
total_lines: $total_lines,
437+
patch_coverage_pct: $patch_coverage_pct,
438+
low_coverage_files: $low_coverage_files,
439+
patch_coverage_summary: $patch_coverage_summary,
440+
ado_url: $ado_url
441+
}' > coverage-comment-data/pr-info.json
442+
443+
# Validate JSON before uploading
444+
echo "Validating generated JSON..."
445+
jq . coverage-comment-data/pr-info.json > /dev/null || {
446+
echo "❌ Invalid JSON generated"
447+
cat coverage-comment-data/pr-info.json
448+
exit 1
449+
}
450+
echo "✅ JSON validation successful"
451+
cat coverage-comment-data/pr-info.json
486452
487-
[Browse Full Coverage Report](${{ env.ADO_URL }}&view=codecoverage-tab)
453+
- name: Upload coverage comment data
454+
# Only upload artifact for forked PRs since same-repo PRs post comment directly
455+
# This prevents unnecessary workflow_run triggers for same-repo PRs
456+
if: github.event.pull_request.head.repo.full_name != github.repository
457+
uses: actions/upload-artifact@v4
458+
with:
459+
name: coverage-comment-data
460+
path: coverage-comment-data/
461+
retention-days: 7
488462

489-
</td>
490-
</tr>
491-
</table>
463+
- name: Comment coverage summary on PR
464+
# Skip for forked PRs due to token permission restrictions
465+
if: github.event.pull_request.head.repo.full_name == github.repository
466+
uses: ./.github/actions/post-coverage-comment
467+
with:
468+
pr_number: ${{ github.event.pull_request.number }}
469+
coverage_percentage: ${{ env.COVERAGE_PERCENTAGE }}
470+
covered_lines: ${{ env.COVERED_LINES }}
471+
total_lines: ${{ env.TOTAL_LINES }}
472+
patch_coverage_pct: ${{ env.PATCH_COVERAGE_PCT }}
473+
low_coverage_files: ${{ env.LOW_COVERAGE_FILES }}
474+
patch_coverage_summary: ${{ env.PATCH_COVERAGE_SUMMARY }}
475+
ado_url: ${{ env.ADO_URL }}

0 commit comments

Comments
 (0)