-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy patheventhandler.py
64 lines (54 loc) · 2.01 KB
/
eventhandler.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
import imp
import json
import os
_warnings = []
class EventHandler:
def on_pr_opened(self, api, payload):
pass
def on_pr_updated(self, api, payload):
pass
def on_new_comment(self, api, payload):
pass
def warn(self, msg):
global _warnings
_warnings += [msg]
def is_open_pr(self, payload):
return payload['issue']['state'] == 'open' and 'pull_request' in payload['issue']
def register_tests(self, path):
from test import create_test
tests_location = os.path.join(path, 'tests')
if not os.path.isdir(tests_location):
return
tests = [os.path.join(tests_location, f) for f in os.listdir(tests_location) if f.endswith('.json')]
for testfile in tests:
with open(testfile) as f:
contents = json.load(f)
if not isinstance(contents['initial'], list):
assert not isinstance(contents['expected'], list)
contents['initial'] = [contents['initial']]
contents['expected'] = [contents['expected']]
for initial, expected in zip(contents['initial'], contents['expected']):
yield create_test(testfile, initial, expected)
def reset_test_state():
global _warnings
_warnings = []
def get_warnings():
global _warnings
return _warnings
def get_handlers():
modules = []
handlers = []
possiblehandlers = os.listdir('handlers')
for i in possiblehandlers:
location = os.path.join('handlers', i)
module = '__init__'
if not os.path.isdir(location) or not module + ".py" in os.listdir(location):
continue
try:
(file, pathname, description) = imp.find_module(module, [location])
module = imp.load_module(module, file, pathname, description)
handlers.append(module.handler_interface())
modules.append((module, location))
finally:
file.close()
return (modules, handlers)