##// END OF EJS Templates
integrations: use a common logic for parsing the commits branches inside the integrations that require it....
dan -
r2644:97776fe4 default
parent child Browse files
Show More
@@ -150,7 +150,34 b' WEBHOOK_URL_VARS = ['
150 CI_URL_VARS = WEBHOOK_URL_VARS
150 CI_URL_VARS = WEBHOOK_URL_VARS
151
151
152
152
153 class WebhookDataHandler(object):
153 class CommitParsingDataHandler(object):
154
155 def aggregate_branch_data(self, branches, commits):
156 branch_data = collections.OrderedDict()
157 for obj in branches:
158 branch_data[obj['name']] = obj
159
160 branches_commits = collections.OrderedDict()
161 for commit in commits:
162 if commit.get('git_ref_change'):
163 # special case for GIT that allows creating tags,
164 # deleting branches without associated commit
165 continue
166 commit_branch = commit['branch']
167
168 if commit_branch not in branches_commits:
169 _branch = branch_data[commit_branch] \
170 if commit_branch else commit_branch
171 branch_commits = {'branch': _branch,
172 'commits': []}
173 branches_commits[commit_branch] = branch_commits
174
175 branch_commits = branches_commits[commit_branch]
176 branch_commits['commits'].append(commit)
177 return branches_commits
178
179
180 class WebhookDataHandler(CommitParsingDataHandler):
154 name = 'webhook'
181 name = 'webhook'
155
182
156 def __init__(self, template_url, headers):
183 def __init__(self, template_url, headers):
@@ -184,25 +211,9 b' class WebhookDataHandler(object):'
184 def repo_push_event_handler(self, event, data):
211 def repo_push_event_handler(self, event, data):
185 url = self.get_base_parsed_template(data)
212 url = self.get_base_parsed_template(data)
186 url_cals = []
213 url_cals = []
187 branch_data = collections.OrderedDict()
188 for obj in data['push']['branches']:
189 branch_data[obj['name']] = obj
190
214
191 branches_commits = collections.OrderedDict()
215 branches_commits = self.aggregate_branch_data(
192 for commit in data['push']['commits']:
216 data['push']['branches'], data['push']['commits'])
193 if commit.get('git_ref_change'):
194 # special case for GIT that allows creating tags,
195 # deleting branches without associated commit
196 continue
197
198 if commit['branch'] not in branches_commits:
199 branch_commits = {'branch': branch_data[commit['branch']],
200 'commits': []}
201 branches_commits[commit['branch']] = branch_commits
202
203 branch_commits = branches_commits[commit['branch']]
204 branch_commits['commits'].append(commit)
205
206 if '${branch}' in url:
217 if '${branch}' in url:
207 # call it multiple times, for each branch if used in variables
218 # call it multiple times, for each branch if used in variables
208 for branch, commit_ids in branches_commits.items():
219 for branch, commit_ids in branches_commits.items():
@@ -24,14 +24,14 b' import logging'
24 import requests
24 import requests
25 import colander
25 import colander
26 import textwrap
26 import textwrap
27 from collections import OrderedDict
28 from mako.template import Template
27 from mako.template import Template
29 from rhodecode import events
28 from rhodecode import events
30 from rhodecode.translation import _
29 from rhodecode.translation import _
31 from rhodecode.lib import helpers as h
30 from rhodecode.lib import helpers as h
32 from rhodecode.lib.celerylib import run_task, async_task, RequestContextTask
31 from rhodecode.lib.celerylib import run_task, async_task, RequestContextTask
33 from rhodecode.lib.colander_utils import strip_whitespace
32 from rhodecode.lib.colander_utils import strip_whitespace
34 from rhodecode.integrations.types.base import IntegrationTypeBase
33 from rhodecode.integrations.types.base import (
34 IntegrationTypeBase, CommitParsingDataHandler)
35
35
36 log = logging.getLogger(__name__)
36 log = logging.getLogger(__name__)
37
37
@@ -92,7 +92,7 b" repo_push_template = Template('''"
92 ''')
92 ''')
93
93
94
94
95 class HipchatIntegrationType(IntegrationTypeBase):
95 class HipchatIntegrationType(IntegrationTypeBase, CommitParsingDataHandler):
96 key = 'hipchat'
96 key = 'hipchat'
97 display_name = _('Hipchat')
97 display_name = _('Hipchat')
98 description = _('Send events such as repo pushes and pull requests to '
98 description = _('Send events such as repo pushes and pull requests to '
@@ -217,18 +217,8 b' class HipchatIntegrationType(Integration'
217 )
217 )
218
218
219 def format_repo_push_event(self, data):
219 def format_repo_push_event(self, data):
220 branch_data = {branch['name']: branch
220 branches_commits = self.aggregate_branch_data(
221 for branch in data['push']['branches']}
221 data['push']['branches'], data['push']['commits'])
222
223 branches_commits = OrderedDict()
224 for commit in data['push']['commits']:
225 if commit['branch'] not in branches_commits:
226 branch_commits = {'branch': branch_data[commit['branch']],
227 'commits': []}
228 branches_commits[commit['branch']] = branch_commits
229
230 branch_commits = branches_commits[commit['branch']]
231 branch_commits['commits'].append(commit)
232
222
233 result = repo_push_template.render(
223 result = repo_push_template.render(
234 data=data,
224 data=data,
@@ -28,14 +28,14 b' import deform'
28 import requests
28 import requests
29 import colander
29 import colander
30 from mako.template import Template
30 from mako.template import Template
31 from collections import OrderedDict
32
31
33 from rhodecode import events
32 from rhodecode import events
34 from rhodecode.translation import _
33 from rhodecode.translation import _
35 from rhodecode.lib import helpers as h
34 from rhodecode.lib import helpers as h
36 from rhodecode.lib.celerylib import run_task, async_task, RequestContextTask
35 from rhodecode.lib.celerylib import run_task, async_task, RequestContextTask
37 from rhodecode.lib.colander_utils import strip_whitespace
36 from rhodecode.lib.colander_utils import strip_whitespace
38 from rhodecode.integrations.types.base import IntegrationTypeBase
37 from rhodecode.integrations.types.base import (
38 IntegrationTypeBase, CommitParsingDataHandler)
39
39
40 log = logging.getLogger(__name__)
40 log = logging.getLogger(__name__)
41
41
@@ -87,7 +87,7 b' class SlackSettingsSchema(colander.Schem'
87 )
87 )
88
88
89
89
90 class SlackIntegrationType(IntegrationTypeBase):
90 class SlackIntegrationType(IntegrationTypeBase, CommitParsingDataHandler):
91 key = 'slack'
91 key = 'slack'
92 display_name = _('Slack')
92 display_name = _('Slack')
93 description = _('Send events such as repo pushes and pull requests to '
93 description = _('Send events such as repo pushes and pull requests to '
@@ -248,18 +248,9 b' class SlackIntegrationType(IntegrationTy'
248 return title, text
248 return title, text
249
249
250 def format_repo_push_event(self, data):
250 def format_repo_push_event(self, data):
251 branch_data = {branch['name']: branch
252 for branch in data['push']['branches']}
253
251
254 branches_commits = OrderedDict()
252 branches_commits = self.aggregate_branch_data(
255 for commit in data['push']['commits']:
253 data['push']['branches'], data['push']['commits'])
256 if commit['branch'] not in branches_commits:
257 branch_commits = {'branch': branch_data[commit['branch']],
258 'commits': []}
259 branches_commits[commit['branch']] = branch_commits
260
261 branch_commits = branches_commits[commit['branch']]
262 branch_commits['commits'].append(commit)
263
254
264 title = Template(r'''
255 title = Template(r'''
265 *${data['actor']['username']}* pushed to repo <${data['repo']['url']}|${data['repo']['repo_name']}>:
256 *${data['actor']['username']}* pushed to repo <${data['repo']['url']}|${data['repo']['repo_name']}>:
General Comments 0
You need to be logged in to leave comments. Login now