Completed
Pull Request — master (#449)
by Manas
02:27
created

CircleCIWebhookSensor.cleanup()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
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, requests
17
18
from flask import Flask, request, jsonify, abort, make_response
19
from st2reactor.sensor.base import Sensor
20
21
22
class CircleCIWebhookSensor(Sensor):
23
24
    def setup(self):
25
        self.host = self._config['host']
26
        self.port = self._config['port']
27
        self._endpoints = self._config['endpoints']
28
        self.app = Flask(__name__)
29
        self.trigger_ref = 'circle_ci.build_event'
30
        self.log = self._sensor_service.get_logger(__name__)
31
32
        @self.app.route('/status')
33
        def status():
34
            return json.dumps({'response': 'OK'})
35
36
        @self.app.route('/webhooks/<path:endpoint>', methods=['POST',])
37
        def build_events(endpoint):
38
39
            if not endpoint in self._endpoints:
40
                self.log.error('Ignoring unknown endpoint : %s', endpoint)
41
                abort(404)
42
43
            webhook_body = request.get_json()
44
            payload = {}
45
            payload['headers'] = self._get_headers_as_dict(request.headers)
46
            payload['body'] = webhook_body
47
48
            response = self._sensor_service.dispatch(self.trigger_ref, payload)
49
            self.log.debug(json.dumps(response))
50
            return json.dumps({'response': 'triggerposted'})
51
52
    def run(self):
53
        self.app.run(host=self.host,port=self.port,debug=True, threaded=True)
54
55
    def cleanup(self):
56
        # This is called when the st2 system goes down. You can perform cleanup operations like
57
        # closing the connections to external system here.
58
        pass
59
60
    def _get_headers_as_dict(self, headers):
61
        headers_dict = {}
62
        for key, value in headers:
63
            headers_dict[key] = value
64
        return headers_dict
65
66
    def add_trigger(self, trigger):
67
        pass
68
69
    def update_trigger(self, trigger):
70
        pass
71
72
    def remove_trigger(self, trigger):
73
        pass
74