-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
58 lines (39 loc) · 1.49 KB
/
main.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
import os
import logging
import logging.config
import json
import requests
from flask import escape
from flask import abort
from logging_config import setup_logging
setup_logging()
logger = logging.getLogger(__name__)
from config import CONFIG
from bitbucket2chat import handle_bitbucket_event
def main(request):
bot_name = CONFIG['bot_name'].get()
if request is None:
logger.info('Hi, I\'m %s!', bot_name)
return 'OK'
logger.info('Bot %s is alive!', bot_name)
logger.info('This request came from: %s', request.remote_addr)
if request.headers.get('User-Agent') == 'Bitbucket-Webhooks/2.0':
event = request.get_json(silent=True)
headers = request.headers
chat_response_payload = handle_bitbucket_event(event, headers)
# Skip successful commits for now.
if 'is *SUCCESSFUL*.' in chat_response_payload:
return '200'
response = send_to_chat(chat_response_payload)
logger.info('Response from chat. Code=%s, Text=%s', response.status_code, response.text)
return '{} {}'.format(response.status_code, response.text)
else:
return abort(400)
def send_to_chat(chat_response_payload):
webhook_url = os.getenv('CHAT_WEBHOOK_URL')
message_headers = {'Content-Type': 'application/json; charset=UTF-8'}
response = requests.post(
webhook_url, data=chat_response_payload, headers=message_headers)
return response
if __name__ == '__main__':
main(None)