annotate bitten/notify.py @ 875:a68027e2245d

Add 'Platform' name to build report for web display and notifications. Fixes #541 and #633.
author osimons
date Thu, 21 Oct 2010 08:41:16 +0000
parents a4676056c8d3
children
rev   line source
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
1 #-*- coding: utf-8 -*-
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
2 #
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
3 # Copyright (C) 2007 Ole Trenner, <ole@jayotee.de>
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
4 # All rights reserved.
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
5 #
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
6 # This software is licensed as described in the file COPYING, which
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
7 # you should have received as part of this distribution.
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
8
824
a4676056c8d3 Changed notification to always use Genshi `NewTextTemplate` which lets us keep just one template for both Trac 0.11 and 0.12+.
osimons
parents: 804
diff changeset
9 from genshi.template.text import NewTextTemplate
625
98675686ec4d notify.py import cleanup and remove config name constants
mgood
parents: 624
diff changeset
10 from trac.core import Component, implements
824
a4676056c8d3 Changed notification to always use Genshi `NewTextTemplate` which lets us keep just one template for both Trac 0.11 and 0.12+.
osimons
parents: 804
diff changeset
11 from trac.web.chrome import ITemplateProvider, Chrome
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
12 from trac.config import BoolOption
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
13 from trac.notification import NotifyEmail
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
14 from bitten.api import IBuildListener
875
a68027e2245d Add 'Platform' name to build report for web display and notifications. Fixes #541 and #633.
osimons
parents: 824
diff changeset
15 from bitten.model import Build, BuildStep, BuildLog, TargetPlatform
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
16
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
17
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
18 class BittenNotify(Component):
534
79dd34e914a7 Merge `BittenNotify` and `BittenNotifyDispatcher` into one component
mgood
parents: 533
diff changeset
19 """Sends notifications on build status by mail."""
79dd34e914a7 Merge `BittenNotify` and `BittenNotifyDispatcher` into one component
mgood
parents: 533
diff changeset
20 implements(IBuildListener, ITemplateProvider)
79dd34e914a7 Merge `BittenNotify` and `BittenNotifyDispatcher` into one component
mgood
parents: 533
diff changeset
21
625
98675686ec4d notify.py import cleanup and remove config name constants
mgood
parents: 624
diff changeset
22 notify_on_failure = BoolOption('notification',
98675686ec4d notify.py import cleanup and remove config name constants
mgood
parents: 624
diff changeset
23 'notify_on_failed_build', 'true',
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
24 """Notify if bitten build fails.""")
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
25
625
98675686ec4d notify.py import cleanup and remove config name constants
mgood
parents: 624
diff changeset
26 notify_on_success = BoolOption('notification',
98675686ec4d notify.py import cleanup and remove config name constants
mgood
parents: 624
diff changeset
27 'notify_on_successful_build', 'false',
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
28 """Notify if bitten build succeeds.""")
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
29
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
30 def __init__(self):
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
31 self.log.debug('Initializing BittenNotify plugin')
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
32
541
2479e2a75afa `BittenNotifyEmail` objects have state, so the same instance cannot be shared
mgood
parents: 540
diff changeset
33 def notify(self, build=None):
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
34 self.log.info('BittenNotify invoked for build %r', build)
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
35 self.log.debug('build status: %s', build.status)
541
2479e2a75afa `BittenNotifyEmail` objects have state, so the same instance cannot be shared
mgood
parents: 540
diff changeset
36 if not self._should_notify(build):
2479e2a75afa `BittenNotifyEmail` objects have state, so the same instance cannot be shared
mgood
parents: 540
diff changeset
37 return
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
38 self.log.info('Sending notification for build %r', build)
541
2479e2a75afa `BittenNotifyEmail` objects have state, so the same instance cannot be shared
mgood
parents: 540
diff changeset
39 try:
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
40 email = BuildNotifyEmail(self.env)
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
41 email.notify(build)
541
2479e2a75afa `BittenNotifyEmail` objects have state, so the same instance cannot be shared
mgood
parents: 540
diff changeset
42 except Exception, e:
2479e2a75afa `BittenNotifyEmail` objects have state, so the same instance cannot be shared
mgood
parents: 540
diff changeset
43 self.log.exception("Failure sending notification for build "
2479e2a75afa `BittenNotifyEmail` objects have state, so the same instance cannot be shared
mgood
parents: 540
diff changeset
44 "%s: %s", build.id, e)
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
45
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
46 def _should_notify(self, build):
541
2479e2a75afa `BittenNotifyEmail` objects have state, so the same instance cannot be shared
mgood
parents: 540
diff changeset
47 if build.status == Build.FAILURE:
2479e2a75afa `BittenNotifyEmail` objects have state, so the same instance cannot be shared
mgood
parents: 540
diff changeset
48 return self.notify_on_failure
2479e2a75afa `BittenNotifyEmail` objects have state, so the same instance cannot be shared
mgood
parents: 540
diff changeset
49 elif build.status == Build.SUCCESS:
2479e2a75afa `BittenNotifyEmail` objects have state, so the same instance cannot be shared
mgood
parents: 540
diff changeset
50 return self.notify_on_success
2479e2a75afa `BittenNotifyEmail` objects have state, so the same instance cannot be shared
mgood
parents: 540
diff changeset
51 else:
2479e2a75afa `BittenNotifyEmail` objects have state, so the same instance cannot be shared
mgood
parents: 540
diff changeset
52 return False
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
53
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
54 # IBuildListener methods
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
55
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
56 def build_started(self, build):
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
57 """build started"""
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
58 self.notify(build)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
59
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
60 def build_aborted(self, build):
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
61 """build aborted"""
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
62 self.notify(build)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
63
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
64 def build_completed(self, build):
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
65 """build completed"""
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
66 self.notify(build)
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
67
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
68 # ITemplateProvider methods
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
69
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
70 def get_templates_dirs(self):
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
71 """Return a list of directories containing the provided template
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
72 files."""
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
73 from pkg_resources import resource_filename
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
74 return [resource_filename(__name__, 'templates')]
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
75
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
76 def get_htdocs_dirs(self):
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
77 """Return the absolute path of a directory containing additional
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
78 static resources (such as images, style sheets, etc)."""
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
79 return []
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
80
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
81
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
82 class BuildNotifyEmail(NotifyEmail):
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
83 """Notification of failed builds."""
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
84
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
85 readable_states = {
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
86 Build.SUCCESS: 'Successful',
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
87 Build.FAILURE: 'Failed',
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
88 }
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
89 template_name = 'bitten_notify_email.txt'
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
90 from_email = 'bitten@localhost'
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
91
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
92 def __init__(self, env):
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
93 NotifyEmail.__init__(self, env)
824
a4676056c8d3 Changed notification to always use Genshi `NewTextTemplate` which lets us keep just one template for both Trac 0.11 and 0.12+.
osimons
parents: 804
diff changeset
94 # Override the template type to always use NewTextTemplate
a4676056c8d3 Changed notification to always use Genshi `NewTextTemplate` which lets us keep just one template for both Trac 0.11 and 0.12+.
osimons
parents: 804
diff changeset
95 if not isinstance(self.template, NewTextTemplate):
a4676056c8d3 Changed notification to always use Genshi `NewTextTemplate` which lets us keep just one template for both Trac 0.11 and 0.12+.
osimons
parents: 804
diff changeset
96 self.template = Chrome(env).templates.load(
a4676056c8d3 Changed notification to always use Genshi `NewTextTemplate` which lets us keep just one template for both Trac 0.11 and 0.12+.
osimons
parents: 804
diff changeset
97 self.template.filepath, cls=NewTextTemplate)
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
98
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
99 def notify(self, build):
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
100 self.build = build
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
101 self.data.update(self.template_data())
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
102 subject = '[%s Build] %s [%s] %s' % (self.readable_states[build.status],
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
103 self.env.project_name,
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
104 self.build.rev,
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
105 self.build.config)
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
106 NotifyEmail.notify(self, self.build.id, subject)
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
107
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
108 def get_recipients(self, resid):
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
109 to = [self.get_author()]
624
cc2383034358 Remove user->email lookup from since this is already handled in the parent class
mgood
parents: 541
diff changeset
110 cc = []
cc2383034358 Remove user->email lookup from since this is already handled in the parent class
mgood
parents: 541
diff changeset
111 return (to, cc)
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
112
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
113 def send(self, torcpts, ccrcpts):
537
b72243e52317 Some style clean-ups to notify.py
mgood
parents: 535
diff changeset
114 mime_headers = {
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
115 'X-Trac-Build-ID': str(self.build.id),
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
116 'X-Trac-Build-URL': self.build_link(),
537
b72243e52317 Some style clean-ups to notify.py
mgood
parents: 535
diff changeset
117 }
533
7c1919719538 Fix line endings and trailing whitespace in new notification files
mgood
parents: 531
diff changeset
118 NotifyEmail.send(self, torcpts, ccrcpts, mime_headers)
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
119
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
120 def build_link(self):
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
121 return self.env.abs_href.build(self.build.config, self.build.id)
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
122
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
123 def template_data(self):
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
124 failed_steps = BuildStep.select(self.env, build=self.build.id,
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
125 status=BuildStep.FAILURE)
875
a68027e2245d Add 'Platform' name to build report for web display and notifications. Fixes #541 and #633.
osimons
parents: 824
diff changeset
126 platform = TargetPlatform.fetch(self.env, id=self.build.platform)
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
127 change = self.get_changeset()
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
128 return {
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
129 'build': {
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
130 'id': self.build.id,
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
131 'status': self.readable_states[self.build.status],
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
132 'link': self.build_link(),
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
133 'config': self.build.config,
875
a68027e2245d Add 'Platform' name to build report for web display and notifications. Fixes #541 and #633.
osimons
parents: 824
diff changeset
134 'platform': getattr(platform, 'name', 'unknown'),
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
135 'slave': self.build.slave,
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
136 'failed_steps': [{
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
137 'name': step.name,
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
138 'description': step.description,
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
139 'errors': step.errors,
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
140 'log_messages': self.get_all_log_messages_for_step(step),
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
141 } for step in failed_steps],
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
142 },
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
143 'change': {
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
144 'rev': change.rev,
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
145 'link': self.env.abs_href.changeset(change.rev),
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
146 'author': change.author,
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
147 },
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
148 }
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
149
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
150 def get_all_log_messages_for_step(self, step):
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
151 messages = []
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
152 for log in BuildLog.select(self.env, build=self.build.id,
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
153 step=step.name):
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
154 messages.extend(log.messages)
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
155 return messages
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
156
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
157 def get_changeset(self):
804
4c73a3cea9f5 Basic Trac 0.12 support, supporting just a `(default)` repository - essentially Trac 0.11 behaviour. Thanks to those that have contributed to #480 to get this working and tested.
osimons
parents: 626
diff changeset
158 repos = self.env.get_repository()
4c73a3cea9f5 Basic Trac 0.12 support, supporting just a `(default)` repository - essentially Trac 0.11 behaviour. Thanks to those that have contributed to #480 to get this working and tested.
osimons
parents: 626
diff changeset
159 assert repos, 'No "(default)" Repository: Add a repository or alias ' \
4c73a3cea9f5 Basic Trac 0.12 support, supporting just a `(default)` repository - essentially Trac 0.11 behaviour. Thanks to those that have contributed to #480 to get this working and tested.
osimons
parents: 626
diff changeset
160 'named "(default)" to Trac.'
4c73a3cea9f5 Basic Trac 0.12 support, supporting just a `(default)` repository - essentially Trac 0.11 behaviour. Thanks to those that have contributed to #480 to get this working and tested.
osimons
parents: 626
diff changeset
161 return repos.get_changeset(self.build.rev)
626
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
162
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
163 def get_author(self):
73ed8c171063 Simplify email notification code by removing BuildInfo class
mgood
parents: 625
diff changeset
164 return self.get_changeset().author
Copyright (C) 2012-2017 Edgewall Software