From a22b71296783855b7c4d150c570b5ece684792e8 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Tue, 17 Nov 2026 15:39:30 +0100 Subject: [PATCH 01/19] Bug 2094122 - Migrate the InvalidBugHelper REST resource to native Mojo API POST /rest/invalid_bug_helper/close/ is now served by the native controller Bugzilla::Extension::InvalidBugHelper::API::V1::InvalidBugHelper instead of the legacy REST server. Same path, parameters, checks and response, mark_as_spam must now be a boolean. The legacy WebService module and its webservice hook are removed, and the extension test now goes through the real route. --- extensions/InvalidBugHelper/Extension.pm | 6 -- .../V1/InvalidBugHelper.pm} | 79 ++++++++-------- .../t/bmo/invalid-bug-helper.t | 89 ++++++++----------- 3 files changed, 77 insertions(+), 97 deletions(-) rename extensions/InvalidBugHelper/lib/{WebService.pm => API/V1/InvalidBugHelper.pm} (62%) diff --git a/extensions/InvalidBugHelper/Extension.pm b/extensions/InvalidBugHelper/Extension.pm index d6b69e3758..6a1fdd56e4 100644 --- a/extensions/InvalidBugHelper/Extension.pm +++ b/extensions/InvalidBugHelper/Extension.pm @@ -21,10 +21,4 @@ sub config_add_panels { = 'Bugzilla::Extension::InvalidBugHelper::Config'; } -sub webservice { - my ($self, $args) = @_; - $args->{dispatch}->{InvalidBugHelper} - = 'Bugzilla::Extension::InvalidBugHelper::WebService'; -} - __PACKAGE__->NAME; diff --git a/extensions/InvalidBugHelper/lib/WebService.pm b/extensions/InvalidBugHelper/lib/API/V1/InvalidBugHelper.pm similarity index 62% rename from extensions/InvalidBugHelper/lib/WebService.pm rename to extensions/InvalidBugHelper/lib/API/V1/InvalidBugHelper.pm index bd2f9e803e..28c839ce77 100644 --- a/extensions/InvalidBugHelper/lib/WebService.pm +++ b/extensions/InvalidBugHelper/lib/API/V1/InvalidBugHelper.pm @@ -5,36 +5,50 @@ # This Source Code Form is "Incompatible With Secondary Licenses", as # defined by the Mozilla Public License, v. 2.0. -package Bugzilla::Extension::InvalidBugHelper::WebService; +package Bugzilla::Extension::InvalidBugHelper::API::V1::InvalidBugHelper; use 5.10.1; -use strict; -use warnings; - -use base qw(Bugzilla::WebService); +use Mojo::Base qw( Mojolicious::Controller ); use Bugzilla; use Bugzilla::Bug; use Bugzilla::BugMail; -use Bugzilla::Comment; use Bugzilla::Constants; use Bugzilla::Error; -use constant PUBLIC_METHODS => qw(close_as_invalid); +use Bugzilla::API::V1::Util qw(coerce_booleans merge_request_params); + +sub setup_routes { + my ($class, $r) = @_; + $r->post('/invalid_bug_helper/close/:id' => [id => qr/\d+/]) + ->to('InvalidBugHelper::API::V1::InvalidBugHelper#close_as_invalid'); +} +# POST /invalid_bug_helper/close/ +# +# Moves the bug to Invalid Bugs :: General, resolves it as INVALID, clears its +# needinfo flags and posts the warning comment. Editbugs members only. Params: +# mark_as_spam also tag the reporter's comments as spam +# Returns {id, product, status}. sub close_as_invalid { - my ($self, $params) = @_; - my $user = Bugzilla->login(LOGIN_REQUIRED); - my $dbh = Bugzilla->dbh; + my ($self) = @_; + Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); + + my $user = $self->bugzilla->login; + $user->id || return $self->user_error('login_required'); + my $dbh = Bugzilla->dbh; + + my ($params, $error) = merge_request_params($self); + return $self->user_error($error) if $error; + + if ($error = coerce_booleans($params, 'mark_as_spam')) { + return $self->user_error('invalid_params', {type_error => $error}); + } $user->in_group('editbugs') || ThrowUserError('auth_failure', {group => 'editbugs', action => 'update', object => 'bug'}); - my $bug_id = $params->{bug_id} - // ThrowCodeError('param_required', - {function => 'InvalidBugHelper.close_as_invalid', param => 'bug_id'}); - - my $bug = Bugzilla::Bug->check({id => $bug_id}); + my $bug = Bugzilla::Bug->check({id => $self->param('id')}); if ($bug->reporter->in_group('editbugs')) { ThrowUserError('invalid_bug_reporter_is_trusted', {bug => $bug}); @@ -43,7 +57,7 @@ sub close_as_invalid { # Block non-members from closing bugs with mandatory security groups. foreach my $group (@{$bug->groups_in}) { my $gc = $bug->product_obj->group_controls->{$group->id}; - if ($gc + if ( $gc && $gc->{membercontrol} == CONTROLMAPMANDATORY && !$user->in_group($group->name)) { @@ -52,8 +66,9 @@ sub close_as_invalid { } } - if ($bug->bug_status eq 'RESOLVED' || $bug->bug_status eq 'VERIFIED' - || $bug->product eq 'Invalid Bugs') + if ( $bug->bug_status eq 'RESOLVED' + || $bug->bug_status eq 'VERIFIED' + || $bug->product eq 'Invalid Bugs') { ThrowUserError('bug_status_unresolvable', {bug => $bug}); } @@ -63,7 +78,6 @@ sub close_as_invalid { = grep { $_->author->id == $bug->reporter->id } @{$bug->comments}; my @clear_flags = map { {id => $_->id, status => 'X'} } grep { $_->type->name eq 'needinfo' } @{$bug->flags}; - my @groups = @{$bug->groups_in}; my $warning_text = Bugzilla->params->{invalidbughelper_warning_text} || 'This bug has been marked as invalid.'; @@ -84,7 +98,8 @@ sub close_as_invalid { comment => {body => $warning_text}, }); - # Non-mandatory security group stripping is handled by _set_product (called via set_all above) + # Non-mandatory security group stripping is handled by _set_product (called + # via set_all above). $bug->update(); # Only tag reporter comments as spam when explicitly requested. @@ -101,23 +116,13 @@ sub close_as_invalid { # are told now rather than with the bug's next change. Bugzilla::BugMail::Send($bug->id, {changer => $user}); - return { - id => $self->type('int', $bug->id), - product => $self->type('string', $bug->product), - status => $self->type('string', $bug->bug_status), - }; -} - -sub rest_resources { - return [ - qr{^/invalid_bug_helper/close/(\d+)$}, - { - POST => { - method => 'close_as_invalid', - params => sub { {bug_id => $_[0]} }, - }, - }, - ]; + return $self->render( + json => { + id => Bugzilla::API::V1::Util->type('int', $bug->id), + product => Bugzilla::API::V1::Util->type('string', $bug->product), + status => Bugzilla::API::V1::Util->type('string', $bug->bug_status), + } + ); } 1; diff --git a/extensions/InvalidBugHelper/t/bmo/invalid-bug-helper.t b/extensions/InvalidBugHelper/t/bmo/invalid-bug-helper.t index c56b3b0085..14a29537d8 100644 --- a/extensions/InvalidBugHelper/t/bmo/invalid-bug-helper.t +++ b/extensions/InvalidBugHelper/t/bmo/invalid-bug-helper.t @@ -17,60 +17,25 @@ BEGIN { } use Test::More; +use Test::Mojo; use Bugzilla; +BEGIN { Bugzilla->extensions } use Bugzilla::Constants; use Bugzilla::Bug; use Bugzilla::Component; use Bugzilla::Group; use Bugzilla::Product; use Bugzilla::User; +use Bugzilla::Test::Util qw(issue_api_key); -# Load Bugzilla::Extension to install the INC_HOOK that maps -# Bugzilla::Extension::NAME::* to extensions/NAME/lib/*. -use_ok('Bugzilla::Extension'); - -# The INC_HOOK maps Bugzilla::Extension::InvalidBugHelper::* to -# extensions/InvalidBugHelper/lib/*. -use_ok('Bugzilla::Extension::InvalidBugHelper::WebService'); use_ok('Bugzilla::Extension::InvalidBugHelper::Config'); -# Verify rest_resources returns the expected REST route. -my $resources = Bugzilla::Extension::InvalidBugHelper::WebService->rest_resources; -ok(ref $resources eq 'ARRAY', 'rest_resources returns an arrayref'); -ok(scalar(@$resources) == 2, 'rest_resources has two elements (pattern + handler)'); - -my ($pattern, $handlers) = @$resources; -ok(ref $pattern eq 'Regexp', 'first element is a regex'); -ok(exists $handlers->{POST}, 'handler defines a POST method'); -ok($handlers->{POST}{method} eq 'close_as_invalid', - 'POST handler maps to close_as_invalid'); - -# Verify the route regex matches valid bug IDs and rejects invalid ones. -ok('/invalid_bug_helper/close/123' =~ $pattern, 'route matches numeric bug id'); -ok('/invalid_bug_helper/close/0' =~ $pattern, 'route matches bug id 0'); -ok('/invalid_bug_helper/close/abc' !~ $pattern, 'route rejects non-numeric id'); -ok('/invalid_bug_helper/close/' !~ $pattern, 'route rejects missing id'); - -# Verify the params sub extracts bug_id from the capture group. -my $params_sub = $handlers->{POST}{params}; -is(ref $params_sub, 'CODE', 'params is a code ref'); -is_deeply($params_sub->(42), {bug_id => 42}, 'params sub extracts bug_id'); - -# Verify PUBLIC_METHODS includes close_as_invalid. -my @public = Bugzilla::Extension::InvalidBugHelper::WebService->PUBLIC_METHODS; -ok(grep({ $_ eq 'close_as_invalid' } @public), - 'close_as_invalid is in PUBLIC_METHODS'); - # Verify Config returns the expected params. my @params = Bugzilla::Extension::InvalidBugHelper::Config->get_param_list; my %param_names = map { $_->{name} => 1 } @params; ok($param_names{invalidbughelper_warning_text}, 'warning_text param defined'); -# Verify close_as_invalid refuses to act on bugs reported by an editbugs -# member, regardless of who is performing the close (Bug 1684509). - -BEGIN { Bugzilla->extensions } Bugzilla->usage_mode(USAGE_MODE_TEST); Bugzilla->error_mode(ERROR_MODE_DIE); @@ -122,19 +87,31 @@ my $bug_from_plain_reporter = make_bug_as($plain_reporter); Bugzilla->set_user($admin); -my $ws = 'Bugzilla::Extension::InvalidBugHelper::WebService'; +# POST /rest/invalid_bug_helper/close/ is a native Mojo route. +my $api_key = issue_api_key($admin->login)->api_key; +my %auth = ('X-Bugzilla-API-Key' => $api_key); +my $t = Test::Mojo->new('Bugzilla::App'); + +my $close_url = '/rest/invalid_bug_helper/close/'; + +$t->post_ok($close_url . $bug_from_plain_reporter->id) + ->status_is(401) + ->json_is('/code' => 410, 'anonymous request needs a login'); -eval { $ws->close_as_invalid({bug_id => $bug_from_editbugs_reporter->id}) }; -like($@, qr/reported by a member of the 'editbugs' group/, - 'close_as_invalid refuses a bug reported by an editbugs member'); +$t->post_ok($close_url + . $bug_from_plain_reporter->id => \%auth => json => {mark_as_spam => 'maybe'}) + ->status_is(400) + ->json_like( + '/message' => qr/mark_as_spam must be true or false/, + 'mark_as_spam must be a boolean' + ); -# The real return-value serialization (->type) is only provided by the -# JSON-RPC/REST server at dispatch time; stub it here so we can exercise -# close_as_invalid's business logic directly. -no strict 'refs'; -no warnings 'redefine'; -local *{"${ws}::type"} = sub { return $_[2] }; -use strict 'refs'; +$t->post_ok($close_url . $bug_from_editbugs_reporter->id => \%auth) + ->status_isnt(200) + ->json_like( + '/message' => qr/reported by a member of the 'editbugs' group/, + 'refuses a bug reported by an editbugs member (bug 1684509)' + ); # The test data has no Invalid Bugs product, the one close_as_invalid moves # bugs to. It needs the version and milestone of the bug it receives. @@ -157,18 +134,22 @@ if (!Bugzilla::Product->new({name => 'Invalid Bugs'})) { } SKIP: { - skip 'Need the Invalid Bugs product to test the non-editbugs-reporter path', 3 + skip 'Need the Invalid Bugs product to test the non-editbugs-reporter path', 6 unless Bugzilla::Product->new({name => 'Invalid Bugs'}); # Flag mail is part of bugmail, so closing must send it. my @sent; local *Bugzilla::BugMail::Send = sub { push @sent, $_[0] }; - my $result = eval { $ws->close_as_invalid({bug_id => $bug_from_plain_reporter->id}) }; - is($@, '', 'close_as_invalid does not throw for a bug reported by a non-editbugs user'); + $t->post_ok($close_url + . $bug_from_plain_reporter->id => \%auth => json => {mark_as_spam => 0}) + ->status_is(200) + ->json_is('/id' => $bug_from_plain_reporter->id) + ->json_is( + '/product' => 'Invalid Bugs', + 'bug from non-editbugs reporter is moved to Invalid Bugs' + )->json_is('/status' => 'RESOLVED'); is_deeply(\@sent, [$bug_from_plain_reporter->id], 'and sends the bugmail'); - is(ref $result eq 'HASH' ? $result->{product} : undef, 'Invalid Bugs', - 'bug from non-editbugs reporter is moved to Invalid Bugs'); } done_testing(); From 01f326266e842c906cdd3ef778949c2130eb00f3 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Tue, 17 Nov 2026 17:46:48 +0100 Subject: [PATCH 02/19] Bug 2094122 - Migrate the Bitly REST resource to native Mojo API GET /rest/bitly/shorten and GET /rest/bitly/list are now served by the native controller Bugzilla::Extension::Bitly::API::V1::Bitly instead of the legacy REST server. Same paths, url parameter, checks and {url} response. The legacy WebService module and its webservice hook are removed, and a test covers both routes. --- extensions/Bitly/Extension.pm | 5 - .../lib/{WebService.pm => API/V1/Bitly.pm} | 102 +++++++++--------- extensions/Bitly/t/bmo/bitly.t | 81 ++++++++++++++ 3 files changed, 130 insertions(+), 58 deletions(-) rename extensions/Bitly/lib/{WebService.pm => API/V1/Bitly.pm} (58%) create mode 100644 extensions/Bitly/t/bmo/bitly.t diff --git a/extensions/Bitly/Extension.pm b/extensions/Bitly/Extension.pm index 31f4b74385..340ed1c647 100644 --- a/extensions/Bitly/Extension.pm +++ b/extensions/Bitly/Extension.pm @@ -16,11 +16,6 @@ our $VERSION = '1'; use Bugzilla; -sub webservice { - my ($self, $args) = @_; - $args->{dispatch}->{Bitly} = "Bugzilla::Extension::Bitly::WebService"; -} - sub config_modify_panels { my ($self, $args) = @_; push @{$args->{panels}->{advanced}->{params}}, diff --git a/extensions/Bitly/lib/WebService.pm b/extensions/Bitly/lib/API/V1/Bitly.pm similarity index 58% rename from extensions/Bitly/lib/WebService.pm rename to extensions/Bitly/lib/API/V1/Bitly.pm index 5e87e9b59f..fa414c2adc 100644 --- a/extensions/Bitly/lib/WebService.pm +++ b/extensions/Bitly/lib/API/V1/Bitly.pm @@ -5,65 +5,52 @@ # This Source Code Form is "Incompatible With Secondary Licenses", as # defined by the Mozilla Public License, v. 2.0. -package Bugzilla::Extension::Bitly::WebService; +package Bugzilla::Extension::Bitly::API::V1::Bitly; use 5.10.1; -use strict; -use warnings; - -use base qw(Bugzilla::WebService); +use Mojo::Base qw( Mojolicious::Controller ); +use Bugzilla; use Bugzilla::CGI; use Bugzilla::Constants; use Bugzilla::Error; use Bugzilla::Search; use Bugzilla::Search::Quicksearch; use Bugzilla::Util qw(mojo_user_agent); -use Bugzilla::API::V1::Util qw(validate); -use JSON; -use LWP::UserAgent; use URI; -use URI::Escape; use URI::QueryParam; -use constant PUBLIC_METHODS => qw( - 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); +sub setup_routes { + my ($class, $r) = @_; + $r->get('/bitly/shorten')->to('Bitly::API::V1::Bitly#shorten'); + $r->get('/bitly/list')->to('Bitly::API::V1::Bitly#list'); } +# GET /bitly/shorten?url= +# +# Shortens a buglist URL of this Bugzilla, without its user-specific list_id. +# Returns {url}. sub shorten { - my ($self) = shift; - my $uri = $self->_validate_uri(@_); + my ($self) = @_; + Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); + $self->bugzilla->login; - # the list_id is user-specific, remove it + my $uri = $self->_validate_uri; $uri->query_param_delete('list_id'); - return $self->_bitly($uri); + return $self->render(json => $self->_bitly($uri)); } +# GET /bitly/list?url= +# +# Runs the search and shortens a buglist URL of the matching bug IDs, so the +# link shows the same bugs whoever opens it. Returns {url}. sub list { - my ($self) = shift; - my $uri = $self->_validate_uri(@_); + my ($self) = @_; + Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); + $self->bugzilla->login; + + my $uri = $self->_validate_uri; # map params to cgi vars, converting quicksearch if required my $params @@ -71,7 +58,6 @@ sub list { ? 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'], @@ -89,32 +75,42 @@ sub list { {message => "Too many bugs returned by search"}); } - # shorten - return $self->_bitly($uri); + return $self->render(json => $self->_bitly($uri)); +} + +# Only buglist queries of this Bugzilla can be shortened. +sub _validate_uri { + my ($self) = @_; + + my $url = $self->every_param('url')->[0]; + defined $url + || ThrowCodeError('param_required', + {function => 'Bitly.shorten', param => 'url'}); + + 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 _bitly { my ($self, $uri) = @_; - # Make the request - my $ua = mojo_user_agent(); my $response - = $ua->post('https://api-ssl.bitly.com/v4/shorten' => + = mojo_user_agent() + ->post('https://api-ssl.bitly.com/v4/shorten' => {'Authorization' => 'Bearer ' . Bugzilla->params->{bitly_token}} => json => - {long_url => $uri->as_string})->result; + {long_url => $uri->as_string}) + ->result; if (!$response->is_success) { ThrowUserError('bitly_failure', {message => $response->message}); } - # return just the short URL return {url => $response->json->{link}}; } -sub rest_resources { - return [ - qr{^/bitly/shorten$}, {GET => {method => 'shorten',},}, - qr{^/bitly/list$}, {GET => {method => 'list',},}, - ]; -} - 1; diff --git a/extensions/Bitly/t/bmo/bitly.t b/extensions/Bitly/t/bmo/bitly.t new file mode 100644 index 0000000000..f2f75be77a --- /dev/null +++ b/extensions/Bitly/t/bmo/bitly.t @@ -0,0 +1,81 @@ +#!/usr/bin/env perl +# 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/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +use strict; +use warnings; +use 5.10.1; +use lib qw( . lib local/lib/perl5 ); + +BEGIN { + $ENV{LOG4PERL_CONFIG_FILE} = 'log4perl-t.conf'; + $ENV{BUGZILLA_DISABLE_HOSTAGE} = 1; +} + +use Test::More; +use Test::Mojo; +use Mojo::UserAgent; +use Mojolicious; + +use Bugzilla; +BEGIN { Bugzilla->extensions } +use Bugzilla::Extension::Bitly::API::V1::Bitly; + +# Stand-in for the Bitly API: echoes the long URL back as the short link. +my @sent; +my $bitly = Mojolicious->new; +$bitly->routes->post( + '/v4/shorten' => sub { + my $c = shift; + push @sent, $c->req->json->{long_url}; + $c->render(json => {link => 'https://bit.ly/' . scalar @sent}); + } +); +{ + no warnings 'redefine'; + *Bugzilla::Extension::Bitly::API::V1::Bitly::mojo_user_agent = sub { + my $ua = Mojo::UserAgent->new; + $ua->server->app($bitly); + $ua->on( + start => sub { + my $url = $_[1]->req->url; + $url->scheme('http')->host('127.0.0.1')->port($ua->server->url->port); + } + ); + return $ua; + }; +} + +my $urlbase = Bugzilla->localconfig->urlbase; +my $t = Test::Mojo->new('Bugzilla::App'); + +$t->get_ok('/rest/bitly/shorten') + ->status_is(400) + ->json_like('/message' => qr/url/, 'url is required'); + +$t->get_ok('/rest/bitly/shorten' => form => + {url => 'https://example.com/buglist.cgi?bug_id=1'}) + ->status_is(400) + ->json_like('/message' => qr/not supported/, 'other sites are refused'); + +$t->get_ok('/rest/bitly/shorten' => form => + {url => "${urlbase}buglist.cgi?bug_status=NEW&list_id=42"}) + ->status_is(200) + ->json_is('/url' => 'https://bit.ly/1'); +is($sent[-1], "${urlbase}buglist.cgi?bug_status=NEW", 'list_id is dropped'); + +$t->get_ok( + '/rest/bitly/list' => form => {url => "${urlbase}buglist.cgi?bug_id=1,2"}) + ->status_is(200) + ->json_is('/url' => 'https://bit.ly/2'); +like( + $sent[-1], + qr{^\Q${urlbase}\Ebuglist\.cgi\?bug_id=[\d,]*$}, + 'list shortens a bug_id only URL' +); + +done_testing; From 1dfb7509b2ff072f75c6cd17de4fec0dca5643b2 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Tue, 17 Nov 2026 19:37:08 +0100 Subject: [PATCH 03/19] Bug 2094122 - Migrate the ProdCompSearch REST resource to native Mojo API GET /rest/prod_comp_search/find/ and GET /rest/prod_comp_search/frequent are now served by the native controller Bugzilla::Extension::ProdCompSearch::API::V1::ProdCompSearch instead of the legacy REST server. Same paths, limit parameter, queries and responses. The legacy WebService module and its webservice hook are removed, and a test covers both routes. --- extensions/ProdCompSearch/Extension.pm | 7 -- .../V1/ProdCompSearch.pm} | 77 +++++++---------- .../ProdCompSearch/t/bmo/prod-comp-search.t | 86 +++++++++++++++++++ 3 files changed, 118 insertions(+), 52 deletions(-) rename extensions/ProdCompSearch/lib/{WebService.pm => API/V1/ProdCompSearch.pm} (78%) create mode 100644 extensions/ProdCompSearch/t/bmo/prod-comp-search.t diff --git a/extensions/ProdCompSearch/Extension.pm b/extensions/ProdCompSearch/Extension.pm index 6647eb08dc..0d14fab05b 100644 --- a/extensions/ProdCompSearch/Extension.pm +++ b/extensions/ProdCompSearch/Extension.pm @@ -15,11 +15,4 @@ use base qw(Bugzilla::Extension); our $VERSION = '1'; -sub webservice { - my ($self, $args) = @_; - my $dispatch = $args->{dispatch}; - $dispatch->{PCS} = "Bugzilla::Extension::ProdCompSearch::WebService"; -} - - __PACKAGE__->NAME; diff --git a/extensions/ProdCompSearch/lib/WebService.pm b/extensions/ProdCompSearch/lib/API/V1/ProdCompSearch.pm similarity index 78% rename from extensions/ProdCompSearch/lib/WebService.pm rename to extensions/ProdCompSearch/lib/API/V1/ProdCompSearch.pm index 21d8a16537..f87944e698 100644 --- a/extensions/ProdCompSearch/lib/WebService.pm +++ b/extensions/ProdCompSearch/lib/API/V1/ProdCompSearch.pm @@ -5,60 +5,43 @@ # This Source Code Form is "Incompatible With Secondary Licenses", as # defined by the Mozilla Public License, v. 2.0. -package Bugzilla::Extension::ProdCompSearch::WebService; +package Bugzilla::Extension::ProdCompSearch::API::V1::ProdCompSearch; use 5.10.1; -use strict; -use warnings; - -use base qw(Bugzilla::WebService); +use Mojo::Base qw( Mojolicious::Controller ); +use Bugzilla; +use Bugzilla::Constants; use Bugzilla::Error; use Bugzilla::Util qw(detaint_natural trim); use Time::localtime; -############# -# Constants # -############# - -use constant PUBLIC_METHODS => qw( - prod_comp_search -); - -sub rest_resources { - return [ - qr{^/prod_comp_search/find/(.*)$}, - { - GET => { - method => 'prod_comp_search', - params => sub { - return {search => $_[0]}; - } - } - }, - qr{^/prod_comp_search/frequent}, - {GET => {method => 'list_frequent_components',}} - ]; +sub setup_routes { + my ($class, $r) = @_; + $r->get('/prod_comp_search/find/*search') + ->to('ProdCompSearch::API::V1::ProdCompSearch#prod_comp_search'); + $r->get('/prod_comp_search/frequent') + ->to('ProdCompSearch::API::V1::ProdCompSearch#list_frequent_components'); } -################## -# Public Methods # -################## - +# GET /prod_comp_search/find/?limit= +# +# Products and components the user can file bugs in matching the words of +# ("product::component" searches each part separately). Returns +# {products => [{product}, {product, component}, ...]}. sub prod_comp_search { - my ($self, $params) = @_; - my $user = Bugzilla->user; + my ($self) = @_; + Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); + my $user = $self->bugzilla->login; my $dbh = Bugzilla->switch_to_shadow_db(); - my $search = trim($params->{'search'} || ''); + my $search = trim($self->param('search') // ''); $search || ThrowCodeError('param_required', {function => 'PCS.prod_comp_search', param => 'search'}); - my $limit - = detaint_natural($params->{'limit'}) - ? $dbh->sql_limit($params->{'limit'}) - : ''; + my $limit = $self->param('limit'); + $limit = detaint_natural($limit) ? $dbh->sql_limit($limit) : ''; # We do this in the DB directly as we want it to be fast and # not have the overhead of loading full product objects @@ -91,7 +74,7 @@ sub prod_comp_search { ); } - return {products => []} if !scalar @$enterable_ids; + return $self->render(json => {products => []}) if !scalar @$enterable_ids; my @terms; my @order; @@ -115,7 +98,7 @@ sub prod_comp_search { push @order, "products.name"; push @order, "components.name"; } - return {products => []} if !scalar @terms; + return $self->render(json => {products => []}) if !scalar @terms; # To help Mozilla staff file BMO administration bugs into the right # component, sort BMO first when searching for 'bugzilla' @@ -148,16 +131,20 @@ sub prod_comp_search { } push @$products, $component; } - return {products => $products}; + return $self->render(json => {products => $products}); } -# Get a list of components the user has frequently reported in the past 2 years +# GET /prod_comp_search/frequent +# +# The components the user has reported the most bugs in over the past 2 years, +# at most 10. Returns {results => [{product, component}, ...]}. sub list_frequent_components { my ($self) = @_; - my $user = Bugzilla->user; + Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); + my $user = $self->bugzilla->login; # Nothing to show if the user is signed out - return {results => []} unless $user->id; + return $self->render(json => {results => []}) unless $user->id; # Select the date of 2 years ago today my $now = localtime; @@ -174,7 +161,7 @@ sub list_frequent_components { }; my $results = $dbh->selectall_arrayref($sql, {Slice => {}}, $user->id, $date); - return {results => $results}; + return $self->render(json => {results => $results}); } ################### diff --git a/extensions/ProdCompSearch/t/bmo/prod-comp-search.t b/extensions/ProdCompSearch/t/bmo/prod-comp-search.t new file mode 100644 index 0000000000..1305af4244 --- /dev/null +++ b/extensions/ProdCompSearch/t/bmo/prod-comp-search.t @@ -0,0 +1,86 @@ +#!/usr/bin/env perl +# 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/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +use strict; +use warnings; +use 5.10.1; +use lib qw( . lib local/lib/perl5 ); + +BEGIN { + $ENV{LOG4PERL_CONFIG_FILE} = 'log4perl-t.conf'; + $ENV{BUGZILLA_DISABLE_HOSTAGE} = 1; +} + +use Test::More; +use Test::Mojo; + +use Bugzilla; +BEGIN { Bugzilla->extensions } +use Bugzilla::Constants; +use Bugzilla::Bug; +use Bugzilla::Product; +use Bugzilla::User; +use Bugzilla::Test::Util qw(issue_api_key); + +Bugzilla->usage_mode(USAGE_MODE_TEST); +Bugzilla->error_mode(ERROR_MODE_DIE); + +my $admin = Bugzilla::User->new({name => 'admin@mozilla.bugs'}); +my ($product) + = grep { $_->is_active && @{$_->components} && @{$_->versions} } + Bugzilla::Product->get_all; +plan skip_all => 'Need the admin user and a product with a component' + unless $admin && $product; +my $component = $product->components->[0]->name; + +# A bug reported by admin, for the frequent components list. +Bugzilla->set_user($admin); +Bugzilla::Bug->create({ + short_desc => "ProdCompSearch frequent test $$", + product => $product->name, + component => $component, + bug_type => 'defect', + bug_severity => 'normal', + op_sys => 'Unspecified', + rep_platform => 'Unspecified', + version => $product->versions->[0]->name, +}); + +my %auth = ('X-Bugzilla-API-Key' => issue_api_key($admin->login)->api_key); +my $t = Test::Mojo->new('Bugzilla::App'); + +$t->get_ok('/rest/prod_comp_search/find/' + . $product->name . '::' + . $component => \%auth => form => {limit => 1})->status_is(200)->json_is( + '/products' => [ + {product => $product->name}, + {product => $product->name, component => $component} + ], + 'product::component finds the component, limit applies' + ); + +$t->get_ok("/rest/prod_comp_search/find/nosuchproduct$$") + ->status_is(200) + ->json_is('/products' => [], 'no match'); + +$t->get_ok('/rest/prod_comp_search/frequent') + ->status_is(200) + ->json_is('/results' => [], 'nothing for a signed out user'); + +$t->get_ok('/rest/prod_comp_search/frequent' => \%auth) + ->status_is(200) + ->json_has('/results/0', 'frequent components for the reporter'); +ok( + ( + grep { $_->{product} eq $product->name && $_->{component} eq $component } + @{$t->tx->res->json->{results}} + ), + 'the reported component is listed' +); + +done_testing; From d8b9ef1c6fed6461d7bb92695f6b1a0c21c1550d Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Tue, 17 Nov 2026 20:01:54 +0100 Subject: [PATCH 04/19] Bug 2094122 - Migrate the EditComments REST resource to native Mojo API GET /rest/editcomments/comment[/], PUT /rest/editcomments/comment/ and PUT /rest/editcomments/revision are now served by the native controller Bugzilla::Extension::EditComments::API::V1::EditComments instead of the legacy REST server. Same paths, parameters, checks and responses and is_hidden must now be a boolean (true/false/1/0). The legacy WebService module and its webservice hook are removed, and a test covers the four routes. --- extensions/EditComments/Extension.pm | 6 - .../EditComments/lib/API/V1/EditComments.pm | 236 +++++++++++++ extensions/EditComments/lib/WebService.pm | 317 ------------------ .../EditComments/t/bmo/editcomments-api.t | 128 +++++++ 4 files changed, 364 insertions(+), 323 deletions(-) create mode 100644 extensions/EditComments/lib/API/V1/EditComments.pm delete mode 100644 extensions/EditComments/lib/WebService.pm create mode 100644 extensions/EditComments/t/bmo/editcomments-api.t diff --git a/extensions/EditComments/Extension.pm b/extensions/EditComments/Extension.pm index 1d45919680..42d77ad20a 100644 --- a/extensions/EditComments/Extension.pm +++ b/extensions/EditComments/Extension.pm @@ -473,12 +473,6 @@ sub get_bug_activity { } } -sub webservice { - my ($self, $args) = @_; - my $dispatch = $args->{dispatch}; - $dispatch->{EditComments} = "Bugzilla::Extension::EditComments::WebService"; -} - sub db_sanitize { my $dbh = Bugzilla->dbh; print "Deleting edited comment histories...\n"; diff --git a/extensions/EditComments/lib/API/V1/EditComments.pm b/extensions/EditComments/lib/API/V1/EditComments.pm new file mode 100644 index 0000000000..38205e3b39 --- /dev/null +++ b/extensions/EditComments/lib/API/V1/EditComments.pm @@ -0,0 +1,236 @@ +# 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/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +package Bugzilla::Extension::EditComments::API::V1::EditComments; + +use 5.10.1; +use Mojo::Base qw( Mojolicious::Controller ); + +use Bugzilla; +use Bugzilla::Bug; +use Bugzilla::Comment; +use Bugzilla::Constants; +use Bugzilla::Error; +use Bugzilla::Template; +use Bugzilla::Util qw(trim); +use Bugzilla::API::V1::Util qw(coerce_booleans merge_request_params); + +sub setup_routes { + my ($class, $r) = @_; + my $to = 'EditComments::API::V1::EditComments#'; + $r->get('/editcomments/comment')->to($to . 'comments'); + $r->get('/editcomments/comment/:comment_id' => [comment_id => qr/\d+/]) + ->to($to . 'comments'); + $r->put('/editcomments/comment/:comment_id' => [comment_id => qr/\d+/]) + ->to($to . 'update_comment'); + $r->put('/editcomments/revision')->to($to . 'modify_revision'); +} + +# Request params, with the comment ID from the path if there is one. +sub _params { + my ($self, $lists) = @_; + my ($params, $error) = merge_request_params($self, $lists); + return (undef, $error) if $error; + if (defined(my $id = $self->param('comment_id'))) { + $params->{comment_id} = $id; + $params->{comment_ids} = [$id] if $lists; + } + return ($params, undef); +} + +# GET /editcomments/comment?comment_ids=&comment_ids=... +# GET /editcomments/comment/ +# +# Returns {comments => { => }}. +sub comments { + my ($self) = @_; + Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); + my $user = $self->bugzilla->login; + Bugzilla->switch_to_shadow_db(); + + my ($params, $error) = $self->_params(['comment_ids']); + return $self->user_error($error) if $error; + + if (!@{$params->{comment_ids} || []}) { + ThrowCodeError('param_required', + {function => 'Bug.comments', param => 'comment_ids'}); + } + + my @ids = map { trim($_) } @{$params->{comment_ids}}; + my $comment_data = Bugzilla::Comment->new_from_list(\@ids); + + # See if we were passed any invalid comment ids. + my %got_ids = map { $_->id => 1 } @$comment_data; + foreach my $comment_id (@ids) { + if (!$got_ids{$comment_id}) { + ThrowUserError('comment_id_invalid', {id => $comment_id}); + } + } + + # Now make sure that we can see all the associated bugs. + my %got_bug_ids = map { $_->bug_id => 1 } @$comment_data; + $user->visible_bugs([keys %got_bug_ids]); # preload cache for visibility check + Bugzilla::Bug->check($_) foreach (keys %got_bug_ids); + + my %comments; + foreach my $comment (@$comment_data) { + if ($comment->is_private && !$user->is_insider) { + ThrowUserError('comment_is_private', {id => $comment->id}); + } + $comments{$comment->id} = $comment->body; + } + + return $self->render(json => {comments => \%comments}); +} + +# PUT /editcomments/comment/ +# +# Replaces the comment's text. Params: +# new_comment the new text +# is_hidden hide the old revision (edit_comments_admins_group only) +# Returns {text, html, count}, count being the number of revisions. +sub update_comment { + my ($self) = @_; + Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); + my $user = $self->bugzilla->login; + $user->id || return $self->user_error('login_required'); + + my ($params, $error) = $self->_params; + return $self->user_error($error) if $error; + if ($error = coerce_booleans($params, 'is_hidden')) { + return $self->user_error('invalid_params', {type_error => $error}); + } + my $comment_id = $params->{comment_id}; + + ThrowCodeError('param_required', + {function => 'EditComments.update_comment', param => 'new_comment'}) + unless defined $params->{new_comment} && trim($params->{new_comment}) ne ''; + + my $comment = Bugzilla::Comment->new($comment_id); + + # Validate comment visibility + ThrowUserError('comment_id_invalid', {id => $comment_id}) unless $comment; + ThrowUserError('comment_is_private', {id => $comment->id}) + unless $user->is_insider || !$comment->is_private; + + my $bug = $comment->bug; + + # Validate bug visibility + $bug->check_is_visible(); + + # Check if user can edit this comment. + ThrowUserError('auth_failure', {action => 'view', object => 'editcomments'}) + unless $comment->is_editable_by($user); + + my $old_comment = $comment->body; + my $new_comment = $comment->_check_thetext($params->{new_comment}); + + # Make sure there is any change in the comment + ThrowCodeError('param_no_changes', + {function => 'EditComments.update_comment', param => 'new_comment'}) + if $old_comment eq $new_comment; + + my $dbh = Bugzilla->dbh; + my $timestamp = $dbh->selectrow_array('SELECT NOW()'); + + # edit_comments_admins_group members can hide comment revisions where needed + my $is_hidden = $user->is_edit_comments_admin && $params->{is_hidden} ? 1 : 0; + + $comment->update_text({ + text => $new_comment, + timestamp => $timestamp, + is_hidden => $is_hidden, + sync_fulltext => 1, + }); + + # Render what the bug page shows, which can differ from the stored text: the + # comment_body_full hook lets extensions hide part of it (eg. Hackbot's front + # matter). + my $body = $comment->body_full({exclude_attachment => 1}); + my $html + = $comment->is_markdown && Bugzilla->params->{use_markdown} + ? Bugzilla->markdown->render_html($body, $bug) + : Bugzilla::Template::quoteUrls($body, $bug); + + my $count + = $dbh->selectrow_array( + 'SELECT COUNT(*) FROM longdescs_activity WHERE comment_id = ?', + undef, $comment_id); + + return $self->render( + json => { + text => Bugzilla::API::V1::Util->type('string', $new_comment), + html => Bugzilla::API::V1::Util->type('string', $html), + count => Bugzilla::API::V1::Util->type('int', $count), + } + ); +} + +# PUT /editcomments/revision +# +# Hides or shows a revision of a comment (edit_comments_admins_group only). +# Params: comment_id, change_when (YYYY-MM-DD HH:MM:SS), is_hidden. +# Returns {change_when, is_hidden}. +sub modify_revision { + my ($self) = @_; + Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); + my $user = $self->bugzilla->login; + $user->id || return $self->user_error('login_required'); + + # Only allow edit_comments_admins_group members to modify revisions + ThrowUserError( + 'auth_failure', + { + group => 'edit_comments_admins_group', + action => 'view', + object => 'editcomments' + } + ) unless $user->is_edit_comments_admin; + + my ($params, $error) = merge_request_params($self); + return $self->user_error($error) if $error; + if ($error = coerce_booleans($params, 'is_hidden')) { + return $self->user_error('invalid_params', {type_error => $error}); + } + + my $comment_id + = (defined $params->{comment_id} && $params->{comment_id} =~ /^(\d+)$/) + ? $1 + : undef; + my $change_when + = (defined $params->{change_when} + && $params->{change_when} =~ /^(\d{4}-\d{2}-\d{2}\ \d{2}:\d{2}:\d{2})$/) + ? $1 + : undef; + my $is_hidden = $params->{is_hidden} ? 1 : 0; + + # Validate parameters + ThrowCodeError('param_required', + {function => 'EditComments.modify_revision', param => 'comment_id'}) + unless defined $comment_id; + ThrowCodeError('param_required', + {function => 'EditComments.modify_revision', param => 'change_when'}) + unless defined $change_when; + + # Update revision visibility + Bugzilla->dbh->do( + 'UPDATE longdescs_activity SET is_hidden = ? WHERE comment_id = ? AND change_when = ?', + undef, $is_hidden, $comment_id, $change_when + ); + + # Clear memcached entry of comment 0 authors + Bugzilla->memcached->clear({key => 'c0:' . $comment_id}); + + return $self->render( + json => { + change_when => Bugzilla::API::V1::Util->type('dateTime', $change_when), + is_hidden => Bugzilla::API::V1::Util->type('boolean', $is_hidden), + } + ); +} + +1; diff --git a/extensions/EditComments/lib/WebService.pm b/extensions/EditComments/lib/WebService.pm deleted file mode 100644 index 23ec807f01..0000000000 --- a/extensions/EditComments/lib/WebService.pm +++ /dev/null @@ -1,317 +0,0 @@ -# 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/. -# -# This Source Code Form is "Incompatible With Secondary Licenses", as -# defined by the Mozilla Public License, v. 2.0. - -package Bugzilla::Extension::EditComments::WebService; - -use 5.10.1; -use strict; -use warnings; - -use base qw(Bugzilla::WebService); - -use Bugzilla; -use Bugzilla::Comment; -use Bugzilla::Constants; -use Bugzilla::Error; -use Bugzilla::Template; -use Bugzilla::Util qw(trim); -use Bugzilla::API::V1::Util qw(validate); - -use constant PUBLIC_METHODS => qw( - comments - update_comment - modify_revision -); - -sub comments { - my ($self, $params) = validate(@_, 'comment_ids'); - my $dbh = Bugzilla->switch_to_shadow_db(); - my $user = Bugzilla->user; - - if (!defined $params->{comment_ids}) { - ThrowCodeError('param_required', - {function => 'Bug.comments', param => 'comment_ids'}); - } - - my @ids = map { trim($_) } @{$params->{comment_ids} || []}; - my $comment_data = Bugzilla::Comment->new_from_list(\@ids); - - # See if we were passed any invalid comment ids. - my %got_ids = map { $_->id => 1 } @$comment_data; - foreach my $comment_id (@ids) { - if (!$got_ids{$comment_id}) { - ThrowUserError('comment_id_invalid', {id => $comment_id}); - } - } - - # Now make sure that we can see all the associated bugs. - my %got_bug_ids = map { $_->bug_id => 1 } @$comment_data; - $user->visible_bugs([keys %got_bug_ids]); # preload cache for visibility check - Bugzilla::Bug->check($_) foreach (keys %got_bug_ids); - - my %comments; - foreach my $comment (@$comment_data) { - if ($comment->is_private && !$user->is_insider) { - ThrowUserError('comment_is_private', {id => $comment->id}); - } - $comments{$comment->id} = $comment->body; - } - - return {comments => \%comments}; -} - -# See Bugzilla::Extension::EditComments->bug_end_of_update for the original implementation. -# This should be migrated to the standard API method at /rest/bug/comment/(comment_id) -sub update_comment { - my ($self, $params) = @_; - my $user = Bugzilla->login(LOGIN_REQUIRED); - - my $comment_id - = (defined $params->{comment_id} && $params->{comment_id} =~ /^(\d+)$/) - ? $1 - : undef; - - # Validate parameters - ThrowCodeError('param_required', - {function => 'EditComments.update_comment', param => 'comment_id'}) - unless defined $comment_id; - ThrowCodeError('param_required', - {function => 'EditComments.update_comment', param => 'new_comment'}) - unless defined $params->{new_comment} && trim($params->{new_comment}) ne ''; - - my $comment = Bugzilla::Comment->new($comment_id); - - # Validate comment visibility - ThrowUserError('comment_id_invalid', {id => $comment_id}) unless $comment; - ThrowUserError('comment_is_private', {id => $comment->id}) - unless $user->is_insider || !$comment->is_private; - - my $bug = $comment->bug; - - # Validate bug visibility - $bug->check_is_visible(); - - # Check if user can edit this comment. - ThrowUserError('auth_failure', {action => 'view', object => 'editcomments'}) - unless $comment->is_editable_by($user); - - my $old_comment = $comment->body; - my $new_comment = $comment->_check_thetext($params->{new_comment}); - - # Make sure there is any change in the comment - ThrowCodeError('param_no_changes', - {function => 'EditComments.update_comment', param => 'new_comment'}) - if $old_comment eq $new_comment; - - my $dbh = Bugzilla->dbh; - my $timestamp = $dbh->selectrow_array('SELECT NOW()'); - - # edit_comments_admins_group members can hide comment revisions where needed - my $is_hidden - = ( $user->is_edit_comments_admin - && defined $params->{is_hidden} - && $params->{is_hidden} == 1) ? 1 : 0; - - $comment->update_text({ - text => $new_comment, - timestamp => $timestamp, - is_hidden => $is_hidden, - sync_fulltext => 1, - }); - - # Render what the bug page shows, which can differ from the stored text: the - # comment_body_full hook lets extensions hide part of it (eg. Hackbot's front - # matter). - my $body = $comment->body_full({exclude_attachment => 1}); - my $html - = $comment->is_markdown && Bugzilla->params->{use_markdown} - ? Bugzilla->markdown->render_html($body, $bug) - : Bugzilla::Template::quoteUrls($body, $bug); - - # Respond with the updated comment and number of revisions - return { - text => $self->type('string', $new_comment), - html => $self->type('string', $html), - count => $self->type( - 'int', - $dbh->selectrow_array( - 'SELECT COUNT(*) FROM longdescs_activity - WHERE comment_id = ?', - undef, ($comment_id) - ) - ), - }; -} - -sub modify_revision { - my ($self, $params) = @_; - my $user = Bugzilla->login(LOGIN_REQUIRED); - - # Only allow edit_comments_admins_group members to modify revisions - ThrowUserError('auth_failure', - {group => 'edit_comments_admins_group', action => 'view', object => 'editcomments'}) - unless $user->is_edit_comments_admin; - - my $comment_id - = (defined $params->{comment_id} && $params->{comment_id} =~ /^(\d+)$/) - ? $1 - : undef; - my $change_when - = (defined $params->{change_when} - && $params->{change_when} =~ /^(\d{4}-\d{2}-\d{2}\ \d{2}:\d{2}:\d{2})$/) - ? $1 - : undef; - my $is_hidden - = defined $params->{is_hidden} && $params->{is_hidden} == 1 ? 1 : 0; - - # Validate parameters - ThrowCodeError('param_required', - {function => 'EditComments.modify_revision', param => 'comment_id'}) - unless defined $comment_id; - ThrowCodeError('param_required', - {function => 'EditComments.modify_revision', param => 'change_when'}) - unless defined $change_when; - - my $dbh = Bugzilla->dbh; - - # Update revision visibility - $dbh->do( - 'UPDATE longdescs_activity SET is_hidden = ? WHERE comment_id = ? AND change_when = ?', - undef, - ($is_hidden, $comment_id, $change_when) - ); - - # Clear memcached entry of comment 0 authors - Bugzilla->memcached->clear({ key => 'c0:' . $comment_id }); - - # Respond with updated revision info - return { - change_when => $self->type('dateTime', $change_when), - is_hidden => $self->type('boolean', $is_hidden), - }; -} - -sub rest_resources { - return [ - qr{^/editcomments/comment/(\d+)$}, - { - GET => { - method => 'comments', - params => sub { - return {comment_ids => $_[0]}; - }, - }, - PUT => { - method => 'update_comment', - params => sub { - return {comment_id => $_[0]}; - }, - }, - }, - qr{^/editcomments/comment$}, - {GET => {method => 'comments',},}, - qr{^/editcomments/revision$}, - {PUT => {method => 'modify_revision',},}, - ]; -} - - -1; - -__END__ - -=head1 NAME - -Bugzilla::Extension::EditComments::Webservice - The EditComments WebServices API - -=head1 DESCRIPTION - -This module contains API methods that are useful to user's of bugzilla.mozilla.org. - -=head1 METHODS - -=head2 comments - -B - -=over - -=item B - -This allows you to get the raw comment text about comments, given a list of comment ids. - -=item B - -To get all comment text for a list of comment ids: - -GET /bug/editcomments/comment?comment_ids=1234&comment_ids=5678... - -To get comment text for a specific comment based on the comment ID: - -GET /bug/editcomments/comment/ - -The returned data format is the same as below. - -=item B - -=over - -=item C (required) - -C An array of integer comment_ids. These comments will be -returned individually, separate from any other comments in their -respective bugs. - -=item B - -1 item is returned: - -=over - -=item C - -Each individual comment requested in C is returned here, -in a hash where the numeric comment id is the key, and the value -is the comment's raw text. - -=back - -=item B - -In addition to standard Bug.get type errors, this method can throw the -following additional errors: - -=over - -=item 110 (Comment Is Private) - -You specified the id of a private comment in the C -argument, and you are not in the "insider group" that can see -private comments. - -=item 111 (Invalid Comment ID) - -You specified an id in the C argument that is invalid--either -you specified something that wasn't a number, or there is no comment with -that id. - -=back - -=item B - -=over - -=item Added in BMO Bugzilla B<4.2>. - -=back - -=back - -=back - -See L for a description of how parameters are passed, -and what B, B, and B mean. diff --git a/extensions/EditComments/t/bmo/editcomments-api.t b/extensions/EditComments/t/bmo/editcomments-api.t new file mode 100644 index 0000000000..85b0f2a73a --- /dev/null +++ b/extensions/EditComments/t/bmo/editcomments-api.t @@ -0,0 +1,128 @@ +#!/usr/bin/env perl +# 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/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +use strict; +use warnings; +use 5.10.1; +use lib qw( . lib local/lib/perl5 ); + +BEGIN { + $ENV{LOG4PERL_CONFIG_FILE} = 'log4perl-t.conf'; + $ENV{BUGZILLA_DISABLE_HOSTAGE} = 1; +} + +use Test::More; +use Test::Mojo; + +use Bugzilla; +BEGIN { Bugzilla->extensions } +use Bugzilla::Constants; +use Bugzilla::Bug; +use Bugzilla::Product; +use Bugzilla::User; +use Bugzilla::Test::Util qw(issue_api_key); + +Bugzilla->usage_mode(USAGE_MODE_TEST); +Bugzilla->error_mode(ERROR_MODE_DIE); + +my $admin = Bugzilla::User->new({name => 'admin@mozilla.bugs'}); +my ($product) + = grep { $_->is_active && @{$_->components} && @{$_->versions} } + Bugzilla::Product->get_all; +plan skip_all => 'Need an edit-comments admin and a product' + unless $admin && $admin->is_edit_comments_admin && $product; + +my $plain + = Bugzilla::User->create({ + login_name => "editcomments-api-$$\@bmo.tld", cryptpassword => '*', + }); + +Bugzilla->set_user($admin); +my $bug = Bugzilla::Bug->create({ + short_desc => "EditComments API test $$", + product => $product->name, + component => $product->components->[0]->name, + bug_type => 'defect', + bug_severity => 'normal', + op_sys => 'Unspecified', + rep_platform => 'Unspecified', + version => $product->versions->[0]->name, + comment => 'first comment', +}); +$bug->add_comment('second comment'); +$bug->update; +my ($c0, $c1) = map { $_->id } @{Bugzilla::Bug->new($bug->id)->comments}; + +my %admin_auth + = ('X-Bugzilla-API-Key' => issue_api_key($admin->login)->api_key); +my %plain_auth + = ('X-Bugzilla-API-Key' => issue_api_key($plain->login)->api_key); +my $t = Test::Mojo->new('Bugzilla::App'); + +# GET +$t->get_ok("/rest/editcomments/comment/$c0") + ->status_is(200) + ->json_is("/comments/$c0" => 'first comment', 'one comment by path'); +$t->get_ok('/rest/editcomments/comment' => form => {comment_ids => [$c0, $c1]}) + ->status_is(200) + ->json_is( + '/comments' => {$c0 => 'first comment', $c1 => 'second comment'}, + 'several comments by query' + ); +$t->get_ok('/rest/editcomments/comment') + ->status_is(400) + ->json_like('/message' => qr/comment_ids/, 'comment_ids is required'); + +# PUT /comment/ +my $comment_url = "/rest/editcomments/comment/$c1"; +$t->put_ok($comment_url => json => {new_comment => 'x'}) + ->status_is(401) + ->json_is('/code' => 410, 'login required'); +$t->put_ok($comment_url => \%plain_auth => json => {new_comment => 'x'}) + ->status_isnt(200) + ->json_like('/message' => qr/not authorized/, 'others cannot edit it'); +$t->put_ok($comment_url => \%admin_auth => json => + {new_comment => 'x', is_hidden => 'maybe'}) + ->status_is(400) + ->json_like('/message' => qr/is_hidden must be true or false/); +$t->put_ok($comment_url => \%admin_auth => json => + {new_comment => 'edited comment', is_hidden => 1}) + ->status_is(200) + ->json_is('/text' => 'edited comment') + ->json_is('/count' => 1, 'one revision'); +like($t->tx->res->json->{html}, qr/edited comment/, 'html is returned'); +$t->put_ok( + $comment_url => \%admin_auth => json => {new_comment => 'edited comment'}) + ->status_isnt(200) + ->json_like('/message' => qr/new_comment/, 'refuses an unchanged comment'); + +# PUT /revision +my $dbh = Bugzilla->dbh; +my ($change_when, $is_hidden) + = $dbh->selectrow_array( + 'SELECT change_when, is_hidden FROM longdescs_activity WHERE comment_id = ?', + undef, $c1); +is($is_hidden, 1, 'the admin hid the old revision'); + +my %revision = (comment_id => $c1, change_when => $change_when, is_hidden => 0); +$t->put_ok('/rest/editcomments/revision' => \%plain_auth => json => \%revision) + ->status_isnt(200) + ->json_like('/message' => qr/edit_comments_admins_group/, 'admins only'); +$t->put_ok('/rest/editcomments/revision' => \%admin_auth => json => \%revision) + ->status_is(200) + ->json_is('/is_hidden' => Mojo::JSON::false); +is( + $dbh->selectrow_array( + 'SELECT is_hidden FROM longdescs_activity WHERE comment_id = ?', + undef, $c1 + ), + 0, + 'the revision is shown again' +); + +done_testing; From 7d7e6fd31491eb553145883a048e9d42fa87fa7c Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Wed, 18 Nov 2026 19:50:22 +0100 Subject: [PATCH 05/19] Bug 2094122 - Migrate the ComponentWatching REST resource to native Mojo API GET and POST /rest/component-watching, GET and DELETE /rest/component-watching/ are now served by the native controller Bugzilla::Extension::ComponentWatching::API::V1::ComponentWatching instead of the legacy REST server. Same paths, parameters, checks and responses, except that product_access_denied now names the product (it was passed as product instead of name, so the message showed ''). The Preferences page gets the same fix. The legacy WebService module and its webservice hook are removed, and a test covers the four routes. rest_legacy_credentials.t moves to GET /rest/bug_modal/initial_field_values, still on the legacy server. --- extensions/ComponentWatching/Extension.pm | 14 +- .../lib/API/V1/ComponentWatching.pm | 134 ++++++++++++++++++ .../ComponentWatching/lib/WebService.pm | 115 --------------- .../t/bmo/component-watching-api.t | 128 +++++++++++++++++ qa/t/rest_legacy_credentials.t | 10 +- 5 files changed, 270 insertions(+), 131 deletions(-) create mode 100644 extensions/ComponentWatching/lib/API/V1/ComponentWatching.pm delete mode 100644 extensions/ComponentWatching/lib/WebService.pm create mode 100644 extensions/ComponentWatching/t/bmo/component-watching-api.t diff --git a/extensions/ComponentWatching/Extension.pm b/extensions/ComponentWatching/Extension.pm index c859e479f7..a655760a60 100644 --- a/extensions/ComponentWatching/Extension.pm +++ b/extensions/ComponentWatching/Extension.pm @@ -297,7 +297,7 @@ sub user_preferences { my $productName = $input->{'add_product'}; my $product = Bugzilla::Product->new({name => $productName, cache => 1}); unless ($product && $user->can_access_product($product)) { - ThrowUserError('product_access_denied', {product => $productName}); + ThrowUserError('product_access_denied', {name => $productName}); } # starting-with @@ -323,7 +323,7 @@ sub user_preferences { name => $componentName, product => $product, cache => 1 }); unless ($component) { - ThrowUserError('product_access_denied', {product => $productName}); + ThrowUserError('product_access_denied', {name => $productName}); } _addComponentWatch($user, $component); } @@ -709,14 +709,4 @@ sub db_sanitize { $dbh->do('TRUNCATE TABLE component_watch'); } -# -# webservice -# - -sub webservice { - my ($self, $args) = @_; - $args->{dispatch}->{ComponentWatching} - = "Bugzilla::Extension::ComponentWatching::WebService"; -} - __PACKAGE__->NAME; diff --git a/extensions/ComponentWatching/lib/API/V1/ComponentWatching.pm b/extensions/ComponentWatching/lib/API/V1/ComponentWatching.pm new file mode 100644 index 0000000000..3e549daa8e --- /dev/null +++ b/extensions/ComponentWatching/lib/API/V1/ComponentWatching.pm @@ -0,0 +1,134 @@ +# 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/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +package Bugzilla::Extension::ComponentWatching::API::V1::ComponentWatching; + +use 5.10.1; +use Mojo::Base qw( Mojolicious::Controller ); + +use Bugzilla; +use Bugzilla::Component; +use Bugzilla::Constants; +use Bugzilla::Error; +use Bugzilla::Product; +use Bugzilla::API::V1::Util qw(merge_request_params); + +sub setup_routes { + my ($class, $r) = @_; + my $to = 'ComponentWatching::API::V1::ComponentWatching#'; + $r->get('/component-watching')->to($to . 'list'); + $r->post('/component-watching')->to($to . 'add'); + $r->get('/component-watching/:id' => [id => qr/\d+/])->to($to . 'get'); + $r->delete('/component-watching/:id' => [id => qr/\d+/])->to($to . 'remove'); +} + +# +# API methods based on Bugzilla::Extension::ComponentWatching->user_preferences +# + +# The logged-in user, or undef after rendering a login_required error. +sub _login { + my ($self) = @_; + Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); + my $user = $self->bugzilla->login; + return $user if $user->id; + $self->user_error('login_required'); + return undef; +} + +# GET /component-watching +# +# Returns the user's watches, sorted by product, component and prefix. +sub list { + my ($self) = @_; + my $user = $self->_login || return; + + return $self->render( + json => Bugzilla::Extension::ComponentWatching::_getWatches($user)); +} + +# POST /component-watching +# +# Watches a product (no component, or an empty one) or components of it. +# Params: product, component (a name or a list of names). Returns the new +# watch, or null when it was already watched. +sub add { + my ($self) = @_; + my $user = $self->_login || return; + + my ($params, $error) = merge_request_params($self, ['component']); + return $self->user_error($error) if $error; + my $result; + + # load product and verify access + my $productName = $params->{'product'}; + my $product = Bugzilla::Product->new({name => $productName, cache => 1}); + unless ($product && $user->can_access_product($product)) { + ThrowUserError('product_access_denied', {name => $productName}); + } + + my $ra_componentNames = $params->{'component'}; + $ra_componentNames = [$ra_componentNames || ''] unless ref($ra_componentNames); + + if (grep { $_ eq '' } @$ra_componentNames) { + + # watching a product + $result + = Bugzilla::Extension::ComponentWatching::_addProductWatch($user, $product); + + } + else { + # watching specific components + foreach my $componentName (@$ra_componentNames) { + my $component + = Bugzilla::Component->new({ + name => $componentName, product => $product, cache => 1 + }); + unless ($component) { + ThrowUserError('product_access_denied', {name => $productName}); + } + $result = Bugzilla::Extension::ComponentWatching::_addComponentWatch($user, + $component); + } + } + + Bugzilla::Extension::ComponentWatching::_addDefaultSettings($user); + + return $self->render(json => $result); +} + +# GET /component-watching/ +# +# Returns the user's watch with this id, or {} if there is none. +sub get { + my ($self) = @_; + my $user = $self->_login || return; + + return $self->render( + json => Bugzilla::Extension::ComponentWatching::_getWatches( + $user, $self->param('id') + ) + ); +} + +# DELETE /component-watching/ +# +# Removes the user's watch with this id. Returns {status => }. +sub remove { + my ($self) = @_; + my $user = $self->_login || return; + + return $self->render( + json => { + status => Bugzilla::Extension::ComponentWatching::_deleteWatch( + $user, $self->param('id') + ) + } + ); +} + +1; diff --git a/extensions/ComponentWatching/lib/WebService.pm b/extensions/ComponentWatching/lib/WebService.pm deleted file mode 100644 index 331842f7bc..0000000000 --- a/extensions/ComponentWatching/lib/WebService.pm +++ /dev/null @@ -1,115 +0,0 @@ -# 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/. -# -# This Source Code Form is "Incompatible With Secondary Licenses", as -# defined by the Mozilla Public License, v. 2.0. - -package Bugzilla::Extension::ComponentWatching::WebService; - -use 5.10.1; -use strict; -use warnings; - -use base qw(Bugzilla::WebService); - -use Bugzilla; -use Bugzilla::Component; -use Bugzilla::Constants; -use Bugzilla::Error; -use Bugzilla::Product; -use Bugzilla::User; - -sub rest_resources { - return [ - qr{^/component-watching$}, - {GET => {method => 'list',}, POST => {method => 'add',},}, - qr{^/component-watching/(\d+)$}, - { - GET => { - method => 'get', - params => sub { - return {id => $_[0]}; - }, - }, - DELETE => { - method => 'remove', - params => sub { - return {id => $_[0]}; - }, - }, - }, - ]; -} - -# -# API methods based on Bugzilla::Extension::ComponentWatching->user_preferences -# - -sub list { - my ($self, $params) = @_; - my $user = Bugzilla->login(LOGIN_REQUIRED); - - return Bugzilla::Extension::ComponentWatching::_getWatches($user); -} - -sub add { - my ($self, $params) = @_; - my $user = Bugzilla->login(LOGIN_REQUIRED); - my $result; - - # load product and verify access - my $productName = $params->{'product'}; - my $product = Bugzilla::Product->new({name => $productName, cache => 1}); - unless ($product && $user->can_access_product($product)) { - ThrowUserError('product_access_denied', {product => $productName}); - } - - my $ra_componentNames = $params->{'component'}; - $ra_componentNames = [$ra_componentNames || ''] unless ref($ra_componentNames); - - if (grep { $_ eq '' } @$ra_componentNames) { - - # watching a product - $result - = Bugzilla::Extension::ComponentWatching::_addProductWatch($user, $product); - - } - else { - # watching specific components - foreach my $componentName (@$ra_componentNames) { - my $component - = Bugzilla::Component->new({ - name => $componentName, product => $product, cache => 1 - }); - unless ($component) { - ThrowUserError('product_access_denied', {product => $productName}); - } - $result = Bugzilla::Extension::ComponentWatching::_addComponentWatch($user, - $component); - } - } - - Bugzilla::Extension::ComponentWatching::_addDefaultSettings($user); - - return $result; -} - -sub get { - my ($self, $params) = @_; - my $user = Bugzilla->login(LOGIN_REQUIRED); - - return Bugzilla::Extension::ComponentWatching::_getWatches($user, - $params->{'id'}); -} - -sub remove { - my ($self, $params) = @_; - my $user = Bugzilla->login(LOGIN_REQUIRED); - my %result = (status => - Bugzilla::Extension::ComponentWatching::_deleteWatch($user, $params->{'id'})); - - return \%result; -} - -1; diff --git a/extensions/ComponentWatching/t/bmo/component-watching-api.t b/extensions/ComponentWatching/t/bmo/component-watching-api.t new file mode 100644 index 0000000000..ded2ed7640 --- /dev/null +++ b/extensions/ComponentWatching/t/bmo/component-watching-api.t @@ -0,0 +1,128 @@ +#!/usr/bin/env perl +# 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/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +use strict; +use warnings; +use 5.10.1; +use lib qw( . lib local/lib/perl5 ); + +BEGIN { + $ENV{LOG4PERL_CONFIG_FILE} = 'log4perl-t.conf'; + $ENV{BUGZILLA_DISABLE_HOSTAGE} = 1; +} + +use Test::More; +use Test::Mojo; + +use Bugzilla; +BEGIN { Bugzilla->extensions } +use Bugzilla::Constants; +use Bugzilla::Product; +use Bugzilla::User; +use Bugzilla::Test::Util qw(issue_api_key); + +Bugzilla->usage_mode(USAGE_MODE_TEST); +Bugzilla->error_mode(ERROR_MODE_DIE); + +my @users = map { + Bugzilla::User->create({ + login_name => "component-watching-$_-$$\@bmo.tld", cryptpassword => '*', + }) +} qw(a b); + +my ($product) = grep { + $_->is_active && @{$_->components} >= 2 && $users[0]->can_access_product($_) +} Bugzilla::Product->get_all; +plan skip_all => 'Need a product with two components' unless $product; +my ($c0, $c1) = map { $_->name } @{$product->components}; +my ($auth_a, $auth_b) + = map { {'X-Bugzilla-API-Key' => issue_api_key($_->login)->api_key} } @users; +my $t = Test::Mojo->new('Bugzilla::App'); + +# Login is required. +$t->get_ok('/rest/component-watching') + ->status_is(401) + ->json_is('/error' => 1, 'list needs a login'); +$t->post_ok('/rest/component-watching' => json => {product => $product->name}) + ->status_is(401); + +$t->get_ok('/rest/component-watching' => $auth_a) + ->status_is(200) + ->json_is('' => [], 'no watches yet'); + +# Product watch, from a JSON body. +$t->post_ok('/rest/component-watching' => $auth_a => json => + {product => $product->name, component => ''}) + ->status_is(200) + ->json_is('/product_name' => $product->name) + ->json_is('/component_name' => '', 'product watch'); +my $product_watch = $t->tx->res->json->{id}; +ok($product_watch, 'the new watch has an id'); + +my ($settings) = Bugzilla->dbh->selectrow_array( + 'SELECT COUNT(*) FROM email_setting WHERE user_id = ? AND relationship = ?', + undef, + $users[0]->id, + Bugzilla::Extension::ComponentWatching::REL_COMPONENT_WATCHER +); +ok($settings, 'default email settings added'); + +$t->post_ok( + '/rest/component-watching' => $auth_a => json => {product => $product->name}) + ->status_is(200) + ->content_is('null', 'already watched'); +$t->post_ok('/rest/component-watching' => $auth_a => form => + {product => $product->name, component => $c0}) + ->status_is(200) + ->content_is('null', 'component covered by the product watch'); + +# Component watches, one from a list in the query string, one from a JSON list. +$t->post_ok("/rest/component-watching" => $auth_b => form => + {product => $product->name, component => [$c0, $c1]}) + ->status_is(200) + ->json_is('/component_name' => $c1, 'last component watch returned'); +$t->get_ok('/rest/component-watching' => $auth_b) + ->status_is(200) + ->json_is('/0/component_name' => (sort $c0, $c1)[0]) + ->json_is('/1/component_name' => (sort $c0, $c1)[1]) + ->json_hasnt('/2', 'both components watched, sorted'); +my @b_watches = map { $_->{id} } @{$t->tx->res->json}; + +# Errors. +$t->post_ok( + '/rest/component-watching' => $auth_a => json => {product => 'No such product'}) + ->json_is('/error' => 1) + ->json_like( + '/message' => qr/'No such product'\s+does not exist/, + 'unknown product' + ); +$t->post_ok('/rest/component-watching' => $auth_a => json => + {product => $product->name, component => ['No such component']}) + ->json_is('/error' => 1, 'unknown component'); + +# Get one watch; another user's watch isn't found. +$t->get_ok("/rest/component-watching/$product_watch" => $auth_a) + ->status_is(200) + ->json_is('/id' => $product_watch) + ->json_is('/product_name' => $product->name, 'own watch'); +$t->get_ok("/rest/component-watching/$product_watch" => $auth_b) + ->status_is(200) + ->json_is('' => {}, "someone else's watch"); + +# Delete only removes the user's own watches. +$t->delete_ok("/rest/component-watching/$product_watch" => $auth_b) + ->status_is(200) + ->json_is('/status' => '0E0', "someone else's watch is kept"); +$t->delete_ok("/rest/component-watching/$product_watch" => $auth_a) + ->status_is(200) + ->json_is('/status' => 1, 'own watch removed'); +$t->get_ok('/rest/component-watching' => $auth_a)->json_is('' => [], 'gone'); +$t->get_ok('/rest/component-watching' => $auth_b) + ->json_is('/0/id' => $b_watches[0], 'the other user keeps theirs'); + +done_testing; diff --git a/qa/t/rest_legacy_credentials.t b/qa/t/rest_legacy_credentials.t index 4058229610..5920273755 100644 --- a/qa/t/rest_legacy_credentials.t +++ b/qa/t/rest_legacy_credentials.t @@ -10,7 +10,7 @@ # Test for Bugzilla_login/Bugzilla_password, which # # resources still on the legacy REST server accept # # (only extensions are left there). # -# GET /rest/component-watching # +# GET /rest/bug_modal/initial_field_values # # Delete with the last legacy resource. # ####################################################### @@ -38,10 +38,11 @@ my $t = Test::Mojo->new(); $t->ua->max_redirects(1); $t->get_ok(rest_get_url( - $url, 'rest/component-watching', + $url, + 'rest/bug_modal/initial_field_values', {Bugzilla_login => $user, Bugzilla_password => $pass} ))->status_is(200); -is(ref $t->tx->res->json, 'ARRAY', 'Valid credentials return the watch list'); +ok($t->tx->res->json('/products'), 'Valid credentials return the field values'); my @tests = ( {login => $user, password => '', test => "Empty password can't log in"}, @@ -67,7 +68,8 @@ my @tests = ( foreach my $test (@tests) { $t->get_ok(rest_get_url( - $url, 'rest/component-watching', + $url, + 'rest/bug_modal/initial_field_values', {Bugzilla_login => $test->{login}, Bugzilla_password => $test->{password}} ))->status_isnt(200); my $expected = $test->{error} // $error; From 639f232ef535e77bcf1c9b5928506253be5a9b01 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Wed, 18 Nov 2026 20:01:55 +0100 Subject: [PATCH 06/19] Bug 2094122 - Migrate the MyDashboard REST resource to native Mojo API GET /rest/mydashboard/run_bug_query, run_flag_query and run_last_changes, and PUT /rest/mydashboard/bug_interest_unmark (also still served without the prefix) are now served by the native controller Bugzilla::Extension::MyDashboard::API::V1::MyDashboard instead of the legacy REST server. Same paths, parameters, checks and responses. The two query methods still read from the shadow database. The legacy WebService module and its webservice hook are removed, and a test covers the five routes. --- extensions/MyDashboard/Extension.pm | 6 - .../{WebService.pm => API/V1/MyDashboard.pm} | 127 ++++++++------- .../MyDashboard/t/bmo/mydashboard-api.t | 150 ++++++++++++++++++ 3 files changed, 220 insertions(+), 63 deletions(-) rename extensions/MyDashboard/lib/{WebService.pm => API/V1/MyDashboard.pm} (60%) create mode 100644 extensions/MyDashboard/t/bmo/mydashboard-api.t diff --git a/extensions/MyDashboard/Extension.pm b/extensions/MyDashboard/Extension.pm index c94ecba4a8..023096749e 100644 --- a/extensions/MyDashboard/Extension.pm +++ b/extensions/MyDashboard/Extension.pm @@ -170,12 +170,6 @@ sub user_preferences { } } -sub webservice { - my ($self, $args) = @_; - my $dispatch = $args->{dispatch}; - $dispatch->{MyDashboard} = "Bugzilla::Extension::MyDashboard::WebService"; -} - sub bug_end_of_create { my ($self, $args) = @_; my ($bug, $params, $timestamp) = @$args{qw(bug params timestamp)}; diff --git a/extensions/MyDashboard/lib/WebService.pm b/extensions/MyDashboard/lib/API/V1/MyDashboard.pm similarity index 60% rename from extensions/MyDashboard/lib/WebService.pm rename to extensions/MyDashboard/lib/API/V1/MyDashboard.pm index 1338f9d412..c702126b1e 100644 --- a/extensions/MyDashboard/lib/WebService.pm +++ b/extensions/MyDashboard/lib/API/V1/MyDashboard.pm @@ -4,42 +4,68 @@ # # This Source Code Form is "Incompatible With Secondary Licenses", as # defined by the Mozilla Public License, v. 2.0. -package Bugzilla::Extension::MyDashboard::WebService; -use 5.10.1; -use strict; -use warnings; +package Bugzilla::Extension::MyDashboard::API::V1::MyDashboard; -use base qw(Bugzilla::WebService); +use 5.10.1; +use Mojo::Base qw( Mojolicious::Controller ); +use Bugzilla; use Bugzilla::API::V1::Bug; +use Bugzilla::Bug; use Bugzilla::Comment; use Bugzilla::Constants; use Bugzilla::Error; use Bugzilla::Template; -use Bugzilla::Util qw(detaint_natural template_var datetime_from); -use Bugzilla::API::V1::Util qw(validate); +use Bugzilla::Util qw(template_var datetime_from); +use Bugzilla::API::V1::Util qw(merge_request_params); use Bugzilla::Extension::MyDashboard::Queries qw(QUERY_DEFS query_bugs query_flags); use Bugzilla::Extension::MyDashboard::BugInterest; -use constant READ_ONLY => qw( - run_bug_query - run_flag_query -); +sub setup_routes { + my ($class, $r) = @_; + my $to = 'MyDashboard::API::V1::MyDashboard#'; + + # bug_interest_unmark is also served without the prefix, for backward + # compatibility; the other methods are newer and always had it. + $r->put('/bug_interest_unmark')->to($to . 'bug_interest_unmark'); + $r->put('/mydashboard/bug_interest_unmark')->to($to . 'bug_interest_unmark'); + $r->get('/mydashboard/run_bug_query')->to($to . 'run_bug_query'); + $r->get('/mydashboard/run_flag_query')->to($to . 'run_flag_query'); + $r->get('/mydashboard/run_last_changes')->to($to . 'run_last_changes'); +} -use constant PUBLIC_METHODS => qw( - bug_interest_unmark - run_bug_query - run_flag_query - run_last_changes -); +sub type { + my ($self, $type, $value) = @_; + return Bugzilla::API::V1::Util->type($type, $value); +} -sub run_last_changes { - my ($self, $params) = @_; +# The logged-in user and the request params, or nothing after rendering an +# error. +sub _login { + my ($self, $lists) = @_; + Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); + my $user = $self->bugzilla->login; + if (!$user->id) { + $self->user_error('login_required'); + return; + } + my ($params, $error) = merge_request_params($self, $lists); + if ($error) { + $self->user_error($error); + return; + } + return ($user, $params); +} - my $user = Bugzilla->login(LOGIN_REQUIRED); +# GET /mydashboard/run_last_changes?bug_id=&changeddate_api= +# +# The last change and the last comment made to the bug after the date. +sub run_last_changes { + my ($self) = @_; + my ($user, $params) = $self->_login or return; # Only reads from here, as Bug.history did. my $dbh = Bugzilla->switch_to_shadow_db(); @@ -55,7 +81,7 @@ sub run_last_changes { my $last_comment_sth = $dbh->prepare($last_comment_sql); my $last_changes = {}; - my $bug = Bugzilla::Bug->check($params->{bug_id}); + my $bug = Bugzilla::Bug->check($params->{bug_id}); my ($activity) = Bugzilla::Bug::GetBugActivity($bug->id, undef, $params->{changeddate_api}, 1); @@ -84,20 +110,23 @@ sub run_last_changes { ? Bugzilla->markdown->render_html($text, $bug) : Bugzilla::Template::quoteUrls($text, $bug); $last_changes->{comment_html} = $html; - $last_changes->{email} = $self->type('email', $comment->author->login) + $last_changes->{email} = $self->type('email', $comment->author->login) if !$last_changes->{email}; my $datetime = datetime_from($comment->creation_ts); $datetime->set_time_zone($user->timezone); $last_changes->{when} = $datetime->strftime('%Y-%m-%d %T %Z'); } - return {results => [{last_changes => $last_changes}]}; + return $self->render(json => {results => [{last_changes => $last_changes}]}); } +# GET /mydashboard/run_bug_query?query= +# +# The bugs of one of the dashboard's queries (QUERY_DEFS). sub run_bug_query { - my ($self, $params) = @_; - my $dbh = Bugzilla->dbh; - my $user = Bugzilla->login(LOGIN_REQUIRED); + my ($self) = @_; + my ($user, $params) = $self->_login or return; + Bugzilla->switch_to_shadow_db(); defined $params->{query} || ThrowCodeError('param_required', @@ -123,12 +152,16 @@ sub run_bug_query { last; } - return {result => $result}; + return $self->render(json => {result => $result}); } +# GET /mydashboard/run_flag_query?type= +# +# The flags requested from or by the user. sub run_flag_query { - my ($self, $params) = @_; - my $user = Bugzilla->login(LOGIN_REQUIRED); + my ($self) = @_; + my ($user, $params) = $self->_login or return; + Bugzilla->switch_to_shadow_db(); my $type = $params->{type}; $type @@ -146,12 +179,15 @@ sub run_flag_query { $flag->{is_patch} = $self->type('boolean', $flag->{is_patch}); } - return {result => {$type => $results}}; + return $self->render(json => {result => {$type => $results}}); } +# PUT /mydashboard/bug_interest_unmark (or /bug_interest_unmark) +# +# Removes the bugs (bug_ids, one ID or a list) from the user's interest list. sub bug_interest_unmark { - my ($self, $params) = @_; - my $user = Bugzilla->login(LOGIN_REQUIRED); + my ($self) = @_; + my ($user, $params) = $self->_login(['bug_ids']) or return; ThrowCodeError('param_required', {function => 'MyDashboard.bug_interest_unmark', param => 'bug_ids'}) @@ -165,32 +201,9 @@ sub bug_interest_unmark { Bugzilla::Extension::MyDashboard::BugInterest->unmark($user->id, $bug_id); } Bugzilla->dbh->bz_commit_transaction(); -} -sub rest_resources { - return [ - # REST API v1: Expose `bug_interest_unmark` method without prefix for backward compatibility - qr{^/(mydashboard/)?bug_interest_unmark$}, {PUT => {method => 'bug_interest_unmark'}}, - # Other methods are new, so require the prefix - qr{^/mydashboard/run_bug_query$}, {GET => {method => 'run_bug_query'}}, - qr{^/mydashboard/run_flag_query$}, {GET => {method => 'run_flag_query'}}, - qr{^/mydashboard/run_last_changes$}, {GET => {method => 'run_last_changes'}}, - ]; + # The legacy method returned what bz_commit_transaction() did, i.e. 0. + return $self->render(json => 0); } 1; - -__END__ - -=head1 NAME - -Bugzilla::Extension::MyDashboard::Webservice - The MyDashboard WebServices API - -=head1 DESCRIPTION - -This module contains API methods that are useful to user's of bugzilla.mozilla.org. - -=head1 METHODS - -See L for a description of how parameters are passed, -and what B, B, and B mean. diff --git a/extensions/MyDashboard/t/bmo/mydashboard-api.t b/extensions/MyDashboard/t/bmo/mydashboard-api.t new file mode 100644 index 0000000000..a1a553cd85 --- /dev/null +++ b/extensions/MyDashboard/t/bmo/mydashboard-api.t @@ -0,0 +1,150 @@ +#!/usr/bin/env perl +# 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/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +use strict; +use warnings; +use 5.10.1; +use lib qw( . lib local/lib/perl5 ); + +BEGIN { + $ENV{LOG4PERL_CONFIG_FILE} = 'log4perl-t.conf'; + $ENV{BUGZILLA_DISABLE_HOSTAGE} = 1; +} + +use Test::More; +use Test::Mojo; + +use Bugzilla; +BEGIN { Bugzilla->extensions } +use Bugzilla::Constants; +use Bugzilla::Bug; +use Bugzilla::Product; +use Bugzilla::User; +use Bugzilla::API::V1::Util qw(extract_flags); +use Bugzilla::Test::Util qw(issue_api_key); +use Bugzilla::Util qw(datetime_from); +use Bugzilla::Extension::MyDashboard::BugInterest; + +Bugzilla->usage_mode(USAGE_MODE_TEST); +Bugzilla->error_mode(ERROR_MODE_DIE); + +my $admin = Bugzilla::User->new({name => 'admin@mozilla.bugs'}); +my $user + = Bugzilla::User->create({ + login_name => "mydashboard-api-$$\@bmo.tld", cryptpassword => '*', + }); +my ($product) + = grep { $_->is_active && @{$_->components} && @{$_->versions} } + Bugzilla::Product->get_all; +plan skip_all => 'Need an admin and a product' unless $admin && $product; + +my %auth = ('X-Bugzilla-API-Key' => issue_api_key($user->login)->api_key); +my $t = Test::Mojo->new('Bugzilla::App'); + +# A bug assigned to the user, with a needinfo request to them and a comment. +Bugzilla->set_user($admin); +my $bug = Bugzilla::Bug->create({ + short_desc => "MyDashboard API test $$", + product => $product->name, + component => $product->components->[0]->name, + bug_type => 'defect', + bug_severity => 'normal', + op_sys => 'Unspecified', + rep_platform => 'Unspecified', + version => $product->versions->[0]->name, + assigned_to => $user->login, + comment => 'first comment', +}); +my $flag_set = eval { + my ($flags, $new_flags) + = extract_flags( + [{name => 'needinfo', status => '?', requestee => $user->login}], $bug); + $bug->set_flags($flags, $new_flags); + $bug->update; + 1; +}; +my $since = $bug->creation_ts; + +# Later than the bug's creation, so only this comment is newer than $since. +my $later = datetime_from($since)->add(seconds => 2)->strftime('%Y-%m-%d %T'); +$bug->add_comment('a newer comment'); +$bug->update($later); + +# Login is required. +for my $path ( + qw(run_bug_query?query=assignedbugs run_flag_query?type=requestee + run_last_changes?bug_id=1) + ) +{ + $t->get_ok("/rest/mydashboard/$path") + ->status_is(401) + ->json_is('/error' => 1, "$path needs a login"); +} +$t->put_ok('/rest/mydashboard/bug_interest_unmark' => json => {bug_ids => 1}) + ->status_is(401); + +# run_bug_query +$t->get_ok('/rest/mydashboard/run_bug_query?query=assignedbugs' => \%auth) + ->status_is(200) + ->json_is('/result/name' => 'assignedbugs') + ->json_is('/result/bugs/0/bug_id' => $bug->id, 'bug id is a number') + ->json_is('/result/bugs/0/short_desc' => $bug->short_desc) + ->json_has('/result/bugs/0/changeddate_api') + ->json_like('/result/buffer' => qr/emailassigned_to1=1/, 'query string'); +$t->get_ok('/rest/mydashboard/run_bug_query?query=nosuchquery' => \%auth) + ->status_is(200) + ->json_is('/result' => undef, 'unknown query'); +$t->get_ok('/rest/mydashboard/run_bug_query' => \%auth) + ->json_is('/error' => 1, 'query is required'); + +# run_flag_query +SKIP: { + skip 'no needinfo flag type', 6 unless $flag_set; + $t->get_ok('/rest/mydashboard/run_flag_query?type=requestee' => \%auth) + ->status_is(200) + ->json_is('/result/requestee/0/bug_id' => $bug->id) + ->json_is('/result/requestee/0/type' => 'needinfo') + ->json_is('/result/requestee/0/is_patch' => Mojo::JSON::false) + ->json_is('/result/requestee/0/requester' => $admin->login); +} +$t->get_ok('/rest/mydashboard/run_flag_query' => \%auth) + ->json_is('/error' => 1, 'type is required'); + +# run_last_changes +$t->get_ok('/rest/mydashboard/run_last_changes' => \%auth => form => + {bug_id => $bug->id, changeddate_api => $since}) + ->status_is(200) + ->json_like('/results/0/last_changes/comment_html' => qr/a newer comment/) + ->json_is('/results/0/last_changes/email' => $admin->login) + ->json_has('/results/0/last_changes/when'); +$t->get_ok('/rest/mydashboard/run_last_changes?bug_id=999999999' => \%auth) + ->json_is('/error' => 1, 'unknown bug'); + +# bug_interest_unmark, with and without the prefix +my $interest = 'Bugzilla::Extension::MyDashboard::BugInterest'; +my $count = sub { + scalar @{$interest->match({user_id => $user->id, bug_id => $bug->id})}; +}; +for my $path ('/rest/mydashboard/bug_interest_unmark', + '/rest/bug_interest_unmark') +{ + $interest->mark($user->id, $bug->id, $since); + is($count->(), 1, 'interested in the bug'); + $t->put_ok($path => \%auth => json => {bug_ids => [$bug->id]}) + ->status_is(200) + ->content_is('0'); + is($count->(), 0, "$path removes the interest"); +} +$interest->mark($user->id, $bug->id, $since); +$t->put_ok('/rest/mydashboard/bug_interest_unmark' => \%auth => form => + {bug_ids => $bug->id})->status_is(200); +is($count->(), 0, 'one bug ID in the query string'); +$t->put_ok('/rest/mydashboard/bug_interest_unmark' => \%auth => json => {}) + ->json_is('/error' => 1, 'bug_ids is required'); + +done_testing; From 3686a95db1feafd394f6dece37a5e19ded813546 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Wed, 18 Nov 2026 20:14:36 +0100 Subject: [PATCH 07/19] Bug 2094122 - Fix - Refuse an unknown type in MyDashboard run_flag_query query_flags meant to accept only requestee and requester, but its check (ne || ne) was always true, so any other type was treated as requester and returned under that key. It now throws param_invalid, and Queries.pm imports Bugzilla::Error, which that error path never needed before. --- extensions/MyDashboard/lib/Queries.pm | 6 ++++-- extensions/MyDashboard/t/bmo/mydashboard-api.t | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/extensions/MyDashboard/lib/Queries.pm b/extensions/MyDashboard/lib/Queries.pm index d8f9366e47..75981685d4 100644 --- a/extensions/MyDashboard/lib/Queries.pm +++ b/extensions/MyDashboard/lib/Queries.pm @@ -15,6 +15,7 @@ use Bugzilla; use Bugzilla::Bug; use Bugzilla::CGI; use Bugzilla::Constants; +use Bugzilla::Error; use Bugzilla::Search; use Bugzilla::Flag; use Bugzilla::Status qw(is_open_state); @@ -260,8 +261,9 @@ sub query_flags { my $dbh = Bugzilla->dbh; my $datetime_now = DateTime->now(time_zone => $user->timezone); - ($type ne 'requestee' || $type ne 'requester') - || ThrowCodeError('param_required', {param => 'type'}); + ($type eq 'requestee' || $type eq 'requester') + || ThrowCodeError('param_invalid', + {function => 'MyDashboard.run_flag_query', param => 'type'}); my $match_params = {status => '?'}; diff --git a/extensions/MyDashboard/t/bmo/mydashboard-api.t b/extensions/MyDashboard/t/bmo/mydashboard-api.t index a1a553cd85..0ac2e9474b 100644 --- a/extensions/MyDashboard/t/bmo/mydashboard-api.t +++ b/extensions/MyDashboard/t/bmo/mydashboard-api.t @@ -114,6 +114,12 @@ SKIP: { } $t->get_ok('/rest/mydashboard/run_flag_query' => \%auth) ->json_is('/error' => 1, 'type is required'); +$t->get_ok('/rest/mydashboard/run_flag_query?type=bogus' => \%auth) + ->json_is('/error' => 1) + ->json_like( + '/message' => qr/type.*is not a valid parameter/s, + 'unknown type' + ); # run_last_changes $t->get_ok('/rest/mydashboard/run_last_changes' => \%auth => form => From 45a6fab2eb4a57d6eb9b00bad4bb6f520b187a3f Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Wed, 18 Nov 2026 21:04:14 +0100 Subject: [PATCH 08/19] Bug 2094122 - Migrate the PhabBugz REST resource to native Mojo API GET /rest/phabbugz/check_bug//, GET /rest/phabbugz/check_enter_bug//, GET /rest/phabbugz/bug_revisions/ and GET /rest/phabbugz/user/ are now served by the existing native controller Bugzilla::Extension::PhabBugz::API::V1::PhabBugz instead of the legacy REST server. Same paths, checks and responses. The three read-only ones still use the shadow database. The legacy WebService module and its webservice hook are removed, and webservice.t now calls the routes. --- extensions/PhabBugz/Extension.pm | 5 - extensions/PhabBugz/lib/API/V1/PhabBugz.pm | 314 +++++++++++++++++- extensions/PhabBugz/lib/WebService.pm | 356 --------------------- extensions/PhabBugz/t/bmo/webservice.t | 177 ++++++---- 4 files changed, 422 insertions(+), 430 deletions(-) delete mode 100644 extensions/PhabBugz/lib/WebService.pm diff --git a/extensions/PhabBugz/Extension.pm b/extensions/PhabBugz/Extension.pm index 606f156d5c..d606228edc 100644 --- a/extensions/PhabBugz/Extension.pm +++ b/extensions/PhabBugz/Extension.pm @@ -59,11 +59,6 @@ sub config_add_panels { $modules->{PhabBugz} = "Bugzilla::Extension::PhabBugz::Config"; } -sub webservice { - my ($self, $args) = @_; - $args->{dispatch}->{PhabBugz} = "Bugzilla::Extension::PhabBugz::WebService"; -} - sub object_end_of_update { my ($self, $args) = @_; my ($object, $changes) = @$args{qw(object changes)}; diff --git a/extensions/PhabBugz/lib/API/V1/PhabBugz.pm b/extensions/PhabBugz/lib/API/V1/PhabBugz.pm index e776cb9c63..9d306a1bf2 100644 --- a/extensions/PhabBugz/lib/API/V1/PhabBugz.pm +++ b/extensions/PhabBugz/lib/API/V1/PhabBugz.pm @@ -10,16 +10,21 @@ package Bugzilla::Extension::PhabBugz::API::V1::PhabBugz; use 5.10.1; use Mojo::Base qw( Mojolicious::Controller ); +use Bugzilla::Bug; use Bugzilla::Constants; use Bugzilla::Error; use Bugzilla::Group; use Bugzilla::Logging; +use Bugzilla::User; use Bugzilla::Util qw(trim); use Bugzilla::Extension::PhabBugz::Constants; +use Bugzilla::Extension::PhabBugz::Revision; +use Bugzilla::Extension::PhabBugz::Util qw(request); use Bugzilla::Extension::TrackingFlags::Flag; -use List::Util qw(uniq); +use List::Util qw(uniq); +use Types::Standard qw(-types slurpy); our %api_field_names = reverse %{Bugzilla::Bug::FIELD_MAP()}; $api_field_names{'bug_group'} = 'groups'; @@ -40,6 +45,19 @@ sub setup_routes { $r->post('/pulsebot/bug/:id/comment') ->to('PhabBugz::API::V1::PhabBugz#add_comment'); $r->put('/pulsebot/bug')->to('PhabBugz::API::V1::PhabBugz#update_bugs'); + + # Bug permission checks, for Phabricator + $r->get('/phabbugz/check_bug/:bug_id/:user_id' => + [bug_id => qr/\d+/, user_id => qr/\d+/]) + ->to('PhabBugz::API::V1::PhabBugz#check_user_permission_for_bug'); + $r->get('/phabbugz/check_enter_bug/#product/:user_id' => [user_id => qr/\d+/]) + ->to('PhabBugz::API::V1::PhabBugz#check_user_enter_bug_permission'); + + # Revisions and Phabricator accounts, for the bug page and user profiles + $r->get('/phabbugz/bug_revisions/:bug_id' => [bug_id => qr/\d+/]) + ->to('PhabBugz::API::V1::PhabBugz#bug_revisions'); + $r->get('/phabbugz/user/:user_id' => [user_id => qr/\d+/]) + ->to('PhabBugz::API::V1::PhabBugz#user'); } sub get_bugs { @@ -266,4 +284,298 @@ sub update_bugs { $self->render(json => {bugs => \@result}); } +sub _check_phabricator { + + # Ensure PhabBugz is on + ThrowUserError('phabricator_not_enabled') + unless Bugzilla->params->{phabricator_enabled}; +} + +sub _validate_phab_user { + my ($self, $user) = @_; + + $self->_check_phabricator(); + + # Validate that the requesting user's email matches phab-bot + ThrowUserError('phabricator_unauthorized_user') + unless $user->login eq PHAB_AUTOMATION_USER; +} + +# The logged-in user, or undef after rendering a login_required error. +sub _login { + my ($self) = @_; + my $user = $self->bugzilla->login; + return $user if $user->id; + $self->user_error('login_required'); + return undef; +} + +# GET /phabbugz/check_bug// +# +# phab-bot only. {result => 1} if the user can see the bug, else 0. +sub check_user_permission_for_bug { + my ($self) = @_; + Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); + my $user = $self->_login || return; + Bugzilla->switch_to_shadow_db(); + + $self->_validate_phab_user($user); + + my $bug_id = $self->param('bug_id'); + my $user_id = $self->param('user_id'); + + # Validate that a bug id and user id are provided + ThrowUserError('phabricator_invalid_request_params') + unless ($bug_id && $user_id); + + # Validate that the user exists + my $target_user = Bugzilla::User->check({id => $user_id, cache => 1}); + + # Send back an object which says { "result": 1|0 } + return $self->render(json => {result => $target_user->can_see_bug($bug_id)}); +} + +# GET /phabbugz/check_enter_bug// +# +# phab-bot only. {result => 1} if the user can file bugs in the product, +# else 0. +sub check_user_enter_bug_permission { + my ($self) = @_; + Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); + my $user = $self->_login || return; + Bugzilla->switch_to_shadow_db(); + + $self->_validate_phab_user($user); + + my $product = $self->param('product'); + my $user_id = $self->param('user_id'); + + # Validate that a product name and user id are provided + ThrowUserError('phabricator_invalid_request_params') + unless ($product && $user_id); + + # Validate that the user exists + my $target_user = Bugzilla::User->check({id => $user_id, cache => 1}); + + # Send back an object with the attribute "result" set to 1 if the user + # can enter bugs into the given product, or 0 if not. + return $self->render( + json => {result => $target_user->can_enter_product($product) ? 1 : 0}); +} + +# GET /phabbugz/bug_revisions/ +# +# The Phabricator revisions attached to the bug, sorted by id, with their +# reviews and stack children. Titles of non-public revisions are hidden. +sub bug_revisions { + my ($self) = @_; + Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); + + $self->_check_phabricator(); + + my $user = $self->_login || return; + Bugzilla->switch_to_shadow_db(); + + my $bug_id = $self->param('bug_id'); + + # Validate that a bug id and user id are provided + ThrowUserError('phabricator_invalid_request_params') unless $bug_id; + + # Validate that the user can see the bug itself + my $bug = Bugzilla::Bug->check({id => $bug_id, cache => 1}); + + my @revision_ids; + foreach my $attachment (@{$bug->attachments}) { + next if $attachment->contenttype ne PHAB_CONTENT_TYPE; + my ($revision_id) = ($attachment->filename =~ PHAB_ATTACHMENT_PATTERN); + next if !$revision_id; + push @revision_ids, int $revision_id; + } + + my $response = request( + 'differential.revision.search', + { + attachments => { + 'projects' => 1, + 'reviewers' => 1, + 'subscribers' => 1, + 'reviewers-extra' => 1, + }, + constraints => {ids => \@revision_ids,}, + order => 'newest', + } + ); + + state $SearchResult = Dict [ + result => Dict [ + + # HashRef below could be better, + # but ::Revision takes a lot of options. + data => ArrayRef [HashRef], + slurpy Any, + ], + slurpy Any, + ]; + + my $error = $SearchResult->validate($response); + ThrowCodeError('phabricator_api_error', {reason => $error}) if defined $error; + + my $children_map = $self->_get_children_map($response); + + my $revision_status_map = { + 'abandoned' => 'Abandoned', + 'accepted' => 'Accepted', + 'changes-planned' => 'Changes Planned', + 'draft' => 'Draft', + 'needs-review' => 'Needs Review', + 'needs-revision' => 'Needs Revision', + }; + + my $review_status_map = { + 'accepted' => 'Accepted', + 'accepted-prior' => 'Accepted Prior Diff', + 'added' => 'Review Requested', + 'blocking' => 'Blocking Review', + 'rejected' => 'Requested Changes', + 'resigned' => 'Resigned' + }; + + my @revisions; + foreach my $revision (@{$response->{result}{data}}) { + + # Skip if revision bug id was moved to a different bug + next if $revision->{fields}->{'bugzilla.bug-id'} ne $bug->id; + + my $revision_obj = Bugzilla::Extension::PhabBugz::Revision->new($revision); + my $id = $revision_obj->id; + my $revision_data = { + id => 'D' . $id, + sortkey => $id, + status => $revision_obj->status, + long_status => $revision_status_map->{$revision_obj->status} + || $revision_obj->status + }; + + my @reviews; + foreach my $review (@{$revision_obj->reviews}) { + push @reviews, + { + user => $review->{user}->name, + status => $review->{status}, + long_status => $review_status_map->{$review->{status}} || $review->{status} + }; + } + $revision_data->{reviews} = \@reviews; + + if ($revision_obj->view_policy ne 'public') { + $revision_data->{title} = '(secured)'; + $revision_data->{call_sign} = '(secured)'; + } + else { + $revision_data->{title} = $revision_obj->title; + $revision_data->{call_sign} + = $revision_obj->repository ? $revision_obj->repository->call_sign : 'N/A'; + } + + $revision_data->{children} = [map {"D$_"} @{$children_map->{$id}}]; + + push @revisions, $revision_data; + } + + # sort by revision id + @revisions = sort { $a->{sortkey} <=> $b->{sortkey} } @revisions; + + return $self->render(json => {revisions => \@revisions}); +} + +sub _get_children_map { + my ($self, $revisions_response) = @_; + + my @revision_phids; + foreach my $revision (@{$revisions_response->{result}{data}}) { + push @revision_phids, $revision->{phid}; + } + + my $edge_response = request('edge.search', + {sourcePHIDs => \@revision_phids, types => ["revision.child"],}); + + my $rev_phid_to_id = {}; + my $children_map = {}; + foreach my $revision (@{$revisions_response->{result}{data}}) { + my $id = $revision->{id}; + my $phid = $revision->{phid}; + $rev_phid_to_id->{$phid} = $id; + $children_map->{$id} = []; + } + + foreach my $edge (@{$edge_response->{result}{data}}) { + if (!exists($rev_phid_to_id->{$edge->{sourcePHID}})) { + next; + } + if (!exists($rev_phid_to_id->{$edge->{destinationPHID}})) { + next; + } + + my $from = $rev_phid_to_id->{$edge->{sourcePHID}}; + my $to = $rev_phid_to_id->{$edge->{destinationPHID}}; + + if (!exists($children_map->{$from})) { + next; + } + + push @{$children_map->{$from}}, $to; + } + + return $children_map; +} + +# GET /phabbugz/user/ +# +# The Phabricator account linked to the BMO user, with links to its profile +# and open revisions, or {} if there is none. +sub user { + my ($self) = @_; + Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); + + $self->_check_phabricator(); + + $self->_login || return; + + my $bmo_user_id = $self->param('user_id'); + + my $response + = request('bmoexternalaccount.search', {accountids => [$bmo_user_id],}); + if (scalar(@{$response->{result}}) == 0) { + return $self->render(json => {}); + } + + my $phid = $response->{result}[0]{phid}; + + $response = request('user.query', {phids => [$phid],}); + if (scalar(@{$response->{result}}) == 0) { + return $self->render(json => {}); + } + + my $user_name = $response->{result}[0]{userName}; + my $real_name = $response->{result}[0]{realName}; + + my $base_url = Bugzilla->params->{phabricator_base_uri}; + $base_url =~ s{/$}{}; + my $user_url = "$base_url/p/$user_name/"; + my $revisions_url + = "$base_url/differential/?responsiblePHIDs%5B0%5D=$phid&statuses%5B0%5D=open()&order=newest&bucket=action"; + + return $self->render( + json => { + user => { + phid => $phid, + userName => $user_name, + realName => $real_name, + userURL => $user_url, + revisionsURL => $revisions_url, + }, + } + ); +} + 1; diff --git a/extensions/PhabBugz/lib/WebService.pm b/extensions/PhabBugz/lib/WebService.pm deleted file mode 100644 index 8280ba929a..0000000000 --- a/extensions/PhabBugz/lib/WebService.pm +++ /dev/null @@ -1,356 +0,0 @@ -# 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/. -# -# This Source Code Form is "Incompatible With Secondary Licenses", as -# defined by the Mozilla Public License, v. 2.0. - -package Bugzilla::Extension::PhabBugz::WebService; - -use 5.10.1; -use strict; -use warnings; - -use base qw(Bugzilla::WebService); - -use Bugzilla::Bug; -use Bugzilla::Constants; -use Bugzilla::Error; -use Bugzilla::Logging; -use Bugzilla::User; -use Bugzilla::Util qw(detaint_natural); -use Bugzilla::WebService::Constants; -use Types::Standard qw(-types slurpy); -use Type::Params qw(compile); - -use Bugzilla::Extension::PhabBugz::Constants; -use Bugzilla::Extension::PhabBugz::Revision; -use Bugzilla::Extension::PhabBugz::Util qw(request); - -use MIME::Base64 qw(decode_base64); -use Try::Tiny; - -use constant READ_ONLY => qw( - bug_revisions - check_user_enter_bug_permission - check_user_permission_for_bug -); - -use constant PUBLIC_METHODS => qw( - bug_revisions - check_user_enter_bug_permission - check_user_permission_for_bug -); - -sub _check_phabricator { - - # Ensure PhabBugz is on - ThrowUserError('phabricator_not_enabled') - unless Bugzilla->params->{phabricator_enabled}; -} - -sub _validate_phab_user { - my ($self, $user) = @_; - - $self->_check_phabricator(); - - # Validate that the requesting user's email matches phab-bot - ThrowUserError('phabricator_unauthorized_user') - unless $user->login eq PHAB_AUTOMATION_USER; -} - -sub check_user_permission_for_bug { - my ($self, $params) = @_; - - my $user = Bugzilla->login(LOGIN_REQUIRED); - - $self->_validate_phab_user($user); - - # Validate that a bug id and user id are provided - ThrowUserError('phabricator_invalid_request_params') - unless ($params->{bug_id} && $params->{user_id}); - - # Validate that the user exists - my $target_user = Bugzilla::User->check({id => $params->{user_id}, cache => 1}); - - # Send back an object which says { "result": 1|0 } - return {result => $target_user->can_see_bug($params->{bug_id})}; -} - -sub check_user_enter_bug_permission { - my ($self, $params) = @_; - - my $user = Bugzilla->login(LOGIN_REQUIRED); - - $self->_validate_phab_user($user); - - # Validate that a product name and user id are provided - ThrowUserError('phabricator_invalid_request_params') - unless ($params->{product} && $params->{user_id}); - - # Validate that the user exists - my $target_user = Bugzilla::User->check({id => $params->{user_id}, cache => 1}); - - # Send back an object with the attribute "result" set to 1 if the user - # can enter bugs into the given product, or 0 if not. - return {result => $target_user->can_enter_product($params->{product}) ? 1 : 0}; -} - -sub bug_revisions { - state $check = compile(Object, Dict [bug_id => Int]); - my ($self, $params) = $check->(@_); - - $self->_check_phabricator(); - - my $user = Bugzilla->login(LOGIN_REQUIRED); - - # Validate that a bug id and user id are provided - ThrowUserError('phabricator_invalid_request_params') unless $params->{bug_id}; - - # Validate that the user can see the bug itself - my $bug = Bugzilla::Bug->check({id => $params->{bug_id}, cache => 1}); - - my @revision_ids; - foreach my $attachment (@{$bug->attachments}) { - next if $attachment->contenttype ne PHAB_CONTENT_TYPE; - my ($revision_id) = ($attachment->filename =~ PHAB_ATTACHMENT_PATTERN); - next if !$revision_id; - push @revision_ids, int $revision_id; - } - - my $response = request( - 'differential.revision.search', - { - attachments => { - 'projects' => 1, - 'reviewers' => 1, - 'subscribers' => 1, - 'reviewers-extra' => 1, - }, - constraints => {ids => \@revision_ids,}, - order => 'newest', - } - ); - - state $SearchResult = Dict [ - result => Dict [ - - # HashRef below could be better, - # but ::Revision takes a lot of options. - data => ArrayRef [HashRef], - slurpy Any, - ], - slurpy Any, - ]; - - my $error = $SearchResult->validate($response); - ThrowCodeError('phabricator_api_error', {reason => $error}) if defined $error; - - my $children_map = $self->_get_children_map($response); - - my $revision_status_map = { - 'abandoned' => 'Abandoned', - 'accepted' => 'Accepted', - 'changes-planned' => 'Changes Planned', - 'draft' => 'Draft', - 'needs-review' => 'Needs Review', - 'needs-revision' => 'Needs Revision', - }; - - my $review_status_map = { - 'accepted' => 'Accepted', - 'accepted-prior' => 'Accepted Prior Diff', - 'added' => 'Review Requested', - 'blocking' => 'Blocking Review', - 'rejected' => 'Requested Changes', - 'resigned' => 'Resigned' - }; - - my @revisions; - foreach my $revision (@{$response->{result}{data}}) { - - # Skip if revision bug id was moved to a different bug - next if $revision->{fields}->{'bugzilla.bug-id'} ne $bug->id; - - my $revision_obj = Bugzilla::Extension::PhabBugz::Revision->new($revision); - my $id = $revision_obj->id; - my $revision_data = { - id => 'D' . $id, - sortkey => $id, - status => $revision_obj->status, - long_status => $revision_status_map->{$revision_obj->status} - || $revision_obj->status - }; - - my @reviews; - foreach my $review (@{$revision_obj->reviews}) { - push @reviews, - { - user => $review->{user}->name, - status => $review->{status}, - long_status => $review_status_map->{$review->{status}} || $review->{status} - }; - } - $revision_data->{reviews} = \@reviews; - - if ($revision_obj->view_policy ne 'public') { - $revision_data->{title} = '(secured)'; - $revision_data->{call_sign} = '(secured)'; - } - else { - $revision_data->{title} = $revision_obj->title; - $revision_data->{call_sign} - = $revision_obj->repository ? $revision_obj->repository->call_sign : 'N/A'; - } - - $revision_data->{children} = [map { "D$_" } @{$children_map->{$id}}]; - - push @revisions, $revision_data; - } - - # sort by revision id - @revisions = sort { $a->{sortkey} <=> $b->{sortkey} } @revisions; - - return {revisions => \@revisions}; -} - -sub _get_children_map { - my ($self, $revisions_response) = @_; - - my @revision_phids; - foreach my $revision (@{$revisions_response->{result}{data}}) { - push @revision_phids, $revision->{phid}; - } - - my $edge_response = request( - 'edge.search', - { - sourcePHIDs => \@revision_phids, - types => ["revision.child"], - } - ); - - my $rev_phid_to_id = {}; - my $children_map = {}; - foreach my $revision (@{$revisions_response->{result}{data}}) { - my $id = $revision->{id}; - my $phid = $revision->{phid}; - $rev_phid_to_id->{$phid} = $id; - $children_map->{$id} = []; - } - - foreach my $edge (@{$edge_response->{result}{data}}) { - if (!exists($rev_phid_to_id->{$edge->{sourcePHID}})) { - next; - } - if (!exists($rev_phid_to_id->{$edge->{destinationPHID}})) { - next; - } - - my $from = $rev_phid_to_id->{$edge->{sourcePHID}}; - my $to = $rev_phid_to_id->{$edge->{destinationPHID}}; - - if (!exists($children_map->{$from})) { - next; - } - - push @{$children_map->{$from}}, $to; - } - - return $children_map; -} - -sub user { - state $check = compile(Object, Dict [user_id => Int]); - my ($self, $params) = $check->(@_); - - $self->_check_phabricator(); - - Bugzilla->login(LOGIN_REQUIRED); - - my $bmo_user_id = $params->{user_id}; - - my $response = request( - 'bmoexternalaccount.search', - { - accountids => [$bmo_user_id], - } - ); - if (scalar(@{$response->{result}}) == 0) { - return {}; - } - - my $phid = $response->{result}[0]{phid}; - - $response = request( - 'user.query', - { - phids => [$phid], - } - ); - if (scalar(@{$response->{result}}) == 0) { - return {}; - } - - my $user_name = $response->{result}[0]{userName}; - my $real_name = $response->{result}[0]{realName}; - - my $base_url = Bugzilla->params->{phabricator_base_uri}; - $base_url =~ s{/$}{}; - my $user_url = "$base_url/p/$user_name/"; - my $revisions_url = "$base_url/differential/?responsiblePHIDs%5B0%5D=$phid&statuses%5B0%5D=open()&order=newest&bucket=action"; - - return { - user => { - phid => $phid, - userName => $user_name, - realName => $real_name, - userURL => $user_url, - revisionsURL => $revisions_url, - }, - }; -} - -sub rest_resources { - return [ - # Bug permission checks - qr{^/phabbugz/check_bug/(\d+)/(\d+)$}, - { - GET => { - method => 'check_user_permission_for_bug', - params => sub { - return {bug_id => $_[0], user_id => $_[1]}; - } - } - }, - qr{^/phabbugz/check_enter_bug/([^/]+)/(\d+)$}, - { - GET => { - method => 'check_user_enter_bug_permission', - params => sub { - return {product => $_[0], user_id => $_[1]}; - }, - }, - }, - qr{^/phabbugz/bug_revisions/(\d+)$}, - { - GET => { - method => 'bug_revisions', - params => sub { - return {bug_id => $_[0]}; - }, - }, - }, - qr{^/phabbugz/user/(\d+)$}, - { - GET => { - method => 'user', - params => sub { - return {user_id => $_[0]}; - }, - }, - }, - ]; -} - -1; diff --git a/extensions/PhabBugz/t/bmo/webservice.t b/extensions/PhabBugz/t/bmo/webservice.t index ee480c4e8d..0ddb1ec21f 100644 --- a/extensions/PhabBugz/t/bmo/webservice.t +++ b/extensions/PhabBugz/t/bmo/webservice.t @@ -6,9 +6,9 @@ # This Source Code Form is "Incompatible With Secondary Licenses", as # defined by the Mozilla Public License, v. 2.0. -# PhabBugz::WebService methods, called in-process so the fake Conduit -# answers their Phabricator requests: bug_revisions (with edge.search for -# stacks), user, and the permission checks used by Phabricator. +# The /rest/phabbugz routes, called in-process so the fake Conduit answers +# their Phabricator requests: bug_revisions (with edge.search for stacks), +# user, and the permission checks used by Phabricator. use 5.10.1; use strict; @@ -17,21 +17,63 @@ use lib qw(. lib local/lib/perl5); use FindBin; use lib "$FindBin::Bin/lib"; +BEGIN { + $ENV{LOG4PERL_CONFIG_FILE} = 'log4perl-t.conf'; + $ENV{BUGZILLA_DISABLE_HOSTAGE} = 1; +} + use Bugzilla; BEGIN { Bugzilla->extensions } -use Bugzilla::Config qw(SetParam); +use Bugzilla::Config qw(SetParam); +use Bugzilla::Test::Util qw(issue_api_key); +use Test::Mojo; use Test::More; use Bugzilla::Extension::PhabBugz::Util qw(create_revision_attachment); -use Bugzilla::Extension::PhabBugz::WebService; -use PhabTest::Conduit qw(:all); +use PhabTest::Conduit qw(:all); use PhabTest::Env; use PhabTest::Fixtures qw(:all); my $env = PhabTest::Env->new; my $conduit = $env->conduit; -my $ws = bless {}, 'Bugzilla::Extension::PhabBugz::WebService'; +my $t = Test::Mojo->new('Bugzilla::App'); + +# Each request starts with a clean request cache, so put the fake Conduit's +# params back (and any test override) before the action runs. +my %params; +$t->app->hook( + around_action => sub { + my ($next) = @_; + $conduit->apply_params; + SetParam($_, $params{$_}) foreach keys %params; + return $next->(); + } +); + +my %key; + +sub auth { + my ($user) = @_; + $key{$user->login} //= issue_api_key($user->login)->api_key; + return {'X-Bugzilla-API-Key' => $key{$user->login}}; +} + +# GET a route as $user and return the decoded response. +sub api { + my ($user, $path) = @_; + $t->get_ok("/rest/phabbugz/$path" => auth($user)); + return $t->tx->res->json; +} + +# Check that GET $path as $user fails with a message matching $re. +sub api_error { + my ($user, $path, $re, $name) = @_; + local $Test::Builder::Level = $Test::Builder::Level + 1; + my $json = api($user, $path); + ok($json->{error}, "$name: error"); + like($json->{message}, $re, "$name: message"); +} my $author = $env->bmo_user('author'); my $author_phab = $env->link_user($author, username => 'phabtest-author'); @@ -69,15 +111,18 @@ $conduit->add_edge($parent->{phid}, 'PHID-DREV-notonthisbug'); my ($timestamp) = Bugzilla->dbh->selectrow_array('SELECT NOW()'); foreach my $raw ($parent, $child, $moved) { - create_revision_attachment(Bugzilla::Bug->new($bug->id), - $conduit->load_revision($raw), $timestamp, $author); + create_revision_attachment( + Bugzilla::Bug->new($bug->id), + $conduit->load_revision($raw), + $timestamp, $author + ); } # bug_revisions { $env->fresh_request; $conduit->reset_calls; - my $result = $ws->bug_revisions({bug_id => $bug->id}); + my $result = api($author, 'bug_revisions/' . $bug->id); my ($search) = $conduit->calls('differential.revision.search'); is_deeply( @@ -141,23 +186,26 @@ foreach my $raw ($parent, $child, $moved) { { $env->fresh_request; $conduit->queue('differential.revision.search' => {data => 'not a list'}); - throws_error( - sub { $ws->bug_revisions({bug_id => $bug->id}) }, - 'bugzilla.code.phabricator_api_error', + api_error( + $author, + 'bug_revisions/' . $bug->id, + qr/Phabricator API Error|Reason:/, 'malformed search result' ); $conduit->queue('edge.search' => http_error(500)); - throws_error( - sub { $ws->bug_revisions({bug_id => $bug->id}) }, - 'bugzilla.code.phabricator_api_error', + api_error( + $author, + 'bug_revisions/' . $bug->id, + qr/Phabricator API Error|Reason:/, 'edge.search failure' ); + $env->fresh_request; my $empty = $env->bug; $conduit->reset_calls; is_deeply( - $ws->bug_revisions({bug_id => $empty->id}), + api($author, 'bug_revisions/' . $empty->id), {revisions => []}, 'bug without revisions' ); @@ -167,7 +215,7 @@ foreach my $raw ($parent, $child, $moved) { { $env->fresh_request; $conduit->reset_calls; - my $result = $ws->user({user_id => $author->id}); + my $result = api($author, 'user/' . $author->id); is_deeply( [$conduit->calls('bmoexternalaccount.search')], [{accountids => [$author->id]}], @@ -196,99 +244,92 @@ foreach my $raw ($parent, $child, $moved) { 'user details and Phabricator URLs' ); + $env->fresh_request; my $stranger = $env->bmo_user('no-phab-account'); $conduit->reset_calls; - is_deeply($ws->user({user_id => $stranger->id}), {}, 'unknown user returns {}'); + is_deeply(api($author, 'user/' . $stranger->id), {}, 'unknown user returns {}'); is($conduit->call_count('user.query'), 0, 'no user.query without a PHID'); $conduit->queue('user.query' => []); - is_deeply($ws->user({user_id => $author->id}), {}, + is_deeply(api($author, 'user/' . $author->id), {}, 'user.query without results'); } # Permission checks are only for phab-bot { + $env->fresh_request; my $group = $env->bug_group('ws'); my $private = $env->bug(groups => [$group]); + my $bot = $env->phab_bot; + my $uid = $author->id; - $env->fresh_request($env->phab_bot); is_deeply( - $ws->check_user_permission_for_bug({bug_id => $bug->id, user_id => $author->id - }), + api($bot, 'check_bug/' . $bug->id . "/$uid"), {result => 1}, 'user can see a public bug' ); is_deeply( - $ws->check_user_permission_for_bug({ - bug_id => $private->id, user_id => $author->id - }), + api($bot, 'check_bug/' . $private->id . "/$uid"), {result => 0}, 'user cannot see a private bug' ); is_deeply( - $ws->check_user_enter_bug_permission({ - product => 'Firefox', user_id => $author->id - }), + api($bot, "check_enter_bug/Firefox/$uid"), {result => 1}, 'user can file bugs in Firefox' ); - throws_error( - sub { $ws->check_user_permission_for_bug({bug_id => $bug->id}) }, - 'bugzilla.user.phabricator_invalid_request_params', - 'user_id is required' + is_deeply( + api($bot, "check_enter_bug/No.Such%20Product/$uid"), + {result => 0}, + 'product names may contain dots and spaces' ); - throws_error( - sub { $ws->check_user_enter_bug_permission({user_id => $author->id}) }, - 'bugzilla.user.phabricator_invalid_request_params', - 'product is required' + + # The routes require both values, so only a 0 can be missing. + api_error( + $bot, + 'check_bug/' . $bug->id . '/0', + qr/must be provided/, + 'user_id is required' ); + api_error($bot, "check_bug/0/$uid", qr/must be provided/, 'bug_id is required'); - $env->fresh_request($author); - throws_error( - sub { - $ws->check_user_permission_for_bug({bug_id => $bug->id, user_id => $author->id - }); - }, - 'bugzilla.user.phabricator_unauthorized_user', + api_error( + $author, + 'check_bug/' . $bug->id . "/$uid", + qr/do not have permission/, 'only phab-bot may call check_bug' ); - throws_error( - sub { - $ws->check_user_enter_bug_permission({ - product => 'Firefox', user_id => $author->id - }); - }, - 'bugzilla.user.phabricator_unauthorized_user', + api_error( + $author, + "check_enter_bug/Firefox/$uid", + qr/do not have permission/, 'only phab-bot may call check_enter_bug' ); + + $t->get_ok('/rest/phabbugz/bug_revisions/' . $bug->id) + ->status_is(401, 'bug_revisions needs a login'); } # Everything is refused while the integration is disabled { - $env->fresh_request($env->phab_bot); - SetParam('phabricator_enabled', 0); + my $bot = $env->phab_bot; + local $params{phabricator_enabled} = 0; $conduit->reset_calls; - throws_error( - sub { $ws->bug_revisions({bug_id => $bug->id}) }, - 'bugzilla.user.phabricator_not_enabled', + api_error( + $author, + 'bug_revisions/' . $bug->id, + qr/is not enabled/, 'bug_revisions disabled' ); - throws_error( - sub { $ws->user({user_id => $author->id}) }, - 'bugzilla.user.phabricator_not_enabled', - 'user disabled' - ); - throws_error( - sub { - $ws->check_user_permission_for_bug({bug_id => $bug->id, user_id => $author->id - }); - }, - 'bugzilla.user.phabricator_not_enabled', + api_error($author, 'user/' . $author->id, qr/is not enabled/, 'user disabled'); + api_error( + $bot, + 'check_bug/' . $bug->id . '/' . $author->id, + qr/is not enabled/, 'check_bug disabled' ); is($conduit->call_count, 0, 'no Conduit calls while disabled'); - $conduit->apply_params; } $env->fresh_request; From 4af97f40d5ce4df9c91df794da2d8bfdb3730db2 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Wed, 18 Nov 2026 22:18:37 +0100 Subject: [PATCH 09/19] Bug 2094122 - Migrate the Review REST resource to native Mojo API GET /rest/review/suggestions (by bug ID, product, product/component or query params) and GET /rest/review/flag_activity (by flag ID, type name, requestee, setter, type ID or query params) are now served by the native controller Bugzilla::Extension::Review::API::V1::Review instead of the legacy REST server. Same paths, parameters, checks and responses, include_fields and exclude_fields included. The legacy WebService module and its webservice hook are removed, and a test covers both methods. --- extensions/Review/Extension.pm | 6 - extensions/Review/lib/API/V1/Review.pm | 313 ++++++++++++++ extensions/Review/lib/WebService.pm | 541 ------------------------- extensions/Review/t/bmo/review-api.t | 145 +++++++ 4 files changed, 458 insertions(+), 547 deletions(-) create mode 100644 extensions/Review/lib/API/V1/Review.pm delete mode 100644 extensions/Review/lib/WebService.pm create mode 100644 extensions/Review/t/bmo/review-api.t diff --git a/extensions/Review/Extension.pm b/extensions/Review/Extension.pm index b83155f3a2..10ebc86cc5 100644 --- a/extensions/Review/Extension.pm +++ b/extensions/Review/Extension.pm @@ -675,12 +675,6 @@ sub search_operator_field_override { # web service / pages # -sub webservice { - my ($self, $args) = @_; - my $dispatch = $args->{dispatch}; - $dispatch->{Review} = "Bugzilla::Extension::Review::WebService"; -} - sub user_preferences { my ($self, $args) = @_; return unless $args->{current_tab} eq 'account' && $args->{save_changes}; diff --git a/extensions/Review/lib/API/V1/Review.pm b/extensions/Review/lib/API/V1/Review.pm new file mode 100644 index 0000000000..f5d42ae84d --- /dev/null +++ b/extensions/Review/lib/API/V1/Review.pm @@ -0,0 +1,313 @@ +# 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/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +package Bugzilla::Extension::Review::API::V1::Review; + +use 5.10.1; +use Mojo::Base qw( Mojolicious::Controller ); + +use Bugzilla; +use Bugzilla::Bug; +use Bugzilla::Component; +use Bugzilla::Constants; +use Bugzilla::Error; +use Bugzilla::FlagActivity; +use Bugzilla::FlagType; +use Bugzilla::Product; +use Bugzilla::User; +use Bugzilla::Util qw(detaint_natural ); +use Bugzilla::API::V1::Util qw(filter merge_request_params); + +use DateTime; + +sub setup_routes { + my ($class, $r) = @_; + my $to = 'Review::API::V1::Review#'; + + # bug-id + $r->get('/review/suggestions/:bug_id' => [bug_id => qr/\d+/]) + ->to($to . 'suggestions'); + + # product/component (the component name may contain slashes) + $r->get('/review/suggestions/#product/*component')->to($to . 'suggestions'); + + # just product + $r->get('/review/suggestions/#product')->to($to . 'suggestions'); + + # named parameters + $r->get('/review/suggestions')->to($to . 'suggestions'); + + # flag activity by flag id + $r->get('/review/flag_activity/:flag_id' => [flag_id => qr/\d+/]) + ->to($to . 'flag_activity'); + $r->get('/review/flag_activity/type_name/:type_name' => [type_name => qr/\w+/]) + ->to($to . 'flag_activity'); + + # flag activity by user or type id + $r->get('/review/flag_activity/:field/*value' => + [field => [qw(requestee setter type_id)]])->to($to . 'flag_activity'); + + # flag activity with only query strings + $r->get('/review/flag_activity')->to($to . 'flag_activity'); +} + +sub type { + my ($self, $type, $value) = @_; + return Bugzilla::API::V1::Util->type($type, $value); +} + +# The request params with the values from the path on top, or nothing after +# rendering an error. Logs the user in if credentials were sent. +sub _params { + my ($self, $lists, @path) = @_; + Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); + $self->bugzilla->login; + + my ($params, $error) = merge_request_params($self, $lists); + if ($error) { + $self->user_error($error); + return; + } + Bugzilla::API::V1::Util->split_include_exclude($params); + foreach my $name (@path) { + my $value = $self->stash($name); + $params->{$name} = $value if defined $value; + } + return $params; +} + +# GET /review/suggestions/ +# GET /review/suggestions/[/] +# GET /review/suggestions?bug_id= or ?product=[&component=] +# +# Suggested reviewers: the bug's mentors, then the component's reviewers, or +# the product's when the component has none. Returns a list of {id, email, +# name, nick, review_count}. +sub suggestions { + my ($self) = @_; + my $params = $self->_params([], qw(bug_id product component)) || return; + 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 authenticated to perform user matching + my $user = Bugzilla->user; + if (!$user->id) { + Bugzilla->set_user(Bugzilla::User->check({name => 'nobody@mozilla.org'})); + 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 @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), + nick => $self->type('string', $reviewer->nick), + review_count => $self->type('int', $reviewer->review_count), + }; + } + return $self->render(json => \@result); +} + +# GET /review/flag_activity/ +# GET /review/flag_activity/type_name/ +# GET /review/flag_activity/(requestee|setter|type_id)/ +# GET /review/flag_activity? +# +# Flag activity matching flag_id(s), type_id, type_name, requestee, setter, +# bug_id and status, between after and before (default: the last 30 days), +# limit and offset. Only activity on bugs and attachments the user can see. +sub flag_activity { + my ($self) = @_; + my $params = $self->_params(['flag_ids'], qw(flag_id type_name)) || return; + if (defined(my $field = $self->stash('field'))) { + $params->{$field} = $self->stash('value'); + } + 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}); + + $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_ids; + } + + if (my $type_id = $params->{type_id}) { + detaint_natural($type_id) + or ThrowUserError('invalid_flag_type_id', {type_id => $type_id}); + + $match_criteria{type_id} = $type_id; + } + + if (my $type_name = $params->{type_name}) { + my $flag_types = Bugzilla::FlagType::match({name => $type_name}); + $match_criteria{type_id} = [map { $_->id } @$flag_types]; + } + + 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'}); + + $match_criteria{$user_field . "_id"} = $user->id; + } + } + + foreach my $field (qw( bug_id status )) { + if (exists $params->{$field}) { + $match_criteria{$field} = $params->{$field}; + } + } + + 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::FlagActivity->match(\%match_criteria); + my $user = Bugzilla->user; + $user->visible_bugs([map { $_->bug_id } @$matches]); + my @results + = map { $self->_flag_activity_to_hash($_, $params) } + grep { $user->can_see_bug($_->bug_id) && _can_see_attachment($user, $_) } + @$matches; + return $self->render(json => \@results); +} + +sub _can_see_attachment { + my ($user, $flag_activity) = @_; + + return 1 if !$flag_activity->attachment_id; + return 0 if $flag_activity->attachment->isprivate && !$user->is_insider; + return 1; +} + +sub _flag_activity_to_hash { + my ($self, $fa, $params) = @_; + + my %flag = ( + id => $self->type('int', $fa->id), + creation_time => $self->type('string', $fa->flag_when), + type => $self->_flagtype_to_hash($fa->type), + setter => $self->_user_to_hash($fa->setter), + bug_id => $self->type('int', $fa->bug_id), + attachment_id => $self->type('int', $fa->attachment_id), + status => $self->type('string', $fa->status), + ); + + $flag{requestee} = $self->_user_to_hash($fa->requestee) if $fa->requestee; + $flag{flag_id} = $self->type('int', $fa->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), + }; +} + +sub _user_to_hash { + my ($self, $user) = @_; + + return { + id => $self->type('int', $user->id), + real_name => $self->type('string', $user->name), + nick => $self->type('string', $user->nick), + name => $self->type('email', $user->login), + }; +} + +1; diff --git a/extensions/Review/lib/WebService.pm b/extensions/Review/lib/WebService.pm deleted file mode 100644 index 5b88b2d8d7..0000000000 --- a/extensions/Review/lib/WebService.pm +++ /dev/null @@ -1,541 +0,0 @@ -# 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/. -# -# This Source Code Form is "Incompatible With Secondary Licenses", as -# defined by the Mozilla Public License, v. 2.0. - -package Bugzilla::Extension::Review::WebService; - -use 5.10.1; -use strict; -use warnings; - -use base qw(Bugzilla::WebService); - -use Bugzilla::Bug; -use Bugzilla::Component; -use Bugzilla::Error; -use Bugzilla::Util qw(detaint_natural ); -use Bugzilla::API::V1::Util qw(filter); - -use constant PUBLIC_METHODS => qw( - 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 @reviewers; - if ($bug) { - - # we always need to be authenticated to perform user matching - my $user = Bugzilla->user; - if (!$user->id) { - Bugzilla->set_user(Bugzilla::User->check({name => 'nobody@mozilla.org'})); - 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 @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), - nick => $self->type('string', $reviewer->nick), - 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; - - 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_ids; - } - - if (my $type_id = $params->{type_id}) { - detaint_natural($type_id) - or ThrowUserError('invalid_flag_type_id', {type_id => $type_id}); - - $match_criteria{type_id} = $type_id; - } - - if (my $type_name = $params->{type_name}) { - my $flag_types = Bugzilla::FlagType::match({name => $type_name}); - $match_criteria{type_id} = [map { $_->id } @$flag_types]; - } - - 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'}); - - $match_criteria{$user_field . "_id"} = $user->id; - } - } - - foreach my $field (qw( bug_id status )) { - if (exists $params->{$field}) { - $match_criteria{$field} = $params->{$field}; - } - } - - 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::FlagActivity->match(\%match_criteria); - my $user = Bugzilla->user; - $user->visible_bugs([map { $_->bug_id } @$matches]); - my @results - = map { $self->_flag_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_activity) = @_; - - return 1 if !$flag_activity->attachment_id; - return 0 if $flag_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]}; - }, - }, - }, - - # just product - qr{^/review/suggestions/([^/]+)$}, - { - GET => { - method => 'suggestions', - params => sub { - return {product => $_[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]}; - }, - }, - }, - - # 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'},}, - ]; -} - -sub _flag_activity_to_hash { - my ($self, $fa, $params) = @_; - - my %flag = ( - id => $self->type('int', $fa->id), - creation_time => $self->type('string', $fa->flag_when), - type => $self->_flagtype_to_hash($fa->type), - setter => $self->_user_to_hash($fa->setter), - bug_id => $self->type('int', $fa->bug_id), - attachment_id => $self->type('int', $fa->attachment_id), - status => $self->type('string', $fa->status), - ); - - $flag{requestee} = $self->_user_to_hash($fa->requestee) if $fa->requestee; - $flag{flag_id} = $self->type('int', $fa->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), - }; -} - -sub _user_to_hash { - my ($self, $user) = @_; - - return { - id => $self->type('int', $user->id), - real_name => $self->type('string', $user->name), - nick => $self->type('string', $user->nick), - name => $self->type('email', $user->login), - }; -} - -1; -__END__ -=head1 NAME - -Bugzilla::Extension::Review::WebService - Functions for the Mozilla specific -'review' flag optimizations. - -=head1 METHODS - -See L for a description of how parameters are passed, -and what B, B, and B mean. - -Although the data input and output is the same for JSON-RPC and REST, -the directions for how to access the data via REST is noted in each method -where applicable. - -=head2 suggestions - -B - -=over - -=item B - -Returns the list of suggestions for reviewers. - -=item B - -GET /rest/review/suggestions/C - -GET /rest/review/suggestions/C - -GET /rest/review/suggestions/C/C - -GET /rest/review/suggestions?product=C - -GET /rest/review/suggestions?product=C&component=C - -The returned data format is the same as below. - -=item B - -=over - -=item Query by Bug: - -=over - -=item C (integer) - The bug ID. - -=back - -=item Query by Product or Component: - -=over - -=item C (string) - The product name. - -=item C (string) - The component name (optional). If providing a C, a C must also be provided. - -=back - -=back - -=item B - -An array of hashes with the following keys/values: - -=over - -=item C (integer) - The user's ID. - -=item C (string) - The user's email address (aka login). - -=item C (string) - The user's display name (may not match the Bugzilla "real name"). - -=item C (string) - The number of "review" and "feedback" requests in the user's queue. - -=back - -=back - -=head2 flag_activity - -B - -=over - -=item B - -Returns the history of flag status changes based on requestee, setter, flag_id, type_id, or all. - -=item B - -GET /rest/review/flag_activity/C - -GET /rest/review/flag_activity/requestee/C - -GET /rest/review/flag_activity/setter/C - -GET /rest/review/flag_activity/type_id/C - -GET /rest/review/flag_activity/type_name/C - -GET /rest/review/flag_activity - -The returned data format is the same as below. - -=item B - -Use one or more of the following parameters to find specific flag status changes. - -=over - -=item C (integer) - The flag ID. - -Note that searching by C is not reliable because when flags are removed, flag_ids cease to exist. - -=item C (string) - The Bugzilla login of the flag's requestee - -=item C (string) - The Bugzilla login of the flag's setter - -=item C (int) - The flag type id of a change - -=item C (string) - the flag type name of a change - -=item C (string) - The flag's current status - -=item C (int) - The flag's bug id - -=back - -=item B - -An array of hashes with the following keys/values: - -=over - -=item C (integer) - -The id of the flag that changed. This field may be absent after a flag is deleted. - -=item C (dateTime) - -Timestamp of when the flag status changed. - -=item C (object) - -An object with the following fields: - -=over - -=item C (integer) - -The id of this activity event. - -=item C (string) - -The name of the flag type (review, feedback, etc) - -=item C (string) - -A plain english description of the flag type. - -=item C (string) - -The content of the target_type field of the flagtypes table. - -=item C (boolean) - -Boolean flag indicating if the flag type is available for use. - -=item C (boolean) - -Boolean flag indicating if the flag type is requesteeable. - -=item C (boolean) - -Boolean flag indicating if the flag type is multiplicable. - -=back - -=item C (object) - -The setter is the Bugzilla user that set the flag. It is represented by an object with the following fields. - -=over - -=item C (integer) - -The id of the Bugzilla user. A unique integer value. - -=item C (string) - -The real name of the Bugzilla user. - -=item C (string) - -The user's nickname. Currently this is extracted from the real_name, name or -email field. - -=item C (string) - -The Bugzilla login of the Bugzilla user (typically an email address). - -=back - -=item C (object) - -The requestee is the Bugzilla user that is specified by the flag. Optional - absent if there is no requestee. - -Requestee has the same keys/values as the setter object. - -=item C (integer) - -The id of the Bugzilla bug that the changed flag belongs to. - -=item C (integer) - -The id of the Bugzilla attachment that the changed flag belongs to. - -=item C (string) - -The status of the Bugzilla flag that changed. One of C<+ - ? X>. - -=back - -=back diff --git a/extensions/Review/t/bmo/review-api.t b/extensions/Review/t/bmo/review-api.t new file mode 100644 index 0000000000..94b3512be0 --- /dev/null +++ b/extensions/Review/t/bmo/review-api.t @@ -0,0 +1,145 @@ +#!/usr/bin/env perl +# 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/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +use strict; +use warnings; +use 5.10.1; +use lib qw( . lib local/lib/perl5 ); + +BEGIN { + $ENV{LOG4PERL_CONFIG_FILE} = 'log4perl-t.conf'; + $ENV{BUGZILLA_DISABLE_HOSTAGE} = 1; +} + +use Test::More; +use Test::Mojo; + +use Bugzilla; +BEGIN { Bugzilla->extensions } +use Bugzilla::Constants; +use Bugzilla::Bug; +use Bugzilla::Product; +use Bugzilla::User; +use Bugzilla::API::V1::Util qw(extract_flags); +use Bugzilla::Test::Util qw(issue_api_key); + +Bugzilla->usage_mode(USAGE_MODE_TEST); +Bugzilla->error_mode(ERROR_MODE_DIE); + +my $admin = Bugzilla::User->new({name => 'admin@mozilla.bugs'}); +my ($product) = grep { + $_->is_active + && @{$_->components} + && @{$_->versions} + && $_->default_security_group +} Bugzilla::Product->get_all; +plan skip_all => 'Need an admin and a product with a security group' + unless $admin && $product; + +my ($mentor, $requestee) = map { + Bugzilla::User->create({ + login_name => "review-api-$_-$$\@bmo.tld", cryptpassword => '*', + }) +} qw(mentor requestee); +my %auth = ('X-Bugzilla-API-Key' => issue_api_key($admin->login)->api_key); +my $t = Test::Mojo->new('Bugzilla::App'); + +Bugzilla->set_user($admin); +my %fields = ( + product => $product->name, + component => $product->components->[0]->name, + bug_type => 'defect', + bug_severity => 'normal', + op_sys => 'Unspecified', + rep_platform => 'Unspecified', + version => $product->versions->[0]->name, + comment => 'a comment', +); +my $bug = Bugzilla::Bug->create({ + %fields, + short_desc => "Review API test $$", + bug_mentors => [$mentor->login], +}); +my $private = Bugzilla::Bug->create({ + %fields, + short_desc => "Review API private $$", + groups => [$product->default_security_group] +}); + +# A needinfo request on each bug, so they have flag activity. +my $flagged = eval { + for my $b ($bug, $private) { + my ($flags, $new_flags) + = extract_flags( + [{name => 'needinfo', status => '?', requestee => $requestee->login}], $b); + $b->set_flags($flags, $new_flags); + $b->update; + } + 1; +}; +Bugzilla->clear_request_cache; + +# suggestions +$t->get_ok('/rest/review/suggestions/' . $bug->id => \%auth) + ->status_is(200) + ->json_is('/0/email' => $mentor->login) + ->json_is('/0/id' => $mentor->id, 'mentor suggested, id is a number'); +$t->get_ok('/rest/review/suggestions?bug_id=' . $bug->id) + ->status_is(200) + ->json_is('/0/id' => $mentor->id, 'bug_id as a query param, signed out'); +$t->get_ok('/rest/review/suggestions/' . $product->name) + ->status_is(200) + ->json_has('', 'by product'); +$t->get_ok('/rest/review/suggestions/' + . $product->name . '/' + . $product->components->[0]->name) + ->status_is(200) + ->json_has('', 'by product and component'); +$t->get_ok('/rest/review/suggestions/No.Such%20Product') + ->json_is('/error' => 1) + ->json_like('/message' => qr/No\.Such Product/, 'unknown product'); +$t->get_ok('/rest/review/suggestions') + ->json_is('/error' => 1) + ->json_like('/message' => qr/must provide either a bug_id/, 'no params'); + +# flag_activity +SKIP: { + skip 'no needinfo flag type', 18 unless $flagged; + my $login = $requestee->login; + + $t->get_ok("/rest/review/flag_activity/requestee/$login" => \%auth) + ->status_is(200) + ->json_is('/0/requestee/name' => $login) + ->json_is('/0/type/name' => 'needinfo') + ->json_is('/0/type/is_active' => Mojo::JSON::true, 'by requestee'); + my ($activity) = grep { $_->{bug_id} == $bug->id } @{$t->tx->res->json}; + ok($activity, 'the public bug is listed'); + my $flag_id = $activity->{flag_id}; + + $t->get_ok("/rest/review/flag_activity/requestee/$login") + ->status_is(200) + ->json_is('/0/bug_id' => $bug->id) + ->json_is('/1' => undef, 'the private bug is hidden when signed out'); + + $t->get_ok("/rest/review/flag_activity/$flag_id" => \%auth) + ->status_is(200) + ->json_hasnt('/0/flag_id', 'by flag id, without flag_id'); + $t->get_ok("/rest/review/flag_activity?flag_ids=$flag_id" => \%auth) + ->json_is('/0/flag_id' => $flag_id, 'flag_ids as a query param'); + + $t->get_ok("/rest/review/flag_activity/type_name/needinfo" + . "?requestee=$login&include_fields=id,status" => \%auth) + ->json_is('/0/status' => '?') + ->json_hasnt('/0/type', 'by type name, include_fields as a list'); +} +$t->get_ok('/rest/review/flag_activity/requestee/nobody-here@bmo.tld') + ->json_is('/error' => 1, 'unknown requestee'); +$t->get_ok('/rest/review/flag_activity?bug_id=1&offset=5') + ->json_is('/error' => 1, 'offset needs a limit'); + +done_testing; From bab1d8b48cf1ca0e1519d059036ae5b8115ed059 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Wed, 18 Nov 2026 23:01:04 +0100 Subject: [PATCH 10/19] Bug 2094122 - Migrate the BugModal REST resource to native Mojo API GET /rest/bug_modal/initial_field_values, product_info, edit/, cc/, comment_tags/ and new_product/, and PUT /rest/bug_modal/update_comment_tags/ are now served by the native controller Bugzilla::Extension::BugModal::API::V1::BugModal instead of the legacy REST server. Same paths, parameters, checks and responses. A bug id sent as a hash is now refused, as on the core bug routes. The legacy WebService module and its webservice hook are removed, and a test covers the seven routes. rest_legacy_credentials.t is deleted: no resource is left on the legacy REST server. --- extensions/BugModal/Extension.pm | 6 - .../lib/{WebService.pm => API/V1/BugModal.pm} | 262 +++++++++--------- extensions/BugModal/t/bmo/bug-modal-api.t | 134 +++++++++ qa/t/rest_legacy_credentials.t | 79 ------ 4 files changed, 269 insertions(+), 212 deletions(-) rename extensions/BugModal/lib/{WebService.pm => API/V1/BugModal.pm} (69%) create mode 100644 extensions/BugModal/t/bmo/bug-modal-api.t delete mode 100644 qa/t/rest_legacy_credentials.t diff --git a/extensions/BugModal/Extension.pm b/extensions/BugModal/Extension.pm index f21b50e683..067f58dac6 100644 --- a/extensions/BugModal/Extension.pm +++ b/extensions/BugModal/Extension.pm @@ -307,12 +307,6 @@ sub bug_start_of_set_all { } } -sub webservice { - my ($self, $args) = @_; - my $dispatch = $args->{dispatch}; - $dispatch->{bug_modal} = 'Bugzilla::Extension::BugModal::WebService'; -} - sub install_before_final_checks { my ($self, $args) = @_; remove_setting('ui_experiments'); diff --git a/extensions/BugModal/lib/WebService.pm b/extensions/BugModal/lib/API/V1/BugModal.pm similarity index 69% rename from extensions/BugModal/lib/WebService.pm rename to extensions/BugModal/lib/API/V1/BugModal.pm index ade6503b3b..4e8e9c8717 100644 --- a/extensions/BugModal/lib/WebService.pm +++ b/extensions/BugModal/lib/API/V1/BugModal.pm @@ -5,14 +5,14 @@ # This Source Code Form is "Incompatible With Secondary Licenses", as # defined by the Mozilla Public License, v. 2.0. -package Bugzilla::Extension::BugModal::WebService; -use 5.10.1; -use strict; -use warnings; +package Bugzilla::Extension::BugModal::API::V1::BugModal; -use base qw(Bugzilla::WebService); +use 5.10.1; +use Mojo::Base qw(Mojolicious::Controller); +use Bugzilla; use Bugzilla::Bug; +use Bugzilla::Comment; use Bugzilla::Constants; use Bugzilla::Error; use Bugzilla::Field; @@ -23,124 +23,117 @@ use Bugzilla::Milestone; use Bugzilla::Product; use Bugzilla::Util qw(detaint_natural); use Bugzilla::Version; -use List::MoreUtils qw(any first_value); +use Bugzilla::API::V1::Util qw(merge_request_params); +use List::MoreUtils qw(any first_value); # these methods are much lighter than our public API calls -sub rest_resources { - return [ - # return all the products accessible by the user. - # required by new-bug - qr{^/bug_modal/initial_field_values}, - {GET => {method => 'initial_field_values'},}, +sub setup_routes { + my ($class, $r) = @_; + my $to = 'BugModal::API::V1::BugModal#'; + my @id = (id => qr/\d+/); - # return all the components pertaining to the product. - # required by new-bug - qr{^/bug_modal/product_info}, - { - GET => { - method => 'product_info', - params => sub { - return {product_name => Bugzilla->input_params->{product}}; - }, - }, - }, - - # return all the lazy-loaded data; kept in sync with the UI's - # requirements. - qr{^/bug_modal/edit/(\d+)$}, - { - GET => { - method => 'edit', - params => sub { - return {id => $_[0]}; - }, - }, - }, - - # returns pre-formatted HTML, enabling reuse of the user template - qr{^/bug_modal/cc/(\d+)$}, - { - GET => { - method => 'cc', - params => sub { - return {id => $_[0]}; - }, - }, - }, - - # returns pre-formatted HTML representing the comment tags - qr{^/bug_modal/comment_tags/(\d+)$}, - { - GET => { - method => 'comment_tags', - params => sub { - return {id => $_[0]}; - }, - }, - }, - - # make changes to comment tags and return preformatted html - qr{^/bug_modal/update_comment_tags/(\d+)$}, - { - PUT => { - method => 'update_comment_tags', - params => sub { - return {id => $_[0]}; - }, - }, - }, - - # returns fields that require touching when the product is changed - qw{^/bug_modal/new_product/(\d+)$}, - { - GET => { - method => 'new_product', - params => sub { - - # products with slashes in their name means we have to grab - # the product from the query-string instead of the path - return {id => $_[0], product_name => Bugzilla->input_params->{product}}; - }, - }, - }, - ]; + # return all the products accessible by the user. + # required by new-bug + $r->get('/bug_modal/initial_field_values')->to($to . 'initial_field_values'); + + # return all the components pertaining to the product (?product=). + # required by new-bug + $r->get('/bug_modal/product_info')->to($to . 'product_info'); + + # return all the lazy-loaded data; kept in sync with the UI's + # requirements. + $r->get('/bug_modal/edit/:id' => [@id])->to($to . 'edit'); + + # returns pre-formatted HTML, enabling reuse of the user template + $r->get('/bug_modal/cc/:id' => [@id])->to($to . 'cc'); + + # returns pre-formatted HTML representing the comment tags + $r->get('/bug_modal/comment_tags/:id' => [@id])->to($to . 'comment_tags'); + + # make changes to comment tags and return preformatted html + $r->put('/bug_modal/update_comment_tags/:id' => [@id]) + ->to($to . 'update_comment_tags'); + + # returns fields that require touching when the product is changed. + # products with slashes in their name means we have to grab the product + # from the query-string (?product=) instead of the path + $r->get('/bug_modal/new_product/:id' => [@id])->to($to . 'new_product'); +} + +sub type { + my ($self, $type, $value) = @_; + return Bugzilla::API::V1::Util->type($type, $value); +} + +# The request params, with the id from the path unless the request has one, +# as on the legacy server. Logs the user in if credentials were sent. Returns +# nothing after rendering an error. +sub _params { + my ($self, $lists) = @_; + Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); + $self->bugzilla->login; + + my ($params, $error) = merge_request_params($self, $lists); + if ($error) { + $self->user_error($error); + return; + } + $params->{id} //= $self->stash('id'); + return $params; +} + +# The bug named by the id param. A hash here would reach Bugzilla::Bug->check() +# as a set of object conditions. +sub _bug { + my ($self, $params) = @_; + ThrowUserError('invalid_params', {type_error => 'id must be a single value'}) + if ref $params->{id}; + return Bugzilla::Bug->check({id => $params->{id}}); } +# GET /bug_modal/initial_field_values sub initial_field_values { + my ($self) = @_; + $self->_params or return; my $user = Bugzilla->user; - return { - products => _name($user->get_enterable_products), - keywords => _name([Bugzilla::Keyword->get_all()]), - default_bug_types => get_legal_field_values('bug_type'), - }; + return $self->render( + json => { + products => _name($user->get_enterable_products), + keywords => _name([Bugzilla::Keyword->get_all()]), + default_bug_types => get_legal_field_values('bug_type'), + } + ); } +# GET /bug_modal/product_info?product= sub product_info { - my ($self, $params) = @_; - if (!ref $params->{product_name}) { - } - else { - ThrowCodeError('params_required', - {function => 'BugModal.components', params => ['product']}); - } + my ($self) = @_; + my $params = $self->_params or return; + ThrowCodeError('params_required', + {function => 'BugModal.components', params => ['product']}) + if ref $params->{product}; my $product - = Bugzilla::Product->check({name => $params->{product_name}, cache => 1}); + = Bugzilla::Product->check({name => $params->{product}, cache => 1}); $product = Bugzilla->user->can_enter_product($product, 1); my @components = map { { - name => $_->name, - description => $_->description, + name => $_->name, + description => $_->description, default_bug_type => $_->default_bug_type, } } @{$product->components}; - return {components => \@components, versions => _name($product->versions),}; + return $self->render( + json => {components => \@components, versions => _name($product->versions)}); } # everything we need for edit mode in a single call, returning just the fields # that the ui requires. +# +# GET /bug_modal/edit/ sub edit { - my ($self, $params) = @_; - my $user = Bugzilla->user; - my $bug = Bugzilla::Bug->check({id => $params->{id}}); + my ($self) = @_; + my $params = $self->_params or return; + my $user = Bugzilla->user; + my $bug = $self->_bug($params); # the keys of the options hash must match the field id in the ui my %options; @@ -163,8 +156,7 @@ sub edit { # custom select fields my @custom_fields = grep { - $_->type == FIELD_TYPE_SINGLE_SELECT - || $_->type == FIELD_TYPE_MULTI_SELECT + $_->type == FIELD_TYPE_SINGLE_SELECT || $_->type == FIELD_TYPE_MULTI_SELECT } Bugzilla->active_custom_fields( {product => $bug->product_obj, component => $bug->component_obj}); foreach my $field (@custom_fields) { @@ -186,7 +178,8 @@ sub edit { my @keywords = grep { $_->is_active } Bugzilla::Keyword->get_all(); # results - return {options => \%options, keywords => [map { $_->name } @keywords],}; + return $self->render( + json => {options => \%options, keywords => [map { $_->name } @keywords]}); } sub _name { @@ -198,14 +191,15 @@ sub _name { $values = Bugzilla::Field->new({name => $values, cache => 1})->legal_values; } return [map { {name => $_->name} } - grep { (defined $current && $_->name eq $current) || $_->is_active } - @$values]; + grep { (defined $current && $_->name eq $current) || $_->is_active } @$values]; } +# GET /bug_modal/cc/ sub cc { - my ($self, $params) = @_; + my ($self) = @_; + my $params = $self->_params or return; my $template = Bugzilla->template; - my $bug = Bugzilla::Bug->check({id => $params->{id}}); + my $bug = $self->_bug($params); my $vars = { bug => $bug, cc_list => [sort { lc($a->identity) cmp lc($b->identity) } @{$bug->cc_users}] @@ -214,11 +208,13 @@ sub cc { my $html = ''; $template->process('bug_modal/cc_list.html.tmpl', $vars, \$html) || ThrowTemplateError($template->error); - return {html => $html}; + return $self->render(json => {html => $html}); } +# GET /bug_modal/comment_tags/ sub comment_tags { - my ($self, $params) = @_; + my ($self) = @_; + my $params = $self->_params or return; my $user = Bugzilla->user; my $template = Bugzilla->template; @@ -237,12 +233,15 @@ sub comment_tags { ThrowUserError('comment_is_private', {id => $comment->id}); } - return {html => _render_comment_html($comment, $user)}; + return $self->render(json => {html => _render_comment_html($comment, $user)}); } +# PUT /bug_modal/update_comment_tags/ {add: [...], remove: [...]} sub update_comment_tags { - my ($self, $params) = @_; - my $user = Bugzilla->login(LOGIN_REQUIRED); + my ($self) = @_; + my $params = $self->_params([qw(add remove)]) or return; + my $user = Bugzilla->user; + $user->id || ThrowUserError('login_required'); Bugzilla->params->{'comment_taggers_group'} || ThrowUserError('comment_tag_disabled'); @@ -281,14 +280,14 @@ sub update_comment_tags { $comment->update(); $dbh->bz_commit_transaction(); - return {html => _render_comment_html($comment, $user)}; + return $self->render(json => {html => _render_comment_html($comment, $user)}); } sub _render_comment_html { my ($comment, $user) = @_; my $template = Bugzilla->template; - my $html = ''; - my $vars = { + my $html = ''; + my $vars = { comment => $comment, user => $user, basepath => Bugzilla->localconfig->basepath @@ -298,13 +297,18 @@ sub _render_comment_html { return $html; } +# GET /bug_modal/new_product/?product= sub new_product { - my ($self, $params) = @_; - my $dbh = Bugzilla->dbh; - my $user = Bugzilla->user; - my $bug = Bugzilla::Bug->check({id => $params->{id}}); + my ($self) = @_; + my $params = $self->_params or return; + my $dbh = Bugzilla->dbh; + my $user = Bugzilla->user; + my $bug = $self->_bug($params); + ThrowCodeError('params_required', + {function => 'BugModal.new_product', params => ['product']}) + if ref $params->{product}; my $product - = Bugzilla::Product->check({name => $params->{product_name}, cache => 1}); + = Bugzilla::Product->check({name => $params->{product}, cache => 1}); my $true = $self->type('boolean', 1); my %result; @@ -319,6 +323,7 @@ sub new_product { $component->{selected} = $true; } elsif (scalar @{$components} == 1) { + # New product has one component so preselect it $components->[0]->{selected} = $true; } @@ -332,10 +337,12 @@ sub new_product { my $milestones = _name($product->milestones); my $current_milestone = $bug->target_milestone; - my $can_change = $bug->check_can_change_field('target_milestone', 0, 1); - if ($can_change->{allowed} + my $can_change = $bug->check_can_change_field('target_milestone', 0, 1); + if ( + $can_change->{allowed} && (my $milestone - = first_value { $_->{name} eq $current_milestone } @$milestones)) + = first_value { $_->{name} eq $current_milestone } @$milestones) + ) { # identical milestone in both products $milestone->{selected} = $true; @@ -385,9 +392,10 @@ sub new_product { } } if (!$selected_version) { + # use default version my $default_version = $product->default_version; - my $version = first_value { $_->{name} eq $default_version } @$versions; + my $version = first_value { $_->{name} eq $default_version } @$versions; $version->{selected} = $true; } $result{version} = $versions; @@ -511,7 +519,7 @@ sub new_product { $vars, \$result{groups}) || ThrowTemplateError($template->error); - return \%result; + return $self->render(json => \%result); } 1; diff --git a/extensions/BugModal/t/bmo/bug-modal-api.t b/extensions/BugModal/t/bmo/bug-modal-api.t new file mode 100644 index 0000000000..4f73137e4b --- /dev/null +++ b/extensions/BugModal/t/bmo/bug-modal-api.t @@ -0,0 +1,134 @@ +#!/usr/bin/env perl +# 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/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +use strict; +use warnings; +use 5.10.1; +use lib qw( . lib local/lib/perl5 ); + +BEGIN { + $ENV{LOG4PERL_CONFIG_FILE} = 'log4perl-t.conf'; + $ENV{BUGZILLA_DISABLE_HOSTAGE} = 1; +} + +use Test::More; +use Test::Mojo; + +use Bugzilla; +BEGIN { Bugzilla->extensions } +use Bugzilla::Constants; +use Bugzilla::Bug; +use Bugzilla::Product; +use Bugzilla::User; +use Bugzilla::Test::Util qw(issue_api_key); + +Bugzilla->usage_mode(USAGE_MODE_TEST); +Bugzilla->error_mode(ERROR_MODE_DIE); + +my $admin = Bugzilla::User->new({name => 'admin@mozilla.bugs'}); +my ($product) + = grep { $_->is_active && @{$_->components} && @{$_->versions} } + Bugzilla::Product->get_all; +plan skip_all => 'Need an admin and a product' unless $admin && $product; + +my %auth = ('X-Bugzilla-API-Key' => issue_api_key($admin->login)->api_key); +my $t = Test::Mojo->new('Bugzilla::App'); + +Bugzilla->set_user($admin); +my $bug = Bugzilla::Bug->create({ + short_desc => "BugModal API test $$", + product => $product->name, + component => $product->components->[0]->name, + bug_type => 'defect', + bug_severity => 'normal', + op_sys => 'Unspecified', + rep_platform => 'Unspecified', + version => $product->versions->[0]->name, + comment => 'a comment', + cc => [$admin->login], +}); +$bug->add_comment('a private comment'); +$bug->update; +my ($public, $private) + = map { $_->id } @{Bugzilla::Bug->new($bug->id)->comments}[0, 1]; + +# The admin isn't an insider here, so it can't add a private comment. +Bugzilla->dbh->do('UPDATE longdescs SET isprivate = 1 WHERE comment_id = ?', + undef, $private); +my $bug_id = $bug->id; +Bugzilla->clear_request_cache; + +# initial_field_values and product_info +$t->get_ok('/rest/bug_modal/initial_field_values') + ->status_is(200) + ->json_has('/products') + ->json_has('/keywords') + ->json_has('/default_bug_types/0', 'initial field values, signed out'); +my $name = $product->name; +$t->get_ok('/rest/bug_modal/product_info' => form => {product => $name}) + ->status_is(200) + ->json_is('/components/0/name' => $product->components->[0]->name) + ->json_has('/versions/0/name', 'product info'); +$t->get_ok('/rest/bug_modal/product_info?product=No.Such%20Product') + ->json_is('/error' => 1) + ->json_like('/message' => qr/No\.Such Product/, 'unknown product'); + +# edit, cc +$t->get_ok("/rest/bug_modal/edit/$bug_id" => \%auth) + ->status_is(200) + ->json_is('/options/component/0/name' => $bug->component) + ->json_has('/options/bug_type/0/name') + ->json_has('/keywords', 'edit options'); +$t->get_ok("/rest/bug_modal/edit/$bug_id" => \%auth => json => + {id => {condition => 'bug_id = ?', values => [$bug_id]}}) + ->json_is('/error' => 1, 'a hash id is refused'); +$t->get_ok("/rest/bug_modal/cc/$bug_id" => \%auth) + ->status_is(200) + ->json_like('/html' => qr/data-user-id="${\ $admin->id }"/, 'cc list html'); +$t->get_ok('/rest/bug_modal/cc/999999999') + ->json_is('/error' => 1, 'unknown bug'); + +# comment_tags, update_comment_tags +$t->get_ok("/rest/bug_modal/comment_tags/$public") + ->status_is(200) + ->json_has('/html', 'comment tags html'); +$t->get_ok("/rest/bug_modal/comment_tags/$private") + ->json_is('/error' => 1) + ->json_like( + '/message' => qr/private/, + 'private comment hidden when signed out' + ); +$t->put_ok( + "/rest/bug_modal/update_comment_tags/$public" => json => {add => ['bmtag']}) + ->status_is(401, 'update_comment_tags needs a login'); +$t->put_ok("/rest/bug_modal/update_comment_tags/$public" => \%auth => json => + {add => ['bmtag']}) + ->status_is(200) + ->json_like('/html' => qr/bmtag/, 'tag added'); +$t->put_ok("/rest/bug_modal/update_comment_tags/$public" => \%auth => json => + {remove => ['bmtag']}) + ->status_is(200) + ->json_unlike('/html' => qr/bmtag/, 'tag removed'); +$t->put_ok("/rest/bug_modal/update_comment_tags/$public" => \%auth => json => + {id => {condition => 'comment_id = ?', values => [$public]}}) + ->json_is('/error' => 1) + ->json_like('/message' => qr/numeric/, 'a hash id is refused'); + +# new_product +$t->get_ok( + "/rest/bug_modal/new_product/$bug_id" => \%auth => form => {product => $name}) + ->status_is(200) + ->json_is('/component/0/name' => $bug->component) + ->json_is('/component/0/selected' => Mojo::JSON::true) + ->json_has('/version/0/name') + ->json_has('/target_milestone/0/name') + ->json_has('/groups', 'fields for the same product'); +$t->get_ok("/rest/bug_modal/new_product/$bug_id" => \%auth) + ->json_is('/error' => 1, 'product is required'); + +done_testing; diff --git a/qa/t/rest_legacy_credentials.t b/qa/t/rest_legacy_credentials.t deleted file mode 100644 index 5920273755..0000000000 --- a/qa/t/rest_legacy_credentials.t +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/env perl -# 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/. -# -# This Source Code Form is "Incompatible With Secondary Licenses", as -# defined by the Mozilla Public License, v. 2.0. - -####################################################### -# Test for Bugzilla_login/Bugzilla_password, which # -# resources still on the legacy REST server accept # -# (only extensions are left there). # -# GET /rest/bug_modal/initial_field_values # -# Delete with the last legacy resource. # -####################################################### - -use 5.10.1; -use strict; -use warnings; -use lib qw(lib ../../lib ../../local/lib/perl5); - -use Bugzilla; -use QA::Util qw(get_config); -use QA::REST::Util qw(rest_get_url); -use Test::Mojo; -use Test::More; - -use constant INVALID_EMAIL => '@invalid_user@'; - -my $config = get_config(); -my $url = Bugzilla->localconfig->urlbase; - -my $user = $config->{unprivileged_user_login}; -my $pass = $config->{unprivileged_user_passwd}; -my $error = "The username or password you entered is not valid"; - -my $t = Test::Mojo->new(); -$t->ua->max_redirects(1); - -$t->get_ok(rest_get_url( - $url, - 'rest/bug_modal/initial_field_values', - {Bugzilla_login => $user, Bugzilla_password => $pass} -))->status_is(200); -ok($t->tx->res->json('/products'), 'Valid credentials return the field values'); - -my @tests = ( - {login => $user, password => '', test => "Empty password can't log in"}, - {login => '', password => $pass, test => "Empty login can't log in"}, - { - login => INVALID_EMAIL, - password => $pass, - test => "Invalid email can't log in" - }, - {login => $user, password => '*', test => "Invalid password can't log in"}, - { - login => $config->{disabled_user_login}, - password => $config->{disabled_user_passwd}, - error => "!!This is the text!!", - test => "Can't log in with a disabled account", - }, - { - login => $config->{disabled_user_login}, - password => '*', - test => "Logging in with invalid password doesn't show disabledtext", - }, -); - -foreach my $test (@tests) { - $t->get_ok(rest_get_url( - $url, - 'rest/bug_modal/initial_field_values', - {Bugzilla_login => $test->{login}, Bugzilla_password => $test->{password}} - ))->status_isnt(200); - my $expected = $test->{error} // $error; - like($t->tx->res->json('/message'), qr/\Q$expected\E/, $test->{test}); -} - -done_testing(); From e3b983a9eac76d1ec79de2ffdaa2e55c53bf7941 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Wed, 18 Nov 2026 23:32:36 +0100 Subject: [PATCH 11/19] Bug 2094122 - Fix - Keep the path id on BugModal GET routes On a GET, the legacy REST server kept the id from the path even when the query string had one, and only a PUT body or query string replaced it. The controller let a query-string id win on GET too, so GET /rest/bug_modal/edit/5?id=6 answered for bug 6. The path id wins again on GET, which also makes the hash id check unneeded, so it is removed. --- extensions/BugModal/lib/API/V1/BugModal.pm | 24 ++++++++-------------- extensions/BugModal/t/bmo/bug-modal-api.t | 9 +++++++- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/extensions/BugModal/lib/API/V1/BugModal.pm b/extensions/BugModal/lib/API/V1/BugModal.pm index 4e8e9c8717..e486e82d41 100644 --- a/extensions/BugModal/lib/API/V1/BugModal.pm +++ b/extensions/BugModal/lib/API/V1/BugModal.pm @@ -66,9 +66,9 @@ sub type { return Bugzilla::API::V1::Util->type($type, $value); } -# The request params, with the id from the path unless the request has one, -# as on the legacy server. Logs the user in if credentials were sent. Returns -# nothing after rendering an error. +# The request params with the id from the path. As on the legacy server, an +# id sent with a PUT replaces the one in the path, but not on a GET. Logs the +# user in if credentials were sent. Returns nothing after rendering an error. sub _params { my ($self, $lists) = @_; Bugzilla->usage_mode(USAGE_MODE_MOJO_REST); @@ -79,19 +79,11 @@ sub _params { $self->user_error($error); return; } - $params->{id} //= $self->stash('id'); + $params->{id} = $self->stash('id') + if $self->req->method eq 'GET' || !defined $params->{id}; return $params; } -# The bug named by the id param. A hash here would reach Bugzilla::Bug->check() -# as a set of object conditions. -sub _bug { - my ($self, $params) = @_; - ThrowUserError('invalid_params', {type_error => 'id must be a single value'}) - if ref $params->{id}; - return Bugzilla::Bug->check({id => $params->{id}}); -} - # GET /bug_modal/initial_field_values sub initial_field_values { my ($self) = @_; @@ -133,7 +125,7 @@ sub edit { my ($self) = @_; my $params = $self->_params or return; my $user = Bugzilla->user; - my $bug = $self->_bug($params); + my $bug = Bugzilla::Bug->check({id => $params->{id}}); # the keys of the options hash must match the field id in the ui my %options; @@ -199,7 +191,7 @@ sub cc { my ($self) = @_; my $params = $self->_params or return; my $template = Bugzilla->template; - my $bug = $self->_bug($params); + my $bug = Bugzilla::Bug->check({id => $params->{id}}); my $vars = { bug => $bug, cc_list => [sort { lc($a->identity) cmp lc($b->identity) } @{$bug->cc_users}] @@ -303,7 +295,7 @@ sub new_product { my $params = $self->_params or return; my $dbh = Bugzilla->dbh; my $user = Bugzilla->user; - my $bug = $self->_bug($params); + my $bug = Bugzilla::Bug->check({id => $params->{id}}); ThrowCodeError('params_required', {function => 'BugModal.new_product', params => ['product']}) if ref $params->{product}; diff --git a/extensions/BugModal/t/bmo/bug-modal-api.t b/extensions/BugModal/t/bmo/bug-modal-api.t index 4f73137e4b..62b589af36 100644 --- a/extensions/BugModal/t/bmo/bug-modal-api.t +++ b/extensions/BugModal/t/bmo/bug-modal-api.t @@ -86,12 +86,19 @@ $t->get_ok("/rest/bug_modal/edit/$bug_id" => \%auth) ->json_has('/keywords', 'edit options'); $t->get_ok("/rest/bug_modal/edit/$bug_id" => \%auth => json => {id => {condition => 'bug_id = ?', values => [$bug_id]}}) - ->json_is('/error' => 1, 'a hash id is refused'); + ->status_is(200) + ->json_is( + '/options/component/0/name' => $bug->component, + 'on a GET, an id in the body is ignored' + ); $t->get_ok("/rest/bug_modal/cc/$bug_id" => \%auth) ->status_is(200) ->json_like('/html' => qr/data-user-id="${\ $admin->id }"/, 'cc list html'); $t->get_ok('/rest/bug_modal/cc/999999999') ->json_is('/error' => 1, 'unknown bug'); +$t->get_ok("/rest/bug_modal/cc/$bug_id?id=999999999" => \%auth) + ->status_is(200) + ->json_has('/html', 'on a GET, the id in the path wins'); # comment_tags, update_comment_tags $t->get_ok("/rest/bug_modal/comment_tags/$public") From 7c98353d3741977d459cc44035bdd5b1d12430a8 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Wed, 18 Nov 2026 23:34:39 +0100 Subject: [PATCH 12/19] Bug 2094122 - Fix - Answer an empty ProdCompSearch search with param_required The legacy route matched /prod_comp_search/find/ with an empty search and returned the param_required error. The *search placeholder needed at least one character, so it was a 404. The placeholder now defaults to ''. --- extensions/ProdCompSearch/lib/API/V1/ProdCompSearch.pm | 5 ++++- extensions/ProdCompSearch/t/bmo/prod-comp-search.t | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/extensions/ProdCompSearch/lib/API/V1/ProdCompSearch.pm b/extensions/ProdCompSearch/lib/API/V1/ProdCompSearch.pm index f87944e698..97e50ef7f7 100644 --- a/extensions/ProdCompSearch/lib/API/V1/ProdCompSearch.pm +++ b/extensions/ProdCompSearch/lib/API/V1/ProdCompSearch.pm @@ -18,8 +18,11 @@ use Time::localtime; sub setup_routes { my ($class, $r) = @_; + + # An empty search gets the param_required error, not a 404. $r->get('/prod_comp_search/find/*search') - ->to('ProdCompSearch::API::V1::ProdCompSearch#prod_comp_search'); + ->to('ProdCompSearch::API::V1::ProdCompSearch#prod_comp_search', + search => ''); $r->get('/prod_comp_search/frequent') ->to('ProdCompSearch::API::V1::ProdCompSearch#list_frequent_components'); } diff --git a/extensions/ProdCompSearch/t/bmo/prod-comp-search.t b/extensions/ProdCompSearch/t/bmo/prod-comp-search.t index 1305af4244..a3c0fda3ee 100644 --- a/extensions/ProdCompSearch/t/bmo/prod-comp-search.t +++ b/extensions/ProdCompSearch/t/bmo/prod-comp-search.t @@ -68,6 +68,10 @@ $t->get_ok("/rest/prod_comp_search/find/nosuchproduct$$") ->status_is(200) ->json_is('/products' => [], 'no match'); +$t->get_ok('/rest/prod_comp_search/find/') + ->json_is('/error' => 1) + ->json_is('/code' => 50, 'an empty search is param_required, not a 404'); + $t->get_ok('/rest/prod_comp_search/frequent') ->status_is(200) ->json_is('/results' => [], 'nothing for a signed out user'); From 250add2dc7076dffc0d818a76eda7460986618ea Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Wed, 18 Nov 2026 23:36:45 +0100 Subject: [PATCH 13/19] Bug 2094122 - Fix - Accept an empty value in Review flag_activity paths The legacy route matched /review/flag_activity/requestee/ (and setter/, type_id/) with an empty value, which was then ignored. The *value placeholder made it a 404. The placeholder now defaults to ''. --- extensions/Review/lib/API/V1/Review.pm | 5 +++-- extensions/Review/t/bmo/review-api.t | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/extensions/Review/lib/API/V1/Review.pm b/extensions/Review/lib/API/V1/Review.pm index f5d42ae84d..e009087bd1 100644 --- a/extensions/Review/lib/API/V1/Review.pm +++ b/extensions/Review/lib/API/V1/Review.pm @@ -47,9 +47,10 @@ sub setup_routes { $r->get('/review/flag_activity/type_name/:type_name' => [type_name => qr/\w+/]) ->to($to . 'flag_activity'); - # flag activity by user or type id + # flag activity by user or type id (an empty one is ignored, as before) $r->get('/review/flag_activity/:field/*value' => - [field => [qw(requestee setter type_id)]])->to($to . 'flag_activity'); + [field => [qw(requestee setter type_id)]]) + ->to($to . 'flag_activity', value => ''); # flag activity with only query strings $r->get('/review/flag_activity')->to($to . 'flag_activity'); diff --git a/extensions/Review/t/bmo/review-api.t b/extensions/Review/t/bmo/review-api.t index 94b3512be0..f62d8488f4 100644 --- a/extensions/Review/t/bmo/review-api.t +++ b/extensions/Review/t/bmo/review-api.t @@ -139,6 +139,9 @@ SKIP: { } $t->get_ok('/rest/review/flag_activity/requestee/nobody-here@bmo.tld') ->json_is('/error' => 1, 'unknown requestee'); +$t->get_ok('/rest/review/flag_activity/requestee/') + ->status_is(200) + ->json_hasnt('/error', 'an empty requestee is ignored, not a 404'); $t->get_ok('/rest/review/flag_activity?bug_id=1&offset=5') ->json_is('/error' => 1, 'offset needs a limit'); From 904f10ff240a864551bbfc5470bc301e4c03f721 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 19 Nov 2026 14:54:34 +0100 Subject: [PATCH 14/19] Bug 2094122 - Fix - Read the EditComments comment ID from the path only --- extensions/EditComments/lib/API/V1/EditComments.pm | 2 +- extensions/EditComments/t/bmo/editcomments-api.t | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/extensions/EditComments/lib/API/V1/EditComments.pm b/extensions/EditComments/lib/API/V1/EditComments.pm index 38205e3b39..396e32533c 100644 --- a/extensions/EditComments/lib/API/V1/EditComments.pm +++ b/extensions/EditComments/lib/API/V1/EditComments.pm @@ -35,7 +35,7 @@ sub _params { my ($self, $lists) = @_; my ($params, $error) = merge_request_params($self, $lists); return (undef, $error) if $error; - if (defined(my $id = $self->param('comment_id'))) { + if (defined(my $id = $self->stash('comment_id'))) { $params->{comment_id} = $id; $params->{comment_ids} = [$id] if $lists; } diff --git a/extensions/EditComments/t/bmo/editcomments-api.t b/extensions/EditComments/t/bmo/editcomments-api.t index 85b0f2a73a..4fc1b57f47 100644 --- a/extensions/EditComments/t/bmo/editcomments-api.t +++ b/extensions/EditComments/t/bmo/editcomments-api.t @@ -74,6 +74,11 @@ $t->get_ok('/rest/editcomments/comment' => form => {comment_ids => [$c0, $c1]}) '/comments' => {$c0 => 'first comment', $c1 => 'second comment'}, 'several comments by query' ); +$t->get_ok('/rest/editcomments/comment' => form => + {comment_ids => [$c0, $c1], comment_id => $c1})->status_is(200)->json_is( + '/comments' => {$c0 => 'first comment', $c1 => 'second comment'}, + 'comment_id in the query string is ignored' + ); $t->get_ok('/rest/editcomments/comment') ->status_is(400) ->json_like('/message' => qr/comment_ids/, 'comment_ids is required'); From c2fc050823bc4b3a54a5124be74ef4fb0a9a6d5f Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 19 Nov 2026 15:06:33 +0100 Subject: [PATCH 15/19] Bug 2094122 - Test - Refuse PhabBugz bug_revisions on a bug the user cannot see --- extensions/PhabBugz/t/bmo/webservice.t | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/extensions/PhabBugz/t/bmo/webservice.t b/extensions/PhabBugz/t/bmo/webservice.t index 0ddb1ec21f..7ba3912702 100644 --- a/extensions/PhabBugz/t/bmo/webservice.t +++ b/extensions/PhabBugz/t/bmo/webservice.t @@ -309,6 +309,15 @@ foreach my $raw ($parent, $child, $moved) { $t->get_ok('/rest/phabbugz/bug_revisions/' . $bug->id) ->status_is(401, 'bug_revisions needs a login'); + + $conduit->reset_calls; + api_error( + $author, + 'bug_revisions/' . $private->id, + qr/not authorized to access/, + 'bug_revisions refuses a bug the user cannot see' + ); + is($conduit->call_count, 0, 'no Conduit calls for a hidden bug'); } # Everything is refused while the integration is disabled From 923ab5540792b70abeea5d5ebce8435952a6bc87 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 19 Nov 2026 15:17:49 +0100 Subject: [PATCH 16/19] Bug 2094122 - Test - Cover private comments and hidden bugs in the EditComments API --- .../EditComments/t/bmo/editcomments-api.t | 55 +++++++++++++++++-- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/extensions/EditComments/t/bmo/editcomments-api.t b/extensions/EditComments/t/bmo/editcomments-api.t index 4fc1b57f47..69ecbde6e9 100644 --- a/extensions/EditComments/t/bmo/editcomments-api.t +++ b/extensions/EditComments/t/bmo/editcomments-api.t @@ -23,6 +23,7 @@ use Bugzilla; BEGIN { Bugzilla->extensions } use Bugzilla::Constants; use Bugzilla::Bug; +use Bugzilla::Group; use Bugzilla::Product; use Bugzilla::User; use Bugzilla::Test::Util qw(issue_api_key); @@ -43,7 +44,7 @@ my $plain }); Bugzilla->set_user($admin); -my $bug = Bugzilla::Bug->create({ +my %fields = ( short_desc => "EditComments API test $$", product => $product->name, component => $product->components->[0]->name, @@ -52,11 +53,29 @@ my $bug = Bugzilla::Bug->create({ op_sys => 'Unspecified', rep_platform => 'Unspecified', version => $product->versions->[0]->name, - comment => 'first comment', -}); +); +my $bug = Bugzilla::Bug->create({%fields, comment => 'first comment'}); $bug->add_comment('second comment'); +$bug->add_comment('private comment'); $bug->update; -my ($c0, $c1) = map { $_->id } @{Bugzilla::Bug->new($bug->id)->comments}; +my ($c0, $c1, $c2) = map { $_->id } @{Bugzilla::Bug->new($bug->id)->comments}; + +# The admin isn't an insider here, so it can't add a private comment. +Bugzilla->dbh->do('UPDATE longdescs SET isprivate = 1 WHERE comment_id = ?', + undef, $c2); + +# A bug in a group the plain user isn't in. +my $group = Bugzilla::Group->create({ + name => "editcomments-api-$$", + description => 'EditComments API test', + isbuggroup => 1, +}); +my $secret = Bugzilla::Bug->create({%fields, comment => 'secret comment'}); +Bugzilla->dbh->do('INSERT INTO bug_group_map (bug_id, group_id) VALUES (?, ?)', + undef, $secret->id, $group->id); +my $s0 = $secret->comments->[0]->id; +my $sid = $secret->id; +Bugzilla->clear_request_cache; my %admin_auth = ('X-Bugzilla-API-Key' => issue_api_key($admin->login)->api_key); @@ -83,6 +102,24 @@ $t->get_ok('/rest/editcomments/comment') ->status_is(400) ->json_like('/message' => qr/comment_ids/, 'comment_ids is required'); +# GET: private comments and hidden bugs +$t->get_ok("/rest/editcomments/comment/$c2" => \%plain_auth)->json_like( + '/message' => qr/Comment id $c2 is private/, + 'a non-insider cannot read a private comment' +); +$t->get_ok("/rest/editcomments/comment/$c2")->json_like( + '/message' => qr/Comment id $c2 is private/, + 'a signed-out user cannot read a private comment' +); +$t->get_ok("/rest/editcomments/comment/$s0" => \%plain_auth)->json_like( + '/message' => qr/not authorized to access bug $sid/, + 'a non-member cannot read a comment on a hidden bug' +); +$t->get_ok("/rest/editcomments/comment/$s0")->json_like( + '/message' => qr/not authorized to access bug $sid/, + 'a signed-out user cannot read a comment on a hidden bug' +); + # PUT /comment/ my $comment_url = "/rest/editcomments/comment/$c1"; $t->put_ok($comment_url => json => {new_comment => 'x'}) @@ -91,6 +128,16 @@ $t->put_ok($comment_url => json => {new_comment => 'x'}) $t->put_ok($comment_url => \%plain_auth => json => {new_comment => 'x'}) ->status_isnt(200) ->json_like('/message' => qr/not authorized/, 'others cannot edit it'); +$t->put_ok("/rest/editcomments/comment/$c2" => \%admin_auth => json => + {new_comment => 'x'})->status_isnt(200)->json_like( + '/message' => qr/Comment id $c2 is private/, + 'a non-insider cannot edit a private comment' + ); +$t->put_ok("/rest/editcomments/comment/$s0" => \%plain_auth => json => + {new_comment => 'x'})->status_isnt(200)->json_like( + '/message' => qr/not authorized to access bug $sid/, + 'a non-member cannot edit a comment on a hidden bug' + ); $t->put_ok($comment_url => \%admin_auth => json => {new_comment => 'x', is_hidden => 'maybe'}) ->status_is(400) From e30e77ddc401d6249fabe6320c126ce340e06b4c Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 19 Nov 2026 15:37:10 +0100 Subject: [PATCH 17/19] Bug 2094122 - Test - Cover hidden bugs and non-taggers in the BugModal API --- extensions/BugModal/t/bmo/bug-modal-api.t | 39 +++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/extensions/BugModal/t/bmo/bug-modal-api.t b/extensions/BugModal/t/bmo/bug-modal-api.t index 62b589af36..95e70081c3 100644 --- a/extensions/BugModal/t/bmo/bug-modal-api.t +++ b/extensions/BugModal/t/bmo/bug-modal-api.t @@ -23,6 +23,7 @@ use Bugzilla; BEGIN { Bugzilla->extensions } use Bugzilla::Constants; use Bugzilla::Bug; +use Bugzilla::Group; use Bugzilla::Product; use Bugzilla::User; use Bugzilla::Test::Util qw(issue_api_key); @@ -40,7 +41,7 @@ my %auth = ('X-Bugzilla-API-Key' => issue_api_key($admin->login)->api_key); my $t = Test::Mojo->new('Bugzilla::App'); Bugzilla->set_user($admin); -my $bug = Bugzilla::Bug->create({ +my %fields = ( short_desc => "BugModal API test $$", product => $product->name, component => $product->components->[0]->name, @@ -51,7 +52,8 @@ my $bug = Bugzilla::Bug->create({ version => $product->versions->[0]->name, comment => 'a comment', cc => [$admin->login], -}); +); +my $bug = Bugzilla::Bug->create({%fields}); $bug->add_comment('a private comment'); $bug->update; my ($public, $private) @@ -61,6 +63,24 @@ my ($public, $private) Bugzilla->dbh->do('UPDATE longdescs SET isprivate = 1 WHERE comment_id = ?', undef, $private); my $bug_id = $bug->id; + +# A bug in a group the plain user below isn't in. +my $group = Bugzilla::Group->create({ + name => "bug-modal-api-$$", + description => 'BugModal API test', + isbuggroup => 1, +}); +my $secret_id = Bugzilla::Bug->create({%fields})->id; +Bugzilla->dbh->do('INSERT INTO bug_group_map (bug_id, group_id) VALUES (?, ?)', + undef, $secret_id, $group->id); + +# A user who isn't in the comment taggers group. +my $plain + = Bugzilla::User->create({ + login_name => "bug-modal-api-$$\@bmo.tld", cryptpassword => '*', + }); +my %plain_auth + = ('X-Bugzilla-API-Key' => issue_api_key($plain->login)->api_key); Bugzilla->clear_request_cache; # initial_field_values and product_info @@ -125,6 +145,12 @@ $t->put_ok("/rest/bug_modal/update_comment_tags/$public" => \%auth => json => {id => {condition => 'comment_id = ?', values => [$public]}}) ->json_is('/error' => 1) ->json_like('/message' => qr/numeric/, 'a hash id is refused'); +$t->put_ok( + "/rest/bug_modal/update_comment_tags/$public" => \%plain_auth => json => + {add => ['bmtag']})->json_is('/error' => 1)->json_like( + '/message' => qr/aren't a member of the '[^']+' group/, + 'only comment taggers can tag' + ); # new_product $t->get_ok( @@ -138,4 +164,13 @@ $t->get_ok( $t->get_ok("/rest/bug_modal/new_product/$bug_id" => \%auth) ->json_is('/error' => 1, 'product is required'); +# edit, cc and new_product on a bug the caller can't see +foreach my $path (qw(edit cc new_product)) { + $t->get_ok("/rest/bug_modal/$path/$secret_id" => \%plain_auth => form => + {product => $name})->json_is('/error' => 1)->json_like( + '/message' => qr/not authorized to access bug $secret_id/, + "$path refuses a hidden bug" + ); +} + done_testing; From 6d0c336e6c61501e676b76a43b260ab0f9872a2c Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 19 Nov 2026 15:50:19 +0100 Subject: [PATCH 18/19] Bug 2094122 - Test - Cover private comments and hidden bugs in MyDashboard run_last_changes --- .../MyDashboard/t/bmo/mydashboard-api.t | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/extensions/MyDashboard/t/bmo/mydashboard-api.t b/extensions/MyDashboard/t/bmo/mydashboard-api.t index 0ac2e9474b..7e38451b38 100644 --- a/extensions/MyDashboard/t/bmo/mydashboard-api.t +++ b/extensions/MyDashboard/t/bmo/mydashboard-api.t @@ -23,6 +23,7 @@ use Bugzilla; BEGIN { Bugzilla->extensions } use Bugzilla::Constants; use Bugzilla::Bug; +use Bugzilla::Group; use Bugzilla::Product; use Bugzilla::User; use Bugzilla::API::V1::Util qw(extract_flags); @@ -48,7 +49,7 @@ my $t = Test::Mojo->new('Bugzilla::App'); # A bug assigned to the user, with a needinfo request to them and a comment. Bugzilla->set_user($admin); -my $bug = Bugzilla::Bug->create({ +my %fields = ( short_desc => "MyDashboard API test $$", product => $product->name, component => $product->components->[0]->name, @@ -59,7 +60,8 @@ my $bug = Bugzilla::Bug->create({ version => $product->versions->[0]->name, assigned_to => $user->login, comment => 'first comment', -}); +); +my $bug = Bugzilla::Bug->create({%fields}); my $flag_set = eval { my ($flags, $new_flags) = extract_flags( @@ -75,6 +77,28 @@ my $later = datetime_from($since)->add(seconds => 2)->strftime('%Y-%m-%d %T'); $bug->add_comment('a newer comment'); $bug->update($later); +# Then a private comment, the only one newer than $later. The admin isn't an +# insider here, so it can't add a private comment. +my $latest = datetime_from($later)->add(seconds => 2)->strftime('%Y-%m-%d %T'); +my $fresh = Bugzilla::Bug->new($bug->id); +$fresh->add_comment('a private comment'); +$fresh->update($latest); +Bugzilla->dbh->do( + 'UPDATE longdescs SET isprivate = 1 WHERE bug_id = ? AND bug_when = ?', + undef, $bug->id, $latest); + +# A bug in a group the user isn't in, not assigned to them. +my $group = Bugzilla::Group->create({ + name => "mydashboard-api-$$", + description => 'MyDashboard API test', + isbuggroup => 1, +}); +my $secret_id + = Bugzilla::Bug->create({%fields, assigned_to => $admin->login})->id; +Bugzilla->dbh->do('INSERT INTO bug_group_map (bug_id, group_id) VALUES (?, ?)', + undef, $secret_id, $group->id); +Bugzilla->clear_request_cache; + # Login is required. for my $path ( qw(run_bug_query?query=assignedbugs run_flag_query?type=requestee @@ -130,6 +154,18 @@ $t->get_ok('/rest/mydashboard/run_last_changes' => \%auth => form => ->json_has('/results/0/last_changes/when'); $t->get_ok('/rest/mydashboard/run_last_changes?bug_id=999999999' => \%auth) ->json_is('/error' => 1, 'unknown bug'); +$t->get_ok('/rest/mydashboard/run_last_changes' => \%auth => form => + {bug_id => $bug->id, changeddate_api => $later}) + ->status_is(200) + ->json_hasnt('/results/0/last_changes/comment_html') + ->content_unlike(qr/a private comment/, 'private comment hidden'); +$t->get_ok('/rest/mydashboard/run_last_changes' => \%auth => form => + {bug_id => $secret_id, changeddate_api => $since}) + ->json_is('/error' => 1) + ->json_like( + '/message' => qr/not authorized to access bug $secret_id/, + 'a bug the user cannot see' + ); # bug_interest_unmark, with and without the prefix my $interest = 'Bugzilla::Extension::MyDashboard::BugInterest'; From ac8a508a5cb3eb85989786649e285918b8ff8ced Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 19 Nov 2026 16:02:31 +0100 Subject: [PATCH 19/19] Bug 2094122 - Test - Refuse InvalidBugHelper close for non-editbugs callers and mandatory groups --- .../t/bmo/invalid-bug-helper.t | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/extensions/InvalidBugHelper/t/bmo/invalid-bug-helper.t b/extensions/InvalidBugHelper/t/bmo/invalid-bug-helper.t index 14a29537d8..dd1b63ac35 100644 --- a/extensions/InvalidBugHelper/t/bmo/invalid-bug-helper.t +++ b/extensions/InvalidBugHelper/t/bmo/invalid-bug-helper.t @@ -65,7 +65,7 @@ sub make_reporter { } sub make_bug_as { - my ($reporter) = @_; + my ($reporter, %extra) = @_; Bugzilla->set_user($reporter); return Bugzilla::Bug->create({ short_desc => 'Invalid bug helper reporter test ' . $$, @@ -76,6 +76,7 @@ sub make_bug_as { op_sys => 'Unspecified', rep_platform => 'Unspecified', version => $product->versions->[0]->name, + %extra, }); } @@ -113,6 +114,61 @@ $t->post_ok($close_url . $bug_from_editbugs_reporter->id => \%auth) 'refuses a bug reported by an editbugs member (bug 1684509)' ); +# A caller without editbugs is refused. +my %plain_auth + = ('X-Bugzilla-API-Key' => issue_api_key($plain_reporter->login)->api_key); +$t->post_ok($close_url . $bug_from_plain_reporter->id => \%plain_auth) + ->status_isnt(200) + ->json_like( + '/message' => qr/aren't a member of the 'editbugs' group/, + 'refuses a caller who is not in editbugs' + ); + +# A bug in a group that is mandatory for its product is refused to an editbugs +# member outside that group, even when they can see the bug (as a CC). The admin +# can't be that caller: new groups make the admin group a member of them. +{ + # Drop the last request's controller, gone with its transaction. + Bugzilla->clear_request_cache; + Bugzilla->set_user($admin); + my $group = Bugzilla::Group->create({ + name => "invalid-bug-helper-$$", + description => 'InvalidBugHelper test', + isbuggroup => 1, + }); + my $secure = make_bug_as( + $plain_reporter, + cc => [$editbugs_reporter->login], + cclist_accessible => 1 + ); + $dbh->do('INSERT INTO bug_group_map (bug_id, group_id) VALUES (?, ?)', + undef, $secure->id, $group->id); + + # Mandatory only for this request, so other tests can still file bugs here. + $dbh->do( + 'INSERT INTO group_control_map + (group_id, product_id, entry, membercontrol, othercontrol, canedit) + VALUES (?, ?, 0, ?, ?, 0)', undef, $group->id, $product->id, + CONTROLMAPMANDATORY, CONTROLMAPMANDATORY + ); + Bugzilla->clear_request_cache; + + my %editbugs_auth + = ('X-Bugzilla-API-Key' => issue_api_key($editbugs_reporter->login)->api_key); + $t->post_ok($close_url . $secure->id => \%editbugs_auth) + ->status_isnt(200) + ->json_like( + '/message' => qr/aren't a member of the '\Q${\ $group->name }\E' group/, + 'refuses a bug in a mandatory group the caller is not in' + ); + is(Bugzilla::Bug->new($secure->id)->product, + $product->name, 'and leaves the bug in its product'); + + $dbh->do('DELETE FROM group_control_map WHERE group_id = ?', undef, $group->id); + Bugzilla->clear_request_cache; + Bugzilla->set_user($admin); +} + # The test data has no Invalid Bugs product, the one close_as_invalid moves # bugs to. It needs the version and milestone of the bug it receives. if (!Bugzilla::Product->new({name => 'Invalid Bugs'})) {