diff options
author | Perl Tidy <perltidy@bugzilla.org> | 2018-12-05 21:38:52 +0100 |
---|---|---|
committer | Dylan William Hardison <dylan@hardison.net> | 2018-12-05 23:49:08 +0100 |
commit | 8ec8da0491ad89604700b3e29a227966f6d84ba1 (patch) | |
tree | 9d270f173330ca19700e0ba9f2ee931300646de1 /extensions/Review/lib | |
parent | a7bb5a65b71644d9efce5fed783ed545b9336548 (diff) | |
download | bugzilla-8ec8da0491ad89604700b3e29a227966f6d84ba1.tar.gz bugzilla-8ec8da0491ad89604700b3e29a227966f6d84ba1.tar.xz |
no bug - reformat all the code using the new perltidy rules
Diffstat (limited to 'extensions/Review/lib')
-rw-r--r-- | extensions/Review/lib/FlagStateActivity.pm | 110 | ||||
-rw-r--r-- | extensions/Review/lib/Util.pm | 71 | ||||
-rw-r--r-- | extensions/Review/lib/WebService.pm | 468 |
3 files changed, 328 insertions, 321 deletions
diff --git a/extensions/Review/lib/FlagStateActivity.pm b/extensions/Review/lib/FlagStateActivity.pm index 35da42351..92efb6c02 100644 --- a/extensions/Review/lib/FlagStateActivity.pm +++ b/extensions/Review/lib/FlagStateActivity.pm @@ -24,65 +24,58 @@ use constant AUDIT_UPDATES => 0; use constant AUDIT_REMOVES => 0; use constant DB_COLUMNS => qw( - id - flag_when - type_id - flag_id - setter_id - requestee_id - bug_id - attachment_id - status + id + flag_when + type_id + flag_id + setter_id + requestee_id + bug_id + attachment_id + status ); sub _check_param_required { - my ($param) = @_; - - return sub { - my ($invocant, $value) = @_; - $value = trim($value) - or ThrowCodeError('param_required', {param => $param}); - return $value; - }, + my ($param) = @_; + + return sub { + my ($invocant, $value) = @_; + $value = trim($value) or ThrowCodeError('param_required', {param => $param}); + return $value; + },; } sub _check_date { - my ($invocant, $date) = @_; + my ($invocant, $date) = @_; - $date = trim($date); - datetime_from($date) - or ThrowUserError('illegal_date', { date => $date, - format => 'YYYY-MM-DD HH24:MI:SS' }); - return $date; + $date = trim($date); + datetime_from($date) + or ThrowUserError('illegal_date', + {date => $date, format => 'YYYY-MM-DD HH24:MI:SS'}); + return $date; } sub _check_status { - my ($self, $status) = @_; - - # - Make sure the status is valid. - # - Make sure the user didn't request the flag unless it's requestable. - # If the flag existed and was requested before it became unrequestable, - # leave it as is. - if (none { $status eq $_ } qw( X + - ? )) { - ThrowUserError( - 'flag_status_invalid', - { - id => $self->id, - status => $status - } - ); - } - return $status; + my ($self, $status) = @_; + + # - Make sure the status is valid. + # - Make sure the user didn't request the flag unless it's requestable. + # If the flag existed and was requested before it became unrequestable, + # leave it as is. + if (none { $status eq $_ } qw( X + - ? )) { + ThrowUserError('flag_status_invalid', {id => $self->id, status => $status}); + } + return $status; } use constant VALIDATORS => { - flag_when => \&_check_date, - type_id => _check_param_required('type_id'), - flag_id => _check_param_required('flag_id'), - setter_id => _check_param_required('setter_id'), - bug_id => _check_param_required('bug_id'), - status => \&_check_status, + flag_when => \&_check_date, + type_id => _check_param_required('type_id'), + flag_id => _check_param_required('flag_id'), + setter_id => _check_param_required('setter_id'), + bug_id => _check_param_required('bug_id'), + status => \&_check_status, }; sub flag_when { return $_[0]->{flag_when} } @@ -95,30 +88,33 @@ sub attachment_id { return $_[0]->{attachment_id} } sub status { return $_[0]->{status} } sub type { - my ($self) = @_; - return $self->{type} //= Bugzilla::FlagType->new({ id => $self->type_id, cache => 1 }); + my ($self) = @_; + return $self->{type} + //= Bugzilla::FlagType->new({id => $self->type_id, cache => 1}); } sub setter { - my ($self) = @_; - return $self->{setter} //= Bugzilla::User->new({ id => $self->setter_id, cache => 1 }); + my ($self) = @_; + return $self->{setter} + //= Bugzilla::User->new({id => $self->setter_id, cache => 1}); } sub requestee { - my ($self) = @_; - return undef unless defined $self->requestee_id; - return $self->{requestee} //= Bugzilla::User->new({ id => $self->requestee_id, cache => 1 }); + my ($self) = @_; + return undef unless defined $self->requestee_id; + return $self->{requestee} + //= Bugzilla::User->new({id => $self->requestee_id, cache => 1}); } sub bug { - my ($self) = @_; - return $self->{bug} //= Bugzilla::Bug->new({ id => $self->bug_id, cache => 1 }); + my ($self) = @_; + return $self->{bug} //= Bugzilla::Bug->new({id => $self->bug_id, cache => 1}); } sub attachment { - my ($self) = @_; - return $self->{attachment} //= - Bugzilla::Attachment->new({ id => $self->attachment_id, cache => 1 }); + my ($self) = @_; + return $self->{attachment} + //= Bugzilla::Attachment->new({id => $self->attachment_id, cache => 1}); } 1; diff --git a/extensions/Review/lib/Util.pm b/extensions/Review/lib/Util.pm index a8744079d..61d4e9117 100644 --- a/extensions/Review/lib/Util.pm +++ b/extensions/Review/lib/Util.pm @@ -17,12 +17,12 @@ use Bugzilla; our @EXPORT = qw( rebuild_review_counters ); sub rebuild_review_counters { - my ($callback) = @_; - my $dbh = Bugzilla->dbh; + my ($callback) = @_; + my $dbh = Bugzilla->dbh; - $dbh->bz_start_transaction; + $dbh->bz_start_transaction; - my $rows = $dbh->selectall_arrayref(" + my $rows = $dbh->selectall_arrayref(" SELECT flags.requestee_id AS user_id, flagtypes.name AS flagtype, COUNT(*) as count @@ -32,55 +32,48 @@ sub rebuild_review_counters { WHERE flags.status = '?' AND flagtypes.name IN ('review', 'feedback', 'needinfo') GROUP BY flags.requestee_id, flagtypes.name - ", { Slice => {} }); + ", {Slice => {}}); - my ($count, $total, $current) = (1, scalar(@$rows), { id => 0 }); - foreach my $row (@$rows) { - $callback->($count++, $total) if $callback; - if ($row->{user_id} != $current->{id}) { - _update_profile($dbh, $current) if $current->{id}; - $current = { id => $row->{user_id} }; - } - $current->{$row->{flagtype}} = $row->{count}; + my ($count, $total, $current) = (1, scalar(@$rows), {id => 0}); + foreach my $row (@$rows) { + $callback->($count++, $total) if $callback; + if ($row->{user_id} != $current->{id}) { + _update_profile($dbh, $current) if $current->{id}; + $current = {id => $row->{user_id}}; } - _update_profile($dbh, $current) if $current->{id}; + $current->{$row->{flagtype}} = $row->{count}; + } + _update_profile($dbh, $current) if $current->{id}; - foreach my $field (qw( review feedback needinfo )) { - _fix_negatives($dbh, $field); - } + foreach my $field (qw( review feedback needinfo )) { + _fix_negatives($dbh, $field); + } - $dbh->bz_commit_transaction; + $dbh->bz_commit_transaction; } sub _fix_negatives { - my ($dbh, $field) = @_; - my $user_ids = $dbh->selectcol_arrayref( - "SELECT userid FROM profiles WHERE ${field}_request_count < 0" - ); - return unless @$user_ids; - $dbh->do( - "UPDATE profiles SET ${field}_request_count = 0 WHERE " . $dbh->sql_in('userid', $user_ids) - ); - foreach my $user_id (@$user_ids) { - Bugzilla->memcached->clear({ table => 'profiles', id => $user_id }); - } + my ($dbh, $field) = @_; + my $user_ids = $dbh->selectcol_arrayref( + "SELECT userid FROM profiles WHERE ${field}_request_count < 0"); + return unless @$user_ids; + $dbh->do("UPDATE profiles SET ${field}_request_count = 0 WHERE " + . $dbh->sql_in('userid', $user_ids)); + foreach my $user_id (@$user_ids) { + Bugzilla->memcached->clear({table => 'profiles', id => $user_id}); + } } sub _update_profile { - my ($dbh, $data) = @_; - $dbh->do(" + my ($dbh, $data) = @_; + $dbh->do(" UPDATE profiles SET review_request_count = ?, feedback_request_count = ?, needinfo_request_count = ? - WHERE userid = ?", - undef, - $data->{review} || 0, - $data->{feedback} || 0, - $data->{needinfo} || 0, - $data->{id} - ); - Bugzilla->memcached->clear({ table => 'profiles', id => $data->{id} }); + WHERE userid = ?", undef, $data->{review} || 0, $data->{feedback} || 0, + $data->{needinfo} || 0, $data->{id}); + Bugzilla->memcached->clear({table => 'profiles', id => $data->{id}}); } 1; diff --git a/extensions/Review/lib/WebService.pm b/extensions/Review/lib/WebService.pm index 0c54d725a..79843cf2c 100644 --- a/extensions/Review/lib/WebService.pm +++ b/extensions/Review/lib/WebService.pm @@ -20,277 +20,295 @@ use Bugzilla::Util qw(detaint_natural trick_taint); use Bugzilla::WebService::Util 'filter'; use constant PUBLIC_METHODS => qw( - flag_activity - suggestions + flag_activity + suggestions ); sub suggestions { - my ($self, $params) = @_; - my $dbh = Bugzilla->switch_to_shadow_db(); - - my ($bug, $product, $component); - if (exists $params->{bug_id}) { - $bug = Bugzilla::Bug->check($params->{bug_id}); - $product = $bug->product_obj; - $component = $bug->component_obj; - } - elsif (exists $params->{product}) { - $product = Bugzilla::Product->check($params->{product}); - if (exists $params->{component}) { - $component = Bugzilla::Component->check({ - product => $product, name => $params->{component} - }); - } - } - else { - ThrowUserError("reviewer_suggestions_param_required"); + my ($self, $params) = @_; + my $dbh = Bugzilla->switch_to_shadow_db(); + + my ($bug, $product, $component); + if (exists $params->{bug_id}) { + $bug = Bugzilla::Bug->check($params->{bug_id}); + $product = $bug->product_obj; + $component = $bug->component_obj; + } + elsif (exists $params->{product}) { + $product = Bugzilla::Product->check($params->{product}); + if (exists $params->{component}) { + $component + = Bugzilla::Component->check({ + product => $product, name => $params->{component} + }); } + } + else { + ThrowUserError("reviewer_suggestions_param_required"); + } - my @reviewers; - if ($bug) { - # we always need to be authentiated to perform user matching - my $user = Bugzilla->user; - if (!$user->id) { - Bugzilla->set_user(Bugzilla::User->check({ name => Bugzilla->params->{'nobody_user'} })); - push @reviewers, @{ $bug->mentors }; - Bugzilla->set_user($user); - } else { - push @reviewers, @{ $bug->mentors }; - } - } - if ($component) { - push @reviewers, @{ $component->reviewers_objs }; - } - if (!$component || !@{ $component->reviewers_objs }) { - push @reviewers, @{ $product->reviewers_objs }; - } + my @reviewers; + if ($bug) { - my @result; - foreach my $reviewer (@reviewers) { - push @result, { - id => $self->type('int', $reviewer->id), - email => $self->type('email', $reviewer->login), - name => $self->type('string', $reviewer->name), - review_count => $self->type('int', $reviewer->review_count), - }; + # we always need to be authentiated to perform user matching + my $user = Bugzilla->user; + if (!$user->id) { + Bugzilla->set_user(Bugzilla::User->check( + {name => Bugzilla->params->{'nobody_user'}})); + push @reviewers, @{$bug->mentors}; + Bugzilla->set_user($user); + } + else { + push @reviewers, @{$bug->mentors}; } - return \@result; + } + if ($component) { + push @reviewers, @{$component->reviewers_objs}; + } + if (!$component || !@{$component->reviewers_objs}) { + push @reviewers, @{$product->reviewers_objs}; + } + + my @result; + foreach my $reviewer (@reviewers) { + push @result, + { + id => $self->type('int', $reviewer->id), + email => $self->type('email', $reviewer->login), + name => $self->type('string', $reviewer->name), + review_count => $self->type('int', $reviewer->review_count), + }; + } + return \@result; } sub flag_activity { - my ($self, $params) = @_; - my $dbh = Bugzilla->switch_to_shadow_db(); - my %match_criteria; + my ($self, $params) = @_; + my $dbh = Bugzilla->switch_to_shadow_db(); + my %match_criteria; - if (my $flag_id = $params->{flag_id}) { - detaint_natural($flag_id) - or ThrowUserError('invalid_flag_id', { flag_id => $flag_id }); + if (my $flag_id = $params->{flag_id}) { + detaint_natural($flag_id) + or ThrowUserError('invalid_flag_id', {flag_id => $flag_id}); - $match_criteria{flag_id} = $flag_id; - } - - if (my $flag_ids = $params->{flag_ids}) { - foreach my $flag_id (@$flag_ids) { - detaint_natural($flag_id) - or ThrowUserError('invalid_flag_id', { flag_id => $flag_id }); - } + $match_criteria{flag_id} = $flag_id; + } - $match_criteria{flag_id} = $flag_ids; + if (my $flag_ids = $params->{flag_ids}) { + foreach my $flag_id (@$flag_ids) { + detaint_natural($flag_id) + or ThrowUserError('invalid_flag_id', {flag_id => $flag_id}); } - if (my $type_id = $params->{type_id}) { - detaint_natural($type_id) - or ThrowUserError('invalid_flag_type_id', { type_id => $type_id }); + $match_criteria{flag_id} = $flag_ids; + } - $match_criteria{type_id} = $type_id; - } + if (my $type_id = $params->{type_id}) { + detaint_natural($type_id) + or ThrowUserError('invalid_flag_type_id', {type_id => $type_id}); - if (my $type_name = $params->{type_name}) { - trick_taint($type_name); - my $flag_types = Bugzilla::FlagType::match({ name => $type_name }); - $match_criteria{type_id} = [map { $_->id } @$flag_types]; - } + $match_criteria{type_id} = $type_id; + } - foreach my $user_field (qw( requestee setter )) { - if (my $user_name = $params->{$user_field}) { - my $user = Bugzilla::User->check({ name => $user_name, cache => 1, _error => 'invalid_username' }); + if (my $type_name = $params->{type_name}) { + trick_taint($type_name); + my $flag_types = Bugzilla::FlagType::match({name => $type_name}); + $match_criteria{type_id} = [map { $_->id } @$flag_types]; + } - $match_criteria{ $user_field . "_id" } = $user->id; - } - } + foreach my $user_field (qw( requestee setter )) { + if (my $user_name = $params->{$user_field}) { + my $user = Bugzilla::User->check( + {name => $user_name, cache => 1, _error => 'invalid_username'}); - foreach my $field (qw( bug_id status )) { - if (exists $params->{$field}) { - $match_criteria{$field} = $params->{$field}; - } + $match_criteria{$user_field . "_id"} = $user->id; } + } - ThrowCodeError('param_required', { param => 'limit', function => 'Review.flag_activity()' }) - if defined $params->{offset} && !defined $params->{limit}; - - my $limit = delete $params->{limit}; - my $offset = delete $params->{offset}; - my $after = delete $params->{after}; - my $before = delete $params->{before}; - my $max_results = Bugzilla->params->{max_search_results}; - - if (!$limit || $limit > $max_results) { - $limit = $max_results; + foreach my $field (qw( bug_id status )) { + if (exists $params->{$field}) { + $match_criteria{$field} = $params->{$field}; } - - if ($after && $after =~ /^(\d{4}-\d{1,2}-\d{1,2})$/) { - $after = $1; - } - else { - my $now = DateTime->now; - $now->subtract(days => 30); - $after = $now->ymd('-'); - } - - if ($before && $before =~ /^(\d{4}-\d{1,2}-\d{1,2})$/) { - $before = $1; - } - else { - my $now = DateTime->now; - $before = $now->ymd('-'); - } - - $match_criteria{LIMIT} = $limit; - $match_criteria{OFFSET} = $offset if defined $offset; - $match_criteria{WHERE} = { 'date(flag_when) BETWEEN ? AND ?' => [$after, $before] }; - - # Throw error if no other parameters have been passed other than limit and offset - if (!grep(!/^(LIMIT|OFFSET)$/, keys %match_criteria)) { - ThrowUserError('flag_activity_parameters_required'); - } - - my $matches = Bugzilla::Extension::Review::FlagStateActivity->match(\%match_criteria); - my $user = Bugzilla->user; - $user->visible_bugs([ map { $_->bug_id } @$matches ]); - my @results = map { $self->_flag_state_activity_to_hash($_, $params) } - grep { $user->can_see_bug($_->bug_id) && _can_see_attachment($user, $_) } - @$matches; - return \@results; + } + + ThrowCodeError('param_required', + {param => 'limit', function => 'Review.flag_activity()'}) + if defined $params->{offset} && !defined $params->{limit}; + + my $limit = delete $params->{limit}; + my $offset = delete $params->{offset}; + my $after = delete $params->{after}; + my $before = delete $params->{before}; + my $max_results = Bugzilla->params->{max_search_results}; + + if (!$limit || $limit > $max_results) { + $limit = $max_results; + } + + if ($after && $after =~ /^(\d{4}-\d{1,2}-\d{1,2})$/) { + $after = $1; + } + else { + my $now = DateTime->now; + $now->subtract(days => 30); + $after = $now->ymd('-'); + } + + if ($before && $before =~ /^(\d{4}-\d{1,2}-\d{1,2})$/) { + $before = $1; + } + else { + my $now = DateTime->now; + $before = $now->ymd('-'); + } + + $match_criteria{LIMIT} = $limit; + $match_criteria{OFFSET} = $offset if defined $offset; + $match_criteria{WHERE} + = {'date(flag_when) BETWEEN ? AND ?' => [$after, $before]}; + + # Throw error if no other parameters have been passed other than limit and offset + if (!grep(!/^(LIMIT|OFFSET)$/, keys %match_criteria)) { + ThrowUserError('flag_activity_parameters_required'); + } + + my $matches + = Bugzilla::Extension::Review::FlagStateActivity->match(\%match_criteria); + my $user = Bugzilla->user; + $user->visible_bugs([map { $_->bug_id } @$matches]); + my @results + = map { $self->_flag_state_activity_to_hash($_, $params) } + grep { $user->can_see_bug($_->bug_id) && _can_see_attachment($user, $_) } + @$matches; + return \@results; } sub _can_see_attachment { - my ($user, $flag_state_activity) = @_; + my ($user, $flag_state_activity) = @_; - return 1 if !$flag_state_activity->attachment_id; - return 0 if $flag_state_activity->attachment->isprivate && !$user->is_insider; - return 1; + return 1 if !$flag_state_activity->attachment_id; + return 0 if $flag_state_activity->attachment->isprivate && !$user->is_insider; + return 1; } sub rest_resources { - return [ - # bug-id - qr{^/review/suggestions/(\d+)$}, { - GET => { - method => 'suggestions', - params => sub { - return { bug_id => $_[0] }; - }, - }, - }, - # product/component - qr{^/review/suggestions/([^/]+)/(.+)$}, { - GET => { - method => 'suggestions', - params => sub { - return { product => $_[0], component => $_[1] }; - }, - }, + return [ + # bug-id + qr{^/review/suggestions/(\d+)$}, + { + GET => { + method => 'suggestions', + params => sub { + return {bug_id => $_[0]}; }, - # just product - qr{^/review/suggestions/([^/]+)$}, { - GET => { - method => 'suggestions', - params => sub { - return { product => $_[0] }; - }, - }, + }, + }, + + # product/component + qr{^/review/suggestions/([^/]+)/(.+)$}, + { + GET => { + method => 'suggestions', + params => sub { + return {product => $_[0], component => $_[1]}; }, - # named parameters - qr{^/review/suggestions$}, { - GET => { - method => 'suggestions', - }, + }, + }, + + # just product + qr{^/review/suggestions/([^/]+)$}, + { + GET => { + method => 'suggestions', + params => sub { + return {product => $_[0]}; }, - # flag activity by flag id - qr{^/review/flag_activity/(\d+)$}, { - GET => { - method => 'flag_activity', - params => sub { - return { flag_id => $_[0] } - }, - }, + }, + }, + + # named parameters + qr{^/review/suggestions$}, + {GET => {method => 'suggestions',},}, + + # flag activity by flag id + qr{^/review/flag_activity/(\d+)$}, + { + GET => { + method => 'flag_activity', + params => sub { + return {flag_id => $_[0]}; }, - qr{^/review/flag_activity/type_name/(\w+)$}, { - GET => { - method => 'flag_activity', - params => sub { - return { type_name => $_[0] } - }, - }, + }, + }, + qr{^/review/flag_activity/type_name/(\w+)$}, + { + GET => { + method => 'flag_activity', + params => sub { + return {type_name => $_[0]}; }, - # flag activity by user - qr{^/review/flag_activity/(requestee|setter|type_id)/(.*)$}, { - GET => { - method => 'flag_activity', - params => sub { - return { $_[0] => $_[1] }; - }, - }, + }, + }, + + # flag activity by user + qr{^/review/flag_activity/(requestee|setter|type_id)/(.*)$}, + { + GET => { + method => 'flag_activity', + params => sub { + return {$_[0] => $_[1]}; }, - # flag activity with only query strings - qr{^/review/flag_activity$}, { - GET => { method => 'flag_activity' }, - }, - ]; + }, + }, + + # flag activity with only query strings + qr{^/review/flag_activity$}, + {GET => {method => 'flag_activity'},}, + ]; } sub _flag_state_activity_to_hash { - my ($self, $fsa, $params) = @_; - - my %flag = ( - id => $self->type('int', $fsa->id), - creation_time => $self->type('string', $fsa->flag_when), - type => $self->_flagtype_to_hash($fsa->type), - setter => $self->_user_to_hash($fsa->setter), - bug_id => $self->type('int', $fsa->bug_id), - attachment_id => $self->type('int', $fsa->attachment_id), - status => $self->type('string', $fsa->status), - ); - - $flag{requestee} = $self->_user_to_hash($fsa->requestee) if $fsa->requestee; - $flag{flag_id} = $self->type('int', $fsa->flag_id) unless $params->{flag_id}; - - return filter($params, \%flag); + my ($self, $fsa, $params) = @_; + + my %flag = ( + id => $self->type('int', $fsa->id), + creation_time => $self->type('string', $fsa->flag_when), + type => $self->_flagtype_to_hash($fsa->type), + setter => $self->_user_to_hash($fsa->setter), + bug_id => $self->type('int', $fsa->bug_id), + attachment_id => $self->type('int', $fsa->attachment_id), + status => $self->type('string', $fsa->status), + ); + + $flag{requestee} = $self->_user_to_hash($fsa->requestee) if $fsa->requestee; + $flag{flag_id} = $self->type('int', $fsa->flag_id) unless $params->{flag_id}; + + return filter($params, \%flag); } sub _flagtype_to_hash { - my ($self, $flagtype) = @_; - my $user = Bugzilla->user; - - return { - id => $self->type('int', $flagtype->id), - name => $self->type('string', $flagtype->name), - description => $self->type('string', $flagtype->description), - type => $self->type('string', $flagtype->target_type), - is_active => $self->type('boolean', $flagtype->is_active), - is_requesteeble => $self->type('boolean', $flagtype->is_requesteeble), - is_multiplicable => $self->type('boolean', $flagtype->is_multiplicable), - }; + my ($self, $flagtype) = @_; + my $user = Bugzilla->user; + + return { + id => $self->type('int', $flagtype->id), + name => $self->type('string', $flagtype->name), + description => $self->type('string', $flagtype->description), + type => $self->type('string', $flagtype->target_type), + is_active => $self->type('boolean', $flagtype->is_active), + is_requesteeble => $self->type('boolean', $flagtype->is_requesteeble), + is_multiplicable => $self->type('boolean', $flagtype->is_multiplicable), + }; } sub _user_to_hash { - my ($self, $user) = @_; + my ($self, $user) = @_; - return { - id => $self->type('int', $user->id), - real_name => $self->type('string', $user->name), - name => $self->type('email', $user->login), - }; + return { + id => $self->type('int', $user->id), + real_name => $self->type('string', $user->name), + name => $self->type('email', $user->login), + }; } 1; |