Skip to content

Commit 0a44b47

Browse files
committed
Bug 2091444 - TrackingFlags bug_check_can_change_field runs a database query for every custom field on show_bug.cgi, including fields that aren't tracking flags
1 parent 9aab6c2 commit 0a44b47

3 files changed

Lines changed: 170 additions & 0 deletions

File tree

‎extensions/TrackingFlags/Extension.pm‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,12 @@ sub bug_check_can_change_field {
589589
= @$args{qw(bug field old_value new_value priv_results)};
590590

591591
return if $field !~ /^cf_/ or $old_value eq $new_value;
592+
593+
# Most cf_ fields are not tracking flags. Skip those without a query each.
594+
my $tracking_names = Bugzilla->request_cache->{tracking_flag_name_set}
595+
//= {map { $_ => 1 } Bugzilla->tracking_flag_names};
596+
return unless $tracking_names->{$field};
597+
592598
return
593599
unless my $flag
594600
= Bugzilla::Extension::TrackingFlags::Flag->new({name => $field});

‎extensions/TrackingFlags/lib/Flag.pm‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,46 @@ sub new {
8585
return $cache->{'tracking_flags'}->{$param};
8686
}
8787

88+
# Lookups by name: reuse a flag already loaded in this request (get_all
89+
# loads all of them) instead of running a query for every lookup.
90+
if ( ref $param eq 'HASH'
91+
&& defined $param->{name}
92+
&& !exists $param->{condition}
93+
&& exists $cache->{'tracking_flags'})
94+
{
95+
my $flag = _cached_flag_by_name($cache, $param->{name});
96+
return $flag if $flag;
97+
}
98+
8899
return $class->SUPER::new($param);
89100
}
90101

102+
# Returns the flag named $name from the request cache, or undef if it is not
103+
# there. The cache can be partial, so callers must fall back to the database.
104+
sub _cached_flag_by_name {
105+
my ($cache, $name) = @_;
106+
my $flags = $cache->{'tracking_flags'};
107+
my $index = $cache->{'tracking_flags_by_name'};
108+
109+
# Rebuild the name index if the cache was replaced or flags were added or
110+
# removed since it was built.
111+
if (!$index || $index->{flags} != $flags || $index->{count} != keys %$flags) {
112+
$index = $cache->{'tracking_flags_by_name'} = {
113+
flags => $flags,
114+
count => scalar keys %$flags,
115+
by_name => {map { lc($_->name) => $_ } values %$flags},
116+
};
117+
}
118+
119+
my $flag = $index->{by_name}{lc $name} // return undef;
120+
121+
# update() and remove_from_db() change the cache in place, so only trust the
122+
# index if the flag is still cached under its id and still has this name.
123+
my $current = $flags->{$flag->flag_id} // return undef;
124+
return undef unless $current == $flag && lc($flag->name) eq lc($name);
125+
return $flag;
126+
}
127+
91128
sub new_from_hash {
92129
my $class = shift;
93130
my $cache = Bugzilla->request_cache->{'tracking_flags'} //= {};
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/usr/bin/env perl
2+
# This Source Code Form is subject to the terms of the Mozilla Public
3+
# License, v. 2.0. If a copy of the MPL was not distributed with this
4+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
#
6+
# This Source Code Form is "Incompatible With Secondary Licenses", as
7+
# defined by the Mozilla Public License, v. 2.0.
8+
use strict;
9+
use warnings;
10+
use 5.10.1;
11+
use lib qw( . lib local/lib/perl5 );
12+
13+
BEGIN { $ENV{LOG4PERL_CONFIG_FILE} = 'log4perl-t.conf' }
14+
15+
use Bugzilla;
16+
BEGIN { Bugzilla->extensions }
17+
18+
use Bugzilla::Constants;
19+
use Bugzilla::Extension::TrackingFlags::Flag;
20+
use Test2::V0;
21+
22+
# Tracking flag lookups by name should be served from the request cache when
23+
# the flag is already loaded, and fall back to the database otherwise.
24+
25+
my $FLAG = 'Bugzilla::Extension::TrackingFlags::Flag';
26+
my @db_lookups;
27+
my $mock_object = mock 'Bugzilla::Object' => (
28+
override => [
29+
new => sub {
30+
my ($class, $param) = @_;
31+
push @db_lookups, $param;
32+
return undef;
33+
}
34+
]
35+
);
36+
37+
sub flag { bless {id => $_[0], name => $_[1]}, $FLAG }
38+
39+
sub new_request {
40+
my (%flags) = @_;
41+
Bugzilla->clear_request_cache;
42+
Bugzilla->request_cache->{tracking_flags}
43+
= {map { $_->flag_id => $_ } values %flags}
44+
if %flags;
45+
@db_lookups = ();
46+
}
47+
48+
subtest 'cached flag found by name without a query' => sub {
49+
my $a = flag(1, 'cf_status_firefox140');
50+
new_request(a => $a);
51+
is($FLAG->new({name => 'cf_status_firefox140'}), exact_ref($a),
52+
'cached object');
53+
is($FLAG->new({name => 'CF_STATUS_FIREFOX140'}),
54+
exact_ref($a), 'case-insensitive, like the database lookup');
55+
is(scalar @db_lookups, 0, 'no database lookups');
56+
};
57+
58+
subtest 'unknown name falls back to the database' => sub {
59+
new_request(a => flag(1, 'cf_status_firefox140'));
60+
is($FLAG->new({name => 'cf_rank'}), undef, 'not a tracking flag');
61+
is(\@db_lookups, [{name => 'cf_rank'}], 'one database lookup');
62+
};
63+
64+
subtest 'no request cache: database lookup' => sub {
65+
new_request();
66+
$FLAG->new({name => 'cf_status_firefox140'});
67+
is(scalar @db_lookups, 1, 'database lookup');
68+
};
69+
70+
subtest 'index follows changes to the cache' => sub {
71+
my $a = flag(1, 'cf_status_firefox140');
72+
new_request(a => $a);
73+
$FLAG->new({name => 'cf_status_firefox140'}); # builds the index
74+
75+
# A flag added to the cache later (e.g. by update()) is found.
76+
my $b = flag(2, 'cf_status_firefox141');
77+
Bugzilla->request_cache->{tracking_flags}{2} = $b;
78+
is($FLAG->new({name => 'cf_status_firefox141'}), exact_ref($b), 'new flag');
79+
80+
# A renamed flag is not returned under its old name.
81+
$a->{name} = 'cf_status_firefox_renamed';
82+
is($FLAG->new({name => 'cf_status_firefox140'}), undef, 'old name');
83+
is(scalar @db_lookups, 1, 'old name went to the database');
84+
85+
# A flag removed from the cache (remove_from_db()) is not returned.
86+
delete Bugzilla->request_cache->{tracking_flags}{2};
87+
@db_lookups = ();
88+
is($FLAG->new({name => 'cf_status_firefox141'}), undef, 'removed flag');
89+
is(scalar @db_lookups, 1, 'removed flag went to the database');
90+
};
91+
92+
subtest 'id lookups are unchanged' => sub {
93+
my $a = flag(1, 'cf_status_firefox140');
94+
new_request(a => $a);
95+
is($FLAG->new(1), exact_ref($a), 'cached by id');
96+
is(scalar @db_lookups, 0, 'no database lookups');
97+
};
98+
99+
subtest 'bug_check_can_change_field skips non-tracking fields' => sub {
100+
my $a = flag(1, 'cf_status_firefox140');
101+
new_request(a => $a);
102+
my $mock_names = mock 'Bugzilla' =>
103+
(override => [tracking_flag_names => sub {'cf_status_firefox140'}]);
104+
my $mock_flag = mock $FLAG => (override => [can_set_value => sub {1}]);
105+
106+
my @results;
107+
my $check = sub {
108+
my ($field) = @_;
109+
Bugzilla::Extension::TrackingFlags->bug_check_can_change_field({
110+
bug => undef,
111+
field => $field,
112+
old_value => '---',
113+
new_value => 'fixed',
114+
priv_results => \@results,
115+
});
116+
};
117+
118+
$check->('cf_rank');
119+
is(\@results, [], 'non-tracking field: no result');
120+
is(\@db_lookups, [], 'non-tracking field: no database lookup');
121+
122+
$check->('cf_status_firefox140');
123+
is(\@results, [{result => PRIVILEGES_REQUIRED_NONE}], 'tracking flag checked');
124+
is(\@db_lookups, [], 'tracking flag served from the request cache');
125+
};
126+
127+
done_testing;

0 commit comments

Comments
 (0)