-
Notifications
You must be signed in to change notification settings - Fork 2
173 lines (156 loc) · 5.95 KB
/
coverage-pr.yml
File metadata and controls
173 lines (156 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
name: Coverage (PR)
on:
pull_request:
types: [opened, synchronize, reopened]
permissions:
contents: read
pull-requests: write
actions: read
jobs:
cov-head:
runs-on: ubuntu-latest
outputs:
pct: ${{ steps.cov.outputs.coverage }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Cache llvm-cov build
id: cache-llvm-cov
uses: actions/cache@v4
continue-on-error: false
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-llvm-cov-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-llvm-cov
- run: which cargo-llvm-cov || cargo install cargo-llvm-cov
- run: rustup component add llvm-tools-preview
- run: cargo llvm-cov --workspace --lcov --output-path lcov.info --ignore-filename-regex '^examples/'
- name: Upload head coverage artifact (for stacked PRs)
uses: actions/upload-artifact@v4
with:
name: coverage-lcov
path: lcov.info
retention-days: 21
- name: Extract total coverage % (head)
id: cov
shell: bash
run: |
pct=$(awk -F: '
/^LH:/ {lh += $2}
/^LF:/ {lf += $2}
END { if (lf>0) printf "%.2f", (lh/lf)*100; else print "0.00" }
' lcov.info)
echo "coverage=$pct" >> "$GITHUB_OUTPUT"
compare-and-comment:
needs: cov-head
runs-on: ubuntu-latest
steps:
# 1) Try to download base artifact from the base branch
- name: Download base branch artifact (if present)
id: dl
uses: dawidd6/action-download-artifact@ac66b43f0e6a346234dd65d4d0c8fbb31cb316e5
with:
name: coverage-lcov
branch: ${{ github.event.pull_request.base.ref }}
if_no_artifact_found: ignore # don't fail if missing
- name: Check if base artifact was found
id: base_art
shell: bash
run: |
if [ -f "coverage-lcov/lcov.info" ]; then
echo "found=true" >> "$GITHUB_OUTPUT"
else
echo "found=false" >> "$GITHUB_OUTPUT"
fi
# 2) Fallback: if no artifact, checkout base commit and compute base coverage here
- name: Checkout base
if: ${{ steps.base_art.outputs.found == 'false' }}
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.sha }}
- name: Cache llvm-cov build
id: cache-llvm-cov
if: ${{ steps.base_art.outputs.found == 'false' }}
uses: actions/cache@v4
continue-on-error: false
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-llvm-cov-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-llvm-cov
- name: Install cargo-llvm-cov (fallback path)
if: ${{ steps.base_art.outputs.found == 'false' }}
run: which cargo-llvm-cov || cargo install cargo-llvm-cov
- name: Install llvm-tools (fallback path)
if: ${{ steps.base_art.outputs.found == 'false' }}
run: rustup component add llvm-tools-preview
- name: Run base coverage (fallback path)
if: ${{ steps.base_art.outputs.found == 'false' }}
run: cargo llvm-cov --workspace --lcov --output-path base.lcov.info --ignore-filename-regex '^examples/'
# 3) Extract base coverage (from artifact or from freshly computed fallback)
- name: Extract total coverage % (base)
id: basecov
shell: bash
run: |
file="coverage-lcov/lcov.info"
if [ "${{ steps.base_art.outputs.found }}" != "true" ]; then
file="base.lcov.info"
fi
pct=$(awk -F: '
/^LH:/ {lh += $2}
/^LF:/ {lf += $2}
END { if (lf>0) printf "%.2f", (lh/lf)*100; else print "0.00" }
' "$file")
echo "coverage=$pct" >> "$GITHUB_OUTPUT"
- name: Find existing coverage comment
id: find_comment
uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: '<!-- coverage-bot -->'
- name: Comment only if head < base
uses: actions/github-script@v7
with:
script: |
const base = parseFloat(`${{ steps.basecov.outputs.coverage }}`);
const head = parseFloat(`${{ needs.cov-head.outputs.pct }}`);
if (!(base >= 0) || !(head >= 0)) {
core.setFailed(`Bad coverage values. base=${base} head=${head}`);
return;
}
if (head >= base) {
core.info(`No drop (head ${head}% >= base ${base}%).`);
return;
}
const delta = (head - base).toFixed(2);
const body = `<!-- coverage-bot -->
**Coverage (base → head):** \`${base}% → ${head}%\` ⬇️ ${Math.abs(delta)} pp
_Only posts when PR coverage is lower than the base branch's latest push (or freshly computed base)._
`;
const commentId = `${{ steps.find_comment.outputs.comment-id }}`;
if (commentId) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: parseInt(commentId, 10),
body
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body
});
}