tartiflette/pullrequests/analyze.py

130 lines
3.4 KiB
Python
Raw Permalink Normal View History

2017-03-08 14:24:58 +01:00
#!/usr/bin/python3
import json
import datetime
prs = {}
2017-03-08 14:24:58 +01:00
2017-05-02 16:25:04 +02:00
prioritary_milestone = "2.6.x"
2017-03-08 17:31:22 +01:00
def get_teams():
with open("repos.json", "r") as f:
return json.loads(f.read()).keys()
def get_repos():
2017-03-08 14:24:58 +01:00
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
2017-03-08 14:24:58 +01:00
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 "postponed" in pr["labels"]:
base_priority = -100
elif "inactive" in pr["labels"]:
base_priority = -100
else:
base_priority = 0
if "work needed" in pr["labels"]:
base_priority += -5
2017-03-08 14:24:58 +01:00
if "dying" in pr["labels"] and base_priority > -100:
base_priority += 5
2017-05-02 16:25:04 +02:00
if pr["milestone"] != None and pr["milestone"] != prioritary_milestone:
base_priority -= 10
2017-03-08 14:24:58 +01:00
return base_priority
def main():
for repo, teams in get_repos().items():
2017-03-08 14:24:58 +01:00
print("Analyzing %s ..." % repo)
with open("data/%s.json" % repo, "r") as f:
2017-03-08 14:24:58 +01:00
j = json.loads(f.read())
for issue in j:
# Ignore non-pullrequest issues
if "pull_request" not in issue.keys():
continue
2017-05-02 16:25:04 +02:00
milestone = None
if issue["milestone"]:
milestone = issue["milestone"]["title"]
2017-03-08 14:24:58 +01:00
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"]),
2017-05-02 16:25:04 +02:00
"url": issue["pull_request"]["html_url"],
"teams": teams,
"milestone": milestone,
"size": None
2017-03-08 14:24:58 +01:00
}
2017-05-02 16:25:04 +02:00
for size in ["small", "medium", "big"]:
if "%s decision" % size in pr["labels"]:
pr["labels"].remove("%s decision" % size)
pr["labels"].insert(0,size)
2017-03-08 14:24:58 +01:00
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 )
2017-03-08 17:31:22 +01:00
summary = {}
summary["teams"] = {}
summary["teams"]["all"] = len([pr for pr in prs.values() if pr["priority"] >= -50])
for team in get_teams():
summary["teams"][team] = len([pr for pr in prs.values()
if team in pr["teams"] and pr["priority"] >= -50])
2017-03-08 14:24:58 +01:00
2017-03-08 17:31:22 +01:00
summary["prs"] = []
2017-03-08 14:24:58 +01:00
for name in prs_sorted:
2017-03-08 17:31:22 +01:00
summary["prs"].append(prs[name])
2017-03-08 14:24:58 +01:00
with open("summary.json", "w") as f:
f.write(json.dumps(summary))
main()