-
Notifications
You must be signed in to change notification settings - Fork 5
/
pullrequests.py
47 lines (36 loc) · 1.46 KB
/
pullrequests.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
import json
from os import path
import requests
from datetime import datetime
class PRCalc(object):
"""Calculation of pull requests' time till merging."""
def __init__(self):
if path.isfile("pullrequests.json"):
self._pr_file = open("pullrequests.json", "r+")
else:
self._pr_file = open("pullrequests.json", "w+")
try:
self._pr = json.loads(self._pr_file.read())
except ValueError:
self._pr = {}
def update(self, repo):
"""Update pull requests."""
r = requests.get(repo)
if r.ok:
repoItem = json.loads(r.text or r.content)
for preq in repoItem:
if preq['user']['id'] not in self._pr:
self._pr[preq['user']['id']] = {
preq['merge_commit_sha']: preq['created_at']}
else:
self._pr[preq['user']['id']][
preq['merge_commit_sha']] = preq['created_at']
self._pr_file.close()
self._pr_file = open("pullrequests.json", "w+")
self._pr_file.write(json.dumps(self._pr))
def get_frustration_time(self, user_id, sha, commit_time):
"""Return time that took to get merged since the commit was done."""
prtime = datetime.strptime(
self._pr[user_id][sha], '%Y-%m-%dT%H:%M:%SZ')
ctime = datetime.strptime(commit_time, '%Y-%m-%dT%H:%M:%SZ')
return ctime - prtime