Skip to content

Commit c54b0e6

Browse files
Make more outputs available
1 parent 3d3b041 commit c54b0e6

5 files changed

Lines changed: 247 additions & 53 deletions

File tree

README.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,33 @@ jobs:
348348
349349
### Outputs
350350
351-
The action makes available some data for downstream processing (Only available when running in PR mode):
352-
353-
- COMMENT_FILE_WRITTEN: A boolean indicating whether a comment file was written to COMMENT_FILENAME or not.
354-
- COVERAGE_PERCENTAGE: The coverage percentage of the pull request.
355-
- REFERENCE_COVERAGE_PERCENTAGE: The coverage percentage of the base branch.
351+
The action makes available some data for downstream processing. All outputs are only available when running in PR mode.
352+
353+
| Name | Description |
354+
| --- | --- |
355+
| `COMMENT_FILE_WRITTEN` | A boolean indicating whether a comment file was written to `COMMENT_FILENAME` or not. |
356+
| `NEW_COVERED_LINES` | The number of covered lines in the pull request. |
357+
| `NEW_NUM_STATEMENTS` | The number of statements in the pull request. |
358+
| `NEW_PERCENT_COVERED` | The coverage percentage of the pull request. |
359+
| `NEW_MISSING_LINES` | The number of lines with missing coverage in the pull request. |
360+
| `NEW_EXCLUDED_LINES` | The number of excluded lines in the pull request. |
361+
| `NEW_NUM_BRANCHES` | The number of branches in the pull request. |
362+
| `NEW_NUM_PARTIAL_BRANCHES` | The number of partial branches in the pull request. |
363+
| `NEW_COVERED_BRANCHES` | The number of covered branches in the pull request. |
364+
| `NEW_MISSING_BRANCHES` | The number of branches with missing coverage in the pull request. |
365+
| `REFERENCE_COVERED_LINES` | The number of covered lines in the base branch. |
366+
| `REFERENCE_NUM_STATEMENTS` | The number of statements in the base branch. |
367+
| `REFERENCE_PERCENT_COVERED` | The coverage percentage of the base branch. |
368+
| `REFERENCE_MISSING_LINES` | The number of lines with missing coverage in the base branch. |
369+
| `REFERENCE_EXCLUDED_LINES` | The number of excluded lines in the base branch. |
370+
| `REFERENCE_NUM_BRANCHES` | The number of branches in the base branch. |
371+
| `REFERENCE_NUM_PARTIAL_BRANCHES` | The number of partial branches in the base branch. |
372+
| `REFERENCE_COVERED_BRANCHES` | The number of covered branches in the base branch. |
373+
| `REFERENCE_MISSING_BRANCHES` | The number of branches with missing coverage in the base branch. |
374+
| `DIFF_TOTAL_NUM_LINES` | The total number of lines in the diff. |
375+
| `DIFF_TOTAL_NUM_VIOLATIONS` | The total number of lines with missing coverage in the diff. |
376+
| `DIFF_TOTAL_PERCENT_COVERED` | The coverage percentage of the diff. |
377+
| `DIFF_NUM_CHANGED_LINES` | The number of changed lines in the diff. |
356378

357379
Usage may look like this
358380

@@ -364,7 +386,7 @@ Usage may look like this
364386
GITHUB_TOKEN: ${{ github.token }}
365387
366388
- name: Enforce coverage
367-
if: ${{ steps.coverage_comment.outputs.COVERAGE_PERCENTAGE < steps.coverage_comment.outputs.REFERENCE_COVERAGE_PERCENTAGE }}
389+
if: ${{ steps.coverage_comment.outputs.NEW_PERCENT_COVERED < steps.coverage_comment.outputs.REFERENCE_PERCENT_COVERED }}
368390
run: echo "Coverage decreased." && exit 1
369391
```
370392

action.yml

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,50 @@ outputs:
117117
If so, you'll need to run the action in workflow_run mode to post it. If "false",
118118
no comment file was written (likely because the comment was already posted to the PR).
119119
Only available when running in PR mode.
120-
COVERAGE_PERCENTAGE:
121-
description: >
122-
The coverage percentage of the pull request (Only available when running in PR mode).
123-
REFERENCE_COVERAGE_PERCENTAGE:
124-
description: >
125-
The coverage percentage of the base branch (Only available when running in PR mode).
120+
NEW_COVERED_LINES:
121+
description: The number of covered lines in the pull request. (Only available in PR mode)
122+
NEW_NUM_STATEMENTS:
123+
description: The number of statements in the pull request. (Only available in PR mode)
124+
NEW_PERCENT_COVERED:
125+
description: The coverage percentage of the pull request. (Only available in PR mode)
126+
NEW_MISSING_LINES:
127+
description: The number of lines with missing coverage in the pull request. (Only available in PR mode)
128+
NEW_EXCLUDED_LINES:
129+
description: The number of excluded lines in the pull request. (Only available in PR mode)
130+
NEW_NUM_BRANCHES:
131+
description: The number of branches in the pull request. (Only available in PR mode)
132+
NEW_NUM_PARTIAL_BRANCHES:
133+
description: The number of partial branches in the pull request. (Only available in PR mode)
134+
NEW_COVERED_BRANCHES:
135+
description: The number of covered branches in the pull request. (Only available in PR mode)
136+
NEW_MISSING_BRANCHES:
137+
description: The number of branches with missing coverage in the pull request. (Only available in PR mode)
138+
REFERENCE_COVERED_LINES:
139+
description: The number of covered lines in the base branch. (Only available in PR mode)
140+
REFERENCE_NUM_STATEMENTS:
141+
description: The number of statements in the base branch. (Only available in PR mode)
142+
REFERENCE_PERCENT_COVERED:
143+
description: The coverage percentage of the base branch. (Only available in PR mode)
144+
REFERENCE_MISSING_LINES:
145+
description: The number of lines with missing coverage in the base branch. (Only available in PR mode)
146+
REFERENCE_EXCLUDED_LINES:
147+
description: The number of excluded lines in the base branch. (Only available in PR mode)
148+
REFERENCE_NUM_BRANCHES:
149+
description: The number of branches in the base branch. (Only available in PR mode)
150+
REFERENCE_NUM_PARTIAL_BRANCHES:
151+
description: The number of partial branches in the base branch. (Only available in PR mode)
152+
REFERENCE_COVERED_BRANCHES:
153+
description: The number of covered branches in the base branch. (Only available in PR mode)
154+
REFERENCE_MISSING_BRANCHES:
155+
description: The number of branches with missing coverage in the base branch. (Only available in PR mode)
156+
DIFF_TOTAL_NUM_LINES:
157+
description: The total number of lines in the diff. (Only available in PR mode)
158+
DIFF_TOTAL_NUM_VIOLATIONS:
159+
description: The total number of lines with missing coverage in the diff. (Only available in PR mode)
160+
DIFF_TOTAL_PERCENT_COVERED:
161+
description: The coverage percentage of the diff. (Only available in PR mode)
162+
DIFF_NUM_CHANGED_LINES:
163+
description: The number of changed lines in the diff. (Only available in PR mode)
126164
runs:
127165
using: docker
128166
image: Dockerfile

coverage_comment/coverage.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ class CoverageInfo:
3333
covered_branches: int = 0
3434
missing_branches: int = 0
3535

36+
def as_output(self, prefix: str) -> dict:
37+
data = dataclasses.asdict(self)
38+
output = {}
39+
for key, value in data.items():
40+
if value is not None:
41+
output[f"{prefix}_{key.upper()}"] = (
42+
float(value) if isinstance(value, decimal.Decimal) else value
43+
)
44+
return output
45+
3646

3747
@dataclasses.dataclass(kw_only=True)
3848
class FileCoverage:
@@ -83,6 +93,16 @@ class DiffCoverage:
8393
num_changed_lines: int
8494
files: dict[pathlib.Path, FileDiffCoverage]
8595

96+
def as_output(self, prefix: str) -> dict:
97+
data = dataclasses.asdict(self)
98+
output = {}
99+
for key, value in data.items():
100+
if value is not None and not isinstance(value, dict):
101+
output[f"{prefix}_{key.upper()}"] = (
102+
float(value) if isinstance(value, decimal.Decimal) else value
103+
)
104+
return output
105+
86106

87107
def compute_coverage(
88108
num_covered: int,

coverage_comment/main.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,10 @@ def process_pr(
268268
],
269269
)
270270

271-
outputs = {
272-
"COVERAGE_PERCENTAGE": float(coverage.info.percent_covered),
273-
"REFERENCE_COVERAGE_PERCENTAGE": float(previous_coverage_rate)
274-
if previous_coverage_rate is not None
275-
else None,
276-
}
271+
outputs = coverage.info.as_output(prefix="NEW")
272+
outputs |= diff_coverage.as_output(prefix="DIFF")
273+
if previous_coverage:
274+
outputs |= previous_coverage.info.as_output(prefix="REFERENCE")
277275

278276
try:
279277
if config.FORCE_WORKFLOW_RUN or not pr_number:

tests/integration/test_main.py

Lines changed: 151 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,29 @@ def checker(payload):
111111
in comment
112112
)
113113

114-
expected_output = [
115-
"COMMENT_FILE_WRITTEN=true",
116-
"COVERAGE_PERCENTAGE=0.7777777777777778",
117-
"REFERENCE_COVERAGE_PERCENTAGE=null",
118-
]
119-
# The order of keys is not guaranteed, so we'll sort the lines
120-
assert sorted(output_file.read_text().strip().splitlines()) == expected_output
114+
expected_output = {
115+
"COMMENT_FILE_WRITTEN": "true",
116+
"NEW_COVERED_LINES": "7",
117+
"NEW_NUM_STATEMENTS": "9",
118+
"NEW_PERCENT_COVERED": "0.7777777777777778",
119+
"NEW_MISSING_LINES": "2",
120+
"NEW_EXCLUDED_LINES": "0",
121+
"NEW_NUM_BRANCHES": "0",
122+
"NEW_NUM_PARTIAL_BRANCHES": "0",
123+
"NEW_COVERED_BRANCHES": "0",
124+
"NEW_MISSING_BRANCHES": "0",
125+
"DIFF_TOTAL_NUM_LINES": "4",
126+
"DIFF_TOTAL_NUM_VIOLATIONS": "1",
127+
"DIFF_TOTAL_PERCENT_COVERED": "0.75",
128+
"DIFF_NUM_CHANGED_LINES": "6",
129+
}
130+
output = {
131+
key: value
132+
for key, value in (
133+
line.split("=") for line in output_file.read_text().strip().splitlines()
134+
)
135+
}
136+
assert output == expected_output
121137

122138

123139
@pytest.mark.add_branches("foo")
@@ -242,13 +258,38 @@ def checker(payload):
242258
assert comment.count("<img") == 10
243259
assert comment == summary_file.read_text()
244260

245-
expected_output = [
246-
"COMMENT_FILE_WRITTEN=false",
247-
"COVERAGE_PERCENTAGE=0.7777777777777778",
248-
"REFERENCE_COVERAGE_PERCENTAGE=0.3",
249-
]
250-
# The order of keys is not guaranteed, so we'll sort the lines
251-
assert sorted(output_file.read_text().strip().splitlines()) == expected_output
261+
expected_output = {
262+
"COMMENT_FILE_WRITTEN": "false",
263+
"NEW_COVERED_LINES": "7",
264+
"NEW_NUM_STATEMENTS": "9",
265+
"NEW_PERCENT_COVERED": "0.7777777777777778",
266+
"NEW_MISSING_LINES": "2",
267+
"NEW_EXCLUDED_LINES": "0",
268+
"NEW_NUM_BRANCHES": "0",
269+
"NEW_NUM_PARTIAL_BRANCHES": "0",
270+
"NEW_COVERED_BRANCHES": "0",
271+
"NEW_MISSING_BRANCHES": "0",
272+
"REFERENCE_COVERED_LINES": "3",
273+
"REFERENCE_NUM_STATEMENTS": "10",
274+
"REFERENCE_PERCENT_COVERED": "0.3",
275+
"REFERENCE_MISSING_LINES": "7",
276+
"REFERENCE_EXCLUDED_LINES": "0",
277+
"REFERENCE_NUM_BRANCHES": "0",
278+
"REFERENCE_NUM_PARTIAL_BRANCHES": "0",
279+
"REFERENCE_COVERED_BRANCHES": "0",
280+
"REFERENCE_MISSING_BRANCHES": "0",
281+
"DIFF_TOTAL_NUM_LINES": "4",
282+
"DIFF_TOTAL_NUM_VIOLATIONS": "1",
283+
"DIFF_TOTAL_PERCENT_COVERED": "0.75",
284+
"DIFF_NUM_CHANGED_LINES": "6",
285+
}
286+
output = {
287+
key: value
288+
for key, value in (
289+
line.split("=") for line in output_file.read_text().strip().splitlines()
290+
)
291+
}
292+
assert output == expected_output
252293

253294

254295
def test_action__push__non_default_branch(
@@ -319,13 +360,38 @@ def checker(payload):
319360
assert "Coverage for the whole project went from 30% to 77.77%" in comment
320361
assert comment == summary_file.read_text()
321362

322-
expected_output = [
323-
"COMMENT_FILE_WRITTEN=false",
324-
"COVERAGE_PERCENTAGE=0.7777777777777778",
325-
"REFERENCE_COVERAGE_PERCENTAGE=0.3",
326-
]
327-
# The order of keys is not guaranteed, so we'll sort the lines
328-
assert sorted(output_file.read_text().strip().splitlines()) == expected_output
363+
expected_output = {
364+
"COMMENT_FILE_WRITTEN": "false",
365+
"NEW_COVERED_LINES": "7",
366+
"NEW_NUM_STATEMENTS": "9",
367+
"NEW_PERCENT_COVERED": "0.7777777777777778",
368+
"NEW_MISSING_LINES": "2",
369+
"NEW_EXCLUDED_LINES": "0",
370+
"NEW_NUM_BRANCHES": "0",
371+
"NEW_NUM_PARTIAL_BRANCHES": "0",
372+
"NEW_COVERED_BRANCHES": "0",
373+
"NEW_MISSING_BRANCHES": "0",
374+
"REFERENCE_COVERED_LINES": "3",
375+
"REFERENCE_NUM_STATEMENTS": "10",
376+
"REFERENCE_PERCENT_COVERED": "0.3",
377+
"REFERENCE_MISSING_LINES": "7",
378+
"REFERENCE_EXCLUDED_LINES": "0",
379+
"REFERENCE_NUM_BRANCHES": "0",
380+
"REFERENCE_NUM_PARTIAL_BRANCHES": "0",
381+
"REFERENCE_COVERED_BRANCHES": "0",
382+
"REFERENCE_MISSING_BRANCHES": "0",
383+
"DIFF_TOTAL_NUM_LINES": "4",
384+
"DIFF_TOTAL_NUM_VIOLATIONS": "1",
385+
"DIFF_TOTAL_PERCENT_COVERED": "0.75",
386+
"DIFF_NUM_CHANGED_LINES": "6",
387+
}
388+
output = {
389+
key: value
390+
for key, value in (
391+
line.split("=") for line in output_file.read_text().strip().splitlines()
392+
)
393+
}
394+
assert output == expected_output
329395

330396

331397
def test_action__push__no_branch(
@@ -400,13 +466,38 @@ def test_action__push__non_default_branch__no_pr(
400466

401467
assert pathlib.Path("python-coverage-comment-action.txt").exists()
402468

403-
expected_output = [
404-
"COMMENT_FILE_WRITTEN=true",
405-
"COVERAGE_PERCENTAGE=0.7777777777777778",
406-
"REFERENCE_COVERAGE_PERCENTAGE=0.3",
407-
]
408-
# The order of keys is not guaranteed, so we'll sort the lines
409-
assert sorted(output_file.read_text().strip().splitlines()) == expected_output
469+
expected_output = {
470+
"COMMENT_FILE_WRITTEN": "true",
471+
"NEW_COVERED_LINES": "7",
472+
"NEW_NUM_STATEMENTS": "9",
473+
"NEW_PERCENT_COVERED": "0.7777777777777778",
474+
"NEW_MISSING_LINES": "2",
475+
"NEW_EXCLUDED_LINES": "0",
476+
"NEW_NUM_BRANCHES": "0",
477+
"NEW_NUM_PARTIAL_BRANCHES": "0",
478+
"NEW_COVERED_BRANCHES": "0",
479+
"NEW_MISSING_BRANCHES": "0",
480+
"REFERENCE_COVERED_LINES": "3",
481+
"REFERENCE_NUM_STATEMENTS": "10",
482+
"REFERENCE_PERCENT_COVERED": "0.3",
483+
"REFERENCE_MISSING_LINES": "7",
484+
"REFERENCE_EXCLUDED_LINES": "0",
485+
"REFERENCE_NUM_BRANCHES": "0",
486+
"REFERENCE_NUM_PARTIAL_BRANCHES": "0",
487+
"REFERENCE_COVERED_BRANCHES": "0",
488+
"REFERENCE_MISSING_BRANCHES": "0",
489+
"DIFF_TOTAL_NUM_LINES": "4",
490+
"DIFF_TOTAL_NUM_VIOLATIONS": "1",
491+
"DIFF_TOTAL_PERCENT_COVERED": "0.75",
492+
"DIFF_NUM_CHANGED_LINES": "6",
493+
}
494+
output = {
495+
key: value
496+
for key, value in (
497+
line.split("=") for line in output_file.read_text().strip().splitlines()
498+
)
499+
}
500+
assert output == expected_output
410501

411502

412503
def test_action__pull_request__force_store_comment(
@@ -436,13 +527,38 @@ def test_action__pull_request__force_store_comment(
436527

437528
assert pathlib.Path("python-coverage-comment-action.txt").exists()
438529

439-
expected_output = [
440-
"COMMENT_FILE_WRITTEN=true",
441-
"COVERAGE_PERCENTAGE=0.7777777777777778",
442-
"REFERENCE_COVERAGE_PERCENTAGE=0.3",
443-
]
444-
# The order of keys is not guaranteed, so we'll sort the lines
445-
assert sorted(output_file.read_text().strip().splitlines()) == expected_output
530+
expected_output = {
531+
"COMMENT_FILE_WRITTEN": "true",
532+
"NEW_COVERED_LINES": "7",
533+
"NEW_NUM_STATEMENTS": "9",
534+
"NEW_PERCENT_COVERED": "0.7777777777777778",
535+
"NEW_MISSING_LINES": "2",
536+
"NEW_EXCLUDED_LINES": "0",
537+
"NEW_NUM_BRANCHES": "0",
538+
"NEW_NUM_PARTIAL_BRANCHES": "0",
539+
"NEW_COVERED_BRANCHES": "0",
540+
"NEW_MISSING_BRANCHES": "0",
541+
"REFERENCE_COVERED_LINES": "3",
542+
"REFERENCE_NUM_STATEMENTS": "10",
543+
"REFERENCE_PERCENT_COVERED": "0.3",
544+
"REFERENCE_MISSING_LINES": "7",
545+
"REFERENCE_EXCLUDED_LINES": "0",
546+
"REFERENCE_NUM_BRANCHES": "0",
547+
"REFERENCE_NUM_PARTIAL_BRANCHES": "0",
548+
"REFERENCE_COVERED_BRANCHES": "0",
549+
"REFERENCE_MISSING_BRANCHES": "0",
550+
"DIFF_TOTAL_NUM_LINES": "4",
551+
"DIFF_TOTAL_NUM_VIOLATIONS": "1",
552+
"DIFF_TOTAL_PERCENT_COVERED": "0.75",
553+
"DIFF_NUM_CHANGED_LINES": "6",
554+
}
555+
output = {
556+
key: value
557+
for key, value in (
558+
line.split("=") for line in output_file.read_text().strip().splitlines()
559+
)
560+
}
561+
assert output == expected_output
446562

447563

448564
def test_action__pull_request__post_comment__no_marker(

0 commit comments

Comments
 (0)