Skip to content

Commit 7675fff

Browse files
authored
Merge pull request #45 from cicirello/custom-labels
Added option to specify custom left-side badge labels
2 parents c7817bc + 0121154 commit 7675fff

File tree

11 files changed

+128
-21
lines changed

11 files changed

+128
-21
lines changed

.github/workflows/build.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,27 @@ jobs:
8989
echo "coverage = ${{ steps.integration4.outputs.coverage }}"
9090
echo "branch coverage = ${{ steps.integration4.outputs.branches }}"
9191
92+
- name: Integration test with custom labels
93+
id: integration5
94+
uses: ./
95+
with:
96+
jacoco-csv-file: tests/jacoco.csv
97+
badges-directory: tests/badges
98+
generate-branches-badge: true
99+
generate-coverage-endpoint: true
100+
generate-branches-endpoint: true
101+
coverage-badge-filename: customCoverage.svg
102+
branches-badge-filename: customBranches.svg
103+
coverage-endpoint-filename: customCoverage.json
104+
branches-endpoint-filename: customBranches.json
105+
coverage-label: custom coverage label one
106+
branches-label: custom coverage label two
107+
108+
- name: Log integration test outputs with a single jacoco.csv
109+
run: |
110+
echo "coverage = ${{ steps.integration5.outputs.coverage }}"
111+
echo "branch coverage = ${{ steps.integration5.outputs.branches }}"
112+
92113
- name: Verify integration test results
93114
run: python3 -u -m unittest tests/integration.py
94115

CHANGELOG.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010

1111
### Changed
12-
* Left-side text width and position calculated rather than hard-coded to
13-
width of "coverage" and "branches".
14-
* Changed Dockerfile to pull base image from GitHub Container Registry, assuming
15-
within GitHub Actions likely faster to pull from GitHub rather than Docker Hub.
16-
* Repository reorganized to move Python source code to a new src directory.
1712

1813
### Deprecated
1914

@@ -24,6 +19,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2419
### CI/CD
2520

2621

22+
## [2.6.0] - 2022-02-17
23+
24+
### Added
25+
* Option to specify custom labels for the left side of the badges controlled
26+
by the new inputs `coverage-label` and `branches-label`.
27+
28+
### Changed
29+
* Left-side text width and position calculated rather than hard-coded to
30+
width of "coverage" and "branches".
31+
* Changed Dockerfile to pull base image from GitHub Container Registry, assuming
32+
within GitHub Actions likely faster to pull from GitHub rather than Docker Hub.
33+
* Repository reorganized to move Python source code to a new src directory.
34+
35+
2736
## [2.5.0] - 2021-11-11
2837

2938
### Added

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,16 @@ created within the `badges-directory`
351351
directory. __The action doesn't commit the JSON file. You will
352352
need to have additional steps in your workflow to do that.__
353353

354+
### `coverage-label`
355+
356+
This input is the text for the label on the left side of the coverage badge, which
357+
defaults to `coverage`.
358+
359+
### `branches-label`
360+
361+
This input is the text for the label on the left side of the branches coverage badge, which
362+
defaults to `branches`.
363+
354364
### `colors`
355365

356366
This input can be used to change the colors used for the badges.
@@ -649,6 +659,8 @@ what these inputs do.
649659
branches-endpoint-filename: branches.json
650660
generate-summary: false
651661
summary-filename: coverage-summary.json
662+
coverage-label: coverage
663+
branches-label: branches
652664
colors: '#4c1 #97ca00 #a4a61d #dfb317 #fe7d37 #e05d44'
653665
intervals: 100 90 80 70 60 0
654666
on-missing-report: fail

action.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ inputs:
5656
description: 'Controls whether or not to generate the branches coverage badge.'
5757
required: false
5858
default: false
59+
coverage-label:
60+
description: 'Text for the left-side of the coverage badge.'
61+
required: false
62+
default: coverage
63+
branches-label:
64+
description: 'Text for the left-side of the branches coverage badge.'
65+
required: false
66+
default: branches
5967
on-missing-report:
6068
description: 'Controls what happens if one or more jacoco.csv files do not exist.'
6169
required: false
@@ -136,3 +144,5 @@ runs:
136144
- ${{ inputs.branches-endpoint-filename }}
137145
- ${{ inputs.generate-summary }}
138146
- ${{ inputs.summary-filename }}
147+
- ${{ inputs.coverage-label }}
148+
- ${{ inputs.branches-label }}

src/JacocoBadgeGenerator.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ def getPriorCoverage(badgeFilename, whichBadge) :
325325
326326
Keyword arguments:
327327
badgeFilename - the filename with path
328-
whichBadge - this input should be one of 'coverage' or 'branches'
328+
whichBadge - the badge label such as 'coverage' or 'branches' or some custom label
329329
"""
330330
if not os.path.isfile(badgeFilename) :
331331
return -1
@@ -347,7 +347,7 @@ def getPriorCoverageFromEndpoint(jsonFilename, whichBadge) :
347347
348348
Keyword arguments:
349349
jsonFilename - the filename with path
350-
whichBadge - this input should be one of 'coverage' or 'branches'
350+
whichBadge - the badge label such as 'coverage' or 'branches' or some custom label
351351
"""
352352
if not os.path.isfile(jsonFilename) :
353353
return -1
@@ -371,7 +371,7 @@ def coverageDecreased(coverage, badgeFilename, whichBadge) :
371371
Keyword arguments:
372372
coverage - The coverage in interval 0.0 to 1.0
373373
badgeFilename - the filename with path
374-
whichBadge - this input should be one of 'coverage' or 'branches'
374+
whichBadge - the badge label
375375
"""
376376
previous = getPriorCoverage(badgeFilename, whichBadge)
377377
# Badge only records 1 decimal place, and thus need
@@ -384,8 +384,7 @@ def coverageDecreased(coverage, badgeFilename, whichBadge) :
384384
# from coverage data at this point.
385385
new = coverage * 1000
386386
if new < old :
387-
s = "Branches coverage" if whichBadge == "branches" else "Coverage"
388-
print(s, "decreased from", coverageTruncatedToString(previous)[0], "to", coverageTruncatedToString(coverage)[0])
387+
print(whichBadge, "decreased from", coverageTruncatedToString(previous)[0], "to", coverageTruncatedToString(coverage)[0])
389388
return True
390389
return False
391390

@@ -396,7 +395,7 @@ def coverageDecreasedEndpoint(coverage, jsonFilename, whichBadge) :
396395
Keyword arguments:
397396
coverage - The coverage in interval 0.0 to 1.0
398397
jsonFilename - the filename with path
399-
whichBadge - this input should be one of 'coverage' or 'branches'
398+
whichBadge - the badge label
400399
"""
401400
previous = getPriorCoverageFromEndpoint(jsonFilename, whichBadge)
402401
# Badge only records 1 decimal place, and thus need
@@ -409,8 +408,7 @@ def coverageDecreasedEndpoint(coverage, jsonFilename, whichBadge) :
409408
# from coverage data at this point.
410409
new = coverage * 1000
411410
if new < old :
412-
s = "Branches coverage" if whichBadge == "branches" else "Coverage"
413-
print(s, "decreased from", coverageTruncatedToString(previous)[0], "to", coverageTruncatedToString(coverage)[0])
411+
print(whichBadge, "decreased from", coverageTruncatedToString(previous)[0], "to", coverageTruncatedToString(coverage)[0])
414412
return True
415413
return False
416414

@@ -482,6 +480,8 @@ def coverageDictionary(cov, branches) :
482480
branchesJSON = sys.argv[17]
483481
generateSummary = sys.argv[18].lower() == "true"
484482
summaryFilename = sys.argv[19]
483+
coverageLabel = sys.argv[20]
484+
branchesLabel = sys.argv[21]
485485

486486
if onMissingReport not in {"fail", "quiet", "badges"} :
487487
print("ERROR: Invalid value for on-missing-report.")
@@ -520,16 +520,16 @@ def coverageDictionary(cov, branches) :
520520
print("Failing the workflow run.")
521521
sys.exit(1)
522522
else : # Otherwise use the prior coverages as stored in badges / JSON.
523-
if failOnCoverageDecrease and generateCoverageBadge and coverageDecreased(cov, coverageBadgeWithPath, "coverage") :
523+
if failOnCoverageDecrease and generateCoverageBadge and coverageDecreased(cov, coverageBadgeWithPath, coverageLabel) :
524524
print("Failing the workflow run.")
525525
sys.exit(1)
526-
if failOnBranchesDecrease and generateBranchesBadge and coverageDecreased(branches, branchesBadgeWithPath, "branches") :
526+
if failOnBranchesDecrease and generateBranchesBadge and coverageDecreased(branches, branchesBadgeWithPath, branchesLabel) :
527527
print("Failing the workflow run.")
528528
sys.exit(1)
529-
if failOnCoverageDecrease and generateCoverageJSON and coverageDecreasedEndpoint(cov, coverageJSONWithPath, "coverage") :
529+
if failOnCoverageDecrease and generateCoverageJSON and coverageDecreasedEndpoint(cov, coverageJSONWithPath, coverageLabel) :
530530
print("Failing the workflow run.")
531531
sys.exit(1)
532-
if failOnBranchesDecrease and generateBranchesJSON and coverageDecreasedEndpoint(branches, branchesJSONWithPath, "branches") :
532+
if failOnBranchesDecrease and generateBranchesJSON and coverageDecreasedEndpoint(branches, branchesJSONWithPath, branchesLabel) :
533533
print("Failing the workflow run.")
534534
sys.exit(1)
535535

@@ -540,19 +540,19 @@ def coverageDictionary(cov, branches) :
540540
covStr, color = badgeCoverageStringColorPair(cov, colorCutoffs, colors)
541541
if generateCoverageBadge :
542542
with open(coverageBadgeWithPath, "w") as badge :
543-
badge.write(generateBadge(covStr, color))
543+
badge.write(generateBadge(covStr, color, coverageLabel))
544544
if generateCoverageJSON :
545545
with open(coverageJSONWithPath, "w") as endpoint :
546-
json.dump(generateDictionaryForEndpoint(covStr, color, "coverage"), endpoint, sort_keys=True)
546+
json.dump(generateDictionaryForEndpoint(covStr, color, coverageLabel), endpoint, sort_keys=True)
547547

548548
if generateBranchesBadge or generateBranchesJSON :
549549
covStr, color = badgeCoverageStringColorPair(branches, colorCutoffs, colors)
550550
if generateBranchesBadge :
551551
with open(branchesBadgeWithPath, "w") as badge :
552-
badge.write(generateBadge(covStr, color, "branches"))
552+
badge.write(generateBadge(covStr, color, branchesLabel))
553553
if generateBranchesJSON :
554554
with open(branchesJSONWithPath, "w") as endpoint :
555-
json.dump(generateDictionaryForEndpoint(covStr, color, "branches"), endpoint, sort_keys=True)
555+
json.dump(generateDictionaryForEndpoint(covStr, color, branchesLabel), endpoint, sort_keys=True)
556556

557557
if generateSummary :
558558
with open(summaryFilenameWithPath, "w") as summaryFile :

tests/custom1.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"color": "#4c1", "label": "custom coverage label one", "message": "100%", "schemaVersion": 1}

tests/custom1.svg

Lines changed: 1 addition & 0 deletions
Loading

tests/custom2.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"color": "#97ca00", "label": "custom coverage label two", "message": "90%", "schemaVersion": 1}

tests/custom2.svg

Lines changed: 1 addition & 0 deletions
Loading

tests/integration.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,26 @@
3434

3535
class IntegrationTest(unittest.TestCase) :
3636

37+
def testIntegrationCustomCoverageLabel(self) :
38+
with open("tests/custom1.svg","r") as expected :
39+
with open("tests/badges/customCoverage.svg","r") as generated :
40+
self.assertEqual(expected.read(), generated.read())
41+
42+
def testIntegrationCustomBranchesLabel(self) :
43+
with open("tests/custom2.svg","r") as expected :
44+
with open("tests/badges/customBranches.svg","r") as generated :
45+
self.assertEqual(expected.read(), generated.read())
46+
47+
def testIntegrationCustomCoverageLabelJSON(self) :
48+
with open("tests/custom1.json","r") as expected :
49+
with open("tests/badges/customCoverage.json","r") as generated :
50+
self.assertEqual(expected.read(), generated.read())
51+
52+
def testIntegrationCustomBranchesLabelJSON(self) :
53+
with open("tests/custom2.json","r") as expected :
54+
with open("tests/badges/customBranches.json","r") as generated :
55+
self.assertEqual(expected.read(), generated.read())
56+
3757
def testIntegrationInstructionsBadge(self) :
3858
with open("tests/100.svg","r") as expected :
3959
with open("tests/badges/jacoco.svg","r") as generated :

0 commit comments

Comments
 (0)