forked from sylvainc/ak-oca-clabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclabot.py
executable file
·175 lines (132 loc) · 4.88 KB
/
clabot.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
#!/usr/bin/env python3
import argparse
import configparser
import hashlib
import hmac
import json
import re
import pprint
from urllib.parse import unquote
from http.server import HTTPServer, BaseHTTPRequestHandler
import requests
from requests.auth import HTTPBasicAuth
from erppeek import Client
class GithubHookHandler(BaseHTTPRequestHandler):
_github_allowed_events = []
def _validate_signature(self, repo, data, hub_signature):
digest_type, signature = hub_signature.split('=')
if digest_type != 'sha1':
return False
config = self.server.config
token_key = 'token_' + repo
if token_key in config:
secret = config[token_key]
else:
secret = config['default_token']
mac = hmac.new(
bytes(secret, 'utf-8'), msg=data, digestmod=hashlib.sha1)
return hmac.compare_digest(mac.hexdigest(), signature)
def do_POST(self):
content_length = int(self.headers['Content-Length'] or 0)
content_type = self.headers['Content-Type']
hub_signature = self.headers['X-Hub-Signature']
github_event = self.headers['X-GitHub-Event']
if github_event not in self._github_allowed_events:
self.send_response(400)
return
try:
post_data = self.rfile.read(content_length)
if content_type == 'application/json':
json_data = post_data.decode('utf-8')
elif content_type == 'application/x-www-form-urlencoded':
json_data = re.sub(
'^payload=', '', unquote(post_data.decode('utf-8'))
)
else:
raise ValueError('Invalid Content-Type')
payload = json.loads(json_data)
repo = payload['repository']['name']
except:
self.send_response(400)
return
if not self._validate_signature(repo, post_data, hub_signature):
self.send_response(401)
return
if self.handle_payload(payload):
self.send_response(200)
else:
self.send_response(400)
class PullRequestHandler(GithubHookHandler):
_github_allowed_events = ['pull_request']
def _have_signed_cla(self, users):
"""
Connect to odoo backend to verify if the users have signed
the OCA CLA
"""
config = self.server.config
client = Client(
config['odoo_host'],
config['odoo_database'],
config['odoo_user'],
config['odoo_password']
)
for user in users:
condition = [
(config['odoo_github_login_field'], '=', user),
('category_id', '=', config['odoo_cla_categ']),
]
if not client.search('res.partner', condition):
return False
return True
def handle_payload(self, event):
if event['action'] not in ('opened', 'synchronize'):
return False
config = self.server.config
base_url = config['github_base_url']
login = config['github_login']
password = config['github_password']
owner = event['repository']['owner']['login']
repo = event['repository']['name']
number = event['number']
path = '/repos/{owner}/{repo}/pulls/{number}/commits'
path = path.format(owner=owner, repo=repo, number=number)
params = {'per_page': 250} # maximum allowed
res = requests.get(
base_url + path,
params=params,
auth=HTTPBasicAuth(login, password)
)
commits = res.json()
users = [c['author']['login'] for c in commits]
users = list(set(users))
if not self._have_signed_cla(users):
# Send CLA comment
path = '/repos/{owner}/{repo}/issues/{number}/comments'
path = path.format(owner=owner, repo=repo, number=number)
user = event['pull_request']['user']['login']
cla_message = config['cla_message'].format(user=user)
data = {'body': cla_message}
res = requests.post(
base_url + path,
data=json.dumps(data),
auth=HTTPBasicAuth(login, password)
)
return True
def run(config):
config = config['clabot']
server_address = (config['interface'], int(config['port']))
server = HTTPServer(server_address, PullRequestHandler)
server.config = config
print('CLA bot listening on %s:%i' % server_address)
server.serve_forever()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Minimal CLA bot server')
parser.add_argument(
'config_file',
help='Configuration ini file',
type=str
)
args = parser.parse_args()
config = configparser.ConfigParser()
config.read(args.config_file)
run(config)