generated from cicirello/python-github-action-template
-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
JacocoBadgeGenerator.py
executable file
·225 lines (195 loc) · 8.09 KB
/
JacocoBadgeGenerator.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python3
#
# jacoco-badge-generator: Github action for generating a jacoco coverage
# percentage badge.
#
# Copyright (c) 2020-2021 Vincent A Cicirello
# https://www.cicirello.org/
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
import csv
import sys
import math
import pathlib
import os
import os.path
badgeTemplate = '<svg xmlns="http://www.w3.org/2000/svg" width="104" \
height="20" role="img" aria-label="{3}: {0}">\
<linearGradient id="s" x2="0" y2="100%">\
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>\
<stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r">\
<rect width="104" height="20" rx="3" fill="#fff"/></clipPath>\
<g clip-path="url(#r)"><rect width="61" height="20" fill="#555"/>\
<rect x="61" width="43" height="20" fill="{1}"/>\
<rect width="104" height="20" fill="url(#s)"/></g>\
<g fill="#fff" text-anchor="middle" \
font-family="Verdana,Geneva,DejaVu Sans,sans-serif" \
text-rendering="geometricPrecision" font-size="110">\
<text aria-hidden="true" x="315" y="150" fill="#010101" \
fill-opacity=".3" transform="scale(.1)" textLength="510">{3}</text>\
<text x="315" y="140" transform="scale(.1)" fill="#fff" \
textLength="510">{3}</text>\
<text aria-hidden="true" x="815" y="150" \
fill="#010101" fill-opacity=".3" transform="scale(.1)" \
textLength="{2}">{0}</text><text x="815" y="140" \
transform="scale(.1)" fill="#fff" textLength="{2}">{0}</text>\
</g></svg>'
colors = [ "#4c1", "#97ca00", "#a4a61d", "#dfb317", "#fe7d37", "#e05d44" ]
def generateBadge(covStr, color, badgeType="coverage") :
"""Generates the badge as a string.
Keyword arguments:
covStr - The coverage as a string.
color - The color for the badge.
badgeType - The text string for a label on the badge.
"""
if len(covStr) >= 4 :
textLength = "330"
elif len(covStr) >= 3 :
textLength = "250"
else :
textLength = "170"
return badgeTemplate.format(covStr, color, textLength, badgeType)
def computeCoverage(fileList) :
"""Parses one or more jacoco.csv files and computes code coverage
percentages. Returns: coverage, branchCoverage. The coverage
is instruction coverage.
Keyword arguments:
fileList - A list (or any iterable) of the filenames, including path, of the jacoco.csv files.
"""
missed = 0
covered = 0
missedBranches = 0
coveredBranches = 0
for filename in fileList :
with open(filename, newline='') as csvfile :
jacocoReader = csv.reader(csvfile)
for i, row in enumerate(jacocoReader) :
if i > 0 :
missed += int(row[3])
covered += int(row[4])
missedBranches += int(row[5])
coveredBranches += int(row[6])
return (calculatePercentage(covered, missed),
calculatePercentage(coveredBranches, missedBranches))
def calculatePercentage(covered, missed) :
"""Calculates the coverage percentage from number of
covered and number of missed. Returns 1 if both are 0
to handle the special case of running on an empty class
(no instructions) or a case with no if, switch, loops (no
branches).
Keyword arguments:
covered - The number of X covered (where X is the metric).
missed - The number of X missed (where X is the metric).
"""
if missed == 0 :
return 1
return covered / (covered + missed)
def badgeCoverageStringColorPair(coverage) :
"""Converts the coverage percentage to a formatted string,
and determines the badge color.
Returns: coveragePercentageAsString, colorAsString
Keyword arguments:
coverage - The coverage percentage.
"""
# Truncate the 2nd decimal place, rather than rounding
# to avoid considering a non-passing percentage as
# passing (e.g., if user considers 70% as passing threshold,
# then 69.99999...% is technically not passing).
coverage = int(1000 * coverage) / 10
c = math.ceil((100 - coverage) / 10)
if c >= len(colors) :
c = len(colors) - 1
if coverage - int(coverage) == 0 :
cov = "{0:d}%".format(int(coverage))
else :
cov = "{0:.1f}%".format(coverage)
return cov, colors[c]
def createOutputDirectories(badgesDirectory) :
"""Creates the output directory if it doesn't already exist.
Keyword arguments:
badgesDirectory - The badges directory
"""
if not os.path.exists(badgesDirectory) :
p = pathlib.Path(badgesDirectory)
os.umask(0)
p.mkdir(mode=0o777, parents=True, exist_ok=True)
def splitPath(filenameWithPath) :
"""Breaks a filename including path into containing directory and filename.
Keyword arguments:
filenameWithPath - The filename including path.
"""
if filenameWithPath.startswith("./") :
filenameWithPath = filenameWithPath[2:]
if filenameWithPath[0] == "/" :
filenameWithPath = filenameWithPath[1:]
i = filenameWithPath.rfind("/")
if i >= 0 :
return filenameWithPath[:i], filenameWithPath[i+1:]
else :
return ".", filenameWithPath
def formFullPathToFile(directory, filename) :
"""Generates path string.
Keyword arguments:
directory - The directory for the badges
filename - The filename for the badge.
"""
if len(filename) > 1 and filename[0:2] == "./" :
filename = filename[2:]
if filename[0] == "/" :
filename = filename[1:]
if len(directory) > 1 and directory[0:2] == "./" :
directory = directory[2:]
if len(directory) > 0 and directory[0] == "/" :
directory = directory[1:]
if directory == "" or directory == "." :
return filename
elif directory[-1] == "/" :
return directory + filename
else :
return directory + "/" + filename
if __name__ == "__main__" :
jacocoCsvFile = sys.argv[1]
badgesDirectory = sys.argv[2]
coverageFilename = sys.argv[3]
branchesFilename = sys.argv[4]
generateCoverageBadge = sys.argv[5].lower() == "true"
generateBranchesBadge = sys.argv[6].lower() == "true"
if len(badgesDirectory) > 1 and badgesDirectory[0:2] == "./" :
badgesDirectory = badgesDirectory[2:]
if len(badgesDirectory) > 0 and badgesDirectory[0] == "/" :
badgesDirectory = badgesDirectory[1:]
if badgesDirectory == "." :
badgesDirectory = ""
jacocoFileList = jacocoCsvFile.split()
cov, branches = computeCoverage(jacocoFileList)
if (generateCoverageBadge or generateBranchesBadge) and badgesDirectory != "" :
createOutputDirectories(badgesDirectory)
if generateCoverageBadge :
covStr, color = badgeCoverageStringColorPair(cov)
with open(formFullPathToFile(badgesDirectory, coverageFilename), "w") as badge :
badge.write(generateBadge(covStr, color))
if generateBranchesBadge :
covStr, color = badgeCoverageStringColorPair(branches)
with open(formFullPathToFile(badgesDirectory, branchesFilename), "w") as badge :
badge.write(generateBadge(covStr, color, "branches"))
print("::set-output name=coverage::" + str(cov))
print("::set-output name=branches::" + str(branches))