Skip to content

Commit c1cf209

Browse files
authored
Merge pull request #87 from cicirello/feat-workflow-summary
Add GitHub Actions workflow job summaries
2 parents 93a781e + a4c5a6e commit c1cf209

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased] - 2022-10-21
88

99
### Added
10+
* Generate and output a GitHub Actions workflow job summary with the coverage percentages.
1011

1112
### Changed
1213

src/jacoco_badge_generator/coverage_badges.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858

5959
defaultColors = [ "#4c1", "#97ca00", "#a4a61d", "#dfb317", "#fe7d37", "#e05d44" ]
6060

61+
markdownSummaryTemplate = """## JaCoCo Test Coverage Summary
62+
* __Coverage:__ {0:.3%}
63+
* __Branches:__ {1:.3%}
64+
* __Generated by:__ jacoco-badge-generator
65+
"""
66+
6167
def generateBadge(covStr, color, badgeType="coverage") :
6268
"""Generates the badge as a string. This includes calculating
6369
label width, etc, to support custom labels.
@@ -461,7 +467,8 @@ def coverageDictionary(cov, branches) :
461467
def set_action_outputs(output_pairs) :
462468
"""Sets the GitHub Action outputs if running as a GitHub Action,
463469
and otherwise logs coverage percentages to terminal if running in
464-
CLI mode.
470+
CLI mode. Note that if the CLI mode is used within a GitHub Actions
471+
workflow, it will be treated the same as GitHub Actions mode.
465472
466473
Keyword arguments:
467474
output_pairs - Dictionary of outputs with values
@@ -474,6 +481,20 @@ def set_action_outputs(output_pairs) :
474481
for key, value in output_pairs.items() :
475482
print("{0}={1}".format(key, value))
476483

484+
def add_workflow_job_summary(cov, branches) :
485+
"""When running as a GitHub Action, adds a job summary, and otherwise
486+
does nothing when running in CLI mode. Note that if the CLI mode is
487+
used within a GitHub Actions workflow, it will be treated the same as
488+
GitHub Actions mode (no convenient way to tell the difference).
489+
490+
Keyword arguments:
491+
cov - Coverage percentage
492+
branches - Branches coverage percentage
493+
"""
494+
if "GITHUB_STEP_SUMMARY" in os.environ :
495+
with open(os.environ["GITHUB_STEP_SUMMARY"], "a") as f :
496+
print(markdownSummaryTemplate.format(cov, branches), file=f)
497+
477498
def main(jacocoCsvFile,
478499
badgesDirectory,
479500
coverageFilename,
@@ -611,3 +632,4 @@ def main(jacocoCsvFile,
611632
json.dump(coverageDictionary(cov, branches), summaryFile, sort_keys=True)
612633

613634
set_action_outputs({"coverage" : cov, "branches" : branches})
635+
add_workflow_job_summary(cov, branches)

0 commit comments

Comments
 (0)