/
__init__.py
56 lines (40 loc) · 1.5 KB
/
__init__.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
from eventhandler import EventHandler
import ConfigParser
import os
LABEL_WATCHERS_CONFIG_FILE = os.path.join(os.path.dirname(__file__),
'watchers.ini')
def get_label_config():
config = ConfigParser.ConfigParser()
config.read(LABEL_WATCHERS_CONFIG_FILE)
return config
def build_label_message(mentions):
message = ['cc']
for watcher in mentions:
message.append("@{}".format(watcher))
return ' '.join(message)
class LabelWatchersHandler(EventHandler):
def on_issue_labeled(self, api, payload):
new_label = payload['label']['name']
config = get_label_config()
repo = api.owner + '/' + api.repo
try:
watchers = config.items(repo)
except ConfigParser.NoSectionError:
return # No watchers
mentions = []
creator = None
if 'issue' in payload:
creator = payload['issue']['user']['login']
elif 'pull_request' in payload:
creator = payload['pull_request']['user']['login']
for (watcher, watched_labels) in watchers:
if watcher == payload['sender']['login'] or watcher == creator:
continue
watched_labels = watched_labels.split(' ')
if new_label in watched_labels:
mentions.append(watcher)
if not mentions:
return
message = build_label_message(mentions)
api.post_comment(message)
handler_interface = LabelWatchersHandler