1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
3
|
|
|
# this work for additional information regarding copyright ownership. |
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
5
|
|
|
# (the 'License'); you may not use this file except in compliance with |
6
|
|
|
# the License. You may obtain a copy of the License at |
7
|
|
|
# |
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
# |
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
11
|
|
|
# distributed under the License is distributed on an 'AS IS' BASIS, |
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
# See the License for the specific language governing permissions and |
14
|
|
|
# limitations under the License. |
15
|
|
|
|
16
|
|
|
import json |
17
|
|
|
|
18
|
|
|
from flask import Flask, request, abort |
19
|
|
|
from st2reactor.sensor.base import Sensor |
20
|
|
|
|
21
|
|
|
TRIGGER_REF = 'circle_ci.build_event' |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class CircleCIWebhookSensor(Sensor): |
25
|
|
|
|
26
|
|
|
def setup(self): |
27
|
|
|
self.host = self._config['host'] |
28
|
|
|
self.port = self._config['port'] |
29
|
|
|
self._endpoints = self._config['endpoints'] |
30
|
|
|
self.app = Flask(__name__) |
31
|
|
|
self.trigger_ref = TRIGGER_REF |
32
|
|
|
self.log = self._sensor_service.get_logger(__name__) |
33
|
|
|
|
34
|
|
|
@self.app.route('/status') |
35
|
|
|
def status(): |
36
|
|
|
return json.dumps({'response': 'OK'}) |
37
|
|
|
|
38
|
|
|
@self.app.route('/webhooks/<path:endpoint>', methods=['POST']) |
39
|
|
|
def build_events(endpoint): |
40
|
|
|
|
41
|
|
|
if endpoint not in self._endpoints: |
42
|
|
|
self.log.error('Ignoring unknown endpoint : %s', endpoint) |
43
|
|
|
abort(404) |
44
|
|
|
|
45
|
|
|
webhook_body = request.get_json() |
46
|
|
|
payload = {} |
47
|
|
|
payload['headers'] = self._get_headers_as_dict(request.headers) |
48
|
|
|
payload['body'] = webhook_body |
49
|
|
|
|
50
|
|
|
response = self._sensor_service.dispatch(self.trigger_ref, payload) |
51
|
|
|
self.log.debug(json.dumps(response)) |
52
|
|
|
return json.dumps({'response': 'triggerposted'}) |
53
|
|
|
|
54
|
|
|
def run(self): |
55
|
|
|
self.app.run(host=self.host, port=self.port, threaded=True) |
56
|
|
|
|
57
|
|
|
def cleanup(self): |
58
|
|
|
# This is called when the st2 system goes down. You can perform cleanup operations like |
59
|
|
|
# closing the connections to external system here. |
60
|
|
|
pass |
61
|
|
|
|
62
|
|
|
def _get_headers_as_dict(self, headers): |
63
|
|
|
headers_dict = {} |
64
|
|
|
for key, value in headers: |
65
|
|
|
headers_dict[key] = value |
66
|
|
|
return headers_dict |
67
|
|
|
|
68
|
|
|
def add_trigger(self, trigger): |
69
|
|
|
pass |
70
|
|
|
|
71
|
|
|
def update_trigger(self, trigger): |
72
|
|
|
pass |
73
|
|
|
|
74
|
|
|
def remove_trigger(self, trigger): |
75
|
|
|
pass |
76
|
|
|
|