tartiflette/analyze.py
2017-03-08 10:56:24 -05:00

99 lines
2.4 KiB
Python
Executable file

#!/usr/bin/python3
import json
import datetime
prs = {}
def get_repos():
repos_by_name = {}
with open("repos.json", "r") as f:
repos_by_team = json.loads(f.read())
for team, team_repos in repos_by_team.items():
for repo in team_repos:
if repo not in repos_by_name.keys():
repos_by_name[repo] = [team]
else:
repos_by_name[repo].append(team)
return repos_by_name
def githubDateToDaysAgo(date):
now = datetime.datetime.now()
date = datetime.datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ")
return (now - date).days
def isPRDying(pr):
return (pr["createdDaysAgo"] > 60 and pr["updatedDaysAgo"] > 30)
def priority(pr):
if "important" in pr["labels"]:
base_priority = 100
elif "opinion needed" in pr["labels"]:
base_priority = 50
elif "work needed" in pr["labels"]:
base_priority = -50
elif "postponed" in pr["labels"]:
base_priority = -100
elif "inactive" in pr["labels"]:
base_priority = -100
else:
base_priority = 0
if "dying" in pr["labels"] and base_priority > -100:
base_priority += 5
return base_priority
def main():
for repo, teams in get_repos().items():
print("Analyzing %s ..." % repo)
with open("data/%s.json" % repo, "r") as f:
j = json.loads(f.read())
for issue in j:
# Ignore non-pullrequest issues
if "pull_request" not in issue.keys():
continue
pr = {
"repo": repo,
"title": issue["title"],
"labels": [label["name"] for label in issue["labels"]],
"id": "%s-%s" % (repo, issue["number"]),
"createdDaysAgo": githubDateToDaysAgo(issue["created_at"]),
"updatedDaysAgo": githubDateToDaysAgo(issue["updated_at"]),
"url": issue["pull_request"]["html_url"],
"teams": teams
}
if isPRDying(pr):
pr["labels"].append("dying")
pr["priority"] = priority(pr)
prs[pr["id"]] = pr
prs_sorted = sorted(prs.keys(), key=lambda x: (prs[x]["priority"],
prs[x]["createdDaysAgo"]), reverse=True )
summary = []
for name in prs_sorted:
summary.append(prs[name])
with open("summary.json", "w") as f:
f.write(json.dumps(summary))
main()