summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Lawrence <dlawrence@mozilla.com>2011-10-17 21:22:20 +0200
committerDavid Lawrence <dlawrence@mozilla.com>2011-10-17 21:22:20 +0200
commit3fdcf22a0fa375c993889909a867c8a68f5101a8 (patch)
tree4a74fd16c906101eb8463dee38709e051172b9fb
parent6d9f7e5de586a9e7a4a7c3ee7aea08e204b8d24e (diff)
downloadbugzilla-3fdcf22a0fa375c993889909a867c8a68f5101a8.tar.gz
bugzilla-3fdcf22a0fa375c993889909a867c8a68f5101a8.tar.xz
Bug 689601 - Include small patches inline in review requests
r=glob
-rw-r--r--Bugzilla/Attachment.pm47
-rw-r--r--extensions/BMO/Extension.pm52
-rw-r--r--template/en/default/request/email.txt.tmpl1
3 files changed, 100 insertions, 0 deletions
diff --git a/Bugzilla/Attachment.pm b/Bugzilla/Attachment.pm
index 3a8e7d5d5..2a89678c5 100644
--- a/Bugzilla/Attachment.pm
+++ b/Bugzilla/Attachment.pm
@@ -414,6 +414,53 @@ sub datasize {
return $self->{datasize};
}
+=over
+
+=item C<linecount>
+
+the number of lines of the attachment content
+
+=back
+
+=cut
+
+# linecount allows for getting the number of lines of an attachment
+# from the database directly if the data has not yet been loaded for
+# performance reasons.
+
+sub linecount {
+ my ($self) = @_;
+
+ return $self->{linecount} if exists $self->{linecount};
+
+ # Limit this to just text/* attachments as this could
+ # cause strange results for binary attachments.
+ return if $self->contenttype !~ /^text\//;
+
+ # If the data has already been loaded, we can just determine
+ # line count from the data directly.
+ if ($self->{data}) {
+ $self->{linecount} = $self->{data} =~ tr/\n/\n/;
+ }
+ else {
+ $self->{linecount} =
+ int(Bugzilla->dbh->selectrow_array("
+ SELECT LENGTH(attach_data.thedata)-LENGTH(REPLACE(attach_data.thedata,'\n',''))/LENGTH('\n')
+ FROM attach_data WHERE id = ?", undef, $self->id));
+
+ }
+
+ # If we still do not have a linecount either the attachment
+ # is stored in a local file or has been deleted. If the former,
+ # we call self->data to force a load from the filesystem and
+ # then do a split on newlines and count again.
+ unless ($self->{linecount}) {
+ $self->{linecount} = $self->data =~ tr/\n/\n/;
+ }
+
+ return $self->{linecount};
+}
+
sub _get_local_filename {
my $self = shift;
my $hash = ($self->id % 100) + 100;
diff --git a/extensions/BMO/Extension.pm b/extensions/BMO/Extension.pm
index 1bb83c977..909e1354b 100644
--- a/extensions/BMO/Extension.pm
+++ b/extensions/BMO/Extension.pm
@@ -34,11 +34,14 @@ use Bugzilla::Util qw(html_quote trick_taint trim datetime_from detaint_natural)
use Bugzilla::Token;
use Bugzilla::Error;
use Bugzilla::Mailer;
+use Bugzilla::Util;
use Scalar::Util qw(blessed);
use Date::Parse;
use DateTime;
+use Encode qw(find_encoding);
+use Bugzilla::Extension::BMO::Constants;
use Bugzilla::Extension::BMO::FakeBug;
use Bugzilla::Extension::BMO::Data qw($cf_visible_in_products
$cf_flags
@@ -743,4 +746,53 @@ sub _short_desc_matches {
Bugzilla::Search::COLUMNS->{'relevance'}->{name} = $select_term;
}
+sub mailer_before_send {
+ my ($self, $args) = @_;
+ my $email = $args->{email};
+
+ # If email is a request for a review, add the attachment itself
+ # to the email as an attachment. Attachment must be content type
+ # text/plain and below a certain size. Otherwise the email already
+ # contain a link to the attachment.
+ if ($email
+ && $email->header('X-Bugzilla-Type') eq 'request'
+ && ($email->header('X-Bugzilla-Flag-Requestee')
+ && $email->header('X-Bugzilla-Flag-Requestee') eq $email->header('to')))
+ {
+ my $body = $email->body;
+
+ if (my ($attach_id) = $body =~ /Attachment\s+(\d+)\s*:/) {
+ my $attachment = Bugzilla::Attachment->new($attach_id);
+ if ($attachment
+ && $attachment->ispatch
+ && $attachment->contenttype eq 'text/plain'
+ && $attachment->linecount
+ && $attachment->linecount < REQUEST_MAX_ATTACH_LINES)
+ {
+ # Don't send a charset header with attachments, as they might
+ # not be UTF-8, unless we can properly detect it.
+ my $charset;
+ if (Bugzilla->feature('detect_charset')) {
+ my $encoding = detect_encoding($attachment->data);
+ if ($encoding) {
+ $charset = find_encoding($encoding)->mime_name;
+ }
+ }
+
+ my $attachment_part = Email::MIME->create(
+ attributes => {
+ content_type => $attachment->contenttype,
+ filename => $attachment->filename,
+ disposition => "attachment",
+ },
+ body => $attachment->data,
+ );
+ $attachment_part->charset_set($charset) if $charset;
+
+ $email->parts_add([ $attachment_part ]);
+ }
+ }
+ }
+}
+
__PACKAGE__->NAME;
diff --git a/template/en/default/request/email.txt.tmpl b/template/en/default/request/email.txt.tmpl
index 989e0c4e3..1ae941bf2 100644
--- a/template/en/default/request/email.txt.tmpl
+++ b/template/en/default/request/email.txt.tmpl
@@ -54,6 +54,7 @@ Subject: [% flagtype_name %] [%+ subject_status %]: [[% terms.Bug %] [%+ bug.bug
[Attachment [% attachment.id %]] [% attachment.description FILTER clean_text %][% END %]
Date: [% date %]
X-Bugzilla-Type: request
+[%+ IF flag.requestee %]X-Bugzilla-Flag-Requestee: [% flag.requestee.email %][% END %]
[%+ threadingmarker %]
[%+ USE wrap -%]