From 2d8badfce2364ffe22bbd8448e7a74bbccba10c7 Mon Sep 17 00:00:00 2001 From: Byron Jones Date: Tue, 17 Mar 2015 12:13:48 +0800 Subject: Bug 1132963: Automatically detect and redirect to Google doc URLs (akin to github-pr and rb handling) --- extensions/BMO/Extension.pm | 26 +++++++++++----------- extensions/BMO/bin/migrate-github-pull-requests.pl | 4 ++-- extensions/BMO/lib/Data.pm | 22 ++++++++++++++---- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/extensions/BMO/Extension.pm b/extensions/BMO/Extension.pm index 637ff3139..ad93604e0 100644 --- a/extensions/BMO/Extension.pm +++ b/extensions/BMO/Extension.pm @@ -45,7 +45,7 @@ use Email::MIME::ContentType qw(parse_content_type); use Encode qw(find_encoding encode_utf8); use File::MimeInfo::Magic; use List::MoreUtils qw(natatime); -use List::Util qw(first); +use List::Util qw(first any); use Scalar::Util qw(blessed); use Sys::Syslog qw(:DEFAULT setlogsock); use Text::Balanced qw( extract_bracketed extract_multiple ); @@ -751,13 +751,13 @@ sub attachment_process_data { $url = $data; } - if (my $content_type = _get_review_content_type($url)) { + if (my $content_type = _detect_attached_url($url)) { $attributes->{mimetype} = $content_type; $attributes->{ispatch} = 0; } } -sub _get_review_content_type { +sub _detect_attached_url { my ($url) = @_; # trim and check for the pull request url @@ -766,13 +766,13 @@ sub _get_review_content_type { $url = trim($url); return if $url =~ /\s/; - if ($url =~ m#^https://github\.com/[^/]+/[^/]+/pull/\d+/?$#i) { - return GITHUB_PR_CONTENT_TYPE; - } - if ($url =~ m#^https?://reviewboard(?:-dev)?\.(?:allizom|mozilla)\.org/r/\d+/?#i) { - return RB_REQUEST_CONTENT_TYPE; + foreach my $key (keys %autodetect_attach_urls) { + if ($url =~ $autodetect_attach_urls{$key}->{regex}) { + return $autodetect_attach_urls{$key}->{content_type}; + } } - return; + + return undef; } # redirect automatically to github urls @@ -784,13 +784,13 @@ sub attachment_view { # don't redirect if the content-type is specified explicitly return if defined $cgi->param('content_type'); - # must be our github/reviewboard content-type + # must be our supported content-type return unless - $attachment->contenttype eq GITHUB_PR_CONTENT_TYPE - or $attachment->contenttype eq RB_REQUEST_CONTENT_TYPE; + any { $attachment->contenttype eq $autodetect_attach_urls{$_}->{content_type} } + keys %autodetect_attach_urls; # must still be a valid url - return unless _get_review_content_type($attachment->data); + return unless _detect_attached_url($attachment->data); # redirect print $cgi->redirect(trim($attachment->data)); diff --git a/extensions/BMO/bin/migrate-github-pull-requests.pl b/extensions/BMO/bin/migrate-github-pull-requests.pl index de71a7856..1d84352a2 100755 --- a/extensions/BMO/bin/migrate-github-pull-requests.pl +++ b/extensions/BMO/bin/migrate-github-pull-requests.pl @@ -65,7 +65,7 @@ foreach my $attachment (@$attachments) { $dbh->do( "UPDATE attachments SET mimetype = ? WHERE attach_id = ?", undef, - GITHUB_PR_CONTENT_TYPE, $attachment->{attach_id} + 'text/x-github-pull-request', $attachment->{attach_id} ); # insert into bugs_activity @@ -75,7 +75,7 @@ foreach my $attachment (@$attachments) { VALUES (?, ?, ?, ?, ?, ?)", undef, $attachment->{bug_id}, $nobody->id, $timestamp, $field->id, - $attachment->{mimetype}, GITHUB_PR_CONTENT_TYPE + $attachment->{mimetype}, 'text/x-github-pull-request' ); $dbh->do( "UPDATE bugs SET delta_ts = ?, lastdiffed = ? WHERE bug_id = ?", diff --git a/extensions/BMO/lib/Data.pm b/extensions/BMO/lib/Data.pm index dc091d23c..63d0eb4ad 100644 --- a/extensions/BMO/lib/Data.pm +++ b/extensions/BMO/lib/Data.pm @@ -19,11 +19,25 @@ our @EXPORT = qw( $cf_visible_in_products %product_sec_groups %create_bug_formats @default_named_queries - GITHUB_PR_CONTENT_TYPE - RB_REQUEST_CONTENT_TYPE ); + %autodetect_attach_urls ); -use constant GITHUB_PR_CONTENT_TYPE => 'text/x-github-pull-request'; -use constant RB_REQUEST_CONTENT_TYPE => 'text/x-review-board-request'; +# Creating an attachment whose contents is a URL matching one of these regexes +# will result in the user being redirected to that URL when viewing the +# attachment. +our %autodetect_attach_urls = ( + github_pr => { + regex => qr#^https://github\.com/[^/]+/[^/]+/pull/\d+/?$#i, + content_type => 'text/x-github-pull-request', + }, + reviewboard => { + regex => qr#^https?://reviewboard(?:-dev)?\.(?:allizom|mozilla)\.org/r/\d+/?#i, + content_type => 'text/x-review-board-request', + }, + google_docs => { + regex => qr#^https://docs\.google\.com/(?:document|spreadsheets|presentation)/d/#i, + content_type => 'text/x-google-doc', + }, +); # Which custom fields are visible in which products and components. # -- cgit v1.2.3-24-g4f1b