[splinter] Add review links to outgoing bug mail



commit e39374236d2ef77b242549937a92e38b7e9ad528
Author: Owen W. Taylor <otaylor fishsoup net>
Date:   Sat Oct 17 18:03:29 2009 -0400

    Add review links to outgoing bug mail
    
    Use the mailer-before_send hook to add links to reviews
    for:
    
     - newly created patch attachments
     - review comments
    
    This hook isn't the ideal place to do it but it works.

 Makefile                             |    1 +
 extension/code/mailer-before_send.pl |   33 +++++++++++++++++++++
 extension/lib/SplinterUtil.pm        |   53 +++++++++++++++++++++++++++++++++-
 3 files changed, 86 insertions(+), 1 deletions(-)
---
diff --git a/Makefile b/Makefile
index 5de31fe..0be02f3 100644
--- a/Makefile
+++ b/Makefile
@@ -42,6 +42,7 @@ WEB_FILES =					\
 EXTENSION_FILES =							\
 	extension/code/bug-format_comment.pl				\
 	extension/code/config-add_panels.pl				\
+	extension/code/mailer-before_send.pl				\
 	extension/code/page-before_template.pl				\
 	extension/code/webservice.pl					\
 	extension/info.pl						\
diff --git a/extension/code/mailer-before_send.pl b/extension/code/mailer-before_send.pl
new file mode 100644
index 0000000..5effb1c
--- /dev/null
+++ b/extension/code/mailer-before_send.pl
@@ -0,0 +1,33 @@
+# -*- Mode: perl; indent-tabs-mode: nil -*-
+#
+# The contents of this file are subject to the Mozilla Public
+# License Version 1.1 (the "License"); you may not use this file
+# except in compliance with the License. You may obtain a copy of
+# the License at http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS
+# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
+# implied. See the License for the specific language governing
+# rights and limitations under the License.
+#
+# The Original Code is the Splinter Bugzilla Extension.
+#
+# The Initial Developer of the Original Code is Red Hat, Inc.
+# Portions created by Red Hat, Inc. are Copyright (C) 2009
+# Red Hat Inc. All Rights Reserved.
+#
+# Contributor(s):
+#   Owen Taylor <otaylor fishsoup net>
+
+use strict;
+use warnings;
+use Bugzilla;
+
+use extensions::splinter::lib::SplinterUtil;
+
+# Post-process bug mail to add review links to bug mail.
+# It would be nice to be able to hook in earlier in the
+# process when the email body is being formatted in the
+# style of the bug-format_comment link for HTML but this
+# is the only hook available as of Bugzilla-3.4.
+add_review_links_to_email(Bugzilla->hook_args->{'email'});
diff --git a/extension/lib/SplinterUtil.pm b/extension/lib/SplinterUtil.pm
index 8aa48db..80c11dd 100644
--- a/extension/lib/SplinterUtil.pm
+++ b/extension/lib/SplinterUtil.pm
@@ -26,7 +26,8 @@ use Bugzilla::Util;
 
 use base qw(Exporter);
 @extensions::splinter::lib::SplinterUtil::EXPORT = qw(attachment_is_visible attachment_id_is_patch
-                                                      get_review_url get_review_link);
+                                                      get_review_url get_review_link
+                                                      add_review_links_to_email);
 
 # Checks if the current user can see an attachment
 # Based on code from attachment.cgi
@@ -75,3 +76,53 @@ sub get_review_link {
     my ($bug, $attach_id, $link_text) = @_;
     return "<a href='" . html_quote(get_review_url($bug, $attach_id)) . "'>$link_text</a>";
 }
+
+sub munge_create_attachment {
+    my ($bug, $intro_text, $attach_id, $view_link) = @_;
+
+    if (attachment_id_is_patch ($attach_id)) {
+	return ("$intro_text" .
+                " View: $view_link\015\012" .
+                " Review: " . get_review_url($bug, $attach_id, 1) . "\015\012");
+    } else {
+	return ("$intro_text" .
+                " --> ($view_link)");
+    }
+}
+
+# This adds review links into a bug mail before we send it out.
+# Since this is happening after newlines have been converted into
+# RFC-2822 style \r\n, we need handle line ends carefully.
+# (\015 and \012 are used because Perl \n is platform-dependent)
+sub add_review_links_to_email {
+    my $email = shift;
+
+    my $body = $email->body;
+    my $new_body = 0;
+
+    my $bug;
+    if ($email->header('Subject') =~ /^\[Bug\s+(\d+)\]/ &&
+        Bugzilla->user->can_see_bug($1))
+    {
+	$bug = new Bugzilla::Bug($1);
+    }
+
+    return unless defined $bug;
+
+    if ($body =~ /Review\s+of\s+attachment\s+\d+\s*:/) {
+	$body =~ s~(Review\s+of\s+attachment\s+(\d+)\s*:)
+                  ~"$1\015\012 --> (" . get_review_url($bug, $2, 1) . ")"
+                  ~egx;
+	$new_body = 1;
+    }
+
+    if ($body =~ /Created an attachment \(id=[0-9]+\)\015\012 --> /) {
+	$body =~ s~(Created\ an\ attachment\ \(id=([0-9]+)\)\015\012)
+                   \ -->\ \(([^\015\012]*)\)[^\015\012]*
+                  ~munge_create_attachment($bug, $1, $2, $3)
+                  ~egx;
+	$new_body = 1;
+    }
+
+    $email->body_set($body) if $new_body;
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]