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