-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtrollreview.py
123 lines (104 loc) · 3.52 KB
/
trollreview.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
import enum
import random
class ReviewType(enum.Enum):
FIXES_REF = 'fixes_ref'
MISSING_FIELDS = 'missing_fields'
MISSING_HASH = 'missing_hash'
MISSING_AM = 'missing_am'
INVALID_HASH = 'invalid_hash'
INCORRECT_PREFIX = 'incorrect_prefix'
ALTERED_UPSTREAM = 'altered_upstream'
BACKPORT = 'backport'
SUCCESS = 'success'
CLEAR_VOTES = 'clear_votes'
KCONFIG_CHANGE = 'kconfig_change'
IN_MAINLINE = 'in_mainline'
UPSTREAM_COMMENTS = 'upstream_comments'
NOT_IN_MAINLINE = 'not_in_mainline'
FORBIDDEN_TREE = 'forbidden_tree'
def __str__(self):
return self.value
def __repr__(self):
return str(self)
class ReviewResult(object):
def __init__(self, change, strings, dry_run=False):
self.change = change
self.strings = strings
self.vote = 0
self.ignore_positive_votes = False
self.notify = False
self.dry_run = dry_run
self.issues = {}
self.feedback = {}
self.web_link = None
self.inline_comments = {}
def add_review(self, review_type, msg, vote=0, notify=False, dry_run=False,
ignore_positive_votes=False):
if ignore_positive_votes:
self.ignore_positive_votes = True
self.vote = 0 if self.vote > 0 else self.vote
if vote <= 0 or not self.ignore_positive_votes:
# Take the lowest negative, or the highest positive
if vote < 0 or self.vote < 0:
self.vote = min(self.vote, vote)
elif vote > 0 or self.vote > 0:
self.vote = max(self.vote, vote)
else:
self.vote = vote
if vote < 0:
self.issues[review_type] = msg
else:
self.feedback[review_type] = msg
self.notify = self.notify or notify
self.dry_run = self.dry_run or dry_run
def add_inline_comment(self, new_file, line, comment):
if not self.inline_comments.get(new_file):
self.inline_comments[new_file] = []
self.inline_comments[new_file].append({"line": line, "message": comment})
def add_web_link(self, link):
self.web_link = link
def generate_issues(self, retry_key):
num_issues = len(self.issues)
if not num_issues:
return ''
if num_issues > 1:
msg = self.strings.FOUND_ISSUES_HEADER_MULTIPLE
else:
msg = self.strings.FOUND_ISSUES_HEADER_SINGLE
for j,i in enumerate(self.issues.values()):
if num_issues > 1:
msg += self.strings.ISSUE_SEPARATOR.format(j + 1)
msg += i
if retry_key:
msg += self.strings.FOUND_ISSUES_RETRY.format(retry_key)
return msg
def generate_feedback(self):
num_feedback = len(self.feedback)
if not num_feedback:
return ''
if len(self.issues):
msg = self.strings.FEEDBACK_AFTER_ISSUES
elif self.vote > 0:
msg = self.strings.POSITIVE_VOTE.format(random.choice(self.strings.SWAG))
else:
msg = ''
for j,f in enumerate(self.feedback.values()):
if num_feedback > 1:
msg += self.strings.FEEDBACK_SEPARATOR.format(j + 1)
msg += f
return msg
def generate_review_message(self, retry_key):
msg = self.strings.HEADER
msg += self.strings.GREETING.format(
self.change.current_revision.uploader_name,
random.choice(self.strings.GREETING_SWAG))
msg += self.strings.REVIEW_SEPARATOR
msg += self.generate_issues(retry_key)
if len(self.issues) and len(self.feedback):
msg += self.strings.REVIEW_SEPARATOR
msg += self.generate_feedback()
msg += self.strings.REVIEW_SEPARATOR
if self.web_link:
msg += self.strings.WEB_LINK.format(self.web_link)
msg += self.strings.FOOTER
return msg