5858
5959defaultColors = [ "#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+
6167def 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) :
461467def 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+
477498def 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