-
Notifications
You must be signed in to change notification settings - Fork 9
/
ghub.py
278 lines (224 loc) · 10.2 KB
/
ghub.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/python
# ------------------------------------------------ import block ------------------------------------------------------ #
import requests
import json
import argparse
import datetime
from datetime import date
# -------------------------------------------------- arguments block ------------------------------------------------- #
parser = argparse.ArgumentParser(usage='%(prog)s <user> <repository name> -num <req number> -o <opt1 opt2 opt3>',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("username", nargs='+', help="GitHub username")
parser.add_argument("repository", nargs='+', help="GitHub repository name")
parser.add_argument("-v", "--version", action='version', version='%(prog)s 0.001')
parser.add_argument("-oad", "--opened_after_date", type=str, metavar="",
help="Option to consider only pull requests opened on or after this date. Format 2017-03-14(Y-M-D)")
parser.add_argument("-ocd", "--opened_before_date", type=str, metavar="",
help="Only consider pull requests opened before this date. Format 2017-03-14(Y-M-D)")
parser.add_argument("-num", "--number", metavar="", help="Works with parameters for single request. More info -h.")
parser.add_argument("-o", "--options", nargs='*', metavar="", type=str, help="Possible additional options:\n"
"bs = Basic statistics about merged/closed rate.\n"
"\nNext parameters will works only with parameter -num:\n"
"ncc = Number of comments created.\n"
"ndo = Number of days opened.\n"
"dwo = Day of the week opened.\n"
"dwc = Day of the week closed.\n"
"wo = Week opened.\n"
"wc = Week closed.\n"
"uo = User who opened.\n"
"uc = User who closed.\n"
"al = Attached labels.\n"
"nla = Number of lines added.\n"
"nld = Number of lines deleted.\n"
"hdc = Hour of the day closed.\n"
"hdo = Hour of the day opened.")
args = parser.parse_args()
# ------------------------------------- Functions for processing options --------------------------------------------- #
week = ['Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday']
# ------------------------------------ Defiitions -------------------------------------------------------------------- #
def summary_rep_inf(res):
for i in res:
print(str(i["number"]) + " " + str(i["state"]) + " " + str(i["user"]["login"])
+ " " + str(i["created_at"]) + " " + str(i["title"]))
def summary_single_inf(res):
print(str(res["number"]) + " " + str(res["state"]) + " " + str(res["user"]["login"])
+ " " + str(res["created_at"]) + " " + str(res["title"]))
def bs(res):
merged = 0
closed = 0
for i in res:
if i["merged_at"]:
merged += 1
if i["closed_at"]:
closed += 1
print("Repository " + str(args.repository[0]) + ": merged requests=" + str(merged) + ". Closed requests="
+ str(closed)+".")
def oad(res, string_date):
is_find_val = 0
result = []
data_array = []
str_d = string_date.split("-")
for val in str_d:
result.append(int(val))
for rec in res:
if rec["state"] == "open":
arr = []
for val in rec["created_at"].split("T")[0].split('-'):
arr.append(int(val))
if tuple(arr) >= tuple(result):
is_find_val = 1
data_array.append(str(rec["number"]) + " " + str(rec["state"]) + " "
+ str(rec["user"]["login"]) + " " + str(rec["created_at"]) + " "
+ str(rec["title"]))
if is_find_val == 0:
print("No such data")
else:
for i in data_array:
print(i)
def obd(res, string_date):
is_find_val = 0
result = []
data_array = []
str_d = string_date.split("-")
for val in str_d:
result.append(int(val))
for rec in res:
if rec["state"] == "open":
arr = []
for val in rec["created_at"].split("T")[0].split('-'):
arr.append(int(val))
if tuple(arr) <= tuple(result):
is_find_val = 1
data_array.append(str(rec["number"]) + " " + str(rec["state"]) + " "
+ str(rec["user"]["login"]) + " " + str(rec["created_at"]) + " "
+ str(rec["title"]))
if is_find_val == 0:
print("No such data")
else:
for i in data_array:
print(i)
def ncc(res):
print("Pull request info: PR num: " + str(res["number"]) + " " + str(res["state"]) + " " + str(res["user"]["login"])
+ ". Number of comments: " + str(res["comments"]))
def ndo(res):
date_arr = []
for val in res["created_at"].split("T")[0].split('-'):
date_arr.append(int(val))
now = datetime.datetime.now()
d0 = date(date_arr[0], date_arr[1], date_arr[2])
d1 = date(now.year, now.month, now.day)
delta = d0 - d1
print("Opened days info: PR num: " + str(res["number"]) + " " + str(res["state"]) + " " + str(res["user"]["login"])
+ ". Days opened: " + str(abs(delta.days)))
def dwo(res):
date_arr = []
for val in res["created_at"].split("T")[0].split('-'):
date_arr.append(int(val))
print("Day of week opened: PR num: " + str(res["number"]) + " " + str(res["state"]) + " " + str(res["user"]["login"])
+ ". Day opened: " + str(week[date(date_arr[0], date_arr[1], date_arr[2]).weekday()]))
def dwc(res):
if res["closed_at"]:
date_arr = []
for val in res["closed_at"].split("T")[0].split('-'):
date_arr.append(int(val))
print("Day of week closed: PR num: " + str(res["number"]) + " " + str(res["state"]) + " " + str(res["user"]["login"])
+ ". Day closed: " + str(week[date(date_arr[0], date_arr[1], date_arr[2]).weekday()]))
else:
print("Day of week closed: Request still not closed.")
def wo(res):
date_arr = []
for val in res["created_at"].split("T")[0].split('-'):
date_arr.append(int(val))
print("Week opened: PR num: " + str(res["number"]) + " " + str(res["state"]) + " " + str(res["user"]["login"])
+ ". Week opened: " + str(datetime.date(date_arr[0], date_arr[1], date_arr[2]).isocalendar()[1]))
def wc(res):
if res["closed_at"]:
date_arr = []
for val in res["created_at"].split("T")[0].split('-'):
date_arr.append(int(val))
print("Week closed: PR num: " + str(res["number"]) + " " + str(res["state"]) + " " + str(res["user"]["login"])
+ ". Week opened: " + str(datetime.date(date_arr[0], date_arr[1], date_arr[2]).isocalendar()[1]))
else:
print("Week in year: Request still not closed.")
def uo(res):
print("User opened: PR num: " + str(res["number"]) + " " + str(res["state"]) + " " + str(res["user"]["login"])
+ ". Opened by user: " + str(res["user"]["login"]))
def uc(res):
if res["state"] == "closed":
response = requests.get('https://api.github.com/repos/' + args.username[0] + '/' + args.repository[0] +
'/issues/' + args.number + '/events')
if (response.ok):
js = json.loads(response.text)
print("User closed request: PR num: " + str(res["number"]) + " " + str(res["state"]) + " " + str(res["user"]["login"])
+ ". User closed request: " + str(js[0]["actor"]["login"]))
else:
print("User closed request: Request still not closed.")
def al(res):
response = requests.get('https://api.github.com/repos/' + args.username[0] + '/' + args.repository[0] +
'/issues/' + args.number + '/events')
if (response.ok):
js = json.loads(response.text)
for i in js:
if "label" in i:
print("Label for PR " + str(res["number"]) + ". Label = " +i["label"]["name"])
def nla(res):
print("Number lines added:" + str(res["additions"]))
def nld(res):
print("Number lines deleted:" + str(res["deletions"]))
def hdc(res):
if res["closed_at"]:
hms = res["created_at"].split("T")[1].split('-')
print("Hour of day closed:" + hms[0].split(":")[0])
def hdo(res):
if res["created_at"]:
hms = res["created_at"].split("T")[1].split('-')
print("Hour of day opened:" + hms[0].split(":")[0])
def ghub_request(type=""):
response = requests.get('https://api.github.com/repos/' + args.username[0] + '/' + args.repository[0] +
'/pulls?state=' + type)
if (response.ok):
print("req.number state user created title")
return json.loads(response.text)
else:
print("ERR: Something goes wrong. Please check username, repository name, options.")
exit()
def ghub_num_request():
response = requests.get('https://api.github.com/repos/' + args.username[0] + '/' + args.repository[0] +
'/pulls/' + args.number)
if (response.ok):
return json.loads(response.text)
else:
print("ERR: Something goes wrong. Please check username, repository name, pull request number options.")
exit()
# ------------------------------------- getting data from GitHub api ------------------------------------------------- #
num_opts = ["ndo", "dwo", "dwc", "wo", "wc", "uo", "uc", "al", "nla", "nld", "hdc", "hdo", "ncc"]
gen_opts = ["bs"]
if args.number:
if args.options:
data = ghub_num_request()
for i in args.options:
if i in num_opts:
globals()[i](data)
else:
summary_single_inf(ghub_num_request())
else:
if args.opened_after_date:
oad(ghub_request("open"), args.opened_after_date)
exit()
if args.opened_before_date:
obd(ghub_request("open"), args.opened_before_date)
exit()
if args.options:
for i in args.options:
if i in gen_opts:
globals()[i](ghub_request("all"))
else:
summary_rep_inf(ghub_request("all"))
else:
summary_rep_inf(ghub_request("all"))