##// END OF EJS Templates
integrations: add email integration, fixes #4159
dan -
r640:6c88399f default
parent child Browse files
Show More
@@ -0,0 +1,127 b''
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2012-2016 RhodeCode GmbH
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
21 from __future__ import unicode_literals
22
23 import deform
24 import logging
25 import colander
26
27 from mako.template import Template
28
29 from rhodecode import events
30 from rhodecode.translation import _, lazy_ugettext
31 from rhodecode.lib.celerylib import run_task
32 from rhodecode.lib.celerylib import tasks
33 from rhodecode.integrations.types.base import IntegrationTypeBase
34 from rhodecode.integrations.schema import IntegrationSettingsSchemaBase
35
36
37 log = logging.getLogger()
38
39
40
41 class EmailSettingsSchema(IntegrationSettingsSchemaBase):
42 @colander.instantiate(validator=colander.Length(min=1))
43 class recipients(colander.SequenceSchema):
44 title = lazy_ugettext('Recipients')
45 description = lazy_ugettext('Email addresses to send push events to')
46 widget = deform.widget.SequenceWidget(min_len=1)
47
48 recipient = colander.SchemaNode(
49 colander.String(),
50 title=lazy_ugettext('Email address'),
51 description=lazy_ugettext('Email address'),
52 default='',
53 validator=colander.Email(),
54 widget=deform.widget.TextInputWidget(
55 placeholder='user@domain.com',
56 ),
57 )
58
59
60 class EmailIntegrationType(IntegrationTypeBase):
61 key = 'email'
62 display_name = lazy_ugettext('Email')
63 SettingsSchema = EmailSettingsSchema
64
65 def settings_schema(self):
66 schema = EmailSettingsSchema()
67 return schema
68
69 def send_event(self, event):
70 data = event.as_dict()
71 log.debug('got event: %r', event)
72
73 if isinstance(event, events.RepoPushEvent):
74 repo_push_handler(data, self.settings)
75 else:
76 log.debug('ignoring event: %r', event)
77
78
79 def repo_push_handler(data, settings):
80 for commit in data['push']['commits']:
81 email_body_plaintext = repo_push_template_plaintext.render(
82 data=data,
83 commit=commit,
84 commit_msg=commit['message'],
85 )
86 email_body_html = repo_push_template_html.render(
87 data=data,
88 commit=commit,
89 commit_msg=commit['message_html'],
90 )
91
92 subject = '[%(repo_name)s] %(commit_id)s: %(commit_msg)s' % {
93 'repo_name': data['repo']['repo_name'],
94 'commit_id': commit['short_id'],
95 'commit_msg': commit['message'].split('\n')[0][:150]
96 }
97 for email_address in settings['recipients']:
98 task = run_task(
99 tasks.send_email, email_address, subject,
100 email_body_plaintext, email_body_html)
101
102
103 # TODO: dan: add changed files, make html pretty
104 repo_push_template_plaintext = Template('''
105 User: ${data['actor']['username']}
106 Branches: ${', '.join(branch['name'] for branch in data['push']['branches'])}
107 Repository: ${data['repo']['url']}
108 Commit: ${commit['raw_id']}
109 URL: ${commit['url']}
110 Author: ${commit['author']}
111 Date: ${commit['date']}
112 Commit Message:
113
114 ${commit_msg}
115 ''')
116
117 repo_push_template_html = Template('''
118 User: ${data['actor']['username']}<br>
119 Branches: ${', '.join(branch['name'] for branch in data['push']['branches'])}<br>
120 Repository: ${data['repo']['url']}<br>
121 Commit: ${commit['raw_id']}<br>
122 URL: ${commit['url']}<br>
123 Author: ${commit['author']}<br>
124 Date: ${commit['date']}<br>
125 Commit Message:<br>
126 <p>${commit_msg}</p>
127 ''')
@@ -21,7 +21,7 b''
21 21 import logging
22 22
23 23 from rhodecode.integrations.registry import IntegrationTypeRegistry
24 from rhodecode.integrations.types import webhook, slack, hipchat
24 from rhodecode.integrations.types import webhook, slack, hipchat, email
25 25
26 26 log = logging.getLogger(__name__)
27 27
@@ -37,6 +37,8 b' integration_type_registry.register_integ'
37 37 slack.SlackIntegrationType)
38 38 integration_type_registry.register_integration_type(
39 39 hipchat.HipchatIntegrationType)
40 integration_type_registry.register_integration_type(
41 email.EmailIntegrationType)
40 42
41 43
42 44 def integrations_event_handler(event):
@@ -70,7 +70,7 b' def send_email(recipients, subject, body'
70 70 """
71 71 log = get_logger(send_email)
72 72
73 email_config = email_config or config
73 email_config = email_config or rhodecode.CONFIG
74 74 subject = "%s %s" % (email_config.get('email_prefix', ''), subject)
75 75 if not recipients:
76 76 # if recipients are not defined we send to email_config + all admins
@@ -13,7 +13,7 b''
13 13 repeat="msg field.error.messages()">
14 14 <p tal:condition="msg"
15 15 id="${errstr if repeat.msg.index==0 else '%s-%s' % (errstr, repeat.msg.index)}"
16 class="${error_class} help-block"
16 class="${error_class} help-block error-block"
17 17 i18n:translate="">${msg}</p>
18 18 </tal:errors>
19 19 </div>
General Comments 0
You need to be logged in to leave comments. Login now