Skip to content

Commit 826982d

Browse files
authored
Fix bug counting script (#381)
Signed-off-by: Tyler Gu <[email protected]>
1 parent 7cb3e63 commit 826982d

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

.github/workflows/bug_counter.py

+44-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
class BugCounter():
1+
class BugCounter:
2+
"""Class to count the number of bugs in bugs.md file and update the number
3+
of bugs, confirmed and fixed bugs in the file.
4+
"""
5+
26
def __init__(self):
37
self.num_bugs = 0
48
self.num_confirmed = 0
@@ -7,38 +11,54 @@ def __init__(self):
711
self.num_by_confirmed = 0
812
self.num_by_fixed = 0
913
self.by_flag = False
10-
11-
def read_file(self, path: str) -> list:
12-
with open (path, 'r') as f:
14+
15+
def read_file(self, path: str) -> list[str]:
16+
"""Read the file and return the data as a list of strings."""
17+
with open(path, "r", encoding="utf-8") as f:
1318
data = f.readlines()
1419
return data
15-
20+
1621
def write_data(self, data: list, path: str):
17-
with open (path, 'w') as f:
22+
"""Write the data to the file."""
23+
with open(path, "w", encoding="utf-8") as f:
1824
f.writelines(data)
19-
20-
def update_number(self) -> list:
21-
data = self.read_file('./bugs.md')
25+
26+
def update_number(self):
27+
"""Update the number of bugs, confirmed and fixed bugs in the file."""
28+
data = self.read_file("./bugs.md")
2229
for line in data:
23-
if '# Byproduct bugs' in line:
30+
if "# Byproduct bugs" in line:
2431
self.by_flag = True
2532
if not self.by_flag:
26-
self.num_bugs += line.count('](')
27-
self.num_confirmed += line.count('confirmed')
28-
self.num_fixed += line.count('fixed')
33+
self.num_bugs += line.count("](")
34+
self.num_confirmed += line.count("confirmed")
35+
self.num_fixed += line.count("fixed")
2936
else:
30-
self.num_by_bugs += line.count('](')
31-
self.num_by_confirmed += line.count('confirmed')
32-
self.num_by_fixed += line.count('fixed')
33-
37+
self.num_by_bugs += line.count("](")
38+
self.num_by_confirmed += line.count("confirmed")
39+
self.num_by_fixed += line.count("fixed")
40+
3441
self.num_confirmed += self.num_fixed
3542
self.num_by_confirmed += self.num_by_fixed
36-
if '(Byproduct bugs included)' not in data[1]:
37-
raise Exception("The first line of bugs.md should start with '(Byproduct bugs included)'")
38-
data[1] = '(Byproduct bugs included) Total bugs: **{}**, confirmed: **{}**, Fixed: **{}**.<br/>\n'.format(self.num_bugs + self.num_by_bugs, self.num_confirmed + self.num_by_confirmed, self.num_fixed + self.num_by_fixed)
39-
data[3] = '(Byproduct bugs excluded) Total bugs: **{}**, confirmed: **{}**, Fixed: **{}**.<br/>\n'.format(self.num_bugs, self.num_confirmed, self.num_fixed)
40-
self.write_data(data, './bugs.md')
41-
42-
if __name__ == '__main__':
43+
if not data[2].startswith("(Byproduct bugs included)"):
44+
raise RuntimeError(
45+
"The first line of bugs.md should start with '(Byproduct bugs included)'"
46+
)
47+
data[2] = (
48+
"(Byproduct bugs included) "
49+
f"Total bugs: **{self.num_bugs + self.num_by_bugs}**, "
50+
f"confirmed: **{self.num_confirmed + self.num_by_confirmed}**, "
51+
f"Fixed: **{self.num_fixed + self.num_by_fixed}**.<br/>\n"
52+
)
53+
data[4] = (
54+
f"(Byproduct bugs excluded) "
55+
f"Total bugs: **{self.num_bugs}**, "
56+
f"confirmed: **{self.num_confirmed}**, "
57+
f"Fixed: **{self.num_fixed}**.<br/>\n"
58+
)
59+
self.write_data(data, "./bugs.md")
60+
61+
62+
if __name__ == "__main__":
4363
bug_counter = BugCounter()
4464
bug_counter.update_number()

0 commit comments

Comments
 (0)