1 | #-*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # Copyright (C) 2007 Ole Trenner, <ole@jayotee.de> |
---|
4 | # All rights reserved. |
---|
5 | # |
---|
6 | # This software is licensed as described in the file COPYING, which |
---|
7 | # you should have received as part of this distribution. |
---|
8 | |
---|
9 | from genshi.template.text import NewTextTemplate |
---|
10 | from trac.core import Component, implements |
---|
11 | from trac.web.chrome import ITemplateProvider, Chrome |
---|
12 | from trac.config import BoolOption |
---|
13 | from trac.notification import NotifyEmail |
---|
14 | from bitten.api import IBuildListener |
---|
15 | from bitten.model import Build, BuildStep, BuildLog, BuildConfig, TargetPlatform |
---|
16 | from bitten.util.repository import get_repos, display_rev |
---|
17 | |
---|
18 | |
---|
19 | class BittenNotify(Component): |
---|
20 | """Sends notifications on build status by mail.""" |
---|
21 | implements(IBuildListener, ITemplateProvider) |
---|
22 | |
---|
23 | notify_on_failure = BoolOption('notification', |
---|
24 | 'notify_on_failed_build', 'true', |
---|
25 | """Notify if bitten build fails.""") |
---|
26 | |
---|
27 | notify_on_success = BoolOption('notification', |
---|
28 | 'notify_on_successful_build', 'false', |
---|
29 | """Notify if bitten build succeeds.""") |
---|
30 | |
---|
31 | def __init__(self): |
---|
32 | self.log.debug('Initializing BittenNotify plugin') |
---|
33 | |
---|
34 | def notify(self, build=None): |
---|
35 | self.log.info('BittenNotify invoked for build %r', build) |
---|
36 | self.log.debug('build status: %s', build.status) |
---|
37 | if not self._should_notify(build): |
---|
38 | return |
---|
39 | self.log.info('Sending notification for build %r', build) |
---|
40 | try: |
---|
41 | email = BuildNotifyEmail(self.env) |
---|
42 | email.notify(build) |
---|
43 | except Exception, e: |
---|
44 | self.log.exception("Failure sending notification for build " |
---|
45 | "%s: %s", build.id, e) |
---|
46 | |
---|
47 | def _should_notify(self, build): |
---|
48 | if build.status == Build.FAILURE: |
---|
49 | return self.notify_on_failure |
---|
50 | elif build.status == Build.SUCCESS: |
---|
51 | return self.notify_on_success |
---|
52 | else: |
---|
53 | return False |
---|
54 | |
---|
55 | # IBuildListener methods |
---|
56 | |
---|
57 | def build_started(self, build): |
---|
58 | """build started""" |
---|
59 | self.notify(build) |
---|
60 | |
---|
61 | def build_aborted(self, build): |
---|
62 | """build aborted""" |
---|
63 | self.notify(build) |
---|
64 | |
---|
65 | def build_completed(self, build): |
---|
66 | """build completed""" |
---|
67 | self.notify(build) |
---|
68 | |
---|
69 | # ITemplateProvider methods |
---|
70 | |
---|
71 | def get_templates_dirs(self): |
---|
72 | """Return a list of directories containing the provided template |
---|
73 | files.""" |
---|
74 | from pkg_resources import resource_filename |
---|
75 | return [resource_filename(__name__, 'templates')] |
---|
76 | |
---|
77 | def get_htdocs_dirs(self): |
---|
78 | """Return the absolute path of a directory containing additional |
---|
79 | static resources (such as images, style sheets, etc).""" |
---|
80 | return [] |
---|
81 | |
---|
82 | |
---|
83 | class BuildNotifyEmail(NotifyEmail): |
---|
84 | """Notification of failed builds.""" |
---|
85 | |
---|
86 | readable_states = { |
---|
87 | Build.SUCCESS: 'Successful', |
---|
88 | Build.FAILURE: 'Failed', |
---|
89 | } |
---|
90 | template_name = 'bitten_notify_email.txt' |
---|
91 | from_email = 'bitten@localhost' |
---|
92 | |
---|
93 | def __init__(self, env): |
---|
94 | NotifyEmail.__init__(self, env) |
---|
95 | # Override the template type to always use NewTextTemplate |
---|
96 | if not isinstance(self.template, NewTextTemplate): |
---|
97 | self.template = Chrome(env).templates.load( |
---|
98 | self.template.filepath, cls=NewTextTemplate) |
---|
99 | |
---|
100 | def notify(self, build): |
---|
101 | self.build = build |
---|
102 | self.data.update(self.template_data()) |
---|
103 | subject = '[%s Build] %s [%s] %s' % (self.readable_states[build.status], |
---|
104 | self.env.project_name, |
---|
105 | self.data['change']['rev'], |
---|
106 | self.build.config) |
---|
107 | NotifyEmail.notify(self, self.build.id, subject) |
---|
108 | |
---|
109 | def get_recipients(self, resid): |
---|
110 | to = [self.get_author()] |
---|
111 | cc = [] |
---|
112 | return (to, cc) |
---|
113 | |
---|
114 | def send(self, torcpts, ccrcpts): |
---|
115 | mime_headers = { |
---|
116 | 'X-Trac-Build-ID': str(self.build.id), |
---|
117 | 'X-Trac-Build-URL': self.build_link(), |
---|
118 | } |
---|
119 | NotifyEmail.send(self, torcpts, ccrcpts, mime_headers) |
---|
120 | |
---|
121 | def build_link(self): |
---|
122 | return self.env.abs_href.build(self.build.config, self.build.id) |
---|
123 | |
---|
124 | def template_data(self): |
---|
125 | failed_steps = BuildStep.select(self.env, build=self.build.id, |
---|
126 | status=BuildStep.FAILURE) |
---|
127 | platform = TargetPlatform.fetch(self.env, id=self.build.platform) |
---|
128 | reposname, repos, change = self.get_change() |
---|
129 | return { |
---|
130 | 'build': { |
---|
131 | 'id': self.build.id, |
---|
132 | 'status': self.readable_states[self.build.status], |
---|
133 | 'link': self.build_link(), |
---|
134 | 'config': self.build.config, |
---|
135 | 'platform': getattr(platform, 'name', 'unknown'), |
---|
136 | 'slave': self.build.slave, |
---|
137 | 'failed_steps': [{ |
---|
138 | 'name': step.name, |
---|
139 | 'description': step.description, |
---|
140 | 'errors': step.errors, |
---|
141 | 'log_messages': self.get_all_log_messages_for_step(step), |
---|
142 | } for step in failed_steps], |
---|
143 | }, |
---|
144 | 'change': { |
---|
145 | 'rev': display_rev(repos, change.rev), |
---|
146 | 'link': self.env.abs_href.changeset(change.rev, |
---|
147 | reposname != '(default)' and reposname or None), |
---|
148 | 'author': change.author, |
---|
149 | }, |
---|
150 | } |
---|
151 | |
---|
152 | def get_all_log_messages_for_step(self, step): |
---|
153 | messages = [] |
---|
154 | for log in BuildLog.select(self.env, build=self.build.id, |
---|
155 | step=step.name): |
---|
156 | messages.extend(log.messages) |
---|
157 | return messages |
---|
158 | |
---|
159 | def get_change(self): |
---|
160 | config_path = BuildConfig.fetch(self.env, name=self.build.config).path |
---|
161 | reposname, repos, _path = get_repos(self.env, config_path, None) |
---|
162 | return reposname, repos, repos.get_changeset(self.build.rev) |
---|
163 | |
---|
164 | def get_author(self): |
---|
165 | reposname, repos, changeset = self.get_change() |
---|
166 | return changeset.author |
---|