- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgithub2csv.py
More file actions
Latest commit
235 lines (184 loc) · 8.36 KB
/
Copy pathgithub2csv.py
File metadata and controls
235 lines (184 loc) · 8.36 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#! /usr/bin/env python
importgithub3
importargparse
importConfigParser
importcsv
importos
importre
sizes= {}
defdefine_sizes(size_config):
ifnotsize_config:
size_config="XS: 1, S: 3, M: 7, L: 15, XL: 25, XXL: 1000"
fors_pairinsize_config.split(","):
if':'ins_pair:
s_label, s_val=s_pair.split(':', 1)
s_label=unicode(s_label)
s_val=float(s_val)
sizes[s_label.strip()] =s_val
deflabel2size(labels):
fors_label, s_valinsizes.items():
ifs_labelinlabels:
returns_val
return0
defmain():
parser=argparse.ArgumentParser()
config=ConfigParser.SafeConfigParser()
parser.add_argument("-c", "--conffile", default="~/.github2csv",
help="Config file name")
parser.add_argument("-r", "--repo", action="append",
help="Repository name (as 'owner/repo')")
parser.add_argument("-u", "--user", help="GitHub username")
parser.add_argument("-p", "--password", help="GitHub password")
parser.add_argument("-m", "--milestone", default=None, help="GitHub milestone")
parser.add_argument("-n", "--unsized", action="store_true",
help="Get unsized open issues")
parser.add_argument("-s", "--sized", action="store_true",
help="Get sized open issues")
parser.add_argument("-l", "--labels", action="store_true",
help="Get labels too")
parser.add_argument("-o", "--outfile", default="gitoutput.csv",
help="CSV file to write")
parser.add_argument("-d", "--daily", action="store_true",
help="Daily activity report")
parser.add_argument("-a", "--all", action="store_true",
help="Get open and closed tickets")
parser.add_argument("-g", "--git-commits", action="store_true",
help="Get Git commit activity")
args=vars(parser.parse_args())
conffile=os.path.expanduser(args.get("conffile"))
try:
config.read(conffile)
exceptConfigParser.Error, e:
print"Error parsing config file", conffile
printe
defcfile_get(option):
try:
returnconfig.get("github2csv", option)
exceptException, e:
returnNone
user=args.get("user") orcfile_get("user")
password=args.get("password") orcfile_get("password")
repos=args.get("repo") or (cfile_get("repo") or'').split(",")
size_str=args.get("sizes") orcfile_get("sizes")
do_unsized=args.get("unsized")
do_sized=args.get("sized")
do_daily=args.get("daily")
do_labels=args.get("labels")
do_all=args.get("all")
do_commits=args.get("git_commits")
milestone=args.get("milestone") orcfile_get("milestone")
outfile=args.get("outfile") orcfile_get("outfile")
milestone_date=None
define_sizes(size_str)
gh=github3.login(user, password)
open_points=0
closed_points=0
notdone_points=0
total_points=0
unsized= []
withopen(outfile, "w+") ascsvfile:
forfullrepoinrepos:
if"/"notinfullrepo:
print"Bad repository spec '%s' -- use 'owner/reponame' format."%fullrepo
continue
owner, repo=fullrepo.strip().split("/", 1)
writer=csv.writer(csvfile, delimiter=',')
ghrepo=gh.repository(owner, repo)
mstones= {}
activity= {}
issue_count=0
print"Looking at milestones in:", fullrepo, owner, repo
formingh.repository(owner, repo).iter_milestones():
mstones[str(m.title)] =m.number
ifmstones.get(milestone) isNone:
print"WARNING: did not find milestone '%s' in %s, using *"% (milestone, repo)
else:
milestone_date=ghrepo.milestone(mstones.get(milestone)).created_at
openissues=gh.iter_repo_issues(owner, repo,
milestone=mstones.get(milestone, "*"),
state="open")
closedissues=gh.iter_repo_issues(owner, repo,
milestone=mstones.get(milestone, "*"),
state="closed")
ifdo_all:
allissues= [iforiinopenissues] + [iforiinclosedissues]
else:
allissues= [iforiinopenissues]
ifdo_commits:
print"Processing commit activity in", repo, "since", milestone_date
allcommits= []
forbranchinghrepo.iter_branches():
branchcommits=ghrepo.iter_commits(unicode(branch.commit.sha),
None, None, -1, None,
milestone_date)
allcommits.extend(branchcommits)
print" .. Found %d total commits"%len(allcommits)
forcinallcommits:
ifc.commit.message:
issues=re.findall("#[0-9]+", c.commit.message)
try:
forcommentinghrepo.iter_comments_on_commit(c.commit.sha):
issues.extend(re.findall("#[0-9]+", comment.body))
exceptException, e:
print" ERROR fetching comments on", c.commit.sha
print" ", e
foriinissues:
committers=activity.setdefault(i[1:], {})
ifstr(c.committer) notincommitters:
committers[c.committer.login] =1
else:
committers[c.committer.login] +=1
forissueinallissues:
issue_sized=False
forlinissue.labels:
ifl.nameinsizes:
issue_sized=True
i= [repo, issue.number,
"https://www.github.com/%s/%s/issues/%d"%
(owner, repo, issue.number),
issue.title]
labels= [l.nameforlinissue.labels]
isize=label2size(labels)
i.append(isize)
ifisize==0:
unsized.append((repo, issue.number))
ifdo_daily:
total_points+=isize
open_points+=isize
i.append(1if"working"inlabelselse'')
i.append(1if"done"inlabelselse'')
if"working"inlabels:
labels.remove("working")
if"done"inlabels:
labels.remove("done")
else:
notdone_points+=isize
ifdo_commits:
committers=activity.get(unicode(issue.number), {})
i.append(','.join(['%s: %s'% (n, count)
forn, countincommitters.items()]))
i.append(','.join(labels))
issue_count+=1
writer.writerow(i)
else:
ifdo_commits:
committers=activity.get(unicode(issue.number), [])
i.append(','.join(committers))
ifdo_labels:
i.append(','.join([l.nameforlinissue.labels]))
if (do_sizedandissue_sized) or (do_unsizedandnotissue_sized):
issue_count+=1
writer.writerow(i)
forissueinclosedissues:
labels= [l.nameforlinissue.labels]
isize=label2size(labels)
closed_points+=isize
total_points+=isize
print"Found %d issues in repo %s"% (issue_count, repo)
ifdo_daily:
iflen(unsized):
print"Open issues with no size set: "
foriinunsized:
print"%s #%s"%i
print ("Point totals: %s open, %s not-done, %s closed, %s total"
% (open_points, notdone_points, closed_points, total_points))