tools/issues: style

pull/726/head
Tomas Susanka 5 years ago
parent 56e84aa0e6
commit 9e33d69607

@ -30,12 +30,12 @@ def write_issues(r, csvout):
if r.status_code != 200: if r.status_code != 200:
raise Exception(r.status_code) raise Exception(r.status_code)
for issue in r.json(): for issue in r.json():
if 'pull_request' not in issue: if "pull_request" not in issue:
priority = "" priority = ""
severity = "" severity = ""
weight = "" weight = ""
labels = [] labels = []
for l in issue['labels']: for l in issue["labels"]:
if l["name"][:2] in PRIORITIES: if l["name"][:2] in PRIORITIES:
priority = l["name"] priority = l["name"]
elif l["name"][:2] in SEVERITIES: elif l["name"][:2] in SEVERITIES:
@ -49,50 +49,84 @@ def write_issues(r, csvout):
if not weight: if not weight:
continue continue
labels = ", ".join(labels) labels = ", ".join(labels)
date = issue['created_at'].split('T')[0] date = issue["created_at"].split("T")[0]
milestone = issue['milestone']['title'] if issue['milestone'] else "" milestone = issue["milestone"]["title"] if issue["milestone"] else ""
assignee = issue['assignee']['login'] if issue['assignee'] else "" assignee = issue["assignee"]["login"] if issue["assignee"] else ""
csvout.writerow([issue['title'], issue['number'], issue['html_url'], issue['state'], assignee, csvout.writerow(
milestone, priority, severity, weight, labels]) [
issue["title"],
issue["number"],
issue["html_url"],
issue["state"],
assignee,
milestone,
priority,
severity,
weight,
labels,
]
)
def get_issues(name): def get_issues(name):
"""Requests issues from GitHub API and writes to CSV file.""" """Requests issues from GitHub API and writes to CSV file."""
url = 'https://api.github.com/repos/{}/issues?state=all'.format(name) url = "https://api.github.com/repos/{}/issues?state=all".format(name)
if token is not None: if token is not None:
headers = {"Authorization": "token " + token} headers = {"Authorization": "token " + token}
else: else:
headers = None headers = None
r = requests.get(url, headers=headers) r = requests.get(url, headers=headers)
csvfilename = '{}-issues.csv'.format(name.replace('/', '-')) csvfilename = "{}-issues.csv".format(name.replace("/", "-"))
with open(csvfilename, 'w', newline='') as csvfile: with open(csvfilename, "w", newline="") as csvfile:
csvout = csv.writer(csvfile) csvout = csv.writer(csvfile)
csvout.writerow(['Title', 'Number', 'URL', 'State', 'Assignee', 'Milestone', 'Priority', 'Severity', 'Weight', 'Labels']) csvout.writerow(
[
"Title",
"Number",
"URL",
"State",
"Assignee",
"Milestone",
"Priority",
"Severity",
"Weight",
"Labels",
]
)
write_issues(r, csvout) write_issues(r, csvout)
# Multiple requests are required if response is paged # Multiple requests are required if response is paged
if 'link' in r.headers: if "link" in r.headers:
pages = {rel[6:-1]: url[url.index('<')+1:-1] for url, rel in pages = {
(link.split(';') for link in rel[6:-1]: url[url.index("<") + 1 : -1]
r.headers['link'].split(','))} for url, rel in (
while 'last' in pages and 'next' in pages: link.split(";") for link in r.headers["link"].split(",")
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:
r = requests.get(pages['next'], headers=headers) 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)
write_issues(r, csvout) write_issues(r, csvout)
if pages['next'] == pages['last']: if pages["next"] == pages["last"]:
break break
parser = argparse.ArgumentParser(description="Write GitHub repository issues " parser = argparse.ArgumentParser(
"to CSV file.") description="Write GitHub repository issues " "to CSV file."
parser.add_argument('repositories', nargs='+', help="Repository names, " )
"formatted as 'username/repo'") parser.add_argument(
parser.add_argument('--all', action='store_true', help="Returns both open " "repositories", nargs="+", help="Repository names, " "formatted as 'username/repo'"
"and closed issues.") )
parser.add_argument(
"--all", action="store_true", help="Returns both open " "and closed issues."
)
args = parser.parse_args() args = parser.parse_args()
for repository in args.repositories: for repository in args.repositories:

Loading…
Cancel
Save