-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmake-sprint-milestone-spreadsheets.py
executable file
·144 lines (117 loc) · 5.09 KB
/
make-sprint-milestone-spreadsheets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
from collections import defaultdict
import csv
import datetime
import dateutil.parser
import doctest
from optparse import OptionParser
import os
from os.path import dirname, join, realpath
import re
import requests
from github import standard_headers, get_issues
# If you use milestones to group the issues that you hope to close
# over the course of the sprint, and 'Difficulty N' labels to indicate
# their relative complexity, this script will generate CSV files with
# the issues for each sprint.
cwd = os.getcwd()
repo_directory = realpath(join(dirname(__file__)))
def get_difficulty(issue):
difficulty_label_names = [i['name'] for i in issue['labels']
if re.search(r'^Difficulty ', i['name'])]
if len(difficulty_label_names) == 0:
return None
elif len(difficulty_label_names) == 1:
label = difficulty_label_names[0]
m = re.search(r'^Difficulty (\d+)', label)
if not m:
message = "Malformed Difficulty label: '{0}'".format(label)
raise Exception, message
return int(m.group(1), 10)
else:
raise Exception, "Found multiple Difficulty labels: '{0}'".format(
difficulty_label_names)
def issues_sort_key(issue):
state_number = 0 if issue['state'] == 'closed' else 1
closed_at = issue['closed_at']
return (state_number, closed_at)
def main(repo, days_in_a_sprint):
milestone_due_dates = {}
milestone_start_dates = {}
for milestone_status in ('open', 'closed'):
milestones_url = 'https://api.github.com/repos/{0}/milestones'.format(repo)
r = requests.get(milestones_url,
params={'state': 'open'},
headers=standard_headers)
if r.status_code != 200:
raise Exception, "HTTP status {0} on fetching {1}".format(
r.status_code,
milestones_url)
for milestone in r.json():
title = milestone['title']
if title.startswith('Sprint'):
due_date = dateutil.parser.parse(milestone['due_on']) + \
datetime.timedelta(hours=4)
start_date = due_date - datetime.timedelta(days=days_in_a_sprint)
milestone_start_dates[title] = start_date
milestone_due_dates[title] = due_date
milestones = sorted(milestone_due_dates.keys())
issues_in_milestone = defaultdict(list)
issues_not_in_milestone = defaultdict(list)
for number, title, body, issue in get_issues(repo, state='all'):
if ('pull_request' in issue) and issue['pull_request']['html_url']:
continue
issue['difficulty'] = get_difficulty(issue)
print "considering issue:", number
date_closed = issue['closed_at']
if date_closed is not None:
date_closed = dateutil.parser.parse(date_closed)
issue['closed_at'] = date_closed
milestone = issue['milestone']
if milestone is not None:
milestone = milestone['title']
if milestone in milestone_due_dates:
issues_in_milestone[milestone].append(issue)
elif date_closed:
for possible_milestone in milestones:
if date_closed > milestone_start_dates[possible_milestone] and \
date_closed <= milestone_due_dates[possible_milestone]:
issues_not_in_milestone[possible_milestone].append(issue)
for milestone in milestones:
filename = milestone + ".csv"
print "writing:", filename
with open(filename, "w") as fp:
writer = csv.writer(fp)
writer.writerow(['OfficiallyInMilestone', 'State', 'URL', 'Number', 'Difficulty', 'Title'])
in_milestone = sorted(issues_in_milestone[milestone],
key=issues_sort_key)
not_in_milestone = sorted(issues_not_in_milestone[milestone],
key=issues_sort_key)
for issue in in_milestone:
writer.writerow([True,
issue['state'],
issue['html_url'],
issue['number'],
issue['difficulty'],
issue['title']])
for issue in not_in_milestone:
writer.writerow([False,
issue['state'],
issue['html_url'],
issue['number'],
issue['difficulty'],
issue['title']])
usage = """Usage: %prog [options] REPOSITORY DAYS-IN-A-SPRINT
Repository should be username/repository from GitHub, e.g. mysociety/pombola"""
parser = OptionParser(usage=usage)
parser.add_option("-t", "--test",
action="store_true", dest="test", default=False,
help="Run doctests")
(options, args) = parser.parse_args()
if options.test:
doctest.testmod()
else:
if len(args) != 2:
parser.print_help()
else:
main(args[0], int(args[1]))