diff --git a/alertserver/config.sample.ini b/alertserver/config.sample.ini index 583d225..6704166 100644 --- a/alertserver/config.sample.ini +++ b/alertserver/config.sample.ini @@ -1,8 +1,8 @@ [Bitbucket] -TRUSTED_REMOTE_ADDRS = 104.192.143.192/28, 104.192.143.208/28 +TRUSTED_REMOTE_ADDRS = 104.192.143.192/28, 104.192.143.208/28, 127.0.0.1 WEBHOOK_PATHS = /webhook, /webhook2 WEBHOOK_HOST = 0.0.0.0 -WEBHOOK_PORT = 5000 +WEBHOOK_PORT = 8080 [Trello] API_KEY = 04a07f688a4d9d86f4a8796795bb5187 diff --git a/alertserver/server.py b/alertserver/server.py index 8df5014..d1be370 100644 --- a/alertserver/server.py +++ b/alertserver/server.py @@ -2,11 +2,13 @@ from functools import reduce from ipaddress import ip_network as parse_addr +from easydict import EasyDict from flask import Flask from flask import abort from flask import request from alertserver.config import Bitbucket as config_bitbucket +from alertserver.trello_client import post_branch_activity, post_commit_activity, post_merge_activity app = Flask(__name__) @@ -23,28 +25,53 @@ def is_trusted_remote_addrs(addr): return addr in trusted_remote_addrs +def parse_commit_message(commit_message): + return tuple(re.split(r'^#?([0-9]+)\s?', commit_message)[1:3]) + + +def parse_branch_name(branch_name): + splitted = re.split(r'^([0-9]+)(-?_?\s?)(.*)', branch_name) + if len(splitted) < 2: + return tuple([None, splitted[0]]) + return tuple([splitted[1], splitted[3]]) + + def webhook_routine(): if not is_trusted_remote_addrs(request.remote_addr): abort(403) - data = request.get_json() - - commit_author = data['actor']['username'] - - is_branch_created = data['push']['changes'][0]['created'] - is_branch_closed = data['push']['changes'][0]['closed'] - if is_branch_created: - # TODO: Alert branch created. - print('Webhook received! %s created branch' % commit_author) - pass - elif is_branch_closed: - # TODO: Alert branch closed. - print('Webhook received! %s deleted branch' % commit_author) - else: - # TODO: Alert commits. - commit_hash = data['push']['changes'][0]['new']['target']['hash'][:7] - commit_url = data['push']['changes'][0]['new']['target']['links']['html']['href'] - print('Webhook received! %s committed %s' % (commit_author, commit_hash)) + data = EasyDict(request.get_json()) + + commit_author = data.actor.username + + for change in data.push.changes: + commit = change.commits[0] + commit_message = commit.message + commit_hash = commit.hash[:7] + commit_url = change.links.html.href + + card_id1, commit_message = parse_commit_message(commit_message) + + card_id2, branch_name = parse_branch_name(change.new.name) + + if card_id2 is None: + card_id = card_id1 + elif card_id1 is None: + card_id = card_id2 + elif card_id2 is not None and card_id1 is not None: + card_id = card_id2 + else: + continue + + if change.created and change.new.type == 'branch': + post_branch_activity(card_id, branch_name, commit_url) + # elif change.closed and change.new.type == 'branch': + # post_merge_activity(card_id, branch_name, commit_url) + elif branch_name == 'develop' and change.new.type == 'branch': + post_merge_activity(card_id, branch_name, commit_url) + else: + post_commit_activity(card_id, branch_name, commit_message, commit_url) + return 'OK'