diff options
Diffstat (limited to 'extensions/Bitly')
-rw-r--r-- | extensions/Bitly/Config.pm | 2 | ||||
-rw-r--r-- | extensions/Bitly/Extension.pm | 13 | ||||
-rw-r--r-- | extensions/Bitly/lib/WebService.pm | 190 |
3 files changed, 98 insertions, 107 deletions
diff --git a/extensions/Bitly/Config.pm b/extensions/Bitly/Config.pm index 7e46a6ad8..ce27508c7 100644 --- a/extensions/Bitly/Config.pm +++ b/extensions/Bitly/Config.pm @@ -16,6 +16,6 @@ use constant NAME => 'Bitly'; use constant REQUIRED_MODULES => []; use constant OPTIONAL_MODULES => []; -use constant API_VERSION_MAP => { '1_0' => '1_0' }; +use constant API_VERSION_MAP => {'1_0' => '1_0'}; __PACKAGE__->NAME; diff --git a/extensions/Bitly/Extension.pm b/extensions/Bitly/Extension.pm index 82f17bc2a..31f4b7438 100644 --- a/extensions/Bitly/Extension.pm +++ b/extensions/Bitly/Extension.pm @@ -17,17 +17,14 @@ our $VERSION = '1'; use Bugzilla; sub webservice { - my ($self, $args) = @_; - $args->{dispatch}->{Bitly} = "Bugzilla::Extension::Bitly::WebService"; + my ($self, $args) = @_; + $args->{dispatch}->{Bitly} = "Bugzilla::Extension::Bitly::WebService"; } sub config_modify_panels { - my ($self, $args) = @_; - push @{ $args->{panels}->{advanced}->{params} }, { - name => 'bitly_token', - type => 't', - default => '', - }; + my ($self, $args) = @_; + push @{$args->{panels}->{advanced}->{params}}, + {name => 'bitly_token', type => 't', default => '',}; } __PACKAGE__->NAME; diff --git a/extensions/Bitly/lib/WebService.pm b/extensions/Bitly/lib/WebService.pm index 4b44faa0e..b900e17ee 100644 --- a/extensions/Bitly/lib/WebService.pm +++ b/extensions/Bitly/lib/WebService.pm @@ -27,121 +27,115 @@ use URI::Escape; use URI::QueryParam; use constant PUBLIC_METHODS => qw( - list - shorten + list + shorten ); sub _validate_uri { - my ($self, $params) = @_; - - # extract url from params - if (!defined $params->{url}) { - ThrowCodeError( - 'param_required', - { function => 'Bitly.shorten', param => 'url' } - ); - } - my $url = ref($params->{url}) ? $params->{url}->[0] : $params->{url}; - - # only allow buglist queries for this bugzilla install - my $uri = URI->new($url); - $uri->query(undef); - $uri->fragment(undef); - if ($uri->as_string ne Bugzilla->localconfig->{urlbase} . 'buglist.cgi') { - ThrowUserError('bitly_unsupported'); - } - - return URI->new($url); + my ($self, $params) = @_; + + # extract url from params + if (!defined $params->{url}) { + ThrowCodeError('param_required', {function => 'Bitly.shorten', param => 'url'}); + } + my $url = ref($params->{url}) ? $params->{url}->[0] : $params->{url}; + + # only allow buglist queries for this bugzilla install + my $uri = URI->new($url); + $uri->query(undef); + $uri->fragment(undef); + if ($uri->as_string ne Bugzilla->localconfig->{urlbase} . 'buglist.cgi') { + ThrowUserError('bitly_unsupported'); + } + + return URI->new($url); } sub shorten { - my ($self) = shift; - my $uri = $self->_validate_uri(@_); + my ($self) = shift; + my $uri = $self->_validate_uri(@_); - # the list_id is user-specific, remove it - $uri->query_param_delete('list_id'); + # the list_id is user-specific, remove it + $uri->query_param_delete('list_id'); - return $self->_bitly($uri); + return $self->_bitly($uri); } sub list { - my ($self) = shift; - my $uri = $self->_validate_uri(@_); - - # map params to cgi vars, converting quicksearch if required - my $params = $uri->query_param('quicksearch') - ? Bugzilla::CGI->new(quicksearch($uri->query_param('quicksearch')))->Vars - : Bugzilla::CGI->new($uri->query)->Vars; - - # execute the search - my $search = Bugzilla::Search->new( - params => $params, - fields => ['bug_id'], - limit => Bugzilla->params->{max_search_results}, - ); - my $data = $search->data; - - # form a bug_id only url, sanity check the length - $uri = URI->new(Bugzilla->localconfig->{urlbase} . 'buglist.cgi?bug_id=' . join(',', map { $_->[0] } @$data)); - if (length($uri->as_string) > CGI_URI_LIMIT) { - ThrowUserError('bitly_failure', { message => "Too many bugs returned by search" }); - } - - # shorten - return $self->_bitly($uri); + my ($self) = shift; + my $uri = $self->_validate_uri(@_); + + # map params to cgi vars, converting quicksearch if required + my $params + = $uri->query_param('quicksearch') + ? Bugzilla::CGI->new(quicksearch($uri->query_param('quicksearch')))->Vars + : Bugzilla::CGI->new($uri->query)->Vars; + + # execute the search + my $search = Bugzilla::Search->new( + params => $params, + fields => ['bug_id'], + limit => Bugzilla->params->{max_search_results}, + ); + my $data = $search->data; + + # form a bug_id only url, sanity check the length + $uri + = URI->new(Bugzilla->localconfig->{urlbase} + . 'buglist.cgi?bug_id=' + . join(',', map { $_->[0] } @$data)); + if (length($uri->as_string) > CGI_URI_LIMIT) { + ThrowUserError('bitly_failure', + {message => "Too many bugs returned by search"}); + } + + # shorten + return $self->_bitly($uri); } sub _bitly { - my ($self, $uri) = @_; - - # form request url - # http://dev.bitly.com/links.html#v3_shorten - my $bitly_url = sprintf( - 'https://api-ssl.bitly.com/v3/shorten?access_token=%s&longUrl=%s', - Bugzilla->params->{bitly_token}, - uri_escape($uri->as_string) - ); - - # is Mozilla::CA isn't installed, skip certificate verification - eval { require Mozilla::CA }; - $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = $@ ? 0 : 1; - - # request - my $ua = LWP::UserAgent->new(agent => 'Bugzilla'); - $ua->timeout(10); - $ua->protocols_allowed(['http', 'https']); - if (my $proxy_url = Bugzilla->params->{proxy_url}) { - $ua->proxy(['http', 'https'], $proxy_url); - } - else { - $ua->env_proxy(); - } - my $response = $ua->get($bitly_url); - if ($response->is_error) { - ThrowUserError('bitly_failure', { message => $response->message }); - } - my $result = decode_json($response->decoded_content); - if ($result->{status_code} != 200) { - ThrowUserError('bitly_failure', { message => $result->{status_txt} }); - } - - # return just the short url - return { url => $result->{data}->{url} }; + my ($self, $uri) = @_; + + # form request url + # http://dev.bitly.com/links.html#v3_shorten + my $bitly_url = sprintf( + 'https://api-ssl.bitly.com/v3/shorten?access_token=%s&longUrl=%s', + Bugzilla->params->{bitly_token}, + uri_escape($uri->as_string) + ); + + # is Mozilla::CA isn't installed, skip certificate verification + eval { require Mozilla::CA }; + $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = $@ ? 0 : 1; + + # request + my $ua = LWP::UserAgent->new(agent => 'Bugzilla'); + $ua->timeout(10); + $ua->protocols_allowed(['http', 'https']); + if (my $proxy_url = Bugzilla->params->{proxy_url}) { + $ua->proxy(['http', 'https'], $proxy_url); + } + else { + $ua->env_proxy(); + } + my $response = $ua->get($bitly_url); + if ($response->is_error) { + ThrowUserError('bitly_failure', {message => $response->message}); + } + my $result = decode_json($response->decoded_content); + if ($result->{status_code} != 200) { + ThrowUserError('bitly_failure', {message => $result->{status_txt}}); + } + + # return just the short url + return {url => $result->{data}->{url}}; } sub rest_resources { - return [ - qr{^/bitly/shorten$}, { - GET => { - method => 'shorten', - }, - }, - qr{^/bitly/list$}, { - GET => { - method => 'list', - }, - }, - ] + return [ + qr{^/bitly/shorten$}, {GET => {method => 'shorten',},}, + qr{^/bitly/list$}, {GET => {method => 'list',},}, + ]; } 1; |