-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathofCodeStyleGuard.py
76 lines (67 loc) · 2.46 KB
/
ofCodeStyleGuard.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
#!/usr/bin/python
"""Make sure openFrameworks Pull Requests conform to the code style"""
import styleguard
import logging
import json
import os
from flask import Flask, request
WEBLOGGER = logging.getLogger('styleguard.webserver')
WEBLOGGER.setLevel(styleguard.cfg['logging_level'])
APP = Flask(__name__)
APP.logger.setLevel(styleguard.cfg['logging_level'])
logging.getLogger('urllib3').setLevel(logging.WARNING)
logging.getLogger('requests.packages.urllib3').setLevel(logging.INFO)
# Add a file handler to the root logger if log filename is set
styleguard.add_file_logger()
@APP.route('/check')
def manual_check():
"""Initiate manual request for checking a PR"""
WEBLOGGER.info('Manual PR check has been requested')
WEBLOGGER.debug('Access route: ' + str(request.access_route[:]))
# get the number of the requested pr (i.e. URL/check?pr=number)
try:
pr_number = int(request.args.get('pr', 0))
except ValueError:
WEBLOGGER.error('Invalid PR ID! Skipping...')
return 'Error: Invalid PR ID!'
if pr_number:
WEBLOGGER.info('PR number ' + str(pr_number))
styleguard.handle_payload(pr_number)
return ('Received request for checking PR ' + str(pr_number))
else:
WEBLOGGER.error('Invalid PR ID! Skipping...')
return 'Error: Invalid PR ID!'
@APP.route('/', methods=['POST'])
def api_pr():
""" React to a received POST request"""
WEBLOGGER.info(60 * "#")
WEBLOGGER.info("Received POST request.")
WEBLOGGER.debug('Access route: ' + str(request.access_route[:]))
origin = request.access_route[0]
# was using request.remote_addr. access_route could possibly be spoofed
if origin not in styleguard.cfg['github_ips']:
WEBLOGGER.warning("Origin of request UNKNOWN: " + origin)
return 'Error'
else:
WEBLOGGER.debug("Origin of request: " + origin)
try:
payload = json.loads(request.form['payload'])['pull_request']
except KeyError:
# crutch: if an invalid request arrives locally, load a json file directly
if origin == '127.0.0.1':
location = os.getenv('OPENSHIFT_REPO_DIR', '')
with open(os.path.join(location, 'sample_payload.json'), 'r') as sample:
payload = json.load(sample)
else:
raise
styleguard.handle_payload(payload)
return 'OK'
def main():
"""Main function"""
# Instantiate a PrHandler, which start waiting on styleguard.MY_QUEUE
WEBLOGGER.debug('In ofCodeStyleGuard main function')
_threaded_pr_worker = styleguard.PrHandler()
APP.run(host='0.0.0.0', port=styleguard.cfg['local_port'])
styleguard.MY_QUEUE.join()
if __name__ == "__main__":
main()