This repository has been archived by the owner on May 27, 2021. It is now read-only.
/
issue.py
223 lines (162 loc) · 7.72 KB
/
issue.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
import datetime
import re
import github3
import requests
from config import CONFIG_VARS as cvar
class Issue:
"""
An abstraction over Github's Issue object.
Provides two primary public functions:
issue.warn(msg='../path/to/message_template.md')
Warns the issue if valid to do so based on the config rules.
@kwarg msg: local or remote path to the markdown file to use
issue.close(msg='../path/to/message_template.md')
Warns the issue if valid to do so based on the config rules.
@kwarg msg: local or remote path to the markdown file to use
"""
def __init__(self, issue):
self.issue = issue
def close(self, **kwargs):
# close because issue is too old
if kwargs['reason'] == 'old':
if self.is_valid('close'):
if cvar['DEBUG'] is False:
self.issue.close()
self.issue.create_comment(kwargs['msg'])
if cvar['ON_CLOSE_LABEL']:
self.issue.add_labels(cvar['ON_CLOSE_LABEL'])
return self.issue.number
return None
# close because issue has not received a reply
if kwargs['reason'] == 'noreply':
if self.is_valid('close_noreply'):
if cvar['DEBUG'] is False:
self.issue.close()
msg = re.sub(r'<%=.*%>', str(cvar['CLOSE_NOREPLY_AFTER']), kwargs['msg'])
self.issue.create_comment(msg)
if cvar['ON_CLOSE_LABEL']:
self.issue.add_labels(cvar['ON_CLOSE_LABEL'])
return self.issue.number
return None
def warn(self, **kwargs):
if self.is_valid('warn'):
if cvar['DEBUG'] is False:
msg = re.sub(r'<%=.*%>', self.issue.user.login, kwargs['msg'])
self.issue.create_comment(msg)
if cvar['ON_WARN_LABEL']:
self.issue.add_labels(cvar['ON_WARN_LABEL'])
return self.issue.number
return None
def is_valid(self, operation):
self.operation = operation
return all({
'not_closed': not self.issue.is_closed(),
'old_enough': self.is_old_enough(),
'no_milestone': not self.issue.milestone,
'labels_valid': self.labels_valid(),
'comments_valid': self.comments_valid(),
'organizations_valid': self.organizations_valid(),
'references_valid': self.references_valid(),
}.values())
def is_old_enough(self):
today = datetime.datetime.now().date()
if self.operation == 'warn':
return (today - self.issue.updated_at.date()).days >= cvar['WARN_INACTIVE_AFTER']
# Adding a warning will update the issue, so updated_at cannot be used
if self.operation == 'close':
diff = cvar['CLOSE_INACTIVE_AFTER'] - cvar['WARN_INACTIVE_AFTER']
return (today - self.issue.updated_at.date()).days >= diff
if self.operation == 'close_noreply':
return (today - self.issue.updated_at.date()).days >= cvar['CLOSE_NOREPLY_AFTER']
def labels_valid(self):
labels = []
if hasattr(self.issue, 'labels'):
labels = [l.name for l in self.issue.labels]
# Do not double warn issus
if self.operation == 'warn' and cvar['ON_WARN_LABEL'] in labels:
return False
# Ensure warning has been added if closing old issue
if self.operation == 'close' and cvar['ON_WARN_LABEL'] not in labels:
return False
# Ensure needs reply label has been added if closing unreplied issue
if self.operation == 'close_noreply' and not self.needs_reply():
return False
# None can be in blacklist
if '*' in cvar['LABEL_WHITELIST']:
return len(set(labels).intersection(set(cvar['LABEL_BLACKLIST']))) == 0
# At least one must be in whitelist
if '*' in cvar['LABEL_BLACKLIST']:
return len(set(labels).intersection(set(cvar['LABEL_WHITELIST']))) >= 0
def comments_valid(self):
# Total comments cannot exceed MAX_COMMENTS
if hasattr(self.issue, 'comments'):
if self.issue.comments > cvar['MAX_COMMENTS']:
return False
# The last comment must be from the bot user
if self.operation == 'close':
comments = list(self.issue.iter_comments())
if len(comments) == 0:
return False
return comments[-1].user.login == cvar['GITHUB_USERNAME']
return True
def organizations_valid(self):
op_orgs = list(self.issue.user.iter_orgs())
if len(op_orgs) > 0:
return len(set(op_orgs).intersection(set(cvar['ORGANIZATION_BLACKLIST']))) == 0
return True
def references_valid(self):
if cvar['IGNORE_REFERENCED'] is False:
return True
# if any events are referenced events, return False
events = list(self.issue.iter_events())
if len(events) > 0:
return not any([e.event == 'referenced' for e in events])
return True
def needs_reply(self):
# issue must have needs reply label
if hasattr(self.issue, 'labels'):
labels = [l.name for l in self.issue.labels]
if len(set(labels).intersection(set(cvar['NEEDS_REPLY_LABELS']))) >= 1:
# get timestamp for needs reply label
events = list(self.issue.iter_events())
for e in events:
ed = e.to_json()
if e.event == 'labeled' and ed['label']['name'] in cvar['NEEDS_REPLY_LABELS']:
# comments must be older than label_time, unless from user who added needs reply label
label_time = datetime.datetime.strptime(ed['created_at'],'%Y-%m-%dT%H:%M:%SZ').replace(tzinfo=None)
comments = list(self.issue.iter_comments())
return not any([c for c in comments if c.created_at.replace(tzinfo=None) > label_time and c.user.login != e.actor.login])
return False
def close_old_issues():
gh = github3.login(cvar['GITHUB_USERNAME'], cvar['GITHUB_PASSWORD'])
repo = gh.repository(cvar['REPO_USERNAME'], cvar['REPO_ID'])
issues = repo.iter_issues()
try: # Read message templates from remote URL
msg = requests.get(cvar['CLOSING_TEMPLATE']).text
except: # Read from local file
msg = open(cvar['CLOSING_TEMPLATE']).read()
closed = [Issue(i).close(msg=msg, reason='old') for i in issues]
print 'Old Issues Closed: '
print filter(lambda x: x is not None, closed)
def close_noreply_issues():
gh = github3.login(cvar['GITHUB_USERNAME'], cvar['GITHUB_PASSWORD'])
repo = gh.repository(cvar['REPO_USERNAME'], cvar['REPO_ID'])
issues = repo.iter_issues()
try: # Read message templates from remote URL
msg = requests.get(cvar['CLOSING_NOREPLY_TEMPLATE']).text
except: # Read from local file
msg = open(cvar['CLOSING_NOREPLY_TEMPLATE']).read()
closed = [Issue(i).close(msg=msg, reason='noreply') for i in issues]
print "No-Reply Issues Closed: "
print filter(lambda x: x is not None, closed)
def warn_old_issues():
gh = github3.login(cvar['GITHUB_USERNAME'], cvar['GITHUB_PASSWORD'])
repo = gh.repository(cvar['REPO_USERNAME'], cvar['REPO_ID'])
issues = repo.iter_issues()
try: # Read from remote URL
warning_msg = requests.get(cvar['WARNING_TEMPLATE']).text
except: # Read from local file
warning_msg = open(cvar['WARNING_TEMPLATE']).read()
warned = [Issue(i).warn(msg=warning_msg) for i in issues]
print "Old Issues Warned: "
print filter(lambda x: x is not None, warned)