diff --git a/rhodecode/integrations/types/base.py b/rhodecode/integrations/types/base.py --- a/rhodecode/integrations/types/base.py +++ b/rhodecode/integrations/types/base.py @@ -150,7 +150,34 @@ WEBHOOK_URL_VARS = [ CI_URL_VARS = WEBHOOK_URL_VARS -class WebhookDataHandler(object): +class CommitParsingDataHandler(object): + + def aggregate_branch_data(self, branches, commits): + branch_data = collections.OrderedDict() + for obj in branches: + branch_data[obj['name']] = obj + + branches_commits = collections.OrderedDict() + for commit in commits: + if commit.get('git_ref_change'): + # special case for GIT that allows creating tags, + # deleting branches without associated commit + continue + commit_branch = commit['branch'] + + if commit_branch not in branches_commits: + _branch = branch_data[commit_branch] \ + if commit_branch else commit_branch + branch_commits = {'branch': _branch, + 'commits': []} + branches_commits[commit_branch] = branch_commits + + branch_commits = branches_commits[commit_branch] + branch_commits['commits'].append(commit) + return branches_commits + + +class WebhookDataHandler(CommitParsingDataHandler): name = 'webhook' def __init__(self, template_url, headers): @@ -184,25 +211,9 @@ class WebhookDataHandler(object): def repo_push_event_handler(self, event, data): url = self.get_base_parsed_template(data) url_cals = [] - branch_data = collections.OrderedDict() - for obj in data['push']['branches']: - branch_data[obj['name']] = obj - branches_commits = collections.OrderedDict() - for commit in data['push']['commits']: - if commit.get('git_ref_change'): - # special case for GIT that allows creating tags, - # deleting branches without associated commit - continue - - if commit['branch'] not in branches_commits: - branch_commits = {'branch': branch_data[commit['branch']], - 'commits': []} - branches_commits[commit['branch']] = branch_commits - - branch_commits = branches_commits[commit['branch']] - branch_commits['commits'].append(commit) - + branches_commits = self.aggregate_branch_data( + data['push']['branches'], data['push']['commits']) if '${branch}' in url: # call it multiple times, for each branch if used in variables for branch, commit_ids in branches_commits.items(): diff --git a/rhodecode/integrations/types/hipchat.py b/rhodecode/integrations/types/hipchat.py --- a/rhodecode/integrations/types/hipchat.py +++ b/rhodecode/integrations/types/hipchat.py @@ -24,14 +24,14 @@ import logging import requests import colander import textwrap -from collections import OrderedDict from mako.template import Template from rhodecode import events from rhodecode.translation import _ from rhodecode.lib import helpers as h from rhodecode.lib.celerylib import run_task, async_task, RequestContextTask from rhodecode.lib.colander_utils import strip_whitespace -from rhodecode.integrations.types.base import IntegrationTypeBase +from rhodecode.integrations.types.base import ( + IntegrationTypeBase, CommitParsingDataHandler) log = logging.getLogger(__name__) @@ -92,7 +92,7 @@ repo_push_template = Template(''' ''') -class HipchatIntegrationType(IntegrationTypeBase): +class HipchatIntegrationType(IntegrationTypeBase, CommitParsingDataHandler): key = 'hipchat' display_name = _('Hipchat') description = _('Send events such as repo pushes and pull requests to ' @@ -217,18 +217,8 @@ class HipchatIntegrationType(Integration ) def format_repo_push_event(self, data): - branch_data = {branch['name']: branch - for branch in data['push']['branches']} - - branches_commits = OrderedDict() - for commit in data['push']['commits']: - if commit['branch'] not in branches_commits: - branch_commits = {'branch': branch_data[commit['branch']], - 'commits': []} - branches_commits[commit['branch']] = branch_commits - - branch_commits = branches_commits[commit['branch']] - branch_commits['commits'].append(commit) + branches_commits = self.aggregate_branch_data( + data['push']['branches'], data['push']['commits']) result = repo_push_template.render( data=data, diff --git a/rhodecode/integrations/types/slack.py b/rhodecode/integrations/types/slack.py --- a/rhodecode/integrations/types/slack.py +++ b/rhodecode/integrations/types/slack.py @@ -28,14 +28,14 @@ import deform import requests import colander from mako.template import Template -from collections import OrderedDict from rhodecode import events from rhodecode.translation import _ from rhodecode.lib import helpers as h from rhodecode.lib.celerylib import run_task, async_task, RequestContextTask from rhodecode.lib.colander_utils import strip_whitespace -from rhodecode.integrations.types.base import IntegrationTypeBase +from rhodecode.integrations.types.base import ( + IntegrationTypeBase, CommitParsingDataHandler) log = logging.getLogger(__name__) @@ -87,7 +87,7 @@ class SlackSettingsSchema(colander.Schem ) -class SlackIntegrationType(IntegrationTypeBase): +class SlackIntegrationType(IntegrationTypeBase, CommitParsingDataHandler): key = 'slack' display_name = _('Slack') description = _('Send events such as repo pushes and pull requests to ' @@ -248,18 +248,9 @@ class SlackIntegrationType(IntegrationTy return title, text def format_repo_push_event(self, data): - branch_data = {branch['name']: branch - for branch in data['push']['branches']} - branches_commits = OrderedDict() - for commit in data['push']['commits']: - if commit['branch'] not in branches_commits: - branch_commits = {'branch': branch_data[commit['branch']], - 'commits': []} - branches_commits[commit['branch']] = branch_commits - - branch_commits = branches_commits[commit['branch']] - branch_commits['commits'].append(commit) + branches_commits = self.aggregate_branch_data( + data['push']['branches'], data['push']['commits']) title = Template(r''' *${data['actor']['username']}* pushed to repo <${data['repo']['url']}|${data['repo']['repo_name']}>: