Source

trac-changeset-notifier / tracext / changesetnotifier / api.py

Diff from to

tracext/changesetnotifier/api.py

 from StringIO import StringIO
 
 from trac.core import *
-from trac.config import BoolOption
+from trac.config import BoolOption, Option
 from trac.util.text import CRLF
 from trac.mimeview import Mimeview
 from trac.versioncontrol import IRepositoryChangeListener
 from trac.versioncontrol.api import Changeset, Node
 from trac.versioncontrol.diff import unified_diff
-from trac.notifications import NotifyEmail
+from trac.notification import NotifyEmail
 from trac.web.chrome import ITemplateProvider
 
 class ChangesetNotifyEmail(NotifyEmail):
         self.data.update({
             'diff_body': diff,
         })
-        NotifyEmail.notify(self, resid, self._format_subject(subject))
+        NotifyEmail.notify(self, resid, subject)
+
+    def get_recipients(self, resid):
+        return (self.config.get('changeset', 'notification_recipients'), '')
 
 class ChangesetNotifier(Component):
     implements(IRepositoryChangeListener, ITemplateProvider)
 
-    notify = BoolOption('changesets', 'notify_changesets', 'true',
+    notify = BoolOption('changeset', 'notify_changesets', 'true',
                         """Send notifications for changeset updates.""")
+    recipients = Option('changeset', 'notification_recipients', ''
+                        """Sets the changeset notification recipients.
+                        The default notification CC option is also applied.""")
 
     _last_cset_id = None
-    
+
     # IRepositoryChangeListener methods
 
     def changeset_added(self, repos, changeset):
+        if not self.notify:
+            return
         if self._is_duplicate(changeset):
             return
         diff = self._make_diff(repos, changeset)
         cn = ChangesetNotifyEmail(self.env)
-        subject = self._format_subject('Changeset %s' % repos.display_rev(changeset.rev))
+        reponame = repos.reponame if repos.reponame else '(default)'
+        subject = self._format_subject('Changeset %s in repository %s' %
+                                       (repos.display_rev(changeset.rev), reponame))
         cn.notify(changeset.rev, subject, diff)
 
     def changeset_modified(self, repos, changeset, old_changeset):
+        if not self.notify:
+            return
         if self._is_duplicate(changeset):
             return
         # currently not used.
+        raise NotImplementedError
 
     # ITemplateProvider methods
 
         mimeview = Mimeview(self.env)
 
         # We always compare the whole changeset from the root.
+        # The below code is borrowed from ChangesetModule._render_diff() in
+        # trac.versioncontrol.web_ui.changeset.
         new_path = '/'
         new_rev = changeset.rev
 
         data = {
             'new_path': new_path,
             'new_rev': new_rev,
-            'old_path': old_path,
-            'old_rev': old_rev,
+            'old_path': prev_path,
+            'old_rev': prev_rev,
             'diff': {
                 'options': {}, # use default options
             }