2023-12-21 18:49:15 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
"""Grab list of bus from Forgejo and use it to make a line graph."""
|
|
|
|
|
|
|
|
import configparser
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import typing
|
2023-12-23 10:05:24 +00:00
|
|
|
from datetime import datetime, timezone
|
2023-12-21 18:49:15 +00:00
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
|
2023-12-21 19:13:29 +00:00
|
|
|
config_file_path = os.path.expanduser(
|
|
|
|
os.path.join(os.getenv("XDG_CONFIG_HOME", "~/.config"), "bug-chart", "config")
|
|
|
|
)
|
|
|
|
assert os.path.exists(config_file_path)
|
|
|
|
config.read(os.path.expanduser(config_file_path))
|
2023-12-21 18:49:15 +00:00
|
|
|
|
|
|
|
Bug = dict[str, typing.Any]
|
|
|
|
CallParams = dict[str, str | int]
|
|
|
|
|
|
|
|
hostname = config.get("forgejo", "hostname")
|
|
|
|
token = config.get("forgejo", "token")
|
|
|
|
url = f"https://{hostname}/api/v1/repos/issues/search"
|
|
|
|
|
|
|
|
|
|
|
|
def download_bugs(state: str) -> list[Bug]:
|
|
|
|
"""Get all bugs from forgejo."""
|
|
|
|
all_bugs: list[Bug] = []
|
|
|
|
page = 0
|
2023-12-21 18:59:28 +00:00
|
|
|
headers = {"Authorization": "token " + token}
|
2023-12-21 18:49:15 +00:00
|
|
|
while bugs := requests.get(
|
|
|
|
url,
|
2023-12-21 18:59:28 +00:00
|
|
|
headers=headers,
|
2023-12-21 18:49:15 +00:00
|
|
|
params=typing.cast(CallParams, {"state": state, "page": page}),
|
|
|
|
).json():
|
|
|
|
if isinstance(bugs, dict):
|
|
|
|
print(bugs)
|
|
|
|
sys.exit(1)
|
2023-12-21 18:59:28 +00:00
|
|
|
|
2023-12-21 18:49:15 +00:00
|
|
|
assert isinstance(bugs, list)
|
|
|
|
all_bugs += bugs
|
|
|
|
page += 1
|
|
|
|
|
|
|
|
return all_bugs
|
|
|
|
|
|
|
|
|
2023-12-22 15:38:29 +00:00
|
|
|
def get_all_bugs(refresh: bool = False) -> list[Bug]:
|
2023-12-21 18:49:15 +00:00
|
|
|
"""Get all bugs."""
|
2023-12-21 19:13:29 +00:00
|
|
|
filename = config.get("output", "cache")
|
2023-12-22 15:38:29 +00:00
|
|
|
if not refresh and os.path.exists(filename):
|
2023-12-21 18:49:15 +00:00
|
|
|
with open(filename) as fh:
|
|
|
|
return typing.cast(list[Bug], json.load(fh))
|
|
|
|
|
|
|
|
all_bugs = download_bugs("open") + download_bugs("closed")
|
|
|
|
with open(filename, "w") as out:
|
|
|
|
json.dump(all_bugs, out, indent=2)
|
|
|
|
|
|
|
|
return all_bugs
|
|
|
|
|
|
|
|
|
2023-12-21 19:54:13 +00:00
|
|
|
def parse_date(date_str: str | None) -> datetime | None:
|
2023-12-21 18:49:15 +00:00
|
|
|
"""Parse a date string with timezone information."""
|
|
|
|
if date_str is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
fmt = "%Y-%m-%dT%H:%M:%SZ" if date_str.endswith("Z") else "%Y-%m-%dT%H:%M:%S%z"
|
2023-12-23 10:05:24 +00:00
|
|
|
return datetime.strptime(date_str, fmt).astimezone(timezone.utc)
|
2023-12-21 18:49:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def count_open_bugs(bug_reports: list[Bug]) -> list[tuple[str, int]]:
|
|
|
|
"""Count the number of open bugs for each date based on a list of bug reports."""
|
2023-12-21 19:54:13 +00:00
|
|
|
dates: list[tuple[datetime, int]] = []
|
2023-12-21 18:49:15 +00:00
|
|
|
|
|
|
|
# Process each bug report
|
|
|
|
seen: set[int] = set()
|
|
|
|
for report in bug_reports:
|
|
|
|
if report["id"] in seen:
|
|
|
|
continue
|
|
|
|
seen.add(report["id"])
|
|
|
|
open_date = parse_date(report["created_at"])
|
2023-12-21 18:59:28 +00:00
|
|
|
assert open_date
|
2023-12-21 19:54:13 +00:00
|
|
|
dates.append((open_date, 1))
|
2023-12-21 18:49:15 +00:00
|
|
|
|
|
|
|
close_date = parse_date(report["closed_at"])
|
|
|
|
if close_date:
|
2023-12-21 19:54:13 +00:00
|
|
|
dates.append((close_date, -1))
|
2023-12-21 18:49:15 +00:00
|
|
|
|
2023-12-21 19:54:13 +00:00
|
|
|
dates.sort()
|
|
|
|
bug_count = 0
|
2023-12-21 18:49:15 +00:00
|
|
|
open_bugs_over_time = []
|
2023-12-21 19:54:13 +00:00
|
|
|
for dt, delta in dates:
|
|
|
|
bug_count += delta
|
|
|
|
iso_date = dt.isoformat()
|
|
|
|
open_bugs_over_time.append((iso_date, bug_count))
|
2023-12-21 18:49:15 +00:00
|
|
|
|
|
|
|
return open_bugs_over_time
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
"""Grab bug reports and generate chart."""
|
2023-12-22 15:38:29 +00:00
|
|
|
refresh = len(sys.argv) > 1 and sys.argv[1] == "--refresh"
|
|
|
|
bug_reports = get_all_bugs(refresh)
|
2023-12-21 18:49:15 +00:00
|
|
|
|
|
|
|
open_bugs_over_time = count_open_bugs(bug_reports)
|
|
|
|
json_data = json.dumps(open_bugs_over_time)
|
|
|
|
|
2023-12-22 15:38:29 +00:00
|
|
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
template_path = os.path.join(script_dir, "template.html")
|
|
|
|
with open(template_path, "r") as fh:
|
|
|
|
template_html = fh.read()
|
2023-12-21 18:59:28 +00:00
|
|
|
dest = config.get("output", "dest")
|
|
|
|
with open(dest, "w") as out:
|
2023-12-21 18:49:15 +00:00
|
|
|
out.write(template_html.replace("jsonData = []", "jsonData = " + json_data))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|