-
Notifications
You must be signed in to change notification settings - Fork 1
/
reviews.py
100 lines (79 loc) · 3.07 KB
/
reviews.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
from dateutil.parser import isoparse
from datetime import datetime, timezone
from deta import Deta
""" schema
{
key: str, // randomly_generated
reviewer: str, // reviewer
pull_request: int, // pull request number
requested_at : int, // posix timestamp of request
submitted_at : int, // posix timestamp of review submission
submitted: bool, // if the review has been submitted
crt: int // code review turnaround
}
"""
# manages storing, fetching and updating review requests information
class ReviewRequestStore:
def __init__(self):
self.db = Deta().Base("code_reviews")
# get review req from pull request number and reviewer
def __get_review_req(self, pr_num: int, reviewer: str):
# generator
review_reqs_gen = next(
self.db.fetch(
{"submitted": False, "pull_request": pr_num, "reviewer": reviewer}
)
)
review_reqs = []
for r in review_reqs_gen:
review_reqs.append(r)
# there should be only one corresponding unsubmitted review request
if len(review_reqs) == 0:
raise Exception("No corresponding review request found")
if len(review_reqs) > 1:
raise Exception(
"Found multiple imcomplete reviews for same pull request and reviewer"
)
return review_reqs[0]
# store review request
def store(self, payload: dict):
# POSIX timestamp
current_time = int(datetime.now(timezone.utc).timestamp())
item = {
"reviewer": payload["requested_reviewer"]["login"],
"pull_request": payload["pull_request"]["number"],
"requested_at": current_time,
"submitted": False,
}
self.db.put(item)
# mark review request complete
def mark_complete(self, payload: dict):
submission_time = int(isoparse(payload["review"]["submitted_at"]).timestamp())
pr_num = payload["pull_request"]["number"]
reviewer = payload["review"]["user"]["login"]
review_req = self.__get_review_req(pr_num, reviewer)
# updates to the review request
updates = {
"submitted": True,
"submitted_at": submission_time,
"crt": submission_time - review_req["requested_at"],
}
self.db.update(updates, review_req["key"])
return
# delete review request
def delete(self, payload: dict):
pr_num = payload["pull_request"]["number"]
reviewer = payload["requested_reviewer"]["login"]
review_req = self.__get_review_req(pr_num, reviewer)
self.db.delete(review_req["key"])
# get review requests created since date
def get(self, created_since: str):
since = int(isoparse(created_since).timestamp())
review_reqs_since_gen = next(
self.db.fetch({"requested_at?gte": since, "submitted": True})
)
review_reqs_since = []
for req in review_reqs_since_gen:
review_reqs_since.append(req)
return review_reqs_since
rev_req_store = ReviewRequestStore()