Show More
@@ -1,264 +1,264 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2012-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | from __future__ import unicode_literals |
|
22 | 22 | import deform |
|
23 | 23 | import re |
|
24 | 24 | import logging |
|
25 | 25 | import requests |
|
26 | 26 | import colander |
|
27 | 27 | import textwrap |
|
28 | 28 | from celery.task import task |
|
29 | 29 | from mako.template import Template |
|
30 | 30 | |
|
31 | 31 | from rhodecode import events |
|
32 | 32 | from rhodecode.translation import _ |
|
33 | 33 | from rhodecode.lib import helpers as h |
|
34 | 34 | from rhodecode.lib.celerylib import run_task |
|
35 | 35 | from rhodecode.lib.colander_utils import strip_whitespace |
|
36 | 36 | from rhodecode.integrations.types.base import IntegrationTypeBase |
|
37 | 37 | |
|
38 | 38 | log = logging.getLogger(__name__) |
|
39 | 39 | |
|
40 | 40 | |
|
41 | 41 | class SlackSettingsSchema(colander.Schema): |
|
42 | 42 | service = colander.SchemaNode( |
|
43 | 43 | colander.String(), |
|
44 | 44 | title=_('Slack service URL'), |
|
45 | 45 | description=h.literal(_( |
|
46 | 46 | 'This can be setup at the ' |
|
47 | 47 | '<a href="https://my.slack.com/services/new/incoming-webhook/">' |
|
48 | 48 | 'slack app manager</a>')), |
|
49 | 49 | default='', |
|
50 | 50 | preparer=strip_whitespace, |
|
51 | 51 | validator=colander.url, |
|
52 | 52 | widget=deform.widget.TextInputWidget( |
|
53 | 53 | placeholder='https://hooks.slack.com/services/...', |
|
54 | 54 | ), |
|
55 | 55 | ) |
|
56 | 56 | username = colander.SchemaNode( |
|
57 | 57 | colander.String(), |
|
58 | 58 | title=_('Username'), |
|
59 | 59 | description=_('Username to show notifications coming from.'), |
|
60 | 60 | missing='Rhodecode', |
|
61 | 61 | preparer=strip_whitespace, |
|
62 | 62 | widget=deform.widget.TextInputWidget( |
|
63 | 63 | placeholder='Rhodecode' |
|
64 | 64 | ), |
|
65 | 65 | ) |
|
66 | 66 | channel = colander.SchemaNode( |
|
67 | 67 | colander.String(), |
|
68 | 68 | title=_('Channel'), |
|
69 | 69 | description=_('Channel to send notifications to.'), |
|
70 | 70 | missing='', |
|
71 | 71 | preparer=strip_whitespace, |
|
72 | 72 | widget=deform.widget.TextInputWidget( |
|
73 | 73 | placeholder='#general' |
|
74 | 74 | ), |
|
75 | 75 | ) |
|
76 | 76 | icon_emoji = colander.SchemaNode( |
|
77 | 77 | colander.String(), |
|
78 | 78 | title=_('Emoji'), |
|
79 | 79 | description=_('Emoji to use eg. :studio_microphone:'), |
|
80 | 80 | missing='', |
|
81 | 81 | preparer=strip_whitespace, |
|
82 | 82 | widget=deform.widget.TextInputWidget( |
|
83 | 83 | placeholder=':studio_microphone:' |
|
84 | 84 | ), |
|
85 | 85 | ) |
|
86 | 86 | |
|
87 | 87 | |
|
88 | 88 | repo_push_template = Template(r''' |
|
89 | 89 | *${data['actor']['username']}* pushed to repo <${data['repo']['url']}|${data['repo']['repo_name']}>: |
|
90 | 90 | %for branch, branch_commits in branches_commits.items(): |
|
91 | 91 | branch: <${branch_commits['branch']['url']}|${branch_commits['branch']['name']}> |
|
92 | 92 | %for commit in branch_commits['commits']: |
|
93 | 93 | > <${commit['url']}|${commit['short_id']}> - ${commit['message_html']|html_to_slack_links} |
|
94 | 94 | %endfor |
|
95 | 95 | %endfor |
|
96 | 96 | ''') |
|
97 | 97 | |
|
98 | 98 | |
|
99 | 99 | class SlackIntegrationType(IntegrationTypeBase): |
|
100 | 100 | key = 'slack' |
|
101 | 101 | display_name = _('Slack') |
|
102 | 102 | description = _('Send events such as repo pushes and pull requests to ' |
|
103 | 103 | 'your slack channel.') |
|
104 | 104 | icon = '''<?xml version="1.0" encoding="UTF-8" standalone="no"?><svg viewBox="0 0 256 256" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid"><g><path d="M165.963541,15.8384262 C162.07318,3.86308197 149.212328,-2.69009836 137.239082,1.20236066 C125.263738,5.09272131 118.710557,17.9535738 122.603016,29.9268197 L181.550164,211.292328 C185.597902,222.478689 197.682361,228.765377 209.282098,225.426885 C221.381246,221.943607 228.756984,209.093246 224.896,197.21023 C224.749115,196.756984 165.963541,15.8384262 165.963541,15.8384262" fill="#DFA22F"></path><path d="M74.6260984,45.515541 C70.7336393,33.5422951 57.8727869,26.9891148 45.899541,30.8794754 C33.9241967,34.7698361 27.3710164,47.6306885 31.2634754,59.6060328 L90.210623,240.971541 C94.2583607,252.157902 106.34282,258.44459 117.942557,255.104 C130.041705,251.62282 137.417443,238.772459 133.556459,226.887344 C133.409574,226.436197 74.6260984,45.515541 74.6260984,45.515541" fill="#3CB187"></path><path d="M240.161574,166.045377 C252.136918,162.155016 258.688,149.294164 254.797639,137.31882 C250.907279,125.345574 238.046426,118.792393 226.07318,122.682754 L44.7076721,181.632 C33.5213115,185.677639 27.234623,197.762098 30.5731148,209.361836 C34.0563934,221.460984 46.9067541,228.836721 58.7897705,224.975738 C59.2430164,224.828852 240.161574,166.045377 240.161574,166.045377" fill="#CE1E5B"></path><path d="M82.507541,217.270557 C94.312918,213.434754 109.528131,208.491016 125.855475,203.186361 C122.019672,191.380984 117.075934,176.163672 111.76918,159.83423 L68.4191475,173.924721 L82.507541,217.270557" fill="#392538"></path><path d="M173.847082,187.591344 C190.235279,182.267803 205.467279,177.31777 217.195016,173.507148 C213.359213,161.70177 208.413377,146.480262 203.106623,130.146623 L159.75659,144.237115 L173.847082,187.591344" fill="#BB242A"></path><path d="M210.484459,74.7058361 C222.457705,70.8154754 229.010885,57.954623 225.120525,45.9792787 C221.230164,34.0060328 208.369311,27.4528525 196.393967,31.3432131 L15.028459,90.292459 C3.84209836,94.3380984 -2.44459016,106.422557 0.896,118.022295 C4.37718033,130.121443 17.227541,137.49718 29.1126557,133.636197 C29.5638033,133.489311 210.484459,74.7058361 210.484459,74.7058361" fill="#72C5CD"></path><path d="M52.8220328,125.933115 C64.6274098,122.097311 79.8468197,117.151475 96.1762623,111.84682 C90.8527213,95.4565246 85.9026885,80.2245246 82.0920656,68.4946885 L38.731541,82.5872787 L52.8220328,125.933115" fill="#248C73"></path><path d="M144.159475,96.256 C160.551869,90.9303607 175.785967,85.9803279 187.515803,82.1676066 C182.190164,65.7752131 177.240131,50.5390164 173.42741,38.807082 L130.068984,52.8996721 L144.159475,96.256" fill="#62803A"></path></g></svg>''' |
|
105 | 105 | valid_events = [ |
|
106 | 106 | events.PullRequestCloseEvent, |
|
107 | 107 | events.PullRequestMergeEvent, |
|
108 | 108 | events.PullRequestUpdateEvent, |
|
109 | 109 | events.PullRequestCommentEvent, |
|
110 | 110 | events.PullRequestReviewEvent, |
|
111 | 111 | events.PullRequestCreateEvent, |
|
112 | 112 | events.RepoPushEvent, |
|
113 | 113 | events.RepoCreateEvent, |
|
114 | 114 | ] |
|
115 | 115 | |
|
116 | 116 | def send_event(self, event): |
|
117 | 117 | if event.__class__ not in self.valid_events: |
|
118 | 118 | log.debug('event not valid: %r' % event) |
|
119 | 119 | return |
|
120 | 120 | |
|
121 | 121 | if event.name not in self.settings['events']: |
|
122 | 122 | log.debug('event ignored: %r' % event) |
|
123 | 123 | return |
|
124 | 124 | |
|
125 | 125 | data = event.as_dict() |
|
126 | 126 | |
|
127 | 127 | text = '*%s* caused a *%s* event' % ( |
|
128 | 128 | data['actor']['username'], event.name) |
|
129 | 129 | |
|
130 | 130 | log.debug('handling slack event for %s' % event.name) |
|
131 | 131 | |
|
132 | 132 | if isinstance(event, events.PullRequestCommentEvent): |
|
133 | 133 | text = self.format_pull_request_comment_event(event, data) |
|
134 | 134 | elif isinstance(event, events.PullRequestReviewEvent): |
|
135 | 135 | text = self.format_pull_request_review_event(event, data) |
|
136 | 136 | elif isinstance(event, events.PullRequestEvent): |
|
137 | 137 | text = self.format_pull_request_event(event, data) |
|
138 | 138 | elif isinstance(event, events.RepoPushEvent): |
|
139 | 139 | text = self.format_repo_push_event(data) |
|
140 | 140 | elif isinstance(event, events.RepoCreateEvent): |
|
141 | 141 | text = self.format_repo_create_event(data) |
|
142 | 142 | else: |
|
143 | 143 | log.error('unhandled event type: %r' % event) |
|
144 | 144 | |
|
145 | 145 | run_task(post_text_to_slack, self.settings, text) |
|
146 | 146 | |
|
147 | 147 | def settings_schema(self): |
|
148 | 148 | schema = SlackSettingsSchema() |
|
149 | 149 | schema.add(colander.SchemaNode( |
|
150 | 150 | colander.Set(), |
|
151 | 151 | widget=deform.widget.CheckboxChoiceWidget( |
|
152 | 152 | values=sorted( |
|
153 | 153 | [(e.name, e.display_name) for e in self.valid_events] |
|
154 | 154 | ) |
|
155 | 155 | ), |
|
156 | 156 | description="Events activated for this integration", |
|
157 | 157 | name='events' |
|
158 | 158 | )) |
|
159 | 159 | |
|
160 | 160 | return schema |
|
161 | 161 | |
|
162 | 162 | def format_pull_request_comment_event(self, event, data): |
|
163 | 163 | comment_text = data['comment']['text'] |
|
164 | 164 | if len(comment_text) > 200: |
|
165 | 165 | comment_text = '<{comment_url}|{comment_text}...>'.format( |
|
166 | 166 | comment_text=comment_text[:200], |
|
167 | 167 | comment_url=data['comment']['url'], |
|
168 | 168 | ) |
|
169 | 169 | |
|
170 | 170 | comment_status = '' |
|
171 | 171 | if data['comment']['status']: |
|
172 | 172 | comment_status = '[{}]: '.format(data['comment']['status']) |
|
173 | 173 | |
|
174 | 174 | return (textwrap.dedent( |
|
175 | 175 | ''' |
|
176 | {user} commented on pull request <{pr_url}|#{number}> - {pr_title}: | |
|
176 | *{user}* commented on pull request <{pr_url}|#{number}> - {pr_title}: | |
|
177 | 177 | >>> {comment_status}{comment_text} |
|
178 | 178 | ''').format( |
|
179 | 179 | comment_status=comment_status, |
|
180 | 180 | user=data['actor']['username'], |
|
181 | 181 | number=data['pullrequest']['pull_request_id'], |
|
182 | 182 | pr_url=data['pullrequest']['url'], |
|
183 | 183 | pr_status=data['pullrequest']['status'], |
|
184 | 184 | pr_title=data['pullrequest']['title'], |
|
185 | 185 | comment_text=comment_text |
|
186 | 186 | ) |
|
187 | 187 | ) |
|
188 | 188 | |
|
189 | 189 | def format_pull_request_review_event(self, event, data): |
|
190 | 190 | return (textwrap.dedent( |
|
191 | 191 | ''' |
|
192 | 192 | Status changed to {pr_status} for pull request <{pr_url}|#{number}> - {pr_title} |
|
193 | 193 | ''').format( |
|
194 | 194 | user=data['actor']['username'], |
|
195 | 195 | number=data['pullrequest']['pull_request_id'], |
|
196 | 196 | pr_url=data['pullrequest']['url'], |
|
197 | 197 | pr_status=data['pullrequest']['status'], |
|
198 | 198 | pr_title=data['pullrequest']['title'], |
|
199 | 199 | ) |
|
200 | 200 | ) |
|
201 | 201 | |
|
202 | 202 | def format_pull_request_event(self, event, data): |
|
203 | 203 | action = { |
|
204 | 204 | events.PullRequestCloseEvent: 'closed', |
|
205 | 205 | events.PullRequestMergeEvent: 'merged', |
|
206 | 206 | events.PullRequestUpdateEvent: 'updated', |
|
207 | 207 | events.PullRequestCreateEvent: 'created', |
|
208 | 208 | }.get(event.__class__, str(event.__class__)) |
|
209 | 209 | |
|
210 | 210 | return ('Pull request <{url}|#{number}> - {title} ' |
|
211 | '{action} by {user}').format( | |
|
211 | '`{action}` by *{user}*').format( | |
|
212 | 212 | user=data['actor']['username'], |
|
213 | 213 | number=data['pullrequest']['pull_request_id'], |
|
214 | 214 | url=data['pullrequest']['url'], |
|
215 | 215 | title=data['pullrequest']['title'], |
|
216 | 216 | action=action |
|
217 | 217 | ) |
|
218 | 218 | |
|
219 | 219 | def format_repo_push_event(self, data): |
|
220 | 220 | branch_data = {branch['name']: branch |
|
221 | 221 | for branch in data['push']['branches']} |
|
222 | 222 | |
|
223 | 223 | branches_commits = {} |
|
224 | 224 | for commit in data['push']['commits']: |
|
225 | 225 | log.critical(commit) |
|
226 | 226 | if commit['branch'] not in branches_commits: |
|
227 | 227 | branch_commits = {'branch': branch_data[commit['branch']], |
|
228 | 228 | 'commits': []} |
|
229 | 229 | branches_commits[commit['branch']] = branch_commits |
|
230 | 230 | |
|
231 | 231 | branch_commits = branches_commits[commit['branch']] |
|
232 | 232 | branch_commits['commits'].append(commit) |
|
233 | 233 | |
|
234 | 234 | result = repo_push_template.render( |
|
235 | 235 | data=data, |
|
236 | 236 | branches_commits=branches_commits, |
|
237 | 237 | html_to_slack_links=html_to_slack_links, |
|
238 | 238 | ) |
|
239 | 239 | return result |
|
240 | 240 | |
|
241 | 241 | def format_repo_create_event(self, data): |
|
242 | 242 | return '<{}|{}> ({}) repository created by *{}*'.format( |
|
243 | 243 | data['repo']['url'], |
|
244 | 244 | data['repo']['repo_name'], |
|
245 | 245 | data['repo']['repo_type'], |
|
246 | 246 | data['actor']['username'], |
|
247 | 247 | ) |
|
248 | 248 | |
|
249 | 249 | |
|
250 | 250 | def html_to_slack_links(message): |
|
251 | 251 | return re.compile(r'<a .*?href=["\'](.+?)".*?>(.+?)</a>').sub( |
|
252 | 252 | r'<\1|\2>', message) |
|
253 | 253 | |
|
254 | 254 | |
|
255 | 255 | @task(ignore_result=True) |
|
256 | 256 | def post_text_to_slack(settings, text): |
|
257 | 257 | log.debug('sending %s to slack %s' % (text, settings['service'])) |
|
258 | 258 | resp = requests.post(settings['service'], json={ |
|
259 | 259 | "channel": settings.get('channel', ''), |
|
260 | 260 | "username": settings.get('username', 'Rhodecode'), |
|
261 | 261 | "text": text, |
|
262 | 262 | "icon_emoji": settings.get('icon_emoji', ':studio_microphone:') |
|
263 | 263 | }) |
|
264 | 264 | resp.raise_for_status() # raise exception on a failed request |
General Comments 0
You need to be logged in to leave comments.
Login now