1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-14 03:30:02 +00:00
trezor-firmware/tools/github_issues_to_csv.py

134 lines
4.1 KiB
Python
Raw Normal View History

2019-08-16 11:19:13 +00:00
"""
Taken from https://gist.github.com/patrickfuller/e2ea8a94badc5b6967ef3ca0a9452a43 and modified.
Currently writes all issues that have some Weight.
2019-08-16 11:19:13 +00:00
"""
import argparse
import csv
import requests
import os.path
2019-08-16 11:19:13 +00:00
token = None
path = os.path.dirname(os.path.realpath(__file__))
filename = path + "/github_issues_to_csv.ignore"
if os.path.exists(filename):
with open(filename, "r") as config:
content = config.read()
if len(content) == 40:
token = content
else:
raise ValueError("Invalid config file")
2019-08-16 11:19:13 +00:00
PRIORITIES = ("P1", "P2", "P3", "P4")
SEVERITIES = ("S1", "S2", "S3", "S4")
WEIGHTS = ("W0", "W1/2", "W1", "W2", "W3", "W5", "W8", "W13", "W20", "W40", "W100")
2019-08-16 11:19:13 +00:00
2019-08-16 11:19:13 +00:00
def write_issues(r, csvout):
"""Parses JSON response and writes to CSV."""
if r.status_code != 200:
raise Exception(r.status_code)
for issue in r.json():
2019-11-14 15:57:47 +00:00
if "pull_request" not in issue:
priority = ""
severity = ""
weight = ""
labels = []
2019-11-14 15:57:47 +00:00
for l in issue["labels"]:
if l["name"][:2] in PRIORITIES:
priority = l["name"]
elif l["name"][:2] in SEVERITIES:
severity = l["name"]
2019-10-03 12:01:27 +00:00
elif l["name"] in WEIGHTS:
weight = l["name"][1:]
2019-08-21 14:09:13 +00:00
if weight == "1/2":
weight = "0.5"
else:
labels.append(l["name"])
if not weight:
continue
labels = ", ".join(labels)
2019-11-14 15:57:47 +00:00
date = issue["created_at"].split("T")[0]
milestone = issue["milestone"]["title"] if issue["milestone"] else ""
assignee = issue["assignee"]["login"] if issue["assignee"] else ""
2019-11-14 15:57:47 +00:00
csvout.writerow(
[
issue["title"],
issue["number"],
issue["html_url"],
issue["state"],
assignee,
milestone,
priority,
severity,
weight,
labels,
]
)
2019-08-16 11:19:13 +00:00
def get_issues(name):
"""Requests issues from GitHub API and writes to CSV file."""
url = f"https://api.github.com/repos/{name}/issues?state=all"
if token is not None:
headers = {"Authorization": "token " + token}
2019-10-03 12:01:27 +00:00
else:
headers = None
r = requests.get(url, headers=headers)
2019-08-16 11:19:13 +00:00
csvfilename = f"{name.replace('/', '-')}-issues.csv"
2019-11-14 15:57:47 +00:00
with open(csvfilename, "w", newline="") as csvfile:
2019-08-16 11:19:13 +00:00
csvout = csv.writer(csvfile)
2019-11-14 15:57:47 +00:00
csvout.writerow(
[
"Title",
"Number",
"URL",
"State",
"Assignee",
"Milestone",
"Priority",
"Severity",
"Weight",
"Labels",
]
)
2019-08-16 11:19:13 +00:00
write_issues(r, csvout)
# Multiple requests are required if response is paged
2019-11-14 15:57:47 +00:00
if "link" in r.headers:
pages = {
rel[6:-1]: url[url.index("<") + 1 : -1]
for url, rel in (
link.split(";") for link in r.headers["link"].split(",")
)
}
while "last" in pages and "next" in pages:
pages = {
rel[6:-1]: url[url.index("<") + 1 : -1]
for url, rel in (
link.split(";") for link in r.headers["link"].split(",")
)
}
r = requests.get(pages["next"], headers=headers)
2019-08-16 11:19:13 +00:00
write_issues(r, csvout)
2019-11-14 15:57:47 +00:00
if pages["next"] == pages["last"]:
2019-08-16 11:19:13 +00:00
break
2019-11-14 15:57:47 +00:00
parser = argparse.ArgumentParser(
description="Write GitHub repository issues " "to CSV file."
)
parser.add_argument(
"repositories", nargs="+", help="Repository names, " "formatted as 'username/repo'"
)
parser.add_argument(
"--all", action="store_true", help="Returns both open " "and closed issues."
)
2019-08-16 11:19:13 +00:00
args = parser.parse_args()
for repository in args.repositories:
get_issues(repository)