Skip to content

Commit

Permalink
Bug 880669 - Extend current BzAPI BMO extension to contain compatibil…
Browse files Browse the repository at this point in the history
…ity changes on top of native rest

r=glob
  • Loading branch information
David Lawrence committed Jun 16, 2014
1 parent dd10df6 commit 0a2592d
Show file tree
Hide file tree
Showing 15 changed files with 2,014 additions and 153 deletions.
1 change: 1 addition & 0 deletions .htaccess
Expand Up @@ -75,3 +75,4 @@ RewriteRule ^form[\.:]mobile[\.\-:]compat$ enter_bug.cgi?product=Tech+Evangelism
RewriteRule ^form[\.:]web[\.:]bounty$ enter_bug.cgi?product=mozilla.org&format=web-bounty
RewriteRule ^form[\.:]automative$ enter_bug.cgi?product=Testing&format=automative
RewriteRule ^rest/(.*)$ rest.cgi/$1 [NE]
RewriteRule ^bzapi/(.*)$ extensions/BzAPI/bin/rest.cgi/$1 [NE]
8 changes: 7 additions & 1 deletion Bugzilla/Util.pm
Expand Up @@ -45,7 +45,7 @@ use base qw(Exporter);
bz_crypt generate_random_password
validate_email_syntax clean_text
get_text template_var disable_utf8
detect_encoding email_filter);
enable_utf8 detect_encoding email_filter);

use Bugzilla::Constants;
use Bugzilla::RNG qw(irand);
Expand Down Expand Up @@ -778,6 +778,12 @@ sub disable_utf8 {
}
}

sub enable_utf8 {
if (Bugzilla->params->{'utf8'}) {
binmode STDOUT, ':utf8'; # Turn on UTF8 encoding.
}
}

use constant UTF8_ACCIDENTAL => qw(shiftjis big5-eten euc-kr euc-jp);

sub detect_encoding {
Expand Down
29 changes: 23 additions & 6 deletions Bugzilla/WebService/Bug.pm
Expand Up @@ -54,11 +54,25 @@ use Storable qw(dclone);

use constant PRODUCT_SPECIFIC_FIELDS => qw(version target_milestone component);

use constant DATE_FIELDS => {
comments => ['new_since'],
history => ['new_since'],
search => ['last_change_time', 'creation_time'],
};
sub DATE_FIELDS {
my $fields = {
comments => ['new_since'],
create => [],
history => ['new_since'],
search => ['last_change_time', 'creation_time'],
update => []
};

# Add date related custom fields
foreach my $field (Bugzilla->active_custom_fields) {
next unless ($field->type == FIELD_TYPE_DATETIME
|| $field->type == FIELD_TYPE_DATE);
push(@{ $fields->{create} }, $field->name);
push(@{ $fields->{update} }, $field->name);
}

return $fields;
}

use constant BASE64_FIELDS => {
add_attachment => ['data'],
Expand Down Expand Up @@ -558,7 +572,7 @@ sub search {

# Backwards compatibility with old method regarding role search
$match_params->{'reporter'} = delete $match_params->{'creator'} if $match_params->{'creator'};
foreach my $role (qw(assigned_to reporter qa_contact longdesc cc)) {
foreach my $role (qw(assigned_to reporter qa_contact commenter cc)) {
next if !exists $match_params->{$role};
my $value = delete $match_params->{$role};
$match_params->{"f${last_field_id}"} = $role;
Expand Down Expand Up @@ -594,6 +608,9 @@ sub search {
my @bugs = map { $bug_objects{$_} } @bug_ids;
@bugs = map { $self->_bug_to_hash($_, $params) } @bugs;

# BzAPI
Bugzilla->request_cache->{bzapi_search_bugs} = [ map { $bug_objects{$_} } @bug_ids ];

return { bugs => \@bugs };
}

Expand Down
26 changes: 19 additions & 7 deletions Bugzilla/WebService/Server/REST.pm
Expand Up @@ -16,7 +16,7 @@ use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::Error;
use Bugzilla::Hook;
use Bugzilla::Util qw(correct_urlbase html_quote);
use Bugzilla::Util qw(correct_urlbase html_quote disable_utf8 enable_utf8);
use Bugzilla::WebService::Constants;
use Bugzilla::WebService::Util qw(taint_data fix_credentials);

Expand Down Expand Up @@ -78,8 +78,12 @@ sub handle {
# Fix includes/excludes for each call
rest_include_exclude($params);

# Set callback name if exists
$self->_bz_callback($params->{'callback'}) if $params->{'callback'};
# Set callback name if content-type is 'application/javascript'
if ($params->{'callback'}
|| $self->content_type eq 'application/javascript')
{
$self->_bz_callback($params->{'callback'} || 'callback');
}

Bugzilla->input_params($params);

Expand Down Expand Up @@ -111,8 +115,13 @@ sub response {
# along with the result/error such as version and id which
# we will strip off for REST calls.
my $content = $response->content;

my $json_data = {};
if ($content) {
# Content is in bytes at this point and needs to be converted
# back to utf8 string.
enable_utf8();
utf8::decode($content) if !utf8::is_utf8($content);
$json_data = $self->json->decode($content);
}

Expand Down Expand Up @@ -152,6 +161,9 @@ sub response {
$content = $self->json->encode($result);
}

utf8::encode($content) if utf8::is_utf8($content);
disable_utf8();

$response->content($content);

$self->SUPER::response($response);
Expand Down Expand Up @@ -307,16 +319,16 @@ sub bz_rest_params {
sub bz_rest_options {
my ($self, $options) = @_;
$self->{_bz_rest_options} = $options if $options;
return $self->{_bz_rest_options};
return [ sort { $a cmp $b } @{ $self->{_bz_rest_options} } ];
}

sub rest_include_exclude {
my ($params) = @_;

if ($params->{'include_fields'} && !ref $params->{'include_fields'}) {
if (exists $params->{'include_fields'} && !ref $params->{'include_fields'}) {
$params->{'include_fields'} = [ split(/[\s+,]/, $params->{'include_fields'}) ];
}
if ($params->{'exclude_fields'} && !ref $params->{'exclude_fields'}) {
if (exists $params->{'exclude_fields'} && !ref $params->{'exclude_fields'}) {
$params->{'exclude_fields'} = [ split(/[\s+,]/, $params->{'exclude_fields'}) ];
}

Expand Down Expand Up @@ -344,7 +356,7 @@ sub _retrieve_json_params {
my $extra_params = {};
my $json = delete $params->{'POSTDATA'} || delete $params->{'PUTDATA'};
if ($json) {
eval { $extra_params = $self->json->decode($json); };
eval { $extra_params = $self->json->utf8(0)->decode($json); };
if ($@) {
ThrowUserError('json_rpc_invalid_params', { err_msg => $@ });
}
Expand Down
10 changes: 5 additions & 5 deletions Bugzilla/WebService/Util.pm
Expand Up @@ -166,14 +166,14 @@ sub filter_wants($$;$$) {
delete $exclude{$key};
}

# If the user has asked to include all or exclude all
return $cache->{$field} = 0 if $exclude_types{'all'};
return $cache->{$field} = 1 if $include_types{'all'};

# Explicit inclusion/exclusion
return $cache->{$field} = 0 if $exclude{$field};
return $cache->{$field} = 1 if $include{$field};

# If the user has asked to include all or exclude all
return $cache->{$field} = 0 if $exclude_types{'all'};
return $cache->{$field} = 1 if $include_types{'all'};

# If the user has not asked for any fields specifically or if the user has asked
# for one or more of the field's types (and not excluded them)
foreach my $type (keys %field_types) {
Expand Down Expand Up @@ -214,7 +214,7 @@ sub _delete_bad_keys {
# However, we need to validate our argument names in some way.
# We know that all hash keys passed in to the WebService will
# match \w+, so we delete any key that doesn't match that.
if ($key !~ /^\w+$/) {
if ($key !~ /^[\w\.\-]+$/) {
delete $item->{$key};
}
}
Expand Down
61 changes: 7 additions & 54 deletions extensions/BzAPI/Config.pm
@@ -1,63 +1,16 @@
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# 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 BzAPI Bugzilla Extension.
#
# The Initial Developer of the Original Code is
# the Mozilla Foundation.
# Portions created by the Initial Developer are Copyright (C) 2010
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Gervase Markham <gerv@gerv.net>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

package Bugzilla::Extension::BzAPI;

use strict;

use constant NAME => 'BzAPI';

use constant REQUIRED_MODULES => [
{
package => 'SOAP-Lite',
module => 'SOAP::Lite',
# 0.710.04 is required for correct UTF-8 handling, but .04 and .05 are
# affected by bug 468009.
version => '0.710.06',
},
{
package => 'Test-Taint',
module => 'Test::Taint',
version => 0,
},
{
package => 'JSON',
module => 'JSON',
version => 0,
},
];
use constant REQUIRED_MODULES => [];

__PACKAGE__->NAME;

0 comments on commit 0a2592d

Please sign in to comment.