From 498ab61f4d4ccbdad7d531ed1063319337f1fc7d Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 2 Jul 2026 13:54:42 +0200 Subject: [PATCH 01/50] Bug 2085398 - Add backfill script to populate flag_activity from bugs_activity --- scripts/flag_activity_backfill.pl | 217 ++++++++++++++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 scripts/flag_activity_backfill.pl diff --git a/scripts/flag_activity_backfill.pl b/scripts/flag_activity_backfill.pl new file mode 100644 index 0000000000..e4b4c5c50e --- /dev/null +++ b/scripts/flag_activity_backfill.pl @@ -0,0 +1,217 @@ +#!/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. + +# Bug 2085398 - Backfill flag_activity from bugs_activity. +# +# Parses historical flagtypes.name rows in bugs_activity and inserts the +# missing records into flag_activity. Safe to run while dual-write is active; +# skips entries already present in flag_activity. +# +# Backfilled rows get flag_id 0: the flag can't be recovered reliably from +# bugs_activity, and bug history and bugmail skip the previous-state lookup +# for flag_id 0. +# +# Works through bugs in bug_id batches, one transaction per batch. +# +# Usage: perl scripts/flag_activity_backfill.pl [--dry-run] + +use 5.10.1; +use strict; +use warnings; + +use File::Basename qw(dirname); +use File::Spec::Functions qw(catdir rel2abs); + +BEGIN { + require lib; + my $dir = rel2abs(catdir(dirname(__FILE__), '..')); + lib->import($dir, catdir($dir, 'lib'), catdir($dir, qw(local lib perl5))); +} + +use Bugzilla; +use Bugzilla::Constants; + +use constant BATCH_SIZE => 1000; + +Bugzilla->usage_mode(USAGE_MODE_CMDLINE); + +my $dry_run = grep { $_ eq '--dry-run' } @ARGV; +print "DRY RUN — no data will be written.\n" if $dry_run; + +my $dbh = Bugzilla->dbh; + +# Get the fieldid for 'flagtypes.name' in bugs_activity. +my ($fieldid) + = $dbh->selectrow_array( + "SELECT id FROM fielddefs WHERE name = 'flagtypes.name'"); +die "Could not find fieldid for 'flagtypes.name' in fielddefs\n" + unless $fieldid; + +# Build a cache of flag type name -> id. +my %type_id_cache; +my $types + = $dbh->selectall_arrayref("SELECT id, name FROM flagtypes", {Slice => {}}); +$type_id_cache{$_->{name}} = $_->{id} for @$types; + +# login_name -> userid, filled as requestees are seen. +my %user_id_cache; + +sub requestee_id { + my ($email) = @_; + return undef unless $email; + $user_id_cache{$email} + //= $dbh->selectrow_array("SELECT userid FROM profiles WHERE login_name = ?", + undef, $email) // 0; + return $user_id_cache{$email} || undef; +} + +# Parse a single flag string like "review?(user@example.com)" or "review+". +# Returns (type_name, status, requestee_email_or_undef) or empty list. +sub parse_flag_str { + my ($str) = @_; + $str =~ s/^\s+|\s+$//g; + return unless $str =~ /^(.+?)([?+\-])(?:\(([^)]+)\))?$/; + return ($1, $2, $3); +} + +# Duplicate keys. The full key includes the requestee, so asking two people +# for the same flag in one change backfills both rows. When the requestee +# can't be resolved (account renamed or deleted since), fall back to the key +# without it so dual-written rows are not inserted twice. +sub short_key { + my ($bug_id, $when, $type_id, $status, $attach_id) = @_; + return join(':', $bug_id, $when, $type_id, $status, $attach_id // ''); +} + +sub full_key { + my ($requestee_id, @short) = @_; + return join(':', short_key(@short), $requestee_id // ''); +} + +my ($max_bug_id) + = $dbh->selectrow_array( + "SELECT MAX(bug_id) FROM bugs_activity WHERE fieldid = ?", + undef, $fieldid); +$max_bug_id //= 0; + +my ($total, $inserted, $skipped, $unknown_type, $unknown_user) + = (0, 0, 0, 0, 0); + +# Flag states to record for one bugs_activity row: +# - Each item in 'added' records its new state directly. +# - Items in 'removed' with no counterpart type in 'added' were deleted. +sub flag_changes { + my ($row) = @_; + my (@changes, %added_types); + + for my $item (split /,\s*/, $row->{added} // '') { + my ($name, $status, $requestee_email) = parse_flag_str($item) or next; + $added_types{$name} = 1; + push @changes, + {name => $name, status => $status, requestee_email => $requestee_email}; + } + + for my $item (split /,\s*/, $row->{removed} // '') { + my ($name, $status, $requestee_email) = parse_flag_str($item) or next; + + # Only record 'X' if this type does not appear in added (truly deleted). + next if $added_types{$name}; + push @changes, + {name => $name, status => 'X', requestee_email => $requestee_email}; + } + + return @changes; +} + +sub backfill_batch { + my ($from, $to) = @_; + + my (%existing, %existing_short); + my $existing_rows = $dbh->selectall_arrayref( + "SELECT bug_id, flag_when, type_id, status, attachment_id, requestee_id + FROM flag_activity + WHERE bug_id BETWEEN ? AND ?", undef, $from, $to + ); + for my $row (@$existing_rows) { + my @short = @$row[0 .. 4]; + $existing{full_key($row->[5], @short)} = 1; + $existing_short{short_key(@short)} = 1; + } + + my $rows = $dbh->selectall_arrayref( + "SELECT bug_id, who, bug_when, removed, added, attach_id + FROM bugs_activity + WHERE fieldid = ? AND bug_id BETWEEN ? AND ? + ORDER BY bug_when, id", {Slice => {}}, $fieldid, $from, $to + ); + return unless @$rows; + + $dbh->bz_start_transaction() unless $dry_run; + + for my $row (@$rows) { + $total++; + + for my $change (flag_changes($row)) { + my $type_id = $type_id_cache{$change->{name}}; + unless ($type_id) { + $unknown_type++; + next; + } + + my $requestee_id = requestee_id($change->{requestee_email}); + $unknown_user++ if $change->{requestee_email} && !$requestee_id; + + my @short = ( + $row->{bug_id}, $row->{bug_when}, $type_id, + $change->{status}, $row->{attach_id} + ); + my $key = full_key($requestee_id, @short); + + # Skip if already in flag_activity. + if ($existing{$key} || (!$requestee_id && $existing_short{short_key(@short)})) { + $skipped++; + next; + } + + unless ($dry_run) { + $dbh->do( + "INSERT INTO flag_activity + (flag_when, type_id, flag_id, setter_id, requestee_id, + bug_id, attachment_id, status) + VALUES (?, ?, 0, ?, ?, ?, ?, ?)", undef, $row->{bug_when}, $type_id, + $row->{who}, $requestee_id, $row->{bug_id}, $row->{attach_id}, $change->{status} + ); + } + + $existing{$key} = 1; + $existing_short{short_key(@short)} = 1; + $inserted++; + } + } + + $dbh->bz_commit_transaction() unless $dry_run; + + printf + " Bugs %d-%d done, %d bugs_activity rows processed (inserted: %d, skipped: %d)\n", + $from, $to, $total, $inserted, $skipped; +} + +print "Processing bugs_activity rows...\n"; + +my $from = 1; +while ($from <= $max_bug_id) { + backfill_batch($from, $from + BATCH_SIZE - 1); + $from += BATCH_SIZE; +} + +print "\nDone.\n"; +printf " bugs_activity rows processed : %d\n", $total; +printf " flag_activity rows inserted : %d\n", $inserted; +printf " already present (skipped) : %d\n", $skipped; +printf " unknown flag type (skipped) : %d\n", $unknown_type; +printf " unknown requestee (NULL inserted): %d\n", $unknown_user; From 67005a981aec734012bea27e4db298c1678c247d Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 2 Jul 2026 13:57:16 +0200 Subject: [PATCH 02/50] Bug 2085398 - Stop writing flag changes to bugs_activity --- Bugzilla/Attachment.pm | 3 ++- Bugzilla/Bug.pm | 1 + editusers.cgi | 19 +++++++++++++------ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Bugzilla/Attachment.pm b/Bugzilla/Attachment.pm index f3874733ec..1896b6ef7d 100644 --- a/Bugzilla/Attachment.pm +++ b/Bugzilla/Attachment.pm @@ -855,8 +855,9 @@ sub update { # Record changes in the activity table. require Bugzilla::Bug; foreach my $field (keys %$changes) { + next if $field eq 'flagtypes.name'; # now tracked in flag_activity my $change = $changes->{$field}; - $field = "attachments.$field" unless $field eq "flagtypes.name"; + $field = "attachments.$field"; Bugzilla::Bug::LogActivityEntry($self->bug_id, $field, $change->[0], $change->[1], $user->id, $timestamp, undef, $self->id); } diff --git a/Bugzilla/Bug.pm b/Bugzilla/Bug.pm index 062cb43d45..f12d1c6fac 100644 --- a/Bugzilla/Bug.pm +++ b/Bugzilla/Bug.pm @@ -1221,6 +1221,7 @@ sub update { # XXX Eventually, when bugs_activity is able to track the dupe_id, # this code should go below the duplicates-table-updating code below. foreach my $field (keys %$changes) { + next if $field eq 'flagtypes.name'; # now tracked in flag_activity my $change = $changes->{$field}; my $from = defined $change->[0] ? $change->[0] : ''; my $to = defined $change->[1] ? $change->[1] : ''; diff --git a/editusers.cgi b/editusers.cgi index 9446ff2922..21ad3be5f0 100755 --- a/editusers.cgi +++ b/editusers.cgi @@ -20,6 +20,7 @@ use Bugzilla::User; use Bugzilla::Bug; use Bugzilla::BugMail; use Bugzilla::Flag; +use Bugzilla::FlagActivity; use Bugzilla::Field; use Bugzilla::Group; use Bugzilla::Token; @@ -521,13 +522,19 @@ elsif ($action eq 'delete') { push(@{$bugs{$_->bug_id}->{$_->attach_id || 0}}, $_) foreach @$flags; foreach my $bug_id (keys %bugs) { foreach my $attach_id (keys %{$bugs{$bug_id}}) { - my @old_summaries = Bugzilla::Flag->snapshot($bugs{$bug_id}->{$attach_id}); $_->_set_requestee() foreach @{$bugs{$bug_id}->{$attach_id}}; - my @new_summaries = Bugzilla::Flag->snapshot($bugs{$bug_id}->{$attach_id}); - my ($removed, $added) - = Bugzilla::Flag->update_activity(\@old_summaries, \@new_summaries); - LogActivityEntry($bug_id, 'flagtypes.name', $removed, $added, $userid, - $timestamp, undef, $attach_id); + foreach my $flag (@{$bugs{$bug_id}->{$attach_id}}) { + Bugzilla::FlagActivity->create({ + flag_when => $timestamp, + setter_id => $userid, + status => $flag->status, + type_id => $flag->type_id, + flag_id => $flag->id, + requestee_id => $flag->requestee_id, + bug_id => $bug_id, + attachment_id => $attach_id || undef, + }); + } } $sth_set_bug_timestamp->execute($timestamp, $bug_id); $updatedbugs{$bug_id} = 1; From d48a83d8aabcac74565bd819d5a9323208b37985 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 2 Jul 2026 14:23:49 +0200 Subject: [PATCH 03/50] Bug 2085398 - GetBugActivity reads flag changes from flag_activity --- Bugzilla/Bug.pm | 55 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/Bugzilla/Bug.pm b/Bugzilla/Bug.pm index f12d1c6fac..7605c21866 100644 --- a/Bugzilla/Bug.pm +++ b/Bugzilla/Bug.pm @@ -4855,6 +4855,18 @@ sub GetBugActivity { $suppwhere = "AND COALESCE(attachments.isprivate, 0) = 0"; } + my $flag_suppjoins = ""; + my $flag_suppwhere = ""; + if (!$user->is_insider) { + $flag_suppjoins = "LEFT JOIN attachments fa_att ON fa_att.attach_id = fsa.attachment_id"; + $flag_suppwhere = "AND COALESCE(fa_att.isprivate, 0) = 0"; + } + + my $flag_attachpart = ""; + if ($attach_id) { + $flag_attachpart = "AND fsa.attachment_id = ?"; + } + my $bug_when = $dbh->sql_date_format('bugs_activity.bug_when', '%Y-%m-%d %H:%i:%s'); @@ -4881,7 +4893,8 @@ sub GetBugActivity { ON profiles.userid = bugs_activity.who WHERE bugs_activity.bug_id = ? $attachpart - $suppwhere "; + $suppwhere + AND (fielddefs.name IS NULL OR fielddefs.name != 'flagtypes.name') "; if ( Bugzilla->params->{'comment_taggers_group'} && $include_comment_activity @@ -4917,6 +4930,46 @@ sub GetBugActivity { push @args, $bug_id; } + my $flag_bug_when = $dbh->sql_date_format('fsa.flag_when', '%Y-%m-%d %H:%i:%s'); + $query .= " + UNION ALL + SELECT 'flagtypes.name' AS field_name, + NULL AS activity_id, + fsa.attachment_id AS attach_id, + $flag_bug_when AS bug_when, + CASE WHEN prev.id IS NULL THEN '' + WHEN prev.status = '?' AND preq.login_name IS NOT NULL + THEN CONCAT(ft.name, '?(', preq.login_name, ')') + ELSE CONCAT(ft.name, prev.status) + END AS removed, + CASE WHEN fsa.status = 'X' THEN '' + WHEN fsa.status = '?' AND req.login_name IS NOT NULL + THEN CONCAT(ft.name, '?(', req.login_name, ')') + ELSE CONCAT(ft.name, fsa.status) + END AS added, + setter.login_name AS who, + NULL AS comment_id + FROM flag_activity fsa + JOIN flagtypes ft ON ft.id = fsa.type_id + JOIN profiles setter ON setter.userid = fsa.setter_id + LEFT JOIN profiles req ON req.userid = fsa.requestee_id + LEFT JOIN flag_activity prev + ON fsa.flag_id != 0 + AND prev.flag_id = fsa.flag_id + AND prev.flag_when = ( + SELECT MAX(p2.flag_when) + FROM flag_activity p2 + WHERE p2.flag_id = fsa.flag_id + AND p2.flag_when < fsa.flag_when) + LEFT JOIN profiles preq ON preq.userid = prev.requestee_id + $flag_suppjoins + WHERE fsa.bug_id = ? + $flag_attachpart + $flag_suppwhere + "; + push @args, $bug_id; + push @args, $attach_id if $attach_id; + $query .= ") AS OUTER_TABLE"; # Only consider changes since $starttime, if given. From 689debf9d2de496d2e391464dd008ed7bba0010b Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 2 Jul 2026 14:28:21 +0200 Subject: [PATCH 04/50] Bug 2085398 - BugMail reads flag changes from flag_activity --- Bugzilla/BugMail.pm | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/Bugzilla/BugMail.pm b/Bugzilla/BugMail.pm index a8cd371129..51813c078f 100644 --- a/Bugzilla/BugMail.pm +++ b/Bugzilla/BugMail.pm @@ -708,9 +708,11 @@ sub _get_diffs { my @args = ($bug->id); # If lastdiffed is NULL, then we don't limit the search on time. - my $when_restriction = ''; + my $when_restriction = ''; + my $flag_when_restriction = ''; if ($bug->lastdiffed) { - $when_restriction = ' AND bug_when > ? AND bug_when <= ?'; + $when_restriction = ' AND bug_when > ? AND bug_when <= ?'; + $flag_when_restriction = ' AND fsa.flag_when > ? AND fsa.flag_when <= ?'; push @args, ($bug->lastdiffed, $end); } @@ -725,8 +727,40 @@ sub _get_diffs { ON fielddefs.id = bugs_activity.fieldid WHERE bugs_activity.bug_id = ? $when_restriction - ORDER BY bugs_activity.bug_when, fielddefs.description", {Slice => {}}, - @args + AND fielddefs.name != 'flagtypes.name' + UNION ALL + SELECT 'flagtypes.name' AS field_name, + (SELECT description FROM fielddefs WHERE name = 'flagtypes.name') AS field_desc, + fsa.flag_when AS bug_when, + CASE WHEN prev.id IS NULL THEN '' + WHEN prev.status = '?' AND preq.login_name IS NOT NULL + THEN CONCAT(ft.name, '?(', preq.login_name, ')') + ELSE CONCAT(ft.name, prev.status) + END AS old, + CASE WHEN fsa.status = 'X' THEN '' + WHEN fsa.status = '?' AND req.login_name IS NOT NULL + THEN CONCAT(ft.name, '?(', req.login_name, ')') + ELSE CONCAT(ft.name, fsa.status) + END AS new, + fsa.attachment_id AS attach_id, + NULL AS comment_id, + fsa.setter_id AS who + FROM flag_activity fsa + JOIN flagtypes ft ON ft.id = fsa.type_id + LEFT JOIN profiles req ON req.userid = fsa.requestee_id + LEFT JOIN flag_activity prev + ON fsa.flag_id != 0 + AND prev.flag_id = fsa.flag_id + AND prev.flag_when = ( + SELECT MAX(p2.flag_when) + FROM flag_activity p2 + WHERE p2.flag_id = fsa.flag_id + AND p2.flag_when < fsa.flag_when) + LEFT JOIN profiles preq ON preq.userid = prev.requestee_id + WHERE fsa.bug_id = ? + $flag_when_restriction + ORDER BY bug_when, field_desc", {Slice => {}}, + @args, $bug->id, ($bug->lastdiffed ? ($bug->lastdiffed, $end) : ()) ); my $referenced_bugs = []; From 471b376b06ffe6c486aa13b9bdca53c7fde371ce Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 2 Jul 2026 14:32:42 +0200 Subject: [PATCH 05/50] Bug 2085398 - ActivityStream uses flag_activity flag_id for lookup --- extensions/BugModal/lib/ActivityStream.pm | 41 ++++++++++++----------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/extensions/BugModal/lib/ActivityStream.pm b/extensions/BugModal/lib/ActivityStream.pm index 5745511763..c71715c51f 100644 --- a/extensions/BugModal/lib/ActivityStream.pm +++ b/extensions/BugModal/lib/ActivityStream.pm @@ -96,35 +96,36 @@ sub find_activity_id_for_attachment { sub find_activity_id_for_flag { my ($self, $flag) = @_; - my $flagtype_name = $flag->type->name; - my $date = $flag->modification_date; - my $setter_id = $flag->setter->id; - my $stream = $self->activity_stream; + my $flag_id = $flag->id; + my $setter_id = $flag->setter->id; + my $stream = $self->activity_stream; - # unfortunately bugs_activity treats all flag changes as the same field, so - # we don't have an object_id to match on + if (!exists $self->{activity_cache}->{flag}->{$flag_id}) { + # flag_activity records each change against the flag's object id, so we + # can look up the exact timestamp without relying on flag-type name matching. + my ($flag_when) = Bugzilla->dbh->selectrow_array( + "SELECT flag_when FROM flag_activity WHERE flag_id = ? + ORDER BY flag_when DESC LIMIT 1", + undef, $flag_id + ); - if (!exists $self->{activity_cache}->{flag}->{$flag->id}) { - foreach my $change_set (reverse @$stream) { + SEARCH: foreach my $change_set (reverse @$stream) { foreach my $activity (@{$change_set->{activity}}) { - - # match by user, timestamp, and flag-type name - next unless $activity->{who}->id == $setter_id && $activity->{when} eq $date; + next unless $flag_when + && $activity->{who}->id == $setter_id + && $activity->{when} eq $flag_when; foreach my $change (@{$activity->{changes}}) { - next - unless $change->{fieldname} eq 'flagtypes.name' - && $change->{flagtype_name} eq $flagtype_name; - $self->{activity_cache}->{flag}->{$flag->id} = $change_set->{id}; - return $change_set->{id}; + next unless $change->{fieldname} eq 'flagtypes.name'; + $self->{activity_cache}->{flag}->{$flag_id} = $change_set->{id}; + last SEARCH; } } } - # if we couldn't find the flag in bugs_activity it means it was set - # during bug creation - $self->{activity_cache}->{flag}->{$flag->id} = 'c0'; + # flag set during bug creation, or no flag_activity entry yet + $self->{activity_cache}->{flag}->{$flag_id} //= 'c0'; } - return $self->{activity_cache}->{flag}->{$flag->id}; + return $self->{activity_cache}->{flag}->{$flag_id}; } # comments are processed first, so there's no need to merge into existing entries From 8d5afe38a7cc00e55d7e4ba0032504b90c2a362a Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Fri, 23 Oct 2026 19:04:26 +0200 Subject: [PATCH 06/50] Bug 2085398 - Add tests for the flag_activity backfill script --- t/flag-activity-backfill.t | 131 +++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 t/flag-activity-backfill.t diff --git a/t/flag-activity-backfill.t b/t/flag-activity-backfill.t new file mode 100644 index 0000000000..6d94115fb3 --- /dev/null +++ b/t/flag-activity-backfill.t @@ -0,0 +1,131 @@ +#!/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 Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo.test'); +use Bugzilla::Test::MockDB; +use Bugzilla::Test::Util qw(create_bug create_user); +use Bugzilla::User; +use Capture::Tiny qw(capture_stdout); +use Test2::V0; + +# scripts/flag_activity_backfill.pl copies the flag changes recorded in +# bugs_activity into flag_activity, skipping the ones already there. + +# super_user() resolves 'automation@bmo.tld', which the mock DB does not ship. +create_user('automation@bmo.tld', '*'); +Bugzilla->set_user(Bugzilla::User->super_user); +my $dbh = Bugzilla->dbh; + +my $setter = create_user('setter@bmo.test', '*'); +my $alice = create_user('alice@bmo.test', '*'); +my $bob = create_user('bob@bmo.test', '*'); + +my $bug_id = create_bug( + short_desc => 'Backfill test bug', + comment => 'Bug for the flag_activity backfill tests', + bug_type => 'defect', + assigned_to => 'nobody@mozilla.org', +)->id; + +my ($fieldid) + = $dbh->selectrow_array( + "SELECT id FROM fielddefs WHERE name = 'flagtypes.name'"); +my ($review_id) + = $dbh->selectrow_array("SELECT id FROM flagtypes WHERE name = 'review'"); + +sub add_activity { + my ($when, $removed, $added) = @_; + $dbh->do( + 'INSERT INTO bugs_activity (bug_id, who, bug_when, fieldid, removed, added) + VALUES (?, ?, ?, ?, ?, ?)', undef, $bug_id, $setter->id, $when, $fieldid, + $removed, $added + ); +} + +# One change asks two people for review. +add_activity('2020-01-01 10:00:00', + '', 'review?(alice@bmo.test), review?(bob@bmo.test)'); + +# A dual-written change, already in flag_activity with its real flag_id. +add_activity('2020-01-02 10:00:00', 'review?(alice@bmo.test)', 'review+'); +$dbh->do( + 'INSERT INTO flag_activity + (flag_when, type_id, flag_id, setter_id, requestee_id, bug_id, status) + VALUES (?, ?, 42, ?, NULL, ?, ?)', undef, '2020-01-02 10:00:00', $review_id, + $setter->id, $bug_id, '+' +); + +# A flag cleared, asked from an account that no longer exists. +add_activity('2020-01-03 10:00:00', 'review?(gone@bmo.test)', ''); + +sub run_backfill { + local @ARGV = (); + local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /redefined/ }; + my $output = capture_stdout { do './scripts/flag_activity_backfill.pl' }; + die $@ if $@; + return $output; +} + +sub rows { + return $dbh->selectall_arrayref( + 'SELECT flag_when, flag_id, requestee_id, status + FROM flag_activity ORDER BY flag_when, requestee_id', {Slice => {}} + ); +} + +my $output = run_backfill(); +like( + $output, + qr/unknown requestee \(NULL inserted\): 1/, + 'the deleted requestee is counted' +); + +is( + rows(), + [ + { + flag_when => '2020-01-01 10:00:00', + flag_id => 0, + requestee_id => $alice->id, + status => '?' + }, + { + flag_when => '2020-01-01 10:00:00', + flag_id => 0, + requestee_id => $bob->id, + status => '?' + }, + { + flag_when => '2020-01-02 10:00:00', + flag_id => 42, + requestee_id => undef, + status => '+' + }, + { + flag_when => '2020-01-03 10:00:00', + flag_id => 0, + requestee_id => undef, + status => 'X' + }, + ], + 'both requestees backfilled, the dual-written row kept once, flag_id 0' +); + +run_backfill(); +is(scalar @{rows()}, 4, 'a second run inserts nothing'); + +done_testing; From 347c44a76ae906b1eb5b680333b67976feb3d9a5 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Tue, 27 Oct 2026 14:42:20 +0100 Subject: [PATCH 07/50] Bug 2085398 - Search, ReleaseTracking and UserActivity read flag changes from flag_activity --- Bugzilla/Search.pm | 79 +++++++++++++++- extensions/BMO/lib/Reports/ReleaseTracking.pm | 39 ++++---- extensions/BMO/lib/Reports/UserActivity.pm | 57 ++++++++++- t/flag-activity-search.t | 94 +++++++++++++++++++ 4 files changed, 249 insertions(+), 20 deletions(-) create mode 100644 t/flag-activity-search.t diff --git a/Bugzilla/Search.pm b/Bugzilla/Search.pm index 5247e51f06..2ca3c33829 100644 --- a/Bugzilla/Search.pm +++ b/Bugzilla/Search.pm @@ -285,7 +285,15 @@ use constant OPERATOR_FIELD_OVERRIDE => { dependson => MULTI_SELECT_OVERRIDE, duplicates => MULTI_SELECT_OVERRIDE, keywords => MULTI_SELECT_OVERRIDE, - 'flagtypes.name' => {_non_changed => \&_flagtypes_nonchanged,}, + 'flagtypes.name' => { + _non_changed => \&_flagtypes_nonchanged, + changedbefore => \&_flagtypes_changed, + changedafter => \&_flagtypes_changed, + changedfrom => \&_flagtypes_changed, + changedto => \&_flagtypes_changed, + changedby => \&_flagtypes_changed, + everchanged => \&_flagtypes_changed, + }, longdesc => { changedby => \&_long_desc_changedby, everchanged => \&_long_desc_everchanged, @@ -3745,6 +3753,75 @@ sub _changed_security_check { } } +# Flag changes are recorded in flag_activity. Older changes are only in +# bugs_activity, so match either. flag_id 0 rows are backfilled copies of +# bugs_activity rows and are skipped. +sub _flagtypes_changed { + my ($self, $args) = @_; + my $dbh = Bugzilla->dbh; + $self->_convert_everchanged($args) if $args->{operator} eq 'everchanged'; + my ($chart_id, $operator, $value, $bugs_table) + = @$args{qw(chart_id operator value bugs_table)}; + + $self->_do_operator_function($args); + my $legacy_term = $args->{term}; + + my $fa = "flag_act_$chart_id"; + my @where = ("$fa.bug_id = $bugs_table.bug_id", "$fa.flag_id != 0"); + + if ($operator =~ /^changed(before|after)$/) { + my $sql_operator = $1 eq 'before' ? '<=' : '>='; + my $sql_date = $dbh->quote(SqlifyDate($value)); + push @where, "$fa.flag_when $sql_operator $sql_date"; + } + elsif ($operator eq 'changedby') { + my $user_id = $value =~ /^\d+$/ ? $value : $self->_get_user_id($value); + push @where, "$fa.setter_id = $user_id"; + } + elsif ($operator eq 'changedto') { + my $match = _flag_state_match($fa, $value) or return; + push @where, $match; + } + else { # changedfrom: the state before this change + my $prev = "flag_prev_$chart_id"; + my $match = _flag_state_match($prev, $value) or return; + push @where, "EXISTS ( + SELECT 1 FROM flag_activity $prev + WHERE $prev.id = (SELECT MAX(p2.id) FROM flag_activity p2 + WHERE p2.flag_id = $fa.flag_id AND p2.id < $fa.id) + AND $match)"; + } + + if (!$self->_user->is_insider) { + push @where, "COALESCE((SELECT isprivate FROM attachments + WHERE attach_id = $fa.attachment_id), 0) = 0"; + } + + $args->{term} = "(($legacy_term) OR EXISTS (SELECT 1 FROM flag_activity $fa + WHERE " . join(' AND ', @where) . '))'; +} + +# SQL matching a flag_activity row against a history value like +# "review+" or "review?(user@example.com)". Returns nothing if the value +# isn't a flag state. +sub _flag_state_match { + my ($table, $value) = @_; + my $dbh = Bugzilla->dbh; + my ($name, $status, $requestee) + = $value =~ /^\s*(.+?)([?+\-])(?:\(([^)]+)\))?\s*$/ + or return; + + my $sql = "$table.type_id IN (SELECT id FROM flagtypes WHERE name = " + . $dbh->quote($name) . ") AND $table.status = " . $dbh->quote($status); + if ($status eq '?') { + $sql .= $requestee + ? " AND $table.requestee_id = (SELECT userid FROM profiles" + . ' WHERE login_name = ' . $dbh->quote($requestee) . ')' + : " AND $table.requestee_id IS NULL"; + } + return $sql; +} + sub _isempty { my ($self, $args) = @_; my $full_field = $args->{full_field}; diff --git a/extensions/BMO/lib/Reports/ReleaseTracking.pm b/extensions/BMO/lib/Reports/ReleaseTracking.pm index fa798a237f..2175c79abc 100644 --- a/extensions/BMO/lib/Reports/ReleaseTracking.pm +++ b/extensions/BMO/lib/Reports/ReleaseTracking.pm @@ -195,29 +195,32 @@ sub report { my $query = " SELECT DISTINCT b.bug_id FROM bugs b - INNER JOIN flags f ON f.bug_id = b.bug_id\n"; + INNER JOIN flags f ON f.bug_id = b.bug_id + WHERE "; - if ($q->{start_date}) { - $query .= "INNER JOIN bugs_activity a ON a.bug_id = b.bug_id\n"; - } - - $query .= "WHERE "; + my ($type_id) = $dbh->selectrow_array("SELECT id FROM flagtypes WHERE name = ?", + undef, $q->{flag_name}); if ($q->{start_date}) { - push @where, "(a.fieldid = ?)"; - push @params, $q->{field_id}; - - push @where, "(CONVERT_TZ(a.bug_when, 'UTC', 'America/Los_Angeles') >= ?)"; - push @params, $q->{start_date} . ' 00:00:00'; - push @where, "(CONVERT_TZ(a.bug_when, 'UTC', 'America/Los_Angeles') <= ?)"; - push @params, $q->{end_date} . ' 23:59:59'; - - push @where, "(a.added LIKE ?)"; - push @params, '%' . $q->{flag_name} . $q->{flag_status} . '%'; + my @range = ($q->{start_date} . ' 00:00:00', $q->{end_date} . ' 23:59:59'); + + # Flag changes are in flag_activity; older ones only in bugs_activity. + # flag_id 0 rows are backfilled copies of bugs_activity rows. + push @where, "(EXISTS ( + SELECT 1 FROM bugs_activity a + WHERE a.bug_id = b.bug_id AND a.fieldid = ? + AND CONVERT_TZ(a.bug_when, 'UTC', 'America/Los_Angeles') BETWEEN ? AND ? + AND a.added LIKE ?) + OR EXISTS ( + SELECT 1 FROM flag_activity fa + WHERE fa.bug_id = b.bug_id AND fa.flag_id != 0 + AND fa.type_id = ? AND fa.status = ? + AND CONVERT_TZ(fa.flag_when, 'UTC', 'America/Los_Angeles') BETWEEN ? AND ?))"; + push @params, $q->{field_id}, @range, + '%' . $q->{flag_name} . $q->{flag_status} . '%', + $type_id, $q->{flag_status}, @range; } - my ($type_id) = $dbh->selectrow_array("SELECT id FROM flagtypes WHERE name = ?", - undef, $q->{flag_name}); push @where, "(f.type_id = ?)"; push @params, $type_id; diff --git a/extensions/BMO/lib/Reports/UserActivity.pm b/extensions/BMO/lib/Reports/UserActivity.pm index ddf7cfca37..76b715aa1e 100644 --- a/extensions/BMO/lib/Reports/UserActivity.pm +++ b/extensions/BMO/lib/Reports/UserActivity.pm @@ -55,6 +55,7 @@ sub report { my ($activity_joins, $activity_where) = ('', ''); my ($attachments_joins, $attachments_where) = ('', ''); my ($tags_activity_joins, $tags_activity_where) = ('', ''); + my ($flag_activity_joins, $flag_activity_where) = ('', ''); if (Bugzilla->params->{"insidergroup"} && !Bugzilla->user->in_group(Bugzilla->params->{'insidergroup'})) { @@ -63,6 +64,10 @@ sub report { $activity_where = "AND COALESCE(attachments.isprivate, 0) = 0"; $attachments_where = $activity_where; + $flag_activity_joins = "LEFT JOIN attachments + ON attachments.attach_id = fsa.attachment_id"; + $flag_activity_where = $activity_where; + $tags_activity_joins = 'LEFT JOIN longdescs ON longdescs_tags_activity.comment_id = longdescs.comment_id'; $tags_activity_where = 'AND COALESCE(longdescs.isprivate, 0) = 0'; @@ -93,7 +98,7 @@ sub report { $from_dt = $from_dt->ymd() . ' 00:00:00'; $to_dt = $to_dt->ymd() . ' 23:59:59'; my @params; - for (1 .. 5) { + for (1 .. 6) { push @params, @who; push @params, ($from_dt, $to_dt); } @@ -108,6 +113,9 @@ sub report { $comment_filter = 'AND longdescs.isprivate = 0'; } + # Flag changes are in flag_activity. Older ones are only in bugs_activity, + # so for each bug only flag_activity rows after its last bugs_activity flag + # row are used. my $query = " SELECT fielddefs.name, @@ -134,6 +142,53 @@ sub report { UNION ALL + SELECT + 'flagtypes.name' AS name, + fsa.bug_id, + fsa.attachment_id AS attach_id, + " + . $dbh->sql_date_format('fsa.flag_when', '%Y-%m-%d %H:%i:%s') + . " AS ts, + CASE WHEN prev.id IS NULL THEN '' + WHEN prev.status = '?' AND preq.login_name IS NOT NULL + THEN CONCAT(ft.name, '?(', preq.login_name, ')') + ELSE CONCAT(ft.name, prev.status) + END AS removed, + CASE WHEN fsa.status = 'X' THEN '' + WHEN fsa.status = '?' AND req.login_name IS NOT NULL + THEN CONCAT(ft.name, '?(', req.login_name, ')') + ELSE CONCAT(ft.name, fsa.status) + END AS added, + profiles.login_name, + NULL AS comment_id, + fsa.flag_when AS bug_when + FROM flag_activity fsa + $flag_activity_joins + INNER JOIN flagtypes ft + ON ft.id = fsa.type_id + INNER JOIN profiles + ON profiles.userid = fsa.setter_id + LEFT JOIN profiles req + ON req.userid = fsa.requestee_id + LEFT JOIN flag_activity prev + ON prev.id = (SELECT MAX(p2.id) FROM flag_activity p2 + WHERE p2.flag_id = fsa.flag_id AND p2.id < fsa.id + AND fsa.flag_id != 0) + LEFT JOIN profiles preq + ON preq.userid = prev.requestee_id + WHERE profiles.login_name IN ($who_bits) + AND CONVERT_TZ(fsa.flag_when, 'UTC', 'America/Los_Angeles') >= ? + AND CONVERT_TZ(fsa.flag_when, 'UTC', 'America/Los_Angeles') <= ? + AND fsa.flag_when > COALESCE( + (SELECT MAX(ba.bug_when) FROM bugs_activity ba + WHERE ba.bug_id = fsa.bug_id + AND ba.fieldid = (SELECT id FROM fielddefs + WHERE name = 'flagtypes.name')), + '1970-01-01') + $flag_activity_where + + UNION ALL + SELECT 'comment_tag' AS name, longdescs_tags_activity.bug_id, diff --git a/t/flag-activity-search.t b/t/flag-activity-search.t new file mode 100644 index 0000000000..aeee0da943 --- /dev/null +++ b/t/flag-activity-search.t @@ -0,0 +1,94 @@ +#!/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 Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo.test'); +use Bugzilla::Test::MockDB; +use Bugzilla::Test::Util qw(create_bug create_user); +use Bugzilla::Search; +use Bugzilla::User; +use Test2::V0; + +# Search changed* on flags matches changes in flag_activity and the older +# ones that are only in bugs_activity. + +create_user('automation@bmo.tld', '*'); +Bugzilla->set_user(Bugzilla::User->super_user); +my $dbh = Bugzilla->dbh; + +my $alice = create_user('alice@bmo.test', '*'); +my $bob = create_user('bob@bmo.test', '*'); + +sub new_bug { + return create_bug( + short_desc => shift, + comment => 'Flag search test bug', + bug_type => 'defect', + assigned_to => 'nobody@mozilla.org', + )->id; +} + +my ($fieldid) + = $dbh->selectrow_array( + "SELECT id FROM fielddefs WHERE name = 'flagtypes.name'"); +my ($review_id) + = $dbh->selectrow_array("SELECT id FROM flagtypes WHERE name = 'review'"); + +# Legacy: review+ by bob in 2020, only in bugs_activity. +my $legacy_bug = new_bug('legacy'); +$dbh->do( + 'INSERT INTO bugs_activity (bug_id, who, bug_when, fieldid, removed, added) + VALUES (?, ?, ?, ?, ?, ?)', undef, $legacy_bug, $bob->id, + '2020-01-01 10:00:00', $fieldid, '', 'review+' +); + +# Current: alice asks bob for review, then grants it, only in flag_activity. +my $current_bug = new_bug('current'); +for my $row (['2026-01-01 10:00:00', '?', $bob->id], + ['2026-01-02 10:00:00', '+', undef]) +{ + $dbh->do( + 'INSERT INTO flag_activity + (flag_when, type_id, flag_id, setter_id, requestee_id, bug_id, status) + VALUES (?, ?, 42, ?, ?, ?, ?)', undef, $row->[0], $review_id, $alice->id, + $row->[2], $current_bug, $row->[1] + ); +} + +sub search { + my ($operator, $value) = @_; + my $search = Bugzilla::Search->new( + fields => ['bug_id'], + params => {f1 => 'flagtypes.name', o1 => $operator, v1 => $value}, + ); + return [sort { $a <=> $b } map { $_->[0] } @{$search->data}]; +} + +is(search(changedto => 'review+'), [$legacy_bug, $current_bug], 'changedto'); +is(search(changedto => 'review?(bob@bmo.test)'), [$current_bug], + 'changedto with requestee'); +is(search(changedfrom => 'review?(bob@bmo.test)'), [$current_bug], + 'changedfrom uses the previous flag_activity row'); +is(search(changedfrom => 'review+'), [], 'changedfrom no match'); +is(search(changedbefore => '2021-01-01'), [$legacy_bug], 'changedbefore'); +is(search(changedafter => '2025-01-01'), [$current_bug], 'changedafter'); +is(search(changedby => 'alice@bmo.test'), [$current_bug], + 'changedby flag_activity'); +is(search(changedby => 'bob@bmo.test'), [$legacy_bug], + 'changedby bugs_activity'); +is(search(everchanged => ''), [$legacy_bug, $current_bug], 'everchanged'); + +done_testing; From 9a84beb88dbf028d927e44beeb3660d24f357796 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Tue, 27 Oct 2026 15:23:10 +0100 Subject: [PATCH 08/50] Bug 2085398 - Record the acting user in flag_activity.who --- Bugzilla/Bug.pm | 4 +- Bugzilla/BugMail.pm | 2 +- Bugzilla/DB/Schema.pm | 7 ++ Bugzilla/Flag.pm | 4 ++ Bugzilla/FlagActivity.pm | 3 + Bugzilla/Install/DB.pm | 28 +++++++- Bugzilla/Search.pm | 2 +- editusers.cgi | 1 + extensions/BMO/lib/Reports/UserActivity.pm | 2 +- extensions/BugModal/lib/ActivityStream.pm | 13 ++-- scripts/flag_activity_backfill.pl | 6 +- t/flag-activity-backfill.t | 6 +- t/flag-activity-search.t | 7 +- t/flag-activity-who.t | 81 ++++++++++++++++++++++ 14 files changed, 142 insertions(+), 24 deletions(-) create mode 100644 t/flag-activity-who.t diff --git a/Bugzilla/Bug.pm b/Bugzilla/Bug.pm index 7605c21866..4914ede9a0 100644 --- a/Bugzilla/Bug.pm +++ b/Bugzilla/Bug.pm @@ -4947,11 +4947,11 @@ sub GetBugActivity { THEN CONCAT(ft.name, '?(', req.login_name, ')') ELSE CONCAT(ft.name, fsa.status) END AS added, - setter.login_name AS who, + changer.login_name AS who, NULL AS comment_id FROM flag_activity fsa JOIN flagtypes ft ON ft.id = fsa.type_id - JOIN profiles setter ON setter.userid = fsa.setter_id + JOIN profiles changer ON changer.userid = fsa.who LEFT JOIN profiles req ON req.userid = fsa.requestee_id LEFT JOIN flag_activity prev ON fsa.flag_id != 0 diff --git a/Bugzilla/BugMail.pm b/Bugzilla/BugMail.pm index 51813c078f..77d8e09bf0 100644 --- a/Bugzilla/BugMail.pm +++ b/Bugzilla/BugMail.pm @@ -744,7 +744,7 @@ sub _get_diffs { END AS new, fsa.attachment_id AS attach_id, NULL AS comment_id, - fsa.setter_id AS who + fsa.who AS who FROM flag_activity fsa JOIN flagtypes ft ON ft.id = fsa.type_id LEFT JOIN profiles req ON req.userid = fsa.requestee_id diff --git a/Bugzilla/DB/Schema.pm b/Bugzilla/DB/Schema.pm index a2bbd3224a..8f3e43e485 100644 --- a/Bugzilla/DB/Schema.pm +++ b/Bugzilla/DB/Schema.pm @@ -747,6 +747,13 @@ use constant ABSTRACT_SCHEMA => { NOTNULL => 1, REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}, }, + # The user who made the change. Not always the setter: redirecting a + # request keeps the original setter. + who => { + TYPE => 'INT3', + NOTNULL => 1, + REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}, + }, requestee_id => { TYPE => 'INT3', REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}, diff --git a/Bugzilla/Flag.pm b/Bugzilla/Flag.pm index 9d90970636..e601e09c72 100644 --- a/Bugzilla/Flag.pm +++ b/Bugzilla/Flag.pm @@ -494,6 +494,7 @@ sub create { Bugzilla::FlagActivity->create({ flag_when => $timestamp, setter_id => $flag->setter_id, + who => Bugzilla->user->id || $flag->setter_id, status => $flag->status, type_id => $flag->type_id, flag_id => $flag->id, @@ -522,6 +523,7 @@ sub update { Bugzilla::FlagActivity->create({ flag_when => $timestamp, setter_id => $self->setter_id, + who => Bugzilla->user->id || $self->setter_id, status => $self->status, type_id => $self->type_id, flag_id => $self->id, @@ -603,6 +605,7 @@ sub update_flags { Bugzilla::FlagActivity->create({ flag_when => $timestamp, setter_id => Bugzilla->user->id || $old_flag->setter_id, + who => Bugzilla->user->id || $old_flag->setter_id, status => 'X', type_id => $old_flag->type_id, flag_id => $old_flag->id, @@ -725,6 +728,7 @@ sub force_retarget { Bugzilla::FlagActivity->create({ flag_when => $timestamp, setter_id => Bugzilla->user->id || $flag->setter_id, + who => Bugzilla->user->id || $flag->setter_id, status => 'X', type_id => $flag->type_id, flag_id => $flag->id, diff --git a/Bugzilla/FlagActivity.pm b/Bugzilla/FlagActivity.pm index da41a79980..4ed09aa76d 100644 --- a/Bugzilla/FlagActivity.pm +++ b/Bugzilla/FlagActivity.pm @@ -29,6 +29,7 @@ use constant DB_COLUMNS => qw( type_id flag_id setter_id + who requestee_id bug_id attachment_id @@ -69,6 +70,7 @@ use constant VALIDATORS => { type_id => _check_param_required('type_id'), flag_id => _check_param_required('flag_id'), setter_id => _check_param_required('setter_id'), + who => _check_param_required('who'), bug_id => _check_param_required('bug_id'), status => \&_check_status, }; @@ -77,6 +79,7 @@ sub flag_when { return $_[0]->{flag_when} } sub type_id { return $_[0]->{type_id} } sub flag_id { return $_[0]->{flag_id} } sub setter_id { return $_[0]->{setter_id} } +sub who { return $_[0]->{who} } sub bug_id { return $_[0]->{bug_id} } sub requestee_id { return $_[0]->{requestee_id} } sub attachment_id { return $_[0]->{attachment_id} } diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index 5147beaa07..d13bfa6391 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -844,6 +844,10 @@ sub update_table_definitions { # Bug 1949556 - dkl@mozilla.com $dbh->bz_add_index('audit_log', 'audit_log_object_id_idx', ['object_id']); + # Bug 2085398 - xavier.lhour@gmail.com + # Before _migrate_flag_state_activity, which copies into this column. + _add_flag_activity_who(); + # Bug 1806896 - xavier.lhour@gmail.com _migrate_flag_state_activity(); @@ -4531,16 +4535,34 @@ sub _migrate_flag_state_activity { # id is not carried over: nothing references flag_activity.id externally. $dbh->do( 'INSERT INTO flag_activity - (flag_when, type_id, flag_id, setter_id, requestee_id, bug_id, + (flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, attachment_id, status) - SELECT flag_when, type_id, flag_id, setter_id, requestee_id, bug_id, - attachment_id, status + SELECT flag_when, type_id, flag_id, setter_id, setter_id, requestee_id, + bug_id, attachment_id, status FROM flag_state_activity' ); $dbh->bz_drop_table('flag_state_activity'); } +sub _add_flag_activity_who { + my $dbh = Bugzilla->dbh; + + return if $dbh->bz_column_info('flag_activity', 'who'); + + # Existing rows don't record who made the change; the setter is the best + # value available. + $dbh->bz_add_column('flag_activity', 'who', + {TYPE => 'INT3', REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}}); + $dbh->do('UPDATE flag_activity SET who = setter_id'); + $dbh->bz_alter_column('flag_activity', 'who', + { + TYPE => 'INT3', + NOTNULL => 1, + REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'} + }); +} + sub _remove_duo_recovery_codes { my $dbh = Bugzilla->dbh; diff --git a/Bugzilla/Search.pm b/Bugzilla/Search.pm index 2ca3c33829..09e3ed4f70 100644 --- a/Bugzilla/Search.pm +++ b/Bugzilla/Search.pm @@ -3776,7 +3776,7 @@ sub _flagtypes_changed { } elsif ($operator eq 'changedby') { my $user_id = $value =~ /^\d+$/ ? $value : $self->_get_user_id($value); - push @where, "$fa.setter_id = $user_id"; + push @where, "$fa.who = $user_id"; } elsif ($operator eq 'changedto') { my $match = _flag_state_match($fa, $value) or return; diff --git a/editusers.cgi b/editusers.cgi index 21ad3be5f0..35dfbf655e 100755 --- a/editusers.cgi +++ b/editusers.cgi @@ -527,6 +527,7 @@ elsif ($action eq 'delete') { Bugzilla::FlagActivity->create({ flag_when => $timestamp, setter_id => $userid, + who => $userid, status => $flag->status, type_id => $flag->type_id, flag_id => $flag->id, diff --git a/extensions/BMO/lib/Reports/UserActivity.pm b/extensions/BMO/lib/Reports/UserActivity.pm index 76b715aa1e..9dd4f72dda 100644 --- a/extensions/BMO/lib/Reports/UserActivity.pm +++ b/extensions/BMO/lib/Reports/UserActivity.pm @@ -167,7 +167,7 @@ sub report { INNER JOIN flagtypes ft ON ft.id = fsa.type_id INNER JOIN profiles - ON profiles.userid = fsa.setter_id + ON profiles.userid = fsa.who LEFT JOIN profiles req ON req.userid = fsa.requestee_id LEFT JOIN flag_activity prev diff --git a/extensions/BugModal/lib/ActivityStream.pm b/extensions/BugModal/lib/ActivityStream.pm index c71715c51f..41b0ae8bee 100644 --- a/extensions/BugModal/lib/ActivityStream.pm +++ b/extensions/BugModal/lib/ActivityStream.pm @@ -96,23 +96,22 @@ sub find_activity_id_for_attachment { sub find_activity_id_for_flag { my ($self, $flag) = @_; - my $flag_id = $flag->id; - my $setter_id = $flag->setter->id; - my $stream = $self->activity_stream; + my $flag_id = $flag->id; + my $stream = $self->activity_stream; if (!exists $self->{activity_cache}->{flag}->{$flag_id}) { # flag_activity records each change against the flag's object id, so we # can look up the exact timestamp without relying on flag-type name matching. - my ($flag_when) = Bugzilla->dbh->selectrow_array( - "SELECT flag_when FROM flag_activity WHERE flag_id = ? - ORDER BY flag_when DESC LIMIT 1", + my ($flag_when, $who) = Bugzilla->dbh->selectrow_array( + "SELECT flag_when, who FROM flag_activity WHERE flag_id = ? + ORDER BY id DESC LIMIT 1", undef, $flag_id ); SEARCH: foreach my $change_set (reverse @$stream) { foreach my $activity (@{$change_set->{activity}}) { next unless $flag_when - && $activity->{who}->id == $setter_id + && $activity->{who}->id == $who && $activity->{when} eq $flag_when; foreach my $change (@{$activity->{changes}}) { next unless $change->{fieldname} eq 'flagtypes.name'; diff --git a/scripts/flag_activity_backfill.pl b/scripts/flag_activity_backfill.pl index e4b4c5c50e..f6fa6f9528 100644 --- a/scripts/flag_activity_backfill.pl +++ b/scripts/flag_activity_backfill.pl @@ -181,10 +181,10 @@ sub backfill_batch { unless ($dry_run) { $dbh->do( "INSERT INTO flag_activity - (flag_when, type_id, flag_id, setter_id, requestee_id, + (flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, attachment_id, status) - VALUES (?, ?, 0, ?, ?, ?, ?, ?)", undef, $row->{bug_when}, $type_id, - $row->{who}, $requestee_id, $row->{bug_id}, $row->{attach_id}, $change->{status} + VALUES (?, ?, 0, ?, ?, ?, ?, ?, ?)", undef, $row->{bug_when}, $type_id, + $row->{who}, $row->{who}, $requestee_id, $row->{bug_id}, $row->{attach_id}, $change->{status} ); } diff --git a/t/flag-activity-backfill.t b/t/flag-activity-backfill.t index 6d94115fb3..f390e4f55d 100644 --- a/t/flag-activity-backfill.t +++ b/t/flag-activity-backfill.t @@ -64,9 +64,9 @@ add_activity('2020-01-01 10:00:00', add_activity('2020-01-02 10:00:00', 'review?(alice@bmo.test)', 'review+'); $dbh->do( 'INSERT INTO flag_activity - (flag_when, type_id, flag_id, setter_id, requestee_id, bug_id, status) - VALUES (?, ?, 42, ?, NULL, ?, ?)', undef, '2020-01-02 10:00:00', $review_id, - $setter->id, $bug_id, '+' + (flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, status) + VALUES (?, ?, 42, ?, ?, NULL, ?, ?)', undef, '2020-01-02 10:00:00', $review_id, + $setter->id, $setter->id, $bug_id, '+' ); # A flag cleared, asked from an account that no longer exists. diff --git a/t/flag-activity-search.t b/t/flag-activity-search.t index aeee0da943..aad72b4360 100644 --- a/t/flag-activity-search.t +++ b/t/flag-activity-search.t @@ -62,9 +62,10 @@ for my $row (['2026-01-01 10:00:00', '?', $bob->id], { $dbh->do( 'INSERT INTO flag_activity - (flag_when, type_id, flag_id, setter_id, requestee_id, bug_id, status) - VALUES (?, ?, 42, ?, ?, ?, ?)', undef, $row->[0], $review_id, $alice->id, - $row->[2], $current_bug, $row->[1] + (flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, + status) + VALUES (?, ?, 42, ?, ?, ?, ?, ?)', undef, $row->[0], $review_id, + $alice->id, $alice->id, $row->[2], $current_bug, $row->[1] ); } diff --git a/t/flag-activity-who.t b/t/flag-activity-who.t new file mode 100644 index 0000000000..ac11cbb34e --- /dev/null +++ b/t/flag-activity-who.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 Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo.test'); +use Bugzilla::Test::MockDB; +use Bugzilla::Test::MockParams (mail_delivery_method => 'None'); +use Bugzilla::Test::Util qw(create_bug create_user); +use Bugzilla::Bug; +use Bugzilla::User; +use Test2::V0; + +# Redirecting a request keeps the original setter, but flag_activity.who and +# the bug history record the user who redirected it. + +create_user('automation@bmo.tld', '*'); +Bugzilla->set_user(Bugzilla::User->super_user); +my $dbh = Bugzilla->dbh; + +my $requester = create_user('requester@bmo.test', '*'); +my $redirector = create_user('redirector@bmo.test', '*'); +create_user($_, '*') for qw(bob@bmo.test carol@bmo.test); + +my $bug_id = create_bug( + short_desc => 'Flag redirect test bug', + comment => 'Bug for the flag_activity who tests', + bug_type => 'defect', + assigned_to => 'nobody@mozilla.org', +)->id; +my ($needinfo_id) + = $dbh->selectrow_array("SELECT id FROM flagtypes WHERE name = 'needinfo'"); + +Bugzilla->set_user($requester); +my $bug = Bugzilla::Bug->new($bug_id); +$bug->set_flags([], + [{type_id => $needinfo_id, status => '?', requestee => 'bob@bmo.test'}]); +$bug->update('2026-01-01 10:00:00'); + +Bugzilla->set_user($redirector); +$bug = Bugzilla::Bug->new($bug_id); +my ($flag) = @{$bug->flags}; +$bug->set_flags([{id => $flag->id, status => '?', requestee => 'carol@bmo.test'}], + []); +$bug->update('2026-01-01 11:00:00'); + +$flag = Bugzilla::Flag->new($flag->id); +is($flag->setter_id, $requester->id, 'the setter is kept on redirect'); + +my $rows = $dbh->selectall_arrayref( + 'SELECT setter_id, who FROM flag_activity WHERE flag_id = ? ORDER BY id', + {Slice => {}}, $flag->id); +is( + $rows, + [ + {setter_id => $requester->id, who => $requester->id}, + {setter_id => $requester->id, who => $redirector->id}, + ], + 'who is the user who made each change' +); + +my ($operations) = Bugzilla::Bug::GetBugActivity($bug_id); +my ($redirect) = grep { + grep { $_->{fieldname} eq 'flagtypes.name' && $_->{added} =~ /carol/ } + @{$_->{changes}} +} @$operations; +is($redirect->{who}, 'redirector@bmo.test', 'history shows the redirector'); + +done_testing; From ecd87d985e0f4162108a5620f75ea3c6569cf125 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Tue, 27 Oct 2026 16:39:14 +0100 Subject: [PATCH 09/50] Bug 2085398 - Read pre-flag_activity flag changes from bugs_activity, drop the backfill script --- Bugzilla/Bug.pm | 9 +- Bugzilla/BugMail.pm | 9 +- Bugzilla/FlagActivity.pm | 14 ++ Bugzilla/Search.pm | 5 +- extensions/BMO/lib/Reports/ReleaseTracking.pm | 3 +- extensions/BMO/lib/Reports/UserActivity.pm | 16 +- scripts/flag_activity_backfill.pl | 217 ------------------ t/flag-activity-backfill.t | 131 ----------- t/flag-activity-history.t | 102 ++++++++ 9 files changed, 134 insertions(+), 372 deletions(-) delete mode 100644 scripts/flag_activity_backfill.pl delete mode 100644 t/flag-activity-backfill.t create mode 100644 t/flag-activity-history.t diff --git a/Bugzilla/Bug.pm b/Bugzilla/Bug.pm index 4914ede9a0..9367b12a36 100644 --- a/Bugzilla/Bug.pm +++ b/Bugzilla/Bug.pm @@ -15,6 +15,7 @@ use Bugzilla::Attachment; use Bugzilla::Constants; use Bugzilla::Field; use Bugzilla::Flag; +use Bugzilla::FlagActivity; use Bugzilla::FlagType; use Bugzilla::Hook; use Bugzilla::Keyword; @@ -4893,8 +4894,7 @@ sub GetBugActivity { ON profiles.userid = bugs_activity.who WHERE bugs_activity.bug_id = ? $attachpart - $suppwhere - AND (fielddefs.name IS NULL OR fielddefs.name != 'flagtypes.name') "; + $suppwhere "; if ( Bugzilla->params->{'comment_taggers_group'} && $include_comment_activity @@ -4931,6 +4931,7 @@ sub GetBugActivity { } my $flag_bug_when = $dbh->sql_date_format('fsa.flag_when', '%Y-%m-%d %H:%i:%s'); + my $flag_newer = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('fsa'); $query .= " UNION ALL SELECT 'flagtypes.name' AS field_name, @@ -4954,8 +4955,7 @@ sub GetBugActivity { JOIN profiles changer ON changer.userid = fsa.who LEFT JOIN profiles req ON req.userid = fsa.requestee_id LEFT JOIN flag_activity prev - ON fsa.flag_id != 0 - AND prev.flag_id = fsa.flag_id + ON prev.flag_id = fsa.flag_id AND prev.flag_when = ( SELECT MAX(p2.flag_when) FROM flag_activity p2 @@ -4964,6 +4964,7 @@ sub GetBugActivity { LEFT JOIN profiles preq ON preq.userid = prev.requestee_id $flag_suppjoins WHERE fsa.bug_id = ? + AND $flag_newer $flag_attachpart $flag_suppwhere "; diff --git a/Bugzilla/BugMail.pm b/Bugzilla/BugMail.pm index 77d8e09bf0..3a1685ab4e 100644 --- a/Bugzilla/BugMail.pm +++ b/Bugzilla/BugMail.pm @@ -17,6 +17,7 @@ use Bugzilla::Constants; use Bugzilla::Util; use Bugzilla::Bug; use Bugzilla::Comment; +use Bugzilla::FlagActivity; use Bugzilla::Logging; use Bugzilla::Mailer; use Bugzilla::Hook; @@ -705,7 +706,8 @@ sub _get_diffs { my ($bug, $end, $user_cache) = @_; my $dbh = Bugzilla->dbh; - my @args = ($bug->id); + my @args = ($bug->id); + my $flag_newer = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('fsa'); # If lastdiffed is NULL, then we don't limit the search on time. my $when_restriction = ''; @@ -727,7 +729,6 @@ sub _get_diffs { ON fielddefs.id = bugs_activity.fieldid WHERE bugs_activity.bug_id = ? $when_restriction - AND fielddefs.name != 'flagtypes.name' UNION ALL SELECT 'flagtypes.name' AS field_name, (SELECT description FROM fielddefs WHERE name = 'flagtypes.name') AS field_desc, @@ -749,8 +750,7 @@ sub _get_diffs { JOIN flagtypes ft ON ft.id = fsa.type_id LEFT JOIN profiles req ON req.userid = fsa.requestee_id LEFT JOIN flag_activity prev - ON fsa.flag_id != 0 - AND prev.flag_id = fsa.flag_id + ON prev.flag_id = fsa.flag_id AND prev.flag_when = ( SELECT MAX(p2.flag_when) FROM flag_activity p2 @@ -758,6 +758,7 @@ sub _get_diffs { AND p2.flag_when < fsa.flag_when) LEFT JOIN profiles preq ON preq.userid = prev.requestee_id WHERE fsa.bug_id = ? + AND $flag_newer $flag_when_restriction ORDER BY bug_when, field_desc", {Slice => {}}, @args, $bug->id, ($bug->lastdiffed ? ($bug->lastdiffed, $end) : ()) diff --git a/Bugzilla/FlagActivity.pm b/Bugzilla/FlagActivity.pm index 4ed09aa76d..e64d23bb3c 100644 --- a/Bugzilla/FlagActivity.pm +++ b/Bugzilla/FlagActivity.pm @@ -85,6 +85,20 @@ sub requestee_id { return $_[0]->{requestee_id} } sub attachment_id { return $_[0]->{attachment_id} } sub status { return $_[0]->{status} } +# SQL condition on flag_activity row $alias: true when it is newer than the +# bug's last flag change in bugs_activity. Older flag changes are read from +# bugs_activity, which has them all; flag_activity has only some of them, +# written to both tables, which must not be listed twice. +sub newer_than_bugs_activity_sql { + my ($class, $alias) = @_; + return "$alias.flag_when > COALESCE( + (SELECT MAX(ba.bug_when) FROM bugs_activity ba + WHERE ba.bug_id = $alias.bug_id + AND ba.fieldid = (SELECT id FROM fielddefs + WHERE name = 'flagtypes.name')), + '1970-01-01')"; +} + sub type { my ($self) = @_; return $self->{type} diff --git a/Bugzilla/Search.pm b/Bugzilla/Search.pm index 09e3ed4f70..8745ac825f 100644 --- a/Bugzilla/Search.pm +++ b/Bugzilla/Search.pm @@ -3754,8 +3754,7 @@ sub _changed_security_check { } # Flag changes are recorded in flag_activity. Older changes are only in -# bugs_activity, so match either. flag_id 0 rows are backfilled copies of -# bugs_activity rows and are skipped. +# bugs_activity, so match either. sub _flagtypes_changed { my ($self, $args) = @_; my $dbh = Bugzilla->dbh; @@ -3767,7 +3766,7 @@ sub _flagtypes_changed { my $legacy_term = $args->{term}; my $fa = "flag_act_$chart_id"; - my @where = ("$fa.bug_id = $bugs_table.bug_id", "$fa.flag_id != 0"); + my @where = ("$fa.bug_id = $bugs_table.bug_id"); if ($operator =~ /^changed(before|after)$/) { my $sql_operator = $1 eq 'before' ? '<=' : '>='; diff --git a/extensions/BMO/lib/Reports/ReleaseTracking.pm b/extensions/BMO/lib/Reports/ReleaseTracking.pm index 2175c79abc..3b6c20b023 100644 --- a/extensions/BMO/lib/Reports/ReleaseTracking.pm +++ b/extensions/BMO/lib/Reports/ReleaseTracking.pm @@ -205,7 +205,6 @@ sub report { my @range = ($q->{start_date} . ' 00:00:00', $q->{end_date} . ' 23:59:59'); # Flag changes are in flag_activity; older ones only in bugs_activity. - # flag_id 0 rows are backfilled copies of bugs_activity rows. push @where, "(EXISTS ( SELECT 1 FROM bugs_activity a WHERE a.bug_id = b.bug_id AND a.fieldid = ? @@ -213,7 +212,7 @@ sub report { AND a.added LIKE ?) OR EXISTS ( SELECT 1 FROM flag_activity fa - WHERE fa.bug_id = b.bug_id AND fa.flag_id != 0 + WHERE fa.bug_id = b.bug_id AND fa.type_id = ? AND fa.status = ? AND CONVERT_TZ(fa.flag_when, 'UTC', 'America/Los_Angeles') BETWEEN ? AND ?))"; push @params, $q->{field_id}, @range, diff --git a/extensions/BMO/lib/Reports/UserActivity.pm b/extensions/BMO/lib/Reports/UserActivity.pm index 9dd4f72dda..924664273f 100644 --- a/extensions/BMO/lib/Reports/UserActivity.pm +++ b/extensions/BMO/lib/Reports/UserActivity.pm @@ -12,6 +12,7 @@ use strict; use warnings; use Bugzilla::Error; +use Bugzilla::FlagActivity; use Bugzilla::User; use Bugzilla::Util qw(trim); use Bugzilla::Util::DateTime; @@ -113,9 +114,8 @@ sub report { $comment_filter = 'AND longdescs.isprivate = 0'; } - # Flag changes are in flag_activity. Older ones are only in bugs_activity, - # so for each bug only flag_activity rows after its last bugs_activity flag - # row are used. + # Flag changes are in flag_activity, older ones in bugs_activity. + my $flag_newer = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('fsa'); my $query = " SELECT fielddefs.name, @@ -172,19 +172,13 @@ sub report { ON req.userid = fsa.requestee_id LEFT JOIN flag_activity prev ON prev.id = (SELECT MAX(p2.id) FROM flag_activity p2 - WHERE p2.flag_id = fsa.flag_id AND p2.id < fsa.id - AND fsa.flag_id != 0) + WHERE p2.flag_id = fsa.flag_id AND p2.id < fsa.id) LEFT JOIN profiles preq ON preq.userid = prev.requestee_id WHERE profiles.login_name IN ($who_bits) AND CONVERT_TZ(fsa.flag_when, 'UTC', 'America/Los_Angeles') >= ? AND CONVERT_TZ(fsa.flag_when, 'UTC', 'America/Los_Angeles') <= ? - AND fsa.flag_when > COALESCE( - (SELECT MAX(ba.bug_when) FROM bugs_activity ba - WHERE ba.bug_id = fsa.bug_id - AND ba.fieldid = (SELECT id FROM fielddefs - WHERE name = 'flagtypes.name')), - '1970-01-01') + AND $flag_newer $flag_activity_where UNION ALL diff --git a/scripts/flag_activity_backfill.pl b/scripts/flag_activity_backfill.pl deleted file mode 100644 index f6fa6f9528..0000000000 --- a/scripts/flag_activity_backfill.pl +++ /dev/null @@ -1,217 +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. - -# Bug 2085398 - Backfill flag_activity from bugs_activity. -# -# Parses historical flagtypes.name rows in bugs_activity and inserts the -# missing records into flag_activity. Safe to run while dual-write is active; -# skips entries already present in flag_activity. -# -# Backfilled rows get flag_id 0: the flag can't be recovered reliably from -# bugs_activity, and bug history and bugmail skip the previous-state lookup -# for flag_id 0. -# -# Works through bugs in bug_id batches, one transaction per batch. -# -# Usage: perl scripts/flag_activity_backfill.pl [--dry-run] - -use 5.10.1; -use strict; -use warnings; - -use File::Basename qw(dirname); -use File::Spec::Functions qw(catdir rel2abs); - -BEGIN { - require lib; - my $dir = rel2abs(catdir(dirname(__FILE__), '..')); - lib->import($dir, catdir($dir, 'lib'), catdir($dir, qw(local lib perl5))); -} - -use Bugzilla; -use Bugzilla::Constants; - -use constant BATCH_SIZE => 1000; - -Bugzilla->usage_mode(USAGE_MODE_CMDLINE); - -my $dry_run = grep { $_ eq '--dry-run' } @ARGV; -print "DRY RUN — no data will be written.\n" if $dry_run; - -my $dbh = Bugzilla->dbh; - -# Get the fieldid for 'flagtypes.name' in bugs_activity. -my ($fieldid) - = $dbh->selectrow_array( - "SELECT id FROM fielddefs WHERE name = 'flagtypes.name'"); -die "Could not find fieldid for 'flagtypes.name' in fielddefs\n" - unless $fieldid; - -# Build a cache of flag type name -> id. -my %type_id_cache; -my $types - = $dbh->selectall_arrayref("SELECT id, name FROM flagtypes", {Slice => {}}); -$type_id_cache{$_->{name}} = $_->{id} for @$types; - -# login_name -> userid, filled as requestees are seen. -my %user_id_cache; - -sub requestee_id { - my ($email) = @_; - return undef unless $email; - $user_id_cache{$email} - //= $dbh->selectrow_array("SELECT userid FROM profiles WHERE login_name = ?", - undef, $email) // 0; - return $user_id_cache{$email} || undef; -} - -# Parse a single flag string like "review?(user@example.com)" or "review+". -# Returns (type_name, status, requestee_email_or_undef) or empty list. -sub parse_flag_str { - my ($str) = @_; - $str =~ s/^\s+|\s+$//g; - return unless $str =~ /^(.+?)([?+\-])(?:\(([^)]+)\))?$/; - return ($1, $2, $3); -} - -# Duplicate keys. The full key includes the requestee, so asking two people -# for the same flag in one change backfills both rows. When the requestee -# can't be resolved (account renamed or deleted since), fall back to the key -# without it so dual-written rows are not inserted twice. -sub short_key { - my ($bug_id, $when, $type_id, $status, $attach_id) = @_; - return join(':', $bug_id, $when, $type_id, $status, $attach_id // ''); -} - -sub full_key { - my ($requestee_id, @short) = @_; - return join(':', short_key(@short), $requestee_id // ''); -} - -my ($max_bug_id) - = $dbh->selectrow_array( - "SELECT MAX(bug_id) FROM bugs_activity WHERE fieldid = ?", - undef, $fieldid); -$max_bug_id //= 0; - -my ($total, $inserted, $skipped, $unknown_type, $unknown_user) - = (0, 0, 0, 0, 0); - -# Flag states to record for one bugs_activity row: -# - Each item in 'added' records its new state directly. -# - Items in 'removed' with no counterpart type in 'added' were deleted. -sub flag_changes { - my ($row) = @_; - my (@changes, %added_types); - - for my $item (split /,\s*/, $row->{added} // '') { - my ($name, $status, $requestee_email) = parse_flag_str($item) or next; - $added_types{$name} = 1; - push @changes, - {name => $name, status => $status, requestee_email => $requestee_email}; - } - - for my $item (split /,\s*/, $row->{removed} // '') { - my ($name, $status, $requestee_email) = parse_flag_str($item) or next; - - # Only record 'X' if this type does not appear in added (truly deleted). - next if $added_types{$name}; - push @changes, - {name => $name, status => 'X', requestee_email => $requestee_email}; - } - - return @changes; -} - -sub backfill_batch { - my ($from, $to) = @_; - - my (%existing, %existing_short); - my $existing_rows = $dbh->selectall_arrayref( - "SELECT bug_id, flag_when, type_id, status, attachment_id, requestee_id - FROM flag_activity - WHERE bug_id BETWEEN ? AND ?", undef, $from, $to - ); - for my $row (@$existing_rows) { - my @short = @$row[0 .. 4]; - $existing{full_key($row->[5], @short)} = 1; - $existing_short{short_key(@short)} = 1; - } - - my $rows = $dbh->selectall_arrayref( - "SELECT bug_id, who, bug_when, removed, added, attach_id - FROM bugs_activity - WHERE fieldid = ? AND bug_id BETWEEN ? AND ? - ORDER BY bug_when, id", {Slice => {}}, $fieldid, $from, $to - ); - return unless @$rows; - - $dbh->bz_start_transaction() unless $dry_run; - - for my $row (@$rows) { - $total++; - - for my $change (flag_changes($row)) { - my $type_id = $type_id_cache{$change->{name}}; - unless ($type_id) { - $unknown_type++; - next; - } - - my $requestee_id = requestee_id($change->{requestee_email}); - $unknown_user++ if $change->{requestee_email} && !$requestee_id; - - my @short = ( - $row->{bug_id}, $row->{bug_when}, $type_id, - $change->{status}, $row->{attach_id} - ); - my $key = full_key($requestee_id, @short); - - # Skip if already in flag_activity. - if ($existing{$key} || (!$requestee_id && $existing_short{short_key(@short)})) { - $skipped++; - next; - } - - unless ($dry_run) { - $dbh->do( - "INSERT INTO flag_activity - (flag_when, type_id, flag_id, setter_id, who, requestee_id, - bug_id, attachment_id, status) - VALUES (?, ?, 0, ?, ?, ?, ?, ?, ?)", undef, $row->{bug_when}, $type_id, - $row->{who}, $row->{who}, $requestee_id, $row->{bug_id}, $row->{attach_id}, $change->{status} - ); - } - - $existing{$key} = 1; - $existing_short{short_key(@short)} = 1; - $inserted++; - } - } - - $dbh->bz_commit_transaction() unless $dry_run; - - printf - " Bugs %d-%d done, %d bugs_activity rows processed (inserted: %d, skipped: %d)\n", - $from, $to, $total, $inserted, $skipped; -} - -print "Processing bugs_activity rows...\n"; - -my $from = 1; -while ($from <= $max_bug_id) { - backfill_batch($from, $from + BATCH_SIZE - 1); - $from += BATCH_SIZE; -} - -print "\nDone.\n"; -printf " bugs_activity rows processed : %d\n", $total; -printf " flag_activity rows inserted : %d\n", $inserted; -printf " already present (skipped) : %d\n", $skipped; -printf " unknown flag type (skipped) : %d\n", $unknown_type; -printf " unknown requestee (NULL inserted): %d\n", $unknown_user; diff --git a/t/flag-activity-backfill.t b/t/flag-activity-backfill.t deleted file mode 100644 index f390e4f55d..0000000000 --- a/t/flag-activity-backfill.t +++ /dev/null @@ -1,131 +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. -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 Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo.test'); -use Bugzilla::Test::MockDB; -use Bugzilla::Test::Util qw(create_bug create_user); -use Bugzilla::User; -use Capture::Tiny qw(capture_stdout); -use Test2::V0; - -# scripts/flag_activity_backfill.pl copies the flag changes recorded in -# bugs_activity into flag_activity, skipping the ones already there. - -# super_user() resolves 'automation@bmo.tld', which the mock DB does not ship. -create_user('automation@bmo.tld', '*'); -Bugzilla->set_user(Bugzilla::User->super_user); -my $dbh = Bugzilla->dbh; - -my $setter = create_user('setter@bmo.test', '*'); -my $alice = create_user('alice@bmo.test', '*'); -my $bob = create_user('bob@bmo.test', '*'); - -my $bug_id = create_bug( - short_desc => 'Backfill test bug', - comment => 'Bug for the flag_activity backfill tests', - bug_type => 'defect', - assigned_to => 'nobody@mozilla.org', -)->id; - -my ($fieldid) - = $dbh->selectrow_array( - "SELECT id FROM fielddefs WHERE name = 'flagtypes.name'"); -my ($review_id) - = $dbh->selectrow_array("SELECT id FROM flagtypes WHERE name = 'review'"); - -sub add_activity { - my ($when, $removed, $added) = @_; - $dbh->do( - 'INSERT INTO bugs_activity (bug_id, who, bug_when, fieldid, removed, added) - VALUES (?, ?, ?, ?, ?, ?)', undef, $bug_id, $setter->id, $when, $fieldid, - $removed, $added - ); -} - -# One change asks two people for review. -add_activity('2020-01-01 10:00:00', - '', 'review?(alice@bmo.test), review?(bob@bmo.test)'); - -# A dual-written change, already in flag_activity with its real flag_id. -add_activity('2020-01-02 10:00:00', 'review?(alice@bmo.test)', 'review+'); -$dbh->do( - 'INSERT INTO flag_activity - (flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, status) - VALUES (?, ?, 42, ?, ?, NULL, ?, ?)', undef, '2020-01-02 10:00:00', $review_id, - $setter->id, $setter->id, $bug_id, '+' -); - -# A flag cleared, asked from an account that no longer exists. -add_activity('2020-01-03 10:00:00', 'review?(gone@bmo.test)', ''); - -sub run_backfill { - local @ARGV = (); - local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /redefined/ }; - my $output = capture_stdout { do './scripts/flag_activity_backfill.pl' }; - die $@ if $@; - return $output; -} - -sub rows { - return $dbh->selectall_arrayref( - 'SELECT flag_when, flag_id, requestee_id, status - FROM flag_activity ORDER BY flag_when, requestee_id', {Slice => {}} - ); -} - -my $output = run_backfill(); -like( - $output, - qr/unknown requestee \(NULL inserted\): 1/, - 'the deleted requestee is counted' -); - -is( - rows(), - [ - { - flag_when => '2020-01-01 10:00:00', - flag_id => 0, - requestee_id => $alice->id, - status => '?' - }, - { - flag_when => '2020-01-01 10:00:00', - flag_id => 0, - requestee_id => $bob->id, - status => '?' - }, - { - flag_when => '2020-01-02 10:00:00', - flag_id => 42, - requestee_id => undef, - status => '+' - }, - { - flag_when => '2020-01-03 10:00:00', - flag_id => 0, - requestee_id => undef, - status => 'X' - }, - ], - 'both requestees backfilled, the dual-written row kept once, flag_id 0' -); - -run_backfill(); -is(scalar @{rows()}, 4, 'a second run inserts nothing'); - -done_testing; diff --git a/t/flag-activity-history.t b/t/flag-activity-history.t new file mode 100644 index 0000000000..d77170d8e7 --- /dev/null +++ b/t/flag-activity-history.t @@ -0,0 +1,102 @@ +#!/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 Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo.test'); +use Bugzilla::Test::MockDB; +use Bugzilla::Test::Util qw(create_bug create_user); +use Bugzilla::Bug; +use Bugzilla::BugMail; +use Bugzilla::User; +use Test2::V0; + +# Bug history and bugmail read the flag changes made before flag_activity +# existed from bugs_activity, and only the newer ones from flag_activity. + +create_user('automation@bmo.tld', '*'); +Bugzilla->set_user(Bugzilla::User->super_user); +my $dbh = Bugzilla->dbh; + +my $setter = create_user('setter@bmo.test', '*'); +my $alice = create_user('alice@bmo.test', '*'); +create_user('bob@bmo.test', '*'); + +my $bug_id = create_bug( + short_desc => 'Flag history test bug', + comment => 'Bug for the flag_activity history tests', + bug_type => 'defect', + assigned_to => 'nobody@mozilla.org', +)->id; + +my ($fieldid) + = $dbh->selectrow_array( + "SELECT id FROM fielddefs WHERE name = 'flagtypes.name'"); +my ($review_id) + = $dbh->selectrow_array("SELECT id FROM flagtypes WHERE name = 'review'"); + +sub add_flag_activity { + my ($when, $flag_id, $who, $requestee_id, $status) = @_; + $dbh->do( + 'INSERT INTO flag_activity + (flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, + status) + VALUES (?, ?, ?, ?, ?, ?, ?, ?)', undef, $when, $review_id, $flag_id, + $who->id, $who->id, $requestee_id, $bug_id, $status + ); +} + +# Legacy changes, only in bugs_activity. +for my $row ( + ['2020-01-01 10:00:00', '', 'review?(bob@bmo.test)'], + ['2020-01-02 10:00:00', 'review?(bob@bmo.test)', ''], + ['2020-01-03 10:00:00', '', 'review+'], + ) +{ + $dbh->do( + 'INSERT INTO bugs_activity (bug_id, who, bug_when, fieldid, removed, added) + VALUES (?, ?, ?, ?, ?, ?)', undef, $bug_id, $setter->id, $row->[0], + $fieldid, $row->[1], $row->[2] + ); +} + +# The last legacy change was also written to flag_activity, then the flag +# changed again once only flag_activity was written. +add_flag_activity('2020-01-03 10:00:00', 42, $setter, undef, '+'); +add_flag_activity('2026-01-01 10:00:00', 42, $alice, undef, '-'); + +my @expected = ( + ['setter@bmo.test', '', 'review?(bob@bmo.test)'], + ['setter@bmo.test', 'review?(bob@bmo.test)', ''], + ['setter@bmo.test', '', 'review+'], + ['alice@bmo.test', 'review+', 'review-'], +); + +my ($operations) = Bugzilla::Bug::GetBugActivity($bug_id); +my @history = map { + my $who = $_->{who}; + map { [$who, $_->{removed}, $_->{added}] } + grep { $_->{fieldname} eq 'flagtypes.name' } @{$_->{changes}} +} @$operations; +is(\@history, \@expected, 'history: each flag change once, with old value'); + +$dbh->do('UPDATE bugs SET lastdiffed = NULL WHERE bug_id = ?', undef, $bug_id); +my ($diffs) = Bugzilla::BugMail::_get_diffs(Bugzilla::Bug->new($bug_id), + '2026-12-31 00:00:00', {}); +my @mail = map { [$_->{who}->login, $_->{old}, $_->{new}] } + grep { $_->{field_name} eq 'flagtypes.name' } @$diffs; +is(\@mail, \@expected, 'bugmail: each flag change once, with old value'); + +done_testing; From ba54af1738de8bf475b09b38b73ec9aed587aa02 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Tue, 27 Oct 2026 17:10:14 +0100 Subject: [PATCH 10/50] Bug 2085398 - Pick the previous flag_activity row by id and show one net change per request --- Bugzilla/Bug.pm | 13 ++-- Bugzilla/BugMail.pm | 21 +++--- Bugzilla/FlagActivity.pm | 26 +++++++ extensions/BMO/lib/Reports/UserActivity.pm | 8 +- t/flag-activity-history.t | 86 +++++++++++++++------- 5 files changed, 104 insertions(+), 50 deletions(-) diff --git a/Bugzilla/Bug.pm b/Bugzilla/Bug.pm index 9367b12a36..5dbc29e677 100644 --- a/Bugzilla/Bug.pm +++ b/Bugzilla/Bug.pm @@ -4932,10 +4932,12 @@ sub GetBugActivity { my $flag_bug_when = $dbh->sql_date_format('fsa.flag_when', '%Y-%m-%d %H:%i:%s'); my $flag_newer = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('fsa'); + my $flag_last = Bugzilla::FlagActivity->last_of_request_sql('fsa'); + my $flag_prev_id = Bugzilla::FlagActivity->previous_id_sql('fsa'); $query .= " UNION ALL SELECT 'flagtypes.name' AS field_name, - NULL AS activity_id, + fsa.id AS activity_id, fsa.attachment_id AS attach_id, $flag_bug_when AS bug_when, CASE WHEN prev.id IS NULL THEN '' @@ -4954,17 +4956,12 @@ sub GetBugActivity { JOIN flagtypes ft ON ft.id = fsa.type_id JOIN profiles changer ON changer.userid = fsa.who LEFT JOIN profiles req ON req.userid = fsa.requestee_id - LEFT JOIN flag_activity prev - ON prev.flag_id = fsa.flag_id - AND prev.flag_when = ( - SELECT MAX(p2.flag_when) - FROM flag_activity p2 - WHERE p2.flag_id = fsa.flag_id - AND p2.flag_when < fsa.flag_when) + LEFT JOIN flag_activity prev ON prev.id = $flag_prev_id LEFT JOIN profiles preq ON preq.userid = prev.requestee_id $flag_suppjoins WHERE fsa.bug_id = ? AND $flag_newer + AND $flag_last $flag_attachpart $flag_suppwhere "; diff --git a/Bugzilla/BugMail.pm b/Bugzilla/BugMail.pm index 3a1685ab4e..443b39a455 100644 --- a/Bugzilla/BugMail.pm +++ b/Bugzilla/BugMail.pm @@ -707,7 +707,9 @@ sub _get_diffs { my $dbh = Bugzilla->dbh; my @args = ($bug->id); - my $flag_newer = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('fsa'); + my $flag_newer = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('fsa'); + my $flag_last = Bugzilla::FlagActivity->last_of_request_sql('fsa'); + my $flag_prev_id = Bugzilla::FlagActivity->previous_id_sql('fsa'); # If lastdiffed is NULL, then we don't limit the search on time. my $when_restriction = ''; @@ -723,7 +725,8 @@ sub _get_diffs { fielddefs.description AS field_desc, bugs_activity.bug_when, bugs_activity.removed AS old, bugs_activity.added AS new, bugs_activity.attach_id, - bugs_activity.comment_id, bugs_activity.who + bugs_activity.comment_id, bugs_activity.who, + bugs_activity.id AS activity_id FROM bugs_activity INNER JOIN fielddefs ON fielddefs.id = bugs_activity.fieldid @@ -745,22 +748,18 @@ sub _get_diffs { END AS new, fsa.attachment_id AS attach_id, NULL AS comment_id, - fsa.who AS who + fsa.who AS who, + fsa.id AS activity_id FROM flag_activity fsa JOIN flagtypes ft ON ft.id = fsa.type_id LEFT JOIN profiles req ON req.userid = fsa.requestee_id - LEFT JOIN flag_activity prev - ON prev.flag_id = fsa.flag_id - AND prev.flag_when = ( - SELECT MAX(p2.flag_when) - FROM flag_activity p2 - WHERE p2.flag_id = fsa.flag_id - AND p2.flag_when < fsa.flag_when) + LEFT JOIN flag_activity prev ON prev.id = $flag_prev_id LEFT JOIN profiles preq ON preq.userid = prev.requestee_id WHERE fsa.bug_id = ? AND $flag_newer + AND $flag_last $flag_when_restriction - ORDER BY bug_when, field_desc", {Slice => {}}, + ORDER BY bug_when, field_desc, activity_id", {Slice => {}}, @args, $bug->id, ($bug->lastdiffed ? ($bug->lastdiffed, $end) : ()) ); my $referenced_bugs = []; diff --git a/Bugzilla/FlagActivity.pm b/Bugzilla/FlagActivity.pm index e64d23bb3c..3c91956223 100644 --- a/Bugzilla/FlagActivity.pm +++ b/Bugzilla/FlagActivity.pm @@ -99,6 +99,32 @@ sub newer_than_bugs_activity_sql { '1970-01-01')"; } +# A flag created and updated in one request has several rows with the same +# flag_when and who. History shows the request's net change, like +# bugs_activity did: only its last row, compared with the flag's last row +# before the request. Rows are picked by id, which orders same-second rows. + +# SQL condition on flag_activity row $alias: true for the request's last row. +sub last_of_request_sql { + my ($class, $alias) = @_; + return "NOT EXISTS ( + SELECT 1 FROM flag_activity next_fa + WHERE next_fa.flag_id = $alias.flag_id + AND next_fa.flag_when = $alias.flag_when + AND next_fa.who = $alias.who + AND next_fa.id > $alias.id)"; +} + +# SQL subquery: the id of the flag's last row before $alias's request. +sub previous_id_sql { + my ($class, $alias) = @_; + return "(SELECT MAX(prev_fa.id) FROM flag_activity prev_fa + WHERE prev_fa.flag_id = $alias.flag_id + AND prev_fa.id < $alias.id + AND NOT (prev_fa.flag_when = $alias.flag_when + AND prev_fa.who = $alias.who))"; +} + sub type { my ($self) = @_; return $self->{type} diff --git a/extensions/BMO/lib/Reports/UserActivity.pm b/extensions/BMO/lib/Reports/UserActivity.pm index 924664273f..75e012dd64 100644 --- a/extensions/BMO/lib/Reports/UserActivity.pm +++ b/extensions/BMO/lib/Reports/UserActivity.pm @@ -115,7 +115,9 @@ sub report { } # Flag changes are in flag_activity, older ones in bugs_activity. - my $flag_newer = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('fsa'); + my $flag_newer = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('fsa'); + my $flag_last = Bugzilla::FlagActivity->last_of_request_sql('fsa'); + my $flag_prev_id = Bugzilla::FlagActivity->previous_id_sql('fsa'); my $query = " SELECT fielddefs.name, @@ -171,14 +173,14 @@ sub report { LEFT JOIN profiles req ON req.userid = fsa.requestee_id LEFT JOIN flag_activity prev - ON prev.id = (SELECT MAX(p2.id) FROM flag_activity p2 - WHERE p2.flag_id = fsa.flag_id AND p2.id < fsa.id) + ON prev.id = $flag_prev_id LEFT JOIN profiles preq ON preq.userid = prev.requestee_id WHERE profiles.login_name IN ($who_bits) AND CONVERT_TZ(fsa.flag_when, 'UTC', 'America/Los_Angeles') >= ? AND CONVERT_TZ(fsa.flag_when, 'UTC', 'America/Los_Angeles') <= ? AND $flag_newer + AND $flag_last $flag_activity_where UNION ALL diff --git a/t/flag-activity-history.t b/t/flag-activity-history.t index d77170d8e7..a69265d4da 100644 --- a/t/flag-activity-history.t +++ b/t/flag-activity-history.t @@ -32,14 +32,16 @@ my $dbh = Bugzilla->dbh; my $setter = create_user('setter@bmo.test', '*'); my $alice = create_user('alice@bmo.test', '*'); -create_user('bob@bmo.test', '*'); +my $bob = create_user('bob@bmo.test', '*'); -my $bug_id = create_bug( - short_desc => 'Flag history test bug', - comment => 'Bug for the flag_activity history tests', - bug_type => 'defect', - assigned_to => 'nobody@mozilla.org', -)->id; +sub new_bug { + return create_bug( + short_desc => shift, + comment => 'Bug for the flag_activity history tests', + bug_type => 'defect', + assigned_to => 'nobody@mozilla.org', + )->id; +} my ($fieldid) = $dbh->selectrow_array( @@ -48,7 +50,7 @@ my ($review_id) = $dbh->selectrow_array("SELECT id FROM flagtypes WHERE name = 'review'"); sub add_flag_activity { - my ($when, $flag_id, $who, $requestee_id, $status) = @_; + my ($bug_id, $when, $flag_id, $who, $requestee_id, $status) = @_; $dbh->do( 'INSERT INTO flag_activity (flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, @@ -58,7 +60,29 @@ sub add_flag_activity { ); } +# [who, removed, added] of the flag changes in bug history and in bugmail. +sub flag_changes { + my ($bug_id) = @_; + + my ($operations) = Bugzilla::Bug::GetBugActivity($bug_id); + my @history = map { + my $who = $_->{who}; + map { [$who, $_->{removed}, $_->{added}] } + grep { $_->{fieldname} eq 'flagtypes.name' } @{$_->{changes}} + } @$operations; + + $dbh->do('UPDATE bugs SET lastdiffed = NULL WHERE bug_id = ?', + undef, $bug_id); + my ($diffs) = Bugzilla::BugMail::_get_diffs(Bugzilla::Bug->new($bug_id), + '2026-12-31 00:00:00', {}); + my @mail = map { [$_->{who}->login, $_->{old}, $_->{new}] } + grep { $_->{field_name} eq 'flagtypes.name' } @$diffs; + + return (\@history, \@mail); +} + # Legacy changes, only in bugs_activity. +my $legacy_bug = new_bug('legacy'); for my $row ( ['2020-01-01 10:00:00', '', 'review?(bob@bmo.test)'], ['2020-01-02 10:00:00', 'review?(bob@bmo.test)', ''], @@ -67,36 +91,42 @@ for my $row ( { $dbh->do( 'INSERT INTO bugs_activity (bug_id, who, bug_when, fieldid, removed, added) - VALUES (?, ?, ?, ?, ?, ?)', undef, $bug_id, $setter->id, $row->[0], + VALUES (?, ?, ?, ?, ?, ?)', undef, $legacy_bug, $setter->id, $row->[0], $fieldid, $row->[1], $row->[2] ); } # The last legacy change was also written to flag_activity, then the flag # changed again once only flag_activity was written. -add_flag_activity('2020-01-03 10:00:00', 42, $setter, undef, '+'); -add_flag_activity('2026-01-01 10:00:00', 42, $alice, undef, '-'); +add_flag_activity($legacy_bug, '2020-01-03 10:00:00', 42, $setter, undef, '+'); +add_flag_activity($legacy_bug, '2026-01-01 10:00:00', 42, $alice, undef, '-'); -my @expected = ( +my $expected = [ ['setter@bmo.test', '', 'review?(bob@bmo.test)'], ['setter@bmo.test', 'review?(bob@bmo.test)', ''], ['setter@bmo.test', '', 'review+'], ['alice@bmo.test', 'review+', 'review-'], -); - -my ($operations) = Bugzilla::Bug::GetBugActivity($bug_id); -my @history = map { - my $who = $_->{who}; - map { [$who, $_->{removed}, $_->{added}] } - grep { $_->{fieldname} eq 'flagtypes.name' } @{$_->{changes}} -} @$operations; -is(\@history, \@expected, 'history: each flag change once, with old value'); - -$dbh->do('UPDATE bugs SET lastdiffed = NULL WHERE bug_id = ?', undef, $bug_id); -my ($diffs) = Bugzilla::BugMail::_get_diffs(Bugzilla::Bug->new($bug_id), - '2026-12-31 00:00:00', {}); -my @mail = map { [$_->{who}->login, $_->{old}, $_->{new}] } - grep { $_->{field_name} eq 'flagtypes.name' } @$diffs; -is(\@mail, \@expected, 'bugmail: each flag change once, with old value'); +]; +my ($history, $mail) = flag_changes($legacy_bug); +is($history, $expected, 'history: each flag change once, with old value'); +is($mail, $expected, 'bugmail: each flag change once, with old value'); + +# A flag created and updated in one request has two rows in the same second: +# history shows the request's net change, like bugs_activity did. +my $same_second_bug = new_bug('same second'); +add_flag_activity($same_second_bug, '2026-02-01 10:00:00', 43, $alice, $bob->id, + '?'); +add_flag_activity($same_second_bug, '2026-02-01 10:00:00', 43, $alice, undef, + '+'); +add_flag_activity($same_second_bug, '2026-02-02 10:00:00', 43, $setter, undef, + '-'); + +$expected = [ + ['alice@bmo.test', '', 'review+'], + ['setter@bmo.test', 'review+', 'review-'], +]; +($history, $mail) = flag_changes($same_second_bug); +is($history, $expected, 'history: one net change per request'); +is($mail, $expected, 'bugmail: one net change per request'); done_testing; From a140f702df6dd78dd11c98c7f0d53abf02e3b582 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 29 Oct 2026 17:21:47 +0100 Subject: [PATCH 11/50] Bug 2085398 - Count flag changes in the non-bot last change time and the user activity stats --- Bugzilla/Bug.pm | 37 ++++++------ extensions/TagNewUsers/Extension.pm | 7 +++ extensions/UserProfile/lib/Util.pm | 23 ++++++-- t/flag-activity-user-stats.t | 88 +++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+), 26 deletions(-) create mode 100644 t/flag-activity-user-stats.t diff --git a/Bugzilla/Bug.pm b/Bugzilla/Bug.pm index 5dbc29e677..1f1b506d35 100644 --- a/Bugzilla/Bug.pm +++ b/Bugzilla/Bug.pm @@ -4558,29 +4558,24 @@ sub delta_ts_non_bot { } } - # Grab the latest timestamp for comments and bug changes that was not made by - # one of the accounts in the bot skip list. Return the greatest of the two values. + # Grab the latest timestamp for comments, bug changes and flag changes that + # was not made by one of the accounts in the bot skip list. if (my $bot_ids_string = $cache->{bot_ids_string}) { - my %time_map; - my @result = $dbh->selectrow_array( - "SELECT bug_when, UNIX_TIMESTAMP(bug_when) - FROM bugs_activity - WHERE bug_id = ? AND who NOT IN ($bot_ids_string) - ORDER BY bug_when DESC LIMIT 1", - undef, $self->id - ); - $time_map{$result[1]} = $result[0] if @result; - @result = $dbh->selectrow_array( - "SELECT bug_when, UNIX_TIMESTAMP(bug_when) - FROM longdescs - WHERE bug_id = ? AND who NOT IN ($bot_ids_string) - ORDER BY bug_when DESC LIMIT 1", - undef, $self->id + ($self->{delta_ts_non_bot}) = $dbh->selectrow_array( + "SELECT MAX(bug_when) + FROM (SELECT MAX(bug_when) AS bug_when + FROM bugs_activity + WHERE bug_id = ? AND who NOT IN ($bot_ids_string) + UNION ALL + SELECT MAX(bug_when) AS bug_when + FROM longdescs + WHERE bug_id = ? AND who NOT IN ($bot_ids_string) + UNION ALL + SELECT MAX(flag_when) AS bug_when + FROM flag_activity + WHERE bug_id = ? AND who NOT IN ($bot_ids_string)) tmp", + undef, ($self->id) x 3 ); - $time_map{$result[1]} = $result[0] if @result; - - my $max_epoch = max keys %time_map; - $self->{delta_ts_non_bot} = $time_map{$max_epoch}; } return $self->{delta_ts_non_bot}; diff --git a/extensions/TagNewUsers/Extension.pm b/extensions/TagNewUsers/Extension.pm index 625e687d57..52310fd861 100644 --- a/extensions/TagNewUsers/Extension.pm +++ b/extensions/TagNewUsers/Extension.pm @@ -75,6 +75,13 @@ sub install_update_db { undef, $user_id ); } + if (!$when) { + ($when) = $dbh->selectrow_array( + "SELECT flag_when FROM flag_activity WHERE who=? ORDER BY flag_when " + . $dbh->sql_limit(1), + undef, $user_id + ); + } if (!$when) { ($when) = $dbh->selectrow_array( "SELECT bug_when FROM longdescs WHERE who=? ORDER BY bug_when " diff --git a/extensions/UserProfile/lib/Util.pm b/extensions/UserProfile/lib/Util.pm index 4972a20323..d4e0dccac5 100644 --- a/extensions/UserProfile/lib/Util.pm +++ b/extensions/UserProfile/lib/Util.pm @@ -118,7 +118,7 @@ EOF EOF # bugs touched - _update_statistics($statistics, 'touched', [$user_id, $user_id], <dbh; # get a list of all users associated with this bug - my $user_ids - = $dbh->selectcol_arrayref(<selectcol_arrayref(<dbh->selectrow_array( - < ? + UNION ALL + SELECT MAX(flag_when) AS bug_when + FROM flag_activity + WHERE who = ? ) tmp EOF } diff --git a/t/flag-activity-user-stats.t b/t/flag-activity-user-stats.t new file mode 100644 index 0000000000..bffbc4d35c --- /dev/null +++ b/t/flag-activity-user-stats.t @@ -0,0 +1,88 @@ +#!/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 Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo.test'); +use Bugzilla::Test::MockDB; +use Bugzilla::Test::MockParams ( + mail_delivery_method => 'None', + last_change_time_non_bot_skip_list => 'automation@bmo.tld', +); +use Bugzilla::Test::Util qw(create_bug create_user); +use Bugzilla::Bug; +use Bugzilla::User; +use Bugzilla::Extension::UserProfile::Util + qw(last_user_activity tag_for_recount_from_bug update_statistics_by_user); +use Test2::V0; + +# Flag changes are only in flag_activity, so the readers of a user's activity +# have to read it too. + +my $bot = create_user('automation@bmo.tld', '*'); +Bugzilla->set_user(Bugzilla::User->super_user); +my $dbh = Bugzilla->dbh; + +my $human = create_user('human@bmo.test', '*'); +create_user('bob@bmo.test', '*'); + +my $bug_id = create_bug( + short_desc => 'Flag activity user stats test bug', + comment => 'Bug for the flag_activity user stats tests', + bug_type => 'defect', + assigned_to => 'nobody@mozilla.org', +)->id; +my ($needinfo_id) + = $dbh->selectrow_array("SELECT id FROM flagtypes WHERE name = 'needinfo'"); + +# Future dates, so that the flag changes come after the bug creation. +Bugzilla->set_user($human); +my $bug = Bugzilla::Bug->new($bug_id); +$bug->set_flags([], + [{type_id => $needinfo_id, status => '?', requestee => 'bob@bmo.test'}]); +$bug->update('2099-01-01 10:00:00'); + +Bugzilla->set_user($bot); +$bug = Bugzilla::Bug->new($bug_id); +$bug->set_flags([], + [{type_id => $needinfo_id, status => '?', requestee => 'human@bmo.test'}]); +$bug->update('2099-01-01 11:00:00'); + +# Requesting needinfo also CCs the requestee: drop those changes, so that the +# flag changes are only in flag_activity. +$dbh->do('DELETE FROM bugs_activity WHERE bug_id = ?', undef, $bug_id); + +is(Bugzilla::Bug->new($bug_id)->delta_ts_non_bot, + '2099-01-01 10:00:00', 'the non-bot last change time counts flag changes'); + +is(last_user_activity($human->id), + '2099-01-01 10:00:00', 'the last user activity counts flag changes'); + +$dbh->do('UPDATE profiles SET last_statistics_ts = ? WHERE userid = ?', + undef, '2099-01-01 12:00:00', $human->id); +tag_for_recount_from_bug($bug_id); +my ($last_statistics_ts) + = $dbh->selectrow_array( + 'SELECT last_statistics_ts FROM profiles WHERE userid = ?', + undef, $human->id); +is($last_statistics_ts, undef, 'a flag change tags the user for a recount'); + +update_statistics_by_user($human->id); +my ($touched) = $dbh->selectrow_array( + "SELECT count FROM profiles_statistics WHERE user_id = ? AND name = 'touched'", + undef, $human->id); +is($touched, 1, 'a flag change counts as touching the bug'); + +done_testing; From 3b3f4ba0f52032bfdf025fba87fd2574bd17a50b Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 29 Oct 2026 17:29:28 +0100 Subject: [PATCH 12/50] Bug 2085398 - Index flag_activity on who and flag_when --- Bugzilla/DB/Schema.pm | 1 + Bugzilla/Install/DB.pm | 31 ++++++++++++++++++------------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Bugzilla/DB/Schema.pm b/Bugzilla/DB/Schema.pm index 8f3e43e485..8c7656f78c 100644 --- a/Bugzilla/DB/Schema.pm +++ b/Bugzilla/DB/Schema.pm @@ -774,6 +774,7 @@ use constant ABSTRACT_SCHEMA => { flag_activity_type_id_idx => ['type_id'], flag_activity_bug_id_idx => ['bug_id'], flag_activity_status_when_idx => ['status', 'flag_when'], + flag_activity_who_idx => ['who', 'flag_when'], ], }, diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index d13bfa6391..eb53ac7cbc 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -4548,19 +4548,24 @@ sub _migrate_flag_state_activity { sub _add_flag_activity_who { my $dbh = Bugzilla->dbh; - return if $dbh->bz_column_info('flag_activity', 'who'); - - # Existing rows don't record who made the change; the setter is the best - # value available. - $dbh->bz_add_column('flag_activity', 'who', - {TYPE => 'INT3', REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}}); - $dbh->do('UPDATE flag_activity SET who = setter_id'); - $dbh->bz_alter_column('flag_activity', 'who', - { - TYPE => 'INT3', - NOTNULL => 1, - REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'} - }); + if (!$dbh->bz_column_info('flag_activity', 'who')) { + + # Existing rows don't record who made the change; the setter is the best + # value available. + $dbh->bz_add_column('flag_activity', 'who', + {TYPE => 'INT3', REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}}); + $dbh->do('UPDATE flag_activity SET who = setter_id'); + $dbh->bz_alter_column('flag_activity', 'who', + { + TYPE => 'INT3', + NOTNULL => 1, + REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'} + }); + } + + # For the user activity report and the changedby search. + $dbh->bz_add_index('flag_activity', 'flag_activity_who_idx', + [qw(who flag_when)]); } sub _remove_duo_recovery_codes { From 3cf9f6de9f6df8521b184cfd5b72e6f5b2551e25 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 29 Oct 2026 18:47:28 +0100 Subject: [PATCH 13/50] Bug 2085398 - Link flags with no flag_activity row to their bugs_activity change in the bug modal --- Bugzilla/DB/Sqlite.pm | 1 + extensions/BugModal/lib/ActivityStream.pm | 19 ++++-- t/flag-activity-stream.t | 79 +++++++++++++++++++++++ 3 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 t/flag-activity-stream.t diff --git a/Bugzilla/DB/Sqlite.pm b/Bugzilla/DB/Sqlite.pm index ccb949d388..3a72f76f10 100644 --- a/Bugzilla/DB/Sqlite.pm +++ b/Bugzilla/DB/Sqlite.pm @@ -225,6 +225,7 @@ sub sql_date_format { my ($self, $date, $format) = @_; $format = "%Y-%m-%d %H:%M:%S" if !$format; $format =~ s/\%i/\%M/g; + $format =~ s/\%s/\%S/g; return "STRFTIME(" . $self->quote($format) . ", $date)"; } diff --git a/extensions/BugModal/lib/ActivityStream.pm b/extensions/BugModal/lib/ActivityStream.pm index 41b0ae8bee..7e200826e4 100644 --- a/extensions/BugModal/lib/ActivityStream.pm +++ b/extensions/BugModal/lib/ActivityStream.pm @@ -108,20 +108,31 @@ sub find_activity_id_for_flag { undef, $flag_id ); + # flag_activity only logged review, feedback and needinfo before, so other + # flags may have no row: match them as before, on the setter, the + # modification date and the flag type name. + my $flagtype_name; + if (!$flag_when) { + ($flag_when, $who, $flagtype_name) + = ($flag->modification_date, $flag->setter->id, $flag->type->name); + } + SEARCH: foreach my $change_set (reverse @$stream) { foreach my $activity (@{$change_set->{activity}}) { - next unless $flag_when - && $activity->{who}->id == $who - && $activity->{when} eq $flag_when; + next + unless $activity->{who}->id == $who && $activity->{when} eq $flag_when; foreach my $change (@{$activity->{changes}}) { next unless $change->{fieldname} eq 'flagtypes.name'; + next + if defined $flagtype_name + && $change->{flagtype_name} ne $flagtype_name; $self->{activity_cache}->{flag}->{$flag_id} = $change_set->{id}; last SEARCH; } } } - # flag set during bug creation, or no flag_activity entry yet + # flag set during bug creation $self->{activity_cache}->{flag}->{$flag_id} //= 'c0'; } return $self->{activity_cache}->{flag}->{$flag_id}; diff --git a/t/flag-activity-stream.t b/t/flag-activity-stream.t new file mode 100644 index 0000000000..bf26484a3c --- /dev/null +++ b/t/flag-activity-stream.t @@ -0,0 +1,79 @@ +#!/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 Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo.test'); +use Bugzilla::Test::MockDB; +use Bugzilla::Test::MockParams (mail_delivery_method => 'None'); +use Bugzilla::Test::Util qw(create_bug create_user); +use Bugzilla::Bug; +use Bugzilla::User; +use Test2::V0; + +# The bug modal links each flag to the change that set it. Flags set before +# flag_activity logged their type have no row there, and are found from their +# bugs_activity change instead. + +create_user('automation@bmo.tld', '*'); +Bugzilla->set_user(Bugzilla::User->super_user); +my $dbh = Bugzilla->dbh; + +my $setter = create_user('setter@bmo.test', '*'); +create_user('bob@bmo.test', '*'); + +my ($needinfo_id) + = $dbh->selectrow_array("SELECT id FROM flagtypes WHERE name = 'needinfo'"); +my ($fieldid) + = $dbh->selectrow_array( + "SELECT id FROM fielddefs WHERE name = 'flagtypes.name'"); + +sub flag_activity_id { + my ($legacy) = @_; + Bugzilla->set_user(Bugzilla::User->super_user); + my $bug_id = create_bug( + short_desc => 'Flag activity stream test bug', + comment => 'Bug for the flag activity stream tests', + bug_type => 'defect', + assigned_to => 'nobody@mozilla.org', + )->id; + + # A future date, so that the change comes after the bug creation. + Bugzilla->set_user($setter); + my $bug = Bugzilla::Bug->new($bug_id); + $bug->set_flags([], + [{type_id => $needinfo_id, status => '?', requestee => 'bob@bmo.test'}]); + $bug->update('2099-01-01 10:00:00'); + + if ($legacy) { + $dbh->do('DELETE FROM flag_activity WHERE bug_id = ?', undef, $bug_id); + $dbh->do( + 'INSERT INTO bugs_activity (bug_id, who, bug_when, fieldid, removed, added) + VALUES (?, ?, ?, ?, ?, ?)', undef, $bug_id, $setter->id, + '2099-01-01 10:00:00', $fieldid, '', 'needinfo?(bob@bmo.test)' + ); + } + + $bug = Bugzilla::Bug->new($bug_id); + my ($flag) = @{$bug->flags}; + return $bug->find_activity_id_for_flag($flag); +} + +like(flag_activity_id(0), qr/^a\d+_\d+$/, + 'a flag is linked to its flag_activity change'); +like(flag_activity_id(1), qr/^a\d+_\d+$/, + 'a flag without flag_activity rows is linked to its bugs_activity change'); + +done_testing; From 32f997deeda22a24419731532305f2ddd8f20e61 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 29 Oct 2026 18:56:22 +0100 Subject: [PATCH 14/50] Bug 2085398 - Count flag_activity rows in the user deletion check --- editusers.cgi | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/editusers.cgi b/editusers.cgi index 35dfbf655e..a83774f2ff 100755 --- a/editusers.cgi +++ b/editusers.cgi @@ -391,9 +391,12 @@ elsif ($action eq 'del') { undef, $otherUserID); $vars->{'cc'} = $dbh->selectrow_array('SELECT COUNT(*) FROM cc WHERE who = ?', undef, $otherUserID); - $vars->{'bugs_activity'} - = $dbh->selectrow_array('SELECT COUNT(*) FROM bugs_activity WHERE who = ?', - undef, $otherUserID); + # Flag changes are in flag_activity, not bugs_activity. + $vars->{'bugs_activity'} = $dbh->selectrow_array( + 'SELECT (SELECT COUNT(*) FROM bugs_activity WHERE who = ?) + + (SELECT COUNT(*) FROM flag_activity WHERE who = ? OR setter_id = ?)', + undef, ($otherUserID) x 3 + ); $vars->{'component_cc'} = $dbh->selectrow_array('SELECT COUNT(*) FROM component_cc WHERE user_id = ?', undef, $otherUserID); From ebde8bb4236d83ff24e2e68740ce316557defc45 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 29 Oct 2026 18:58:28 +0100 Subject: [PATCH 15/50] Bug 2085398 - Keep the flag setter in flag_activity when a deleted requestee is removed --- editusers.cgi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/editusers.cgi b/editusers.cgi index a83774f2ff..4bd609106e 100755 --- a/editusers.cgi +++ b/editusers.cgi @@ -529,7 +529,7 @@ elsif ($action eq 'delete') { foreach my $flag (@{$bugs{$bug_id}->{$attach_id}}) { Bugzilla::FlagActivity->create({ flag_when => $timestamp, - setter_id => $userid, + setter_id => $flag->setter_id, who => $userid, status => $flag->status, type_id => $flag->type_id, From 34ee87d55eff48109ff149a25fa4a5bd4bafe213 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 29 Oct 2026 21:31:59 +0100 Subject: [PATCH 16/50] Bug 2085398 - Fill flag_activity.who in the bugmail flag events test --- t/bmo/bugmail-flag-events.t | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/t/bmo/bugmail-flag-events.t b/t/bmo/bugmail-flag-events.t index 7f93630320..5aa729d259 100644 --- a/t/bmo/bugmail-flag-events.t +++ b/t/bmo/bugmail-flag-events.t @@ -94,10 +94,10 @@ sub new_bug { sub add_activity { my ($bug, $flag_id, $when, $status, $setter, $requestee) = @_; $dbh->do( - 'INSERT INTO flag_activity (flag_when, type_id, flag_id, setter_id, + 'INSERT INTO flag_activity (flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, status) - VALUES (?, ?, ?, ?, ?, ?, ?)', undef, $when, $type_id, $flag_id, - $setter->id, $requestee ? $requestee->id : undef, $bug->id, $status + VALUES (?, ?, ?, ?, ?, ?, ?, ?)', undef, $when, $type_id, $flag_id, + $setter->id, $setter->id, $requestee ? $requestee->id : undef, $bug->id, $status ); } From e18e3fabf71ff493083f050f3e4c7ad22ae2ea01 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Fri, 30 Oct 2026 00:28:23 +0100 Subject: [PATCH 17/50] Bug 2085398 - Keep flag_activity.who in the public data dump --- scripts/remove-non-public-data.pl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/remove-non-public-data.pl b/scripts/remove-non-public-data.pl index c4cffc202a..29d9915387 100755 --- a/scripts/remove-non-public-data.pl +++ b/scripts/remove-non-public-data.pl @@ -94,7 +94,7 @@ BEGIN ], flag_activity => [ qw( - id flag_when type_id flag_id setter_id requestee_id bug_id + id flag_when type_id flag_id setter_id who requestee_id bug_id attachment_id status ) ], @@ -268,6 +268,7 @@ BEGIN AND (SELECT COUNT(*) FROM flags WHERE flags.setter_id = profiles.userid) = 0 AND (SELECT COUNT(*) FROM flags WHERE flags.requestee_id = profiles.userid) = 0 AND (SELECT COUNT(*) FROM flag_activity WHERE flag_activity.setter_id = profiles.userid) = 0 + AND (SELECT COUNT(*) FROM flag_activity WHERE flag_activity.who = profiles.userid) = 0 AND (SELECT COUNT(*) FROM flag_activity WHERE flag_activity.requestee_id = profiles.userid) = 0 "); From 717eb5382a666b95084a3cc505c157608131f816 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Fri, 30 Oct 2026 00:42:35 +0100 Subject: [PATCH 18/50] Bug 2085398 - Refuse to delete a flag type that has flag_activity rows --- Bugzilla/FlagType.pm | 28 +++++++ qa/t/2_test_flags.t | 29 ++++++-- qa/t/2_test_flags2.t | 25 +++++-- t/flag-type-delete.t | 74 +++++++++++++++++++ .../admin/flag-type/confirm-delete.html.tmpl | 11 +++ .../en/default/global/user-error.html.tmpl | 6 ++ 6 files changed, 159 insertions(+), 14 deletions(-) create mode 100644 t/flag-type-delete.t diff --git a/Bugzilla/FlagType.pm b/Bugzilla/FlagType.pm index bf09a72e89..ce580ee2f4 100644 --- a/Bugzilla/FlagType.pm +++ b/Bugzilla/FlagType.pm @@ -487,6 +487,16 @@ must be in order to request or clear a flag. Returns the number of flags belonging to the flagtype. +=item C + +Returns 1 if the flagtype has rows in flag_activity, 0 otherwise. Such a +flagtype cannot be deleted, as its rows would be deleted with it. + +=item C + +Deletes the flagtype. Throws C if it has rows in +flag_activity. + =item C Return a hash of product/component IDs and names @@ -531,6 +541,24 @@ sub request_group { return $self->{'request_group'}; } +# flag_activity is the only record of the changes to the type's flags, and its +# rows are deleted with the type: such a type can only be deactivated. +sub has_activity { + my $self = shift; + my $dbh = Bugzilla->dbh; + $self->{'has_activity'} //= $dbh->selectrow_array( + 'SELECT 1 FROM flag_activity WHERE type_id = ? ' . $dbh->sql_limit(1), + undef, $self->id) ? 1 : 0; + return $self->{'has_activity'}; +} + +sub remove_from_db { + my $self = shift; + ThrowUserError('flag_type_has_activity', {flagtype => $self}) + if $self->has_activity; + $self->SUPER::remove_from_db(@_); +} + sub flag_count { my $self = shift; diff --git a/qa/t/2_test_flags.t b/qa/t/2_test_flags.t index bce5c40195..877b609a24 100644 --- a/qa/t/2_test_flags.t +++ b/qa/t/2_test_flags.t @@ -552,20 +552,33 @@ $sel->wait_for_page_to_load_ok(WAIT_TIME); $sel->title_is("Administer Flag Types"); foreach my $flagtype ( - [$flagtype1_id, "SeleniumBugFlag1Test"], - [$flagtype2_id, "SeleniumBugFlag2Test"], - [$flagtype3_id, "SeleniumBugFlag3Test"], - [$aflagtype1_id, "SeleniumAttachmentFlag1Test"], - [$aflagtype2_id, "SeleniumAttachmentFlag2Test"], - [$aflagtype3_id, "SeleniumAttachmentFlag3Test"] + [$flagtype1_id, "SeleniumBugFlag1Test", 1], + [$flagtype2_id, "SeleniumBugFlag2Test", 1], + [$flagtype3_id, "SeleniumBugFlag3Test", 1], + [$aflagtype1_id, "SeleniumAttachmentFlag1Test", 1], + [$aflagtype2_id, "SeleniumAttachmentFlag2Test", 1], + [$aflagtype3_id, "SeleniumAttachmentFlag3Test", 0] ) { - my $flag_id = $flagtype->[0]; - my $flag_name = $flagtype->[1]; + my ($flag_id, $flag_name, $used) = @$flagtype; $sel->click_ok( "//a[contains(\@href,'/editflagtypes.cgi?action=confirmdelete&id=$flag_id')]"); $sel->wait_for_page_to_load_ok(WAIT_TIME); $sel->title_is("Confirm Deletion of Flag Type '$flag_name'"); + + # A used type can only be deactivated: deleting it would delete its flag + # history. + if ($used) { + $sel->is_text_present_ok('so it cannot be deleted'); + ok(!$sel->is_element_present('link=Yes, delete'), 'No delete link'); + $sel->click_ok("link=deactivate it"); + $sel->wait_for_page_to_load_ok(WAIT_TIME); + $sel->title_is("Flag Type '$flag_name' Deactivated"); + my $msg = trim($sel->get_text("message")); + ok($msg eq "The flag type $flag_name has been deactivated.", + "Flag type $flag_name deactivated"); + next; + } $sel->click_ok("link=Yes, delete"); $sel->wait_for_page_to_load_ok(WAIT_TIME); $sel->title_is("Flag Type '$flag_name' Deleted"); diff --git a/qa/t/2_test_flags2.t b/qa/t/2_test_flags2.t index 616f01aa41..13dc2c56ca 100644 --- a/qa/t/2_test_flags2.t +++ b/qa/t/2_test_flags2.t @@ -339,18 +339,31 @@ $sel->wait_for_page_to_load_ok(WAIT_TIME); $sel->title_is("Administer Flag Types"); foreach my $flagtype ( - [$flagtype1_id, "selenium"], - [$flagtype2_id, "selenium"], - [$aflagtype1_id, "selenium_review"], - [$aflagtype2_id, "selenium_review"] + [$flagtype1_id, "selenium", 1], + [$flagtype2_id, "selenium", 1], + [$aflagtype1_id, "selenium_review", 0], + [$aflagtype2_id, "selenium_review", 1] ) { - my $flag_id = $flagtype->[0]; - my $flag_name = $flagtype->[1]; + my ($flag_id, $flag_name, $used) = @$flagtype; $sel->click_ok( "//a[contains(\@href,'/editflagtypes.cgi?action=confirmdelete&id=$flag_id')]"); $sel->wait_for_page_to_load_ok(WAIT_TIME); $sel->title_is("Confirm Deletion of Flag Type '$flag_name'"); + + # A used type can only be deactivated: deleting it would delete its flag + # history. + if ($used) { + $sel->is_text_present_ok('so it cannot be deleted'); + ok(!$sel->is_element_present('link=Yes, delete'), 'No delete link'); + $sel->click_ok("link=deactivate it"); + $sel->wait_for_page_to_load_ok(WAIT_TIME); + $sel->title_is("Flag Type '$flag_name' Deactivated"); + my $msg = trim($sel->get_text("message")); + ok($msg eq "The flag type $flag_name has been deactivated.", + "Flag type $flag_name deactivated"); + next; + } $sel->click_ok("link=Yes, delete"); $sel->wait_for_page_to_load_ok(WAIT_TIME); $sel->title_is("Flag Type '$flag_name' Deleted"); diff --git a/t/flag-type-delete.t b/t/flag-type-delete.t new file mode 100644 index 0000000000..e8c3cc3c48 --- /dev/null +++ b/t/flag-type-delete.t @@ -0,0 +1,74 @@ +#!/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 Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo.test'); +use Bugzilla::Test::MockDB; +use Bugzilla::Test::MockParams (mail_delivery_method => 'None'); +use Bugzilla::Test::Util qw(create_bug create_user); +use Bugzilla::Bug; +use Bugzilla::Constants; +use Bugzilla::FlagType; +use Bugzilla::User; +use Test2::V0; + +# flag_activity is the only record of flag changes, and its rows are deleted +# with their flag type: a type with some can't be deleted. + +create_user('automation@bmo.tld', '*'); +Bugzilla->set_user(Bugzilla::User->super_user); +Bugzilla->error_mode(ERROR_MODE_TEST); +my $dbh = Bugzilla->dbh; + +my $unused = Bugzilla::FlagType->create({ + name => 'unused', + description => 'A flag type that is never set', + target_type => 'bug', +}); +ok(lives { $unused->remove_from_db }, 'an unused flag type can be deleted'); +ok(!Bugzilla::FlagType->new($unused->id), 'it is gone'); + +my $setter = create_user('setter@bmo.test', '*'); +create_user('bob@bmo.test', '*'); +my $bug_id = create_bug( + short_desc => 'Flag type delete test bug', + comment => 'Bug for the flag type delete tests', + bug_type => 'defect', + assigned_to => 'nobody@mozilla.org', +)->id; +my $needinfo = Bugzilla::FlagType->new({name => 'needinfo'}); + +Bugzilla->set_user($setter); +my $bug = Bugzilla::Bug->new($bug_id); +$bug->set_flags([], + [{type_id => $needinfo->id, status => '?', requestee => 'bob@bmo.test'}]); +$bug->update(); + +# Even once its flags are gone, the type keeps its history. +$dbh->do('DELETE FROM flags WHERE type_id = ?', undef, $needinfo->id); + +Bugzilla->set_user(Bugzilla::User->super_user); +$needinfo = Bugzilla::FlagType->new($needinfo->id); +like(dies { $needinfo->remove_from_db }, qr/flag_type_has_activity/, + 'a flag type with flag_activity rows cannot be deleted'); +ok( + $dbh->selectrow_array( + 'SELECT COUNT(*) FROM flag_activity WHERE type_id = ?', undef, $needinfo->id + ), + 'its flag_activity rows are kept' +); + +done_testing; diff --git a/template/en/default/admin/flag-type/confirm-delete.html.tmpl b/template/en/default/admin/flag-type/confirm-delete.html.tmpl index 599c52a659..8f7f1617cc 100644 --- a/template/en/default/admin/flag-type/confirm-delete.html.tmpl +++ b/template/en/default/admin/flag-type/confirm-delete.html.tmpl @@ -27,6 +27,16 @@ doc_section = "flags-overview.html#flags-delete" %] +[% IF flag_type.has_activity %] +

+ The flag type [% flag_type.name FILTER html %] has been used, and deleting it + would also delete the history of its flags, so it cannot be deleted. You can + deactivate it + instead: the type and its flags will remain in the database but will not + appear in the [% terms.Bugzilla %] UI. +

+[% ELSE %]

[% IF flag_type.flag_count %] There are [% flag_type.flag_count %] flags of type [% flag_type.name FILTER html %]. @@ -59,5 +69,6 @@ +[% END %] [% PROCESS global/footer.html.tmpl %] diff --git a/template/en/default/global/user-error.html.tmpl b/template/en/default/global/user-error.html.tmpl index f7ea869ff1..2c9e297d2e 100644 --- a/template/en/default/global/user-error.html.tmpl +++ b/template/en/default/global/user-error.html.tmpl @@ -801,6 +801,12 @@ Sorry, but the flag type '[% flagtype.name FILTER html %]' also applies to some products you cannot see, and so you are not allowed to delete it. + [% ELSIF error == "flag_type_has_activity" %] + [% title = "Flag Type Deletion Not Allowed" %] + The flag type '[% flagtype.name FILTER html %]' has been used, and deleting + it would also delete the history of its flags. You can deactivate it + instead. + [% ELSIF error == "flag_type_cc_list_invalid" %] [% title = "Flag Type CC List Invalid" %] [% admindocslinks = {'administering/flags.md' => 'Administering Flags'} %] From 59fe15bffd685d9edf5c1f861ddba8958904862d Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Fri, 30 Oct 2026 00:58:48 +0100 Subject: [PATCH 19/50] Bug 2085398 - Count flag changes in owner idle time, changes to any field and the activity by product --- Bugzilla/Search.pm | 54 ++++++++++++++++++++++++------ extensions/UserProfile/lib/Util.pm | 13 +++++++ t/flag-activity-search.t | 37 ++++++++++++++++++++ t/flag-activity-user-stats.t | 19 +++++++++++ 4 files changed, 113 insertions(+), 10 deletions(-) diff --git a/Bugzilla/Search.pm b/Bugzilla/Search.pm index 8745ac825f..6272685504 100644 --- a/Bugzilla/Search.pm +++ b/Bugzilla/Search.pm @@ -3177,13 +3177,24 @@ sub _owner_idle_time_greater_less { extra => ["$act_table.bug_when > $quoted"] }; - push(@$joins, $comments_join, $activity_join); + my $flag_table = "flag_$table"; + my $flag_join = { + table => 'flag_activity', + as => $flag_table, + from => 'assigned_to', + to => 'who', + extra => ["$flag_table.flag_when > $quoted"] + }; + + push(@$joins, $comments_join, $activity_join, $flag_join); if ($operator =~ /greater/) { - $args->{term} = "$ld_table.who IS NULL AND $act_table.who IS NULL"; + $args->{term} + = "$ld_table.who IS NULL AND $act_table.who IS NULL AND $flag_table.who IS NULL"; } else { - $args->{term} = "($ld_table.who IS NOT NULL OR $act_table.who IS NOT NULL)"; + $args->{term} + = "($ld_table.who IS NOT NULL OR $act_table.who IS NOT NULL OR $flag_table.who IS NOT NULL)"; } } @@ -3671,6 +3682,15 @@ sub _changedbefore_changedafter { $args->{term} = "$table.bug_when IS NOT NULL"; $self->_changed_security_check($args, $join); push(@$joins, $join); + + # Flag changes are in flag_activity. + if ($field eq 'anything') { + my $fa = "flag_act_$chart_id"; + $args->{term} = "($args->{term} OR " + . $self->_flag_activity_exists($args, $fa, + "$fa.flag_when $sql_operator $sql_date") + . ')'; + } } sub _changedfrom_changedto { @@ -3729,6 +3749,13 @@ sub _changedby { $args->{term} = "$table.bug_when IS NOT NULL"; $self->_changed_security_check($args, $join); push(@$joins, $join); + + # Flag changes are in flag_activity. + if ($field eq 'anything') { + my $fa = "flag_act_$chart_id"; + $args->{term} = "($args->{term} OR " + . $self->_flag_activity_exists($args, $fa, "$fa.who = $user_id") . ')'; + } } sub _changed_security_check { @@ -3759,14 +3786,13 @@ sub _flagtypes_changed { my ($self, $args) = @_; my $dbh = Bugzilla->dbh; $self->_convert_everchanged($args) if $args->{operator} eq 'everchanged'; - my ($chart_id, $operator, $value, $bugs_table) - = @$args{qw(chart_id operator value bugs_table)}; + my ($chart_id, $operator, $value) = @$args{qw(chart_id operator value)}; $self->_do_operator_function($args); my $legacy_term = $args->{term}; - my $fa = "flag_act_$chart_id"; - my @where = ("$fa.bug_id = $bugs_table.bug_id"); + my $fa = "flag_act_$chart_id"; + my @where; if ($operator =~ /^changed(before|after)$/) { my $sql_operator = $1 eq 'before' ? '<=' : '>='; @@ -3791,13 +3817,21 @@ sub _flagtypes_changed { AND $match)"; } + $args->{term} = "(($legacy_term) OR " + . $self->_flag_activity_exists($args, $fa, @where) . ')'; +} + +# SQL true when the bug has a flag_activity row $fa matching @where. Changes to +# private attachments are hidden from non-insiders. +sub _flag_activity_exists { + my ($self, $args, $fa, @where) = @_; + unshift @where, "$fa.bug_id = $args->{bugs_table}.bug_id"; if (!$self->_user->is_insider) { push @where, "COALESCE((SELECT isprivate FROM attachments WHERE attach_id = $fa.attachment_id), 0) = 0"; } - - $args->{term} = "(($legacy_term) OR EXISTS (SELECT 1 FROM flag_activity $fa - WHERE " . join(' AND ', @where) . '))'; + return "EXISTS (SELECT 1 FROM flag_activity $fa WHERE " + . join(' AND ', @where) . ')'; } # SQL matching a flag_activity row against a history value like diff --git a/extensions/UserProfile/lib/Util.pm b/extensions/UserProfile/lib/Util.pm index d4e0dccac5..58dc33419b 100644 --- a/extensions/UserProfile/lib/Util.pm +++ b/extensions/UserProfile/lib/Util.pm @@ -17,6 +17,7 @@ our @EXPORT = qw( update_statistics_by_user last_user_activity ); use Bugzilla; +use Bugzilla::FlagActivity; sub update_statistics_by_user { my ($user_id) = @_; @@ -305,6 +306,18 @@ sub _activity_by_product { EOF map { $products{$_->{product}} += $_->{count} } @$rows; + # flag changes, skipping the older ones that are also in bugs_activity + my $newer = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('fa'); + $rows = $dbh->selectall_arrayref(< {}}, $user_id); + SELECT products.name AS product, count(*) AS count + FROM flag_activity fa + INNER JOIN bugs ON bugs.bug_id = fa.bug_id + INNER JOIN products ON products.id = bugs.product_id + WHERE fa.who = ? AND $newer + GROUP BY bugs.product_id +EOF + map { $products{$_->{product}} += $_->{count} } @$rows; + # comments $rows = $dbh->selectall_arrayref(< {}}, $user_id); SELECT products.name AS product, count(*) AS count diff --git a/t/flag-activity-search.t b/t/flag-activity-search.t index aad72b4360..b68a604419 100644 --- a/t/flag-activity-search.t +++ b/t/flag-activity-search.t @@ -92,4 +92,41 @@ is(search(changedby => 'bob@bmo.test'), [$legacy_bug], 'changedby bugs_activity'); is(search(everchanged => ''), [$legacy_bug, $current_bug], 'everchanged'); +# Changes to any field include flag changes. +sub search_anything { + my ($operator, $value) = @_; + my $search = Bugzilla::Search->new( + fields => ['bug_id'], + params => {f1 => 'anything', o1 => $operator, v1 => $value}, + ); + return [sort { $a <=> $b } map { $_->[0] } @{$search->data}]; +} +is(search_anything(changedby => 'alice@bmo.test'), [$current_bug], + 'anything changedby flag_activity'); +is(search_anything(changedby => 'bob@bmo.test'), [$legacy_bug], + 'anything changedby bugs_activity'); +is(search_anything(changedafter => '2025-01-01'), [$current_bug], + 'anything changedafter'); + +# An assignee whose only recent changes are flag changes isn't idle. +my $owned_bug = create_bug( + short_desc => 'owned', + comment => 'Flag search test bug', + bug_type => 'defect', + assigned_to => 'alice@bmo.test', +)->id; +sub idle { + my ($operator) = @_; + my $search = Bugzilla::Search->new( + fields => ['bug_id'], + params => { + f1 => 'owner_idle_time', o1 => $operator, v1 => '2025-12-01', + f2 => 'assigned_to', o2 => 'equals', v2 => 'alice@bmo.test', + }, + ); + return [map { $_->[0] } @{$search->data}]; +} +is(idle('lessthan'), [$owned_bug], 'owner_idle_time lessthan'); +is(idle('greaterthan'), [], 'owner_idle_time greaterthan'); + done_testing; diff --git a/t/flag-activity-user-stats.t b/t/flag-activity-user-stats.t index bffbc4d35c..34f1796e3b 100644 --- a/t/flag-activity-user-stats.t +++ b/t/flag-activity-user-stats.t @@ -85,4 +85,23 @@ my ($touched) = $dbh->selectrow_array( undef, $human->id); is($touched, 1, 'a flag change counts as touching the bug'); +sub product_count { + return scalar $dbh->selectrow_array( + 'SELECT SUM(count) FROM profiles_statistics_products WHERE user_id = ?', + undef, $human->id); +} +is(product_count(), 1, 'a flag change counts in the activity by product'); + +# An older change is in both tables: count it once. +my ($fieldid) + = $dbh->selectrow_array( + "SELECT id FROM fielddefs WHERE name = 'flagtypes.name'"); +$dbh->do( + 'INSERT INTO bugs_activity (bug_id, who, bug_when, fieldid, removed, added) + VALUES (?, ?, ?, ?, ?, ?)', undef, $bug_id, $human->id, + '2099-01-01 10:00:00', $fieldid, '', 'needinfo?(bob@bmo.test)' +); +update_statistics_by_user($human->id); +is(product_count(), 1, 'a change in both tables is counted once'); + done_testing; From c560c6502de3fa51eefbd226d65a8dbb2c15ddc0 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Fri, 30 Oct 2026 02:30:05 +0100 Subject: [PATCH 20/50] Bug 2085398 - Add a starting flag_activity row for flags that have none --- Bugzilla/Install/DB.pm | 34 ++++++++++++ t/flag-activity-start-rows.t | 104 +++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 t/flag-activity-start-rows.t diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index eb53ac7cbc..fd21893082 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -851,6 +851,11 @@ sub update_table_definitions { # Bug 1806896 - xavier.lhour@gmail.com _migrate_flag_state_activity(); + # Bug 2085398 - xavier.lhour@gmail.com + # After _migrate_flag_state_activity, so that flags with copied rows are + # skipped. + _add_flag_activity_start_rows(); + # Bug 2060356 - dkl@mozilla.com _remove_duo_recovery_codes(); @@ -4545,6 +4550,35 @@ sub _migrate_flag_state_activity { $dbh->bz_drop_table('flag_state_activity'); } +# Until 2026-07-29, flag_activity only logged review, feedback and needinfo +# flags. Other flags last changed before then have no row there, so their next +# change would show no previous value in history and bugmail. Give each of them +# a row with its current state, dated at its last change: history and bugmail +# don't show it, as it isn't newer than that change in bugs_activity. +sub _add_flag_activity_start_rows { + my $dbh = Bugzilla->dbh; + my ($max_id) = $dbh->selectrow_array('SELECT MAX(id) FROM flags'); + return unless $max_id; + + my $batch_size = 10_000; + my $added = 0; + for (my $from = 0; $from < $max_id; $from += $batch_size) { + $added += $dbh->do( + 'INSERT INTO flag_activity + (flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, + attachment_id, status) + SELECT modification_date, type_id, id, setter_id, setter_id, + requestee_id, bug_id, attach_id, status + FROM flags + WHERE id > ? AND id <= ? + AND NOT EXISTS (SELECT 1 FROM flag_activity + WHERE flag_activity.flag_id = flags.id)', + undef, $from, $from + $batch_size + ); + } + print "Added a flag_activity row for $added flags.\n" if $added > 0; +} + sub _add_flag_activity_who { my $dbh = Bugzilla->dbh; diff --git a/t/flag-activity-start-rows.t b/t/flag-activity-start-rows.t new file mode 100644 index 0000000000..f268f589d3 --- /dev/null +++ b/t/flag-activity-start-rows.t @@ -0,0 +1,104 @@ +#!/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 Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo.test'); +use Bugzilla::Test::MockDB; +use Bugzilla::Test::MockParams (mail_delivery_method => 'None'); +use Bugzilla::Test::Util qw(create_bug create_user); +use Bugzilla::Bug; +use Bugzilla::Install::DB; +use Bugzilla::User; +use Test2::V0; + +# A flag last changed before flag_activity logged its type gets a starting +# row, so that its next change shows the previous value. + +create_user('automation@bmo.tld', '*'); +Bugzilla->set_user(Bugzilla::User->super_user); +my $dbh = Bugzilla->dbh; + +my $setter = create_user('setter@bmo.test', '*'); +create_user('bob@bmo.test', '*'); +my ($needinfo_id) + = $dbh->selectrow_array("SELECT id FROM flagtypes WHERE name = 'needinfo'"); +my ($fieldid) + = $dbh->selectrow_array( + "SELECT id FROM fielddefs WHERE name = 'flagtypes.name'"); + +my $bug_id = create_bug( + short_desc => 'Flag activity start rows test bug', + comment => 'Bug for the flag activity start rows tests', + bug_type => 'defect', + assigned_to => 'nobody@mozilla.org', +)->id; + +# Future dates, so that the changes come after the bug creation. +Bugzilla->set_user($setter); +my $bug = Bugzilla::Bug->new($bug_id); +$bug->set_flags([], + [{type_id => $needinfo_id, status => '?', requestee => 'bob@bmo.test'}]); +$bug->update('2099-01-01 10:00:00'); +my ($flag) = @{Bugzilla::Bug->new($bug_id)->flags}; + +# As if it had been set before flag_activity logged it. +$dbh->do('DELETE FROM flag_activity WHERE bug_id = ?', undef, $bug_id); +$dbh->do( + 'INSERT INTO bugs_activity (bug_id, who, bug_when, fieldid, removed, added) + VALUES (?, ?, ?, ?, ?, ?)', undef, $bug_id, $setter->id, + '2099-01-01 10:00:00', $fieldid, '', 'needinfo?(bob@bmo.test)' +); + +Bugzilla::Install::DB::_add_flag_activity_start_rows() for 1 .. 2; +is( + scalar $dbh->selectrow_array( + 'SELECT COUNT(*) FROM flag_activity WHERE flag_id = ?', undef, $flag->id + ), + 1, + 'one starting row, however many times checksetup runs' +); + +sub flag_history { + my ($activity) = Bugzilla::Bug::GetBugActivity($bug_id); + return [ + map { + my $when = $_->{when}; + map {"$when: $_->{removed} -> $_->{added}"} + grep { $_->{fieldname} eq 'flagtypes.name' } @{$_->{changes}} + } @$activity + ]; +} + +is( + flag_history(), + ['2099-01-01 10:00:00: -> needinfo?(bob@bmo.test)'], + 'the starting row is not shown in the history' +); + +$bug = Bugzilla::Bug->new($bug_id); +$bug->set_flags([{id => $flag->id, status => 'X'}], []); +$bug->update('2099-01-01 11:00:00'); + +is( + flag_history(), + [ + '2099-01-01 10:00:00: -> needinfo?(bob@bmo.test)', + '2099-01-01 11:00:00: needinfo?(bob@bmo.test) -> ' + ], + 'the next change shows the previous value' +); + +done_testing; From aca6a3b830e4fd8bbf0094160ebf6c52fbc2b879 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Fri, 30 Oct 2026 02:34:01 +0100 Subject: [PATCH 21/50] Bug 2085398 - Skip flag changes that change nothing in bugmail --- Bugzilla/BugMail.pm | 6 ++++++ t/flag-activity-history.t | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/Bugzilla/BugMail.pm b/Bugzilla/BugMail.pm index 6096995816..af0b66e193 100644 --- a/Bugzilla/BugMail.pm +++ b/Bugzilla/BugMail.pm @@ -1101,6 +1101,12 @@ sub _get_diffs { ORDER BY bug_when, field_desc, activity_id", {Slice => {}}, @args, $bug->id, ($bug->lastdiffed ? ($bug->lastdiffed, $end) : ()) ); + + # As in history, skip a request that left a flag as it was, such as a flag + # created and removed at once. + @$diffs + = grep { $_->{field_name} ne 'flagtypes.name' || $_->{old} ne $_->{new} } + @$diffs; my $referenced_bugs = []; foreach my $diff (@$diffs) { diff --git a/t/flag-activity-history.t b/t/flag-activity-history.t index a69265d4da..5ff5adf097 100644 --- a/t/flag-activity-history.t +++ b/t/flag-activity-history.t @@ -129,4 +129,16 @@ $expected = [ is($history, $expected, 'history: one net change per request'); is($mail, $expected, 'bugmail: one net change per request'); +# A flag created and removed in one request changes nothing: neither history +# nor bugmail show it. +my $no_change_bug = new_bug('no change'); +add_flag_activity($no_change_bug, '2026-03-01 10:00:00', 44, $alice, $bob->id, + '?'); +add_flag_activity($no_change_bug, '2026-03-01 10:00:00', 44, $alice, undef, + 'X'); + +($history, $mail) = flag_changes($no_change_bug); +is($history, [], 'history: no change for a flag created and removed'); +is($mail, [], 'bugmail: no change for a flag created and removed'); + done_testing; From f6a636a41a30c3d0648e0b326a5ff843a880c577 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Fri, 30 Oct 2026 02:38:24 +0100 Subject: [PATCH 22/50] Bug 2085398 - Match flag changes in changed from and changed to searches on any field --- Bugzilla/Search.pm | 39 ++++++++++++++++++++++++++++----------- t/flag-activity-search.t | 4 ++++ 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/Bugzilla/Search.pm b/Bugzilla/Search.pm index 6272685504..8d8dd2d891 100644 --- a/Bugzilla/Search.pm +++ b/Bugzilla/Search.pm @@ -3718,6 +3718,16 @@ sub _changedfrom_changedto { $args->{term} = "$table.bug_when IS NOT NULL"; $self->_changed_security_check($args, $join); push(@$joins, $join); + + # Flag changes are in flag_activity. + if ($field eq 'anything') { + my $fa = "flag_act_$chart_id"; + my $match + = _flag_state_changed_sql($chart_id, $fa, $operator, $args->{value}); + $args->{term} = "($args->{term} OR " + . $self->_flag_activity_exists($args, $fa, $match) . ')' + if $match; + } } sub _changedby { @@ -3803,19 +3813,11 @@ sub _flagtypes_changed { my $user_id = $value =~ /^\d+$/ ? $value : $self->_get_user_id($value); push @where, "$fa.who = $user_id"; } - elsif ($operator eq 'changedto') { - my $match = _flag_state_match($fa, $value) or return; + else { # changedto, changedfrom + my $match = _flag_state_changed_sql($chart_id, $fa, $operator, $value) + or return; push @where, $match; } - else { # changedfrom: the state before this change - my $prev = "flag_prev_$chart_id"; - my $match = _flag_state_match($prev, $value) or return; - push @where, "EXISTS ( - SELECT 1 FROM flag_activity $prev - WHERE $prev.id = (SELECT MAX(p2.id) FROM flag_activity p2 - WHERE p2.flag_id = $fa.flag_id AND p2.id < $fa.id) - AND $match)"; - } $args->{term} = "(($legacy_term) OR " . $self->_flag_activity_exists($args, $fa, @where) . ')'; @@ -3837,6 +3839,21 @@ sub _flag_activity_exists { # SQL matching a flag_activity row against a history value like # "review+" or "review?(user@example.com)". Returns nothing if the value # isn't a flag state. +# SQL condition on flag_activity row $fa for changedto or changedfrom $value +# (the state before this change), or undef if $value isn't a flag state. +sub _flag_state_changed_sql { + my ($chart_id, $fa, $operator, $value) = @_; + return _flag_state_match($fa, $value) if $operator eq 'changedto'; + + my $prev = "flag_prev_$chart_id"; + my $match = _flag_state_match($prev, $value) or return undef; + return "EXISTS ( + SELECT 1 FROM flag_activity $prev + WHERE $prev.id = (SELECT MAX(p2.id) FROM flag_activity p2 + WHERE p2.flag_id = $fa.flag_id AND p2.id < $fa.id) + AND $match)"; +} + sub _flag_state_match { my ($table, $value) = @_; my $dbh = Bugzilla->dbh; diff --git a/t/flag-activity-search.t b/t/flag-activity-search.t index b68a604419..5192df33c3 100644 --- a/t/flag-activity-search.t +++ b/t/flag-activity-search.t @@ -107,6 +107,10 @@ is(search_anything(changedby => 'bob@bmo.test'), [$legacy_bug], 'anything changedby bugs_activity'); is(search_anything(changedafter => '2025-01-01'), [$current_bug], 'anything changedafter'); +is(search_anything(changedto => 'review+'), [$legacy_bug, $current_bug], + 'anything changedto'); +is(search_anything(changedfrom => 'review?(bob@bmo.test)'), [$current_bug], + 'anything changedfrom'); # An assignee whose only recent changes are flag changes isn't idle. my $owned_bug = create_bug( From 98f9ae84d82129cbed54b9b6efb36bd1fbc83036 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Fri, 30 Oct 2026 02:50:15 +0100 Subject: [PATCH 23/50] Bug 2085398 - Check owner idle time with EXISTS instead of joins --- Bugzilla/Search.pm | 54 ++++++++++++++-------------------------------- 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/Bugzilla/Search.pm b/Bugzilla/Search.pm index 8d8dd2d891..fb1ee7c45a 100644 --- a/Bugzilla/Search.pm +++ b/Bugzilla/Search.pm @@ -3153,49 +3153,27 @@ sub _deadline { sub _owner_idle_time_greater_less { my ($self, $args) = @_; - my ($chart_id, $joins, $value, $operator) - = @$args{qw(chart_id joins value operator)}; + my ($chart_id, $value, $operator) = @$args{qw(chart_id value operator)}; my $dbh = Bugzilla->dbh; my $table = "idle_$chart_id"; my $quoted = $dbh->quote(SqlifyDate($value)); + my $owner = "$args->{bugs_table}.assigned_to"; + + # The assignee commented, changed a bug or changed a flag since $value. One + # EXISTS each, as joins would multiply their rows. + my $active = join( + ' OR ', + map { + my ($activity_table, $when) = @$_; + my $alias = "${activity_table}_$table"; + "EXISTS (SELECT 1 FROM $activity_table $alias + WHERE $alias.who = $owner AND $alias.$when > $quoted)" + } (['longdescs', 'bug_when'], ['bugs_activity', 'bug_when'], + ['flag_activity', 'flag_when']) + ); - my $ld_table = "comment_$table"; - my $act_table = "activity_$table"; - my $comments_join = { - table => 'longdescs', - as => $ld_table, - from => 'assigned_to', - to => 'who', - extra => ["$ld_table.bug_when > $quoted"], - }; - my $activity_join = { - table => 'bugs_activity', - as => $act_table, - from => 'assigned_to', - to => 'who', - extra => ["$act_table.bug_when > $quoted"] - }; - - my $flag_table = "flag_$table"; - my $flag_join = { - table => 'flag_activity', - as => $flag_table, - from => 'assigned_to', - to => 'who', - extra => ["$flag_table.flag_when > $quoted"] - }; - - push(@$joins, $comments_join, $activity_join, $flag_join); - - if ($operator =~ /greater/) { - $args->{term} - = "$ld_table.who IS NULL AND $act_table.who IS NULL AND $flag_table.who IS NULL"; - } - else { - $args->{term} - = "($ld_table.who IS NOT NULL OR $act_table.who IS NOT NULL OR $flag_table.who IS NOT NULL)"; - } + $args->{term} = $operator =~ /greater/ ? "NOT ($active)" : "($active)"; } sub _multiselect_negative { From 873f5835eec3f1c2049a7bf0081cbd2a2541c19b Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Mon, 2 Nov 2026 16:11:00 +0100 Subject: [PATCH 24/50] Bug 2085398 - Date start rows at creation for flags without a modification date --- Bugzilla/Install/DB.pm | 4 ++-- t/flag-activity-start-rows.t | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index fd21893082..b599bf38b8 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -4567,8 +4567,8 @@ sub _add_flag_activity_start_rows { 'INSERT INTO flag_activity (flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, attachment_id, status) - SELECT modification_date, type_id, id, setter_id, setter_id, - requestee_id, bug_id, attach_id, status + SELECT COALESCE(modification_date, creation_date), type_id, id, + setter_id, setter_id, requestee_id, bug_id, attach_id, status FROM flags WHERE id > ? AND id <= ? AND NOT EXISTS (SELECT 1 FROM flag_activity diff --git a/t/flag-activity-start-rows.t b/t/flag-activity-start-rows.t index f268f589d3..c87cdcd2d2 100644 --- a/t/flag-activity-start-rows.t +++ b/t/flag-activity-start-rows.t @@ -101,4 +101,25 @@ is( 'the next change shows the previous value' ); +# flags.modification_date can be NULL, flag_activity.flag_when can't. +$bug = Bugzilla::Bug->new($bug_id); +$bug->set_flags([], + [{type_id => $needinfo_id, status => '?', requestee => 'bob@bmo.test'}]); +$bug->update('2099-01-01 12:00:00'); +my ($undated) = grep { $_->status eq '?' } @{Bugzilla::Bug->new($bug_id)->flags}; +$dbh->do('DELETE FROM flag_activity WHERE flag_id = ?', undef, $undated->id); +$dbh->do('UPDATE flags SET modification_date = NULL WHERE id = ?', + undef, $undated->id); + +Bugzilla::Install::DB::_add_flag_activity_start_rows(); +is( + scalar $dbh->selectrow_array( + 'SELECT flag_when FROM flag_activity WHERE flag_id = ?', undef, $undated->id + ), + scalar $dbh->selectrow_array( + 'SELECT creation_date FROM flags WHERE id = ?', undef, $undated->id + ), + 'a flag without a modification date is dated at its creation' +); + done_testing; From ef867b0fc435e3c742ff3d395b4d154e92f23e0d Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Mon, 2 Nov 2026 16:11:24 +0100 Subject: [PATCH 25/50] Bug 2085398 - Index bugs_activity for the last flag change of a bug --- Bugzilla/DB/Schema.pm | 13 +++++++------ Bugzilla/Install/DB.pm | 4 ++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Bugzilla/DB/Schema.pm b/Bugzilla/DB/Schema.pm index 8c7656f78c..101b2c022e 100644 --- a/Bugzilla/DB/Schema.pm +++ b/Bugzilla/DB/Schema.pm @@ -361,12 +361,13 @@ use constant ABSTRACT_SCHEMA => { }, ], INDEXES => [ - bugs_activity_bug_id_idx => ['bug_id'], - bugs_activity_who_idx => ['who'], - bugs_activity_bug_when_idx => ['bug_when'], - bugs_activity_fieldid_idx => ['fieldid'], - bugs_activity_added_idx => ['added'], - bugs_activity_removed_idx => ['removed'], + bugs_activity_bug_id_idx => ['bug_id'], + bugs_activity_bug_field_when_idx => ['bug_id', 'fieldid', 'bug_when'], + bugs_activity_who_idx => ['who'], + bugs_activity_bug_when_idx => ['bug_when'], + bugs_activity_fieldid_idx => ['fieldid'], + bugs_activity_added_idx => ['added'], + bugs_activity_removed_idx => ['removed'], ], }, diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index b599bf38b8..669fc2e1ec 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -4577,6 +4577,10 @@ sub _add_flag_activity_start_rows { ); } print "Added a flag_activity row for $added flags.\n" if $added > 0; + + # For FlagActivity->newer_than_bugs_activity_sql. + $dbh->bz_add_index('bugs_activity', 'bugs_activity_bug_field_when_idx', + [qw(bug_id fieldid bug_when)]); } sub _add_flag_activity_who { From ae19b88b981a802ff4387520cae8256980291eae Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Mon, 2 Nov 2026 16:11:35 +0100 Subject: [PATCH 26/50] Bug 2085398 - Add the flag_activity start rows only once --- Bugzilla/Install/DB.pm | 6 +++++- t/flag-activity-start-rows.t | 21 +++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index 669fc2e1ec..cfa48ad627 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -4557,8 +4557,12 @@ sub _migrate_flag_state_activity { # don't show it, as it isn't newer than that change in bugs_activity. sub _add_flag_activity_start_rows { my $dbh = Bugzilla->dbh; + + # The index is added at the end, so this runs once, or again if it died. + return if $dbh->bz_index_info('bugs_activity', 'bugs_activity_bug_field_when_idx'); + my ($max_id) = $dbh->selectrow_array('SELECT MAX(id) FROM flags'); - return unless $max_id; + $max_id //= 0; my $batch_size = 10_000; my $added = 0; diff --git a/t/flag-activity-start-rows.t b/t/flag-activity-start-rows.t index c87cdcd2d2..414a2d41a3 100644 --- a/t/flag-activity-start-rows.t +++ b/t/flag-activity-start-rows.t @@ -62,7 +62,13 @@ $dbh->do( '2099-01-01 10:00:00', $fieldid, '', 'needinfo?(bob@bmo.test)' ); -Bugzilla::Install::DB::_add_flag_activity_start_rows() for 1 .. 2; +# The index is in the schema of a new database; older ones don't have it yet. +sub add_start_rows { + $dbh->bz_drop_index('bugs_activity', 'bugs_activity_bug_field_when_idx'); + Bugzilla::Install::DB::_add_flag_activity_start_rows(); +} + +add_start_rows() for 1 .. 2; is( scalar $dbh->selectrow_array( 'SELECT COUNT(*) FROM flag_activity WHERE flag_id = ?', undef, $flag->id @@ -111,7 +117,7 @@ $dbh->do('DELETE FROM flag_activity WHERE flag_id = ?', undef, $undated->id); $dbh->do('UPDATE flags SET modification_date = NULL WHERE id = ?', undef, $undated->id); -Bugzilla::Install::DB::_add_flag_activity_start_rows(); +add_start_rows(); is( scalar $dbh->selectrow_array( 'SELECT flag_when FROM flag_activity WHERE flag_id = ?', undef, $undated->id @@ -122,4 +128,15 @@ is( 'a flag without a modification date is dated at its creation' ); +# Once the index is there, checksetup doesn't walk the flags again. +$dbh->do('DELETE FROM flag_activity WHERE flag_id = ?', undef, $undated->id); +Bugzilla::Install::DB::_add_flag_activity_start_rows(); +is( + scalar $dbh->selectrow_array( + 'SELECT COUNT(*) FROM flag_activity WHERE flag_id = ?', undef, $undated->id + ), + 0, + 'the start rows are only added once' +); + done_testing; From 4071ba9054619fbc5dbfff17aef17c4db45bf4c6 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Mon, 2 Nov 2026 16:11:51 +0100 Subject: [PATCH 27/50] Bug 2085398 - Skip empty flag changes in the user activity report --- extensions/BMO/lib/Reports/UserActivity.pm | 3 +++ 1 file changed, 3 insertions(+) diff --git a/extensions/BMO/lib/Reports/UserActivity.pm b/extensions/BMO/lib/Reports/UserActivity.pm index 75e012dd64..fa0935cc72 100644 --- a/extensions/BMO/lib/Reports/UserActivity.pm +++ b/extensions/BMO/lib/Reports/UserActivity.pm @@ -294,6 +294,9 @@ sub report { next unless Bugzilla->user->can_see_bug($bugid); + # A flag created and cleared in one request changed nothing. + next if $fieldname eq 'flagtypes.name' && $removed eq $added; + # check if the user should see this field's activity if ( $fieldname eq 'remaining_time' || $fieldname eq 'estimated_time' From 66203f8ce791d27663025f49e87f831cd06fafd6 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Mon, 2 Nov 2026 22:34:00 +0100 Subject: [PATCH 28/50] Bug 2085398 - Refuse to delete a user who is a requestee in flag_activity, with a selenium test --- editusers.cgi | 4 ++ qa/t/4_test_user_delete_requestee.t | 53 +++++++++++++++++++ .../admin/users/confirm-delete.html.tmpl | 11 +++- 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 qa/t/4_test_user_delete_requestee.t diff --git a/editusers.cgi b/editusers.cgi index 4bd609106e..9761a92917 100755 --- a/editusers.cgi +++ b/editusers.cgi @@ -397,6 +397,10 @@ elsif ($action eq 'del') { + (SELECT COUNT(*) FROM flag_activity WHERE who = ? OR setter_id = ?)', undef, ($otherUserID) x 3 ); + # The foreign key forbids deleting a requestee kept in the flag history. + $vars->{'flag_activity_requestee'} = $dbh->selectrow_array( + 'SELECT COUNT(*) FROM flag_activity WHERE requestee_id = ?', + undef, $otherUserID); $vars->{'component_cc'} = $dbh->selectrow_array('SELECT COUNT(*) FROM component_cc WHERE user_id = ?', undef, $otherUserID); diff --git a/qa/t/4_test_user_delete_requestee.t b/qa/t/4_test_user_delete_requestee.t new file mode 100644 index 0000000000..95f21ad89a --- /dev/null +++ b/qa/t/4_test_user_delete_requestee.t @@ -0,0 +1,53 @@ +# 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 lib qw(lib ../../lib ../../local/lib/perl5); + +use Bugzilla; + +use Test::More "no_plan"; + +use QA::Util; + +my ($sel, $config) = get_selenium(); + +log_in($sel, $config, 'admin'); +set_parameters($sel, + {"Administrative Policies" => {"allowuserdeletion-on" => undef}}); + +# flag_activity.requestee_id keeps a user from being deleted, so the +# confirmation page must not offer it. +my $dbh = Bugzilla->dbh; +$dbh->do("INSERT INTO profiles (login_name, realname, cryptpassword) + VALUES ('requestee\@selenium.bugzilla.org', 'Requestee', '*')"); +my $user_id = $dbh->bz_last_key('profiles', 'userid'); +my ($admin_id) = $dbh->selectrow_array( + 'SELECT userid FROM profiles WHERE login_name = ?', + undef, $config->{admin_user_login}); +my ($bug_id) = $dbh->selectrow_array('SELECT MIN(bug_id) FROM bugs'); +my ($type_id) = $dbh->selectrow_array('SELECT MIN(id) FROM flagtypes'); +$dbh->do( + "INSERT INTO flag_activity + (flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, status) + VALUES (NOW(), ?, 0, ?, ?, ?, ?, 'X')", undef, $type_id, $admin_id, $admin_id, + $user_id, $bug_id +); + +# The page has its verdict only if there is also something safe to report. +$dbh->do('INSERT INTO cc (bug_id, who) VALUES (?, ?)', undef, $bug_id, $user_id); + +$sel->open_ok("/editusers.cgi?action=del&userid=$user_id"); +$sel->is_text_present_ok('named in the flag history'); +$sel->is_text_present_ok('You cannot delete this user account'); + +$dbh->do('DELETE FROM cc WHERE who = ?', undef, $user_id); +$dbh->do('DELETE FROM flag_activity WHERE requestee_id = ?', undef, $user_id); +$dbh->do('DELETE FROM profiles WHERE userid = ?', undef, $user_id); + +logout($sel); diff --git a/template/en/default/admin/users/confirm-delete.html.tmpl b/template/en/default/admin/users/confirm-delete.html.tmpl index 203edfc838..445671be1f 100644 --- a/template/en/default/admin/users/confirm-delete.html.tmpl +++ b/template/en/default/admin/users/confirm-delete.html.tmpl @@ -26,6 +26,8 @@ # entries on # cc number of bugs the viewed user is cc list member # of + # flag_activity_requestee: number of flag history rows naming the viewed user + # as requestee # flags.requestee: number of flags the viewed user is being asked for # flags.setter: number of flags the viewed user has set # longdescs: number of bug comments the viewed user has written @@ -102,7 +104,8 @@ [% ELSE %] [% accept_deletion = 1 %] - [% IF attachments || reporter || bugs_activity || flags.setter || longdescs || profiles_activity %] + [% IF attachments || reporter || bugs_activity || flag_activity_requestee || + flags.setter || longdescs || profiles_activity %]

The following deletions are unsafe and would generate referential integrity inconsistencies!

@@ -166,6 +169,12 @@ not showing up in [% terms.bug %] activity logs any more. [% END %] + [% IF flag_activity_requestee %] +
  • + [% otheruser.login FILTER html %] has been asked to set flags. + The database refuses to delete a user named in the flag history. +
  • + [% END %] [% IF flags.setter %]
  • [% otheruser.login FILTER html %] has From c5f3d23b442e48878d4f662b63b37bfb71a154df Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Mon, 2 Nov 2026 22:53:43 +0100 Subject: [PATCH 29/50] Bug 2085398 - Leave flags set at creation out of the new bug mail, with a test --- Bugzilla/BugMail.pm | 4 ++++ t/bmo/bugmail-flag-events.t | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/Bugzilla/BugMail.pm b/Bugzilla/BugMail.pm index af0b66e193..05c34d21d0 100644 --- a/Bugzilla/BugMail.pm +++ b/Bugzilla/BugMail.pm @@ -121,6 +121,10 @@ sub Send { } else { my ($diffs, $referenced) = _get_diffs($bug, $end, \%user_cache); + + # The mail of a new bug lists its fields, not changes: flags set at + # creation are no change. + @$diffs = grep { $_->{field_name} ne 'flagtypes.name' } @$diffs if !$start; push(@diffs, @$diffs); push(@referenced_bugs, @$referenced); } diff --git a/t/bmo/bugmail-flag-events.t b/t/bmo/bugmail-flag-events.t index 5aa729d259..c3169003fa 100644 --- a/t/bmo/bugmail-flag-events.t +++ b/t/bmo/bugmail-flag-events.t @@ -293,6 +293,21 @@ sub sent_mails { unlike($raw[0], qr/private reasons/, 'but not the private ones'); } +# The mail of a new bug doesn't report the flags set at creation as changes. +# The assignee gets the whole mail: the flag recipients have no diffs anyway. +{ + my $bug = new_bug(assigned_to => $stranger->login); + add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', '?', $requester, + $requestee); + $dbh->do('UPDATE bugs SET lastdiffed = NULL WHERE bug_id = ?', undef, $bug->id); + + my ($mail) = grep { $_->{to_user}->id == $stranger->id } + sent_mails(Bugzilla::Bug->new($bug->id), $requester); + ok(@{$mail->{diffs}}, 'the assignee gets the fields of the new bug'); + ok(!grep({ $_->{field_name} eq 'flagtypes.name' } @{$mail->{diffs}}), + 'but no flag change'); +} + # A flag type edit queues one bugmail job per bug it touched. { my @jobs; From f290a4623b987eaca0c45f52320d795fcd0ab6b6 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Mon, 2 Nov 2026 23:10:42 +0100 Subject: [PATCH 30/50] Bug 2085398 - Finish an interrupted flag_activity.who migration, with a test --- Bugzilla/Install/DB.pm | 9 ++++++--- t/flag-activity-who.t | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index cfa48ad627..a0d872f995 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -4590,13 +4590,16 @@ sub _add_flag_activity_start_rows { sub _add_flag_activity_who { my $dbh = Bugzilla->dbh; - if (!$dbh->bz_column_info('flag_activity', 'who')) { + # Still nullable if it died before the end: pick up from there. + my $who = $dbh->bz_column_info('flag_activity', 'who'); + if (!$who || !$who->{NOTNULL}) { # Existing rows don't record who made the change; the setter is the best # value available. $dbh->bz_add_column('flag_activity', 'who', - {TYPE => 'INT3', REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}}); - $dbh->do('UPDATE flag_activity SET who = setter_id'); + {TYPE => 'INT3', REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}}) + if !$who; + $dbh->do('UPDATE flag_activity SET who = setter_id WHERE who IS NULL'); $dbh->bz_alter_column('flag_activity', 'who', { TYPE => 'INT3', diff --git a/t/flag-activity-who.t b/t/flag-activity-who.t index ac11cbb34e..3ea86f789c 100644 --- a/t/flag-activity-who.t +++ b/t/flag-activity-who.t @@ -20,6 +20,7 @@ use Bugzilla::Test::MockDB; use Bugzilla::Test::MockParams (mail_delivery_method => 'None'); use Bugzilla::Test::Util qw(create_bug create_user); use Bugzilla::Bug; +use Bugzilla::Install::DB; use Bugzilla::User; use Test2::V0; @@ -78,4 +79,20 @@ my ($redirect) = grep { } @$operations; is($redirect->{who}, 'redirector@bmo.test', 'history shows the redirector'); +# A checksetup that died after adding the column left it nullable: the next +# one finishes the job. +$dbh->bz_alter_column('flag_activity', 'who', + {TYPE => 'INT3', REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}}); +$dbh->do('UPDATE flag_activity SET who = NULL WHERE flag_id = ?', + undef, $flag->id); +Bugzilla::Install::DB::_add_flag_activity_who(); +is( + scalar $dbh->selectrow_array( + 'SELECT COUNT(*) FROM flag_activity WHERE who IS NULL'), + 0, + 'an interrupted migration fills the rows left' +); +ok($dbh->bz_column_info('flag_activity', 'who')->{NOTNULL}, + 'and makes the column NOT NULL'); + done_testing; From 2635ec36e6409e6d03579d28b6ba119d81073a91 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Mon, 2 Nov 2026 23:22:15 +0100 Subject: [PATCH 31/50] Bug 2085398 - Match one net change per request in the flag changedfrom and changedto searches, with tests --- Bugzilla/Search.pm | 19 ++++++++++++------- t/flag-activity-search.t | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/Bugzilla/Search.pm b/Bugzilla/Search.pm index fb1ee7c45a..562a083126 100644 --- a/Bugzilla/Search.pm +++ b/Bugzilla/Search.pm @@ -20,6 +20,7 @@ use base qw(Exporter); use Bugzilla::Constants; use Bugzilla::Error; use Bugzilla::Field; +use Bugzilla::FlagActivity; use Bugzilla::Group; use Bugzilla::Keyword; use Bugzilla::Logging; @@ -3821,15 +3822,19 @@ sub _flag_activity_exists { # (the state before this change), or undef if $value isn't a flag state. sub _flag_state_changed_sql { my ($chart_id, $fa, $operator, $value) = @_; - return _flag_state_match($fa, $value) if $operator eq 'changedto'; + # As in history, a request counts as one net change. + my $last = Bugzilla::FlagActivity->last_of_request_sql($fa); + if ($operator eq 'changedto') { + my $match = _flag_state_match($fa, $value) or return undef; + return "$match AND $last"; + } - my $prev = "flag_prev_$chart_id"; - my $match = _flag_state_match($prev, $value) or return undef; - return "EXISTS ( + my $prev = "flag_prev_$chart_id"; + my $prev_id = Bugzilla::FlagActivity->previous_id_sql($fa); + my $match = _flag_state_match($prev, $value) or return undef; + return "$last AND EXISTS ( SELECT 1 FROM flag_activity $prev - WHERE $prev.id = (SELECT MAX(p2.id) FROM flag_activity p2 - WHERE p2.flag_id = $fa.flag_id AND p2.id < $fa.id) - AND $match)"; + WHERE $prev.id = $prev_id AND $match)"; } sub _flag_state_match { diff --git a/t/flag-activity-search.t b/t/flag-activity-search.t index 5192df33c3..99460bceeb 100644 --- a/t/flag-activity-search.t +++ b/t/flag-activity-search.t @@ -112,6 +112,26 @@ is(search_anything(changedto => 'review+'), [$legacy_bug, $current_bug], is(search_anything(changedfrom => 'review?(bob@bmo.test)'), [$current_bug], 'anything changedfrom'); +# A flag requested and granted in one request is one net change, as in history. +my $same_second_bug = new_bug('same second'); +for my $row (['?', $alice->id], ['+', undef]) { + $dbh->do( + 'INSERT INTO flag_activity + (flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, + status) + VALUES (?, ?, 43, ?, ?, ?, ?, ?)', undef, '2026-02-01 10:00:00', $review_id, + $bob->id, $bob->id, $row->[1], $same_second_bug, $row->[0] + ); +} +is(search(changedto => 'review?(alice@bmo.test)'), [], + 'changedto skips a state within a request'); +is(search(changedfrom => 'review?(alice@bmo.test)'), [], + 'changedfrom skips a state within a request'); +is(search_anything(changedfrom => 'review?(alice@bmo.test)'), [], + 'anything changedfrom skips a state within a request'); +is(search(changedto => 'review+'), + [$legacy_bug, $current_bug, $same_second_bug], 'changedto the net change'); + # An assignee whose only recent changes are flag changes isn't idle. my $owned_bug = create_bug( short_desc => 'owned', From 62a6b1b8d6ad4bb5aba4a65adfab007bed26991a Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Mon, 2 Nov 2026 23:29:12 +0100 Subject: [PATCH 32/50] Bug 2085398 - Skip flag_activity rows with a guessed who in changedby and the user activity, with tests --- Bugzilla/Bug.pm | 5 ++++- Bugzilla/FlagActivity.pm | 3 ++- Bugzilla/Search.pm | 7 +++++-- extensions/UserProfile/lib/Util.pm | 8 ++++++-- t/flag-activity-search.t | 20 ++++++++++++++++++++ t/flag-activity-user-stats.t | 16 ++++++++++++++++ 6 files changed, 53 insertions(+), 6 deletions(-) diff --git a/Bugzilla/Bug.pm b/Bugzilla/Bug.pm index 1f1b506d35..167d5bf4a6 100644 --- a/Bugzilla/Bug.pm +++ b/Bugzilla/Bug.pm @@ -4561,6 +4561,8 @@ sub delta_ts_non_bot { # Grab the latest timestamp for comments, bug changes and flag changes that # was not made by one of the accounts in the bot skip list. if (my $bot_ids_string = $cache->{bot_ids_string}) { + my $flag_newer + = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('flag_activity'); ($self->{delta_ts_non_bot}) = $dbh->selectrow_array( "SELECT MAX(bug_when) FROM (SELECT MAX(bug_when) AS bug_when @@ -4573,7 +4575,8 @@ sub delta_ts_non_bot { UNION ALL SELECT MAX(flag_when) AS bug_when FROM flag_activity - WHERE bug_id = ? AND who NOT IN ($bot_ids_string)) tmp", + WHERE bug_id = ? AND who NOT IN ($bot_ids_string) + AND $flag_newer) tmp", undef, ($self->id) x 3 ); } diff --git a/Bugzilla/FlagActivity.pm b/Bugzilla/FlagActivity.pm index 3c91956223..7be489c855 100644 --- a/Bugzilla/FlagActivity.pm +++ b/Bugzilla/FlagActivity.pm @@ -88,7 +88,8 @@ sub status { return $_[0]->{status} } # SQL condition on flag_activity row $alias: true when it is newer than the # bug's last flag change in bugs_activity. Older flag changes are read from # bugs_activity, which has them all; flag_activity has only some of them, -# written to both tables, which must not be listed twice. +# written to both tables, which must not be listed twice. Older rows also don't +# always know their who: it was filled in from the setter. sub newer_than_bugs_activity_sql { my ($class, $alias) = @_; return "$alias.flag_when > COALESCE( diff --git a/Bugzilla/Search.pm b/Bugzilla/Search.pm index 562a083126..42cdb344b3 100644 --- a/Bugzilla/Search.pm +++ b/Bugzilla/Search.pm @@ -3743,7 +3743,9 @@ sub _changedby { if ($field eq 'anything') { my $fa = "flag_act_$chart_id"; $args->{term} = "($args->{term} OR " - . $self->_flag_activity_exists($args, $fa, "$fa.who = $user_id") . ')'; + . $self->_flag_activity_exists($args, $fa, "$fa.who = $user_id", + Bugzilla::FlagActivity->newer_than_bugs_activity_sql($fa)) + . ')'; } } @@ -3790,7 +3792,8 @@ sub _flagtypes_changed { } elsif ($operator eq 'changedby') { my $user_id = $value =~ /^\d+$/ ? $value : $self->_get_user_id($value); - push @where, "$fa.who = $user_id"; + push @where, "$fa.who = $user_id", + Bugzilla::FlagActivity->newer_than_bugs_activity_sql($fa); } else { # changedto, changedfrom my $match = _flag_state_changed_sql($chart_id, $fa, $operator, $value) diff --git a/extensions/UserProfile/lib/Util.pm b/extensions/UserProfile/lib/Util.pm index 58dc33419b..a4f127726d 100644 --- a/extensions/UserProfile/lib/Util.pm +++ b/extensions/UserProfile/lib/Util.pm @@ -119,6 +119,8 @@ EOF EOF # bugs touched + my $flag_newer + = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('flag_activity'); _update_statistics($statistics, 'touched', [($user_id) x 3], <newer_than_bugs_activity_sql('flag_activity'); return Bugzilla->dbh->selectrow_array( <dbh; my $alice = create_user('alice@bmo.test', '*'); my $bob = create_user('bob@bmo.test', '*'); +my $carol = create_user('carol@bmo.test', '*'); sub new_bug { return create_bug( @@ -132,6 +133,25 @@ is(search_anything(changedfrom => 'review?(alice@bmo.test)'), [], is(search(changedto => 'review+'), [$legacy_bug, $current_bug, $same_second_bug], 'changedto the net change'); +# A legacy change also in flag_activity: bugs_activity knows who made it, the +# flag_activity row only has the setter as a guess. +my $guessed_bug = new_bug('guessed who'); +$dbh->do( + 'INSERT INTO bugs_activity (bug_id, who, bug_when, fieldid, removed, added) + VALUES (?, ?, ?, ?, ?, ?)', undef, $guessed_bug, $bob->id, + '2020-02-01 10:00:00', $fieldid, '', 'review+' +); +$dbh->do( + 'INSERT INTO flag_activity + (flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, status) + VALUES (?, ?, 44, ?, ?, NULL, ?, ?)', undef, '2020-02-01 10:00:00', $review_id, + $carol->id, $carol->id, $guessed_bug, '+' +); +is(search(changedby => 'carol@bmo.test'), [], + 'changedby skips a flag_activity row older than bugs_activity'); +is(search_anything(changedby => 'carol@bmo.test'), [], + 'anything changedby skips it too'); + # An assignee whose only recent changes are flag changes isn't idle. my $owned_bug = create_bug( short_desc => 'owned', diff --git a/t/flag-activity-user-stats.t b/t/flag-activity-user-stats.t index 34f1796e3b..9b47291af2 100644 --- a/t/flag-activity-user-stats.t +++ b/t/flag-activity-user-stats.t @@ -104,4 +104,20 @@ $dbh->do( update_statistics_by_user($human->id); is(product_count(), 1, 'a change in both tables is counted once'); +# bugs_activity knows the bot made this change; flag_activity only has the +# setter, the human, as a guess. +$dbh->do( + 'INSERT INTO bugs_activity (bug_id, who, bug_when, fieldid, removed, added) + VALUES (?, ?, ?, ?, ?, ?)', undef, $bug_id, $bot->id, + '2099-01-01 13:00:00', $fieldid, 'needinfo?(bob@bmo.test)', '' +); +$dbh->do( + 'UPDATE flag_activity SET flag_when = ?, who = ? WHERE flag_when = ?', + undef, '2099-01-01 13:00:00', $human->id, '2099-01-01 11:00:00' +); +is(Bugzilla::Bug->new($bug_id)->delta_ts_non_bot, + '2099-01-01 10:00:00', 'the non-bot last change time skips a guessed who'); +is(last_user_activity($human->id), + '2099-01-01 10:00:00', 'the last user activity skips a guessed who'); + done_testing; From 1b517651a6e464c53ab6f1119f88a68a291fafbc Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Tue, 3 Nov 2026 00:24:18 +0100 Subject: [PATCH 33/50] Bug 2085398 - Rename a variable Perl::Critic rejects in the flag search --- Bugzilla/Search.pm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Bugzilla/Search.pm b/Bugzilla/Search.pm index 42cdb344b3..7264c90fab 100644 --- a/Bugzilla/Search.pm +++ b/Bugzilla/Search.pm @@ -3826,16 +3826,16 @@ sub _flag_activity_exists { sub _flag_state_changed_sql { my ($chart_id, $fa, $operator, $value) = @_; # As in history, a request counts as one net change. - my $last = Bugzilla::FlagActivity->last_of_request_sql($fa); + my $flag_last = Bugzilla::FlagActivity->last_of_request_sql($fa); if ($operator eq 'changedto') { my $match = _flag_state_match($fa, $value) or return undef; - return "$match AND $last"; + return "$match AND $flag_last"; } my $prev = "flag_prev_$chart_id"; my $prev_id = Bugzilla::FlagActivity->previous_id_sql($fa); my $match = _flag_state_match($prev, $value) or return undef; - return "$last AND EXISTS ( + return "$flag_last AND EXISTS ( SELECT 1 FROM flag_activity $prev WHERE $prev.id = $prev_id AND $match)"; } From 00598756f041891d6345f0c607675840246cfb75 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Tue, 3 Nov 2026 00:25:06 +0100 Subject: [PATCH 34/50] Bug 2085398 - Count one net flag change per request in the activity by product, with a test --- extensions/UserProfile/lib/Util.pm | 13 ++++++++++--- t/flag-activity-user-stats.t | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/extensions/UserProfile/lib/Util.pm b/extensions/UserProfile/lib/Util.pm index a4f127726d..8e64fa646d 100644 --- a/extensions/UserProfile/lib/Util.pm +++ b/extensions/UserProfile/lib/Util.pm @@ -310,14 +310,21 @@ sub _activity_by_product { EOF map { $products{$_->{product}} += $_->{count} } @$rows; - # flag changes, skipping the older ones that are also in bugs_activity - my $newer = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('fa'); + # flag changes, skipping the older ones that are also in bugs_activity. As in + # history, a request is one net change, and none if it left the flag as it was. + my $newer = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('fa'); + my $flag_last = Bugzilla::FlagActivity->last_of_request_sql('fa'); + my $prev_id = Bugzilla::FlagActivity->previous_id_sql('fa'); $rows = $dbh->selectall_arrayref(< {}}, $user_id); SELECT products.name AS product, count(*) AS count FROM flag_activity fa INNER JOIN bugs ON bugs.bug_id = fa.bug_id INNER JOIN products ON products.id = bugs.product_id - WHERE fa.who = ? AND $newer + LEFT JOIN flag_activity prev ON prev.id = $prev_id + WHERE fa.who = ? AND $newer AND $flag_last + AND NOT (COALESCE(prev.status, 'X') = fa.status + AND COALESCE(prev.requestee_id, 0) + = COALESCE(fa.requestee_id, 0)) GROUP BY bugs.product_id EOF map { $products{$_->{product}} += $_->{count} } @$rows; diff --git a/t/flag-activity-user-stats.t b/t/flag-activity-user-stats.t index 9b47291af2..089ff730d0 100644 --- a/t/flag-activity-user-stats.t +++ b/t/flag-activity-user-stats.t @@ -120,4 +120,21 @@ is(Bugzilla::Bug->new($bug_id)->delta_ts_non_bot, is(last_user_activity($human->id), '2099-01-01 10:00:00', 'the last user activity skips a guessed who'); +# One request: a flag requested and granted, another requested and removed. +sub add_request_rows { + my ($flag_id, @statuses) = @_; + $dbh->do( + 'INSERT INTO flag_activity + (flag_when, type_id, flag_id, setter_id, who, bug_id, status) + VALUES (?, ?, ?, ?, ?, ?, ?)', undef, '2099-01-01 14:00:00', $needinfo_id, + $flag_id, $human->id, $human->id, $bug_id, $_ + ) for @statuses; +} +my $before = product_count(); +add_request_rows(900, '?', '+'); +add_request_rows(901, '?', 'X'); +update_statistics_by_user($human->id); +is(product_count(), $before + 1, + 'the activity by product counts one net change per request'); + done_testing; From 97fa12e058d75ee873bb8cebbc05688fcc065bf2 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Tue, 3 Nov 2026 01:15:57 +0100 Subject: [PATCH 35/50] Bug 2085398 - Remove the literal question marks from the user activity flag query --- extensions/BMO/lib/Reports/UserActivity.pm | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/extensions/BMO/lib/Reports/UserActivity.pm b/extensions/BMO/lib/Reports/UserActivity.pm index fa0935cc72..1dccd3e8b3 100644 --- a/extensions/BMO/lib/Reports/UserActivity.pm +++ b/extensions/BMO/lib/Reports/UserActivity.pm @@ -114,7 +114,9 @@ sub report { $comment_filter = 'AND longdescs.isprivate = 0'; } - # Flag changes are in flag_activity, older ones in bugs_activity. + # Flag changes are in flag_activity, older ones in bugs_activity. CHAR(63) + # is a question mark: a literal one would be taken for a placeholder by + # the debug output below. my $flag_newer = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('fsa'); my $flag_last = Bugzilla::FlagActivity->last_of_request_sql('fsa'); my $flag_prev_id = Bugzilla::FlagActivity->previous_id_sql('fsa'); @@ -152,13 +154,13 @@ sub report { . $dbh->sql_date_format('fsa.flag_when', '%Y-%m-%d %H:%i:%s') . " AS ts, CASE WHEN prev.id IS NULL THEN '' - WHEN prev.status = '?' AND preq.login_name IS NOT NULL - THEN CONCAT(ft.name, '?(', preq.login_name, ')') + WHEN prev.status = CHAR(63) AND preq.login_name IS NOT NULL + THEN CONCAT(ft.name, prev.status, '(', preq.login_name, ')') ELSE CONCAT(ft.name, prev.status) END AS removed, CASE WHEN fsa.status = 'X' THEN '' - WHEN fsa.status = '?' AND req.login_name IS NOT NULL - THEN CONCAT(ft.name, '?(', req.login_name, ')') + WHEN fsa.status = CHAR(63) AND req.login_name IS NOT NULL + THEN CONCAT(ft.name, fsa.status, '(', req.login_name, ')') ELSE CONCAT(ft.name, fsa.status) END AS added, profiles.login_name, From 91b8a07a71f2af7f1d0b515076bd6c34d1b5c2d7 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Tue, 3 Nov 2026 22:49:00 +0100 Subject: [PATCH 36/50] Bug 2085398 - List the flags set at creation in the new bug mail again --- Bugzilla/BugMail.pm | 4 ---- t/bmo/bugmail-flag-events.t | 11 ++++++----- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/Bugzilla/BugMail.pm b/Bugzilla/BugMail.pm index 05c34d21d0..af0b66e193 100644 --- a/Bugzilla/BugMail.pm +++ b/Bugzilla/BugMail.pm @@ -121,10 +121,6 @@ sub Send { } else { my ($diffs, $referenced) = _get_diffs($bug, $end, \%user_cache); - - # The mail of a new bug lists its fields, not changes: flags set at - # creation are no change. - @$diffs = grep { $_->{field_name} ne 'flagtypes.name' } @$diffs if !$start; push(@diffs, @$diffs); push(@referenced_bugs, @$referenced); } diff --git a/t/bmo/bugmail-flag-events.t b/t/bmo/bugmail-flag-events.t index 0cdf23d575..f194b38e31 100644 --- a/t/bmo/bugmail-flag-events.t +++ b/t/bmo/bugmail-flag-events.t @@ -352,8 +352,8 @@ foreach my $later_first (0, 1) { unlike($raw[0], qr/private reasons/, 'but not the private ones'); } -# The mail of a new bug doesn't report the flags set at creation as changes. -# The assignee gets the whole mail: the flag recipients have no diffs anyway. +# The mail of a new bug lists the flags set at creation, once, as bugs_activity +# did: watchers see e.g. a needinfo requested on a new bug. { my $bug = new_bug(assigned_to => $stranger->login); add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', '?', $requester, @@ -362,9 +362,10 @@ foreach my $later_first (0, 1) { my ($mail) = grep { $_->{to_user}->id == $stranger->id } sent_mails(Bugzilla::Bug->new($bug->id), $requester); - ok(@{$mail->{diffs}}, 'the assignee gets the fields of the new bug'); - ok(!grep({ $_->{field_name} eq 'flagtypes.name' } @{$mail->{diffs}}), - 'but no flag change'); + my @flags = grep { $_->{field_name} eq 'flagtypes.name' } @{$mail->{diffs}}; + is(scalar @flags, 1, 'the new bug mail lists the flag set at creation once'); + like($flags[0]{new}, qr/\?\(\Q@{[$requestee->login]}\E\)$/, + 'as requested from the requestee'); } # The cc_list of a flag on a private attachment only reaches insiders. From 5c3d05865eb45f4a921974a44cb785a6c960e1a8 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 5 Nov 2026 17:16:56 +0100 Subject: [PATCH 37/50] Bug 2085398 - Drop the foreign key on flag_activity.who before making it NOT NULL --- Bugzilla/Install/DB.pm | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index bd6edbc627..ee66ed31f5 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -4614,6 +4614,9 @@ sub _set_flag_activity_who_not_null { my $dbh = Bugzilla->dbh; return if $dbh->bz_column_info('flag_activity', 'who')->{NOTNULL}; + # MySQL can't make a column of a foreign key NOT NULL. Drop the key: + # bz_setup_foreign_keys adds it back at the end of checksetup. + $dbh->bz_drop_fk('flag_activity', 'who'); $dbh->bz_alter_column('flag_activity', 'who', { TYPE => 'INT3', From 3c0d4781bbcbe215800d2b3ff71c046062a18f28 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 5 Nov 2026 17:18:02 +0100 Subject: [PATCH 38/50] Bug 2085398 - Read the flag changes up to a stored cutoff from bugs_activity, the later ones from flag_activity --- Bugzilla/Bug.pm | 8 +++-- Bugzilla/BugMail.pm | 5 ++- Bugzilla/DB/Schema.pm | 20 +++++++---- Bugzilla/FlagActivity.pm | 39 +++++++++++++++------- Bugzilla/Install/DB.pm | 12 +++---- Bugzilla/Search.pm | 4 +-- Bugzilla/Test/Util.pm | 15 +++++++-- extensions/BMO/lib/Reports/UserActivity.pm | 5 ++- extensions/UserProfile/lib/Util.pm | 14 ++++---- t/bmo/bugmail-flag-events.t | 10 +++++- t/flag-activity-history.t | 15 ++++++--- t/flag-activity-search.t | 9 ++--- t/flag-activity-start-rows.t | 12 ++++--- t/flag-activity-stream.t | 3 +- t/flag-activity-user-stats.t | 10 +++--- t/flag-activity-who.t | 3 +- 16 files changed, 126 insertions(+), 58 deletions(-) diff --git a/Bugzilla/Bug.pm b/Bugzilla/Bug.pm index 167d5bf4a6..12a828456b 100644 --- a/Bugzilla/Bug.pm +++ b/Bugzilla/Bug.pm @@ -4562,7 +4562,7 @@ sub delta_ts_non_bot { # was not made by one of the accounts in the bot skip list. if (my $bot_ids_string = $cache->{bot_ids_string}) { my $flag_newer - = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('flag_activity'); + = Bugzilla::FlagActivity->after_cutoff_sql('flag_activity'); ($self->{delta_ts_non_bot}) = $dbh->selectrow_array( "SELECT MAX(bug_when) FROM (SELECT MAX(bug_when) AS bug_when @@ -4869,6 +4869,9 @@ sub GetBugActivity { my $bug_when = $dbh->sql_date_format('bugs_activity.bug_when', '%Y-%m-%d %H:%i:%s'); + my $ba_before_cutoff + = Bugzilla::FlagActivity->bugs_activity_before_cutoff_sql('bugs_activity'); + # Use an outer table so alias column `bug_when` will work within the `where` # clause. Also use `DISTINCT` to suppress duplicated changes weirdly made at # the same time by the same user. @@ -4891,6 +4894,7 @@ sub GetBugActivity { INNER JOIN profiles ON profiles.userid = bugs_activity.who WHERE bugs_activity.bug_id = ? + AND $ba_before_cutoff $attachpart $suppwhere "; @@ -4929,7 +4933,7 @@ sub GetBugActivity { } my $flag_bug_when = $dbh->sql_date_format('fsa.flag_when', '%Y-%m-%d %H:%i:%s'); - my $flag_newer = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('fsa'); + my $flag_newer = Bugzilla::FlagActivity->after_cutoff_sql('fsa'); my $flag_last = Bugzilla::FlagActivity->last_of_request_sql('fsa'); my $flag_prev_id = Bugzilla::FlagActivity->previous_id_sql('fsa'); $query .= " diff --git a/Bugzilla/BugMail.pm b/Bugzilla/BugMail.pm index af0b66e193..9258d62a6f 100644 --- a/Bugzilla/BugMail.pm +++ b/Bugzilla/BugMail.pm @@ -1046,7 +1046,9 @@ sub _get_diffs { my $dbh = Bugzilla->dbh; my @args = ($bug->id); - my $flag_newer = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('fsa'); + my $flag_newer = Bugzilla::FlagActivity->after_cutoff_sql('fsa'); + my $ba_before_cutoff + = Bugzilla::FlagActivity->bugs_activity_before_cutoff_sql('bugs_activity'); my $flag_last = Bugzilla::FlagActivity->last_of_request_sql('fsa'); my $flag_prev_id = Bugzilla::FlagActivity->previous_id_sql('fsa'); @@ -1070,6 +1072,7 @@ sub _get_diffs { INNER JOIN fielddefs ON fielddefs.id = bugs_activity.fieldid WHERE bugs_activity.bug_id = ? + AND $ba_before_cutoff $when_restriction UNION ALL SELECT 'flagtypes.name' AS field_name, diff --git a/Bugzilla/DB/Schema.pm b/Bugzilla/DB/Schema.pm index 101b2c022e..c4cf819ad3 100644 --- a/Bugzilla/DB/Schema.pm +++ b/Bugzilla/DB/Schema.pm @@ -361,13 +361,12 @@ use constant ABSTRACT_SCHEMA => { }, ], INDEXES => [ - bugs_activity_bug_id_idx => ['bug_id'], - bugs_activity_bug_field_when_idx => ['bug_id', 'fieldid', 'bug_when'], - bugs_activity_who_idx => ['who'], - bugs_activity_bug_when_idx => ['bug_when'], - bugs_activity_fieldid_idx => ['fieldid'], - bugs_activity_added_idx => ['added'], - bugs_activity_removed_idx => ['removed'], + bugs_activity_bug_id_idx => ['bug_id'], + bugs_activity_who_idx => ['who'], + bugs_activity_bug_when_idx => ['bug_when'], + bugs_activity_fieldid_idx => ['fieldid'], + bugs_activity_added_idx => ['added'], + bugs_activity_removed_idx => ['removed'], ], }, @@ -779,6 +778,13 @@ use constant ABSTRACT_SCHEMA => { ], }, + # One row: flag changes up to this time are read from bugs_activity, later + # ones from flag_activity. + # See Bugzilla::FlagActivity->cutoff. + flag_activity_migration => { + FIELDS => [cutoff => {TYPE => 'DATETIME', NOTNULL => 1},], + }, + # "flagtypes" defines the types of flags that can be set. flagtypes => { FIELDS => [ diff --git a/Bugzilla/FlagActivity.pm b/Bugzilla/FlagActivity.pm index 7be489c855..e0c4056a07 100644 --- a/Bugzilla/FlagActivity.pm +++ b/Bugzilla/FlagActivity.pm @@ -12,6 +12,7 @@ use strict; use warnings; use Bugzilla::Error; +use Bugzilla::Field qw(get_field_id); use Bugzilla::Util qw(trim datetime_from); use List::MoreUtils qw(none); @@ -85,19 +86,33 @@ sub requestee_id { return $_[0]->{requestee_id} } sub attachment_id { return $_[0]->{attachment_id} } sub status { return $_[0]->{status} } -# SQL condition on flag_activity row $alias: true when it is newer than the -# bug's last flag change in bugs_activity. Older flag changes are read from -# bugs_activity, which has them all; flag_activity has only some of them, -# written to both tables, which must not be listed twice. Older rows also don't -# always know their who: it was filled in from the setter. -sub newer_than_bugs_activity_sql { +# Flag changes up to the cutoff are read from bugs_activity, which has them +# all: flag_activity has only some of them, and doesn't always know their who. +# Later ones are read from flag_activity only. The cutoff is when checksetup +# stopped writing flag changes to bugs_activity: pods still on the older code +# write both tables until the end of the rolling deploy, and their +# bugs_activity rows must not be listed twice. +sub cutoff { + my $cache = Bugzilla->request_cache; + $cache->{flag_activity_cutoff} + //= Bugzilla->dbh->selectrow_array('SELECT cutoff FROM flag_activity_migration') + // '1970-01-01 00:00:00'; + return $cache->{flag_activity_cutoff}; +} + +# SQL condition on flag_activity row $alias: true after the cutoff. +sub after_cutoff_sql { + my ($class, $alias) = @_; + return "$alias.flag_when > " . Bugzilla->dbh->quote($class->cutoff); +} + +# SQL condition on bugs_activity row $alias: false for a flag change after the +# cutoff. +sub bugs_activity_before_cutoff_sql { my ($class, $alias) = @_; - return "$alias.flag_when > COALESCE( - (SELECT MAX(ba.bug_when) FROM bugs_activity ba - WHERE ba.bug_id = $alias.bug_id - AND ba.fieldid = (SELECT id FROM fielddefs - WHERE name = 'flagtypes.name')), - '1970-01-01')"; + my $field_id = get_field_id('flagtypes.name'); + return "($alias.fieldid <> $field_id OR $alias.bug_when <= " + . Bugzilla->dbh->quote($class->cutoff) . ')'; } # A flag created and updated in one request has several rows with the same diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index ee66ed31f5..55cd455cd5 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -4558,12 +4558,12 @@ sub _migrate_flag_state_activity { # flags. Other flags last changed before then have no row there, so their next # change would show no previous value in history and bugmail. Give each of them # a row with its current state, dated at its last change: history and bugmail -# don't show it, as it isn't newer than that change in bugs_activity. +# don't show it, as it is older than the cutoff. sub _add_flag_activity_start_rows { my $dbh = Bugzilla->dbh; - # The index is added at the end, so this runs once, or again if it died. - return if $dbh->bz_index_info('bugs_activity', 'bugs_activity_bug_field_when_idx'); + # The cutoff is set at the end, so this runs once, or again if it died. + return if $dbh->selectrow_array('SELECT 1 FROM flag_activity_migration'); my ($max_id) = $dbh->selectrow_array('SELECT MAX(id) FROM flags'); $max_id //= 0; @@ -4586,9 +4586,9 @@ sub _add_flag_activity_start_rows { } print "Added a flag_activity row for $added flags.\n" if $added > 0; - # For FlagActivity->newer_than_bugs_activity_sql. - $dbh->bz_add_index('bugs_activity', 'bugs_activity_bug_field_when_idx', - [qw(bug_id fieldid bug_when)]); + # See Bugzilla::FlagActivity->cutoff. + $dbh->do( + 'INSERT INTO flag_activity_migration (cutoff) VALUES (LOCALTIMESTAMP(0))'); } # Nullable for now: during the rolling deploy, pods still on the previous code diff --git a/Bugzilla/Search.pm b/Bugzilla/Search.pm index 7264c90fab..d8cab17b34 100644 --- a/Bugzilla/Search.pm +++ b/Bugzilla/Search.pm @@ -3744,7 +3744,7 @@ sub _changedby { my $fa = "flag_act_$chart_id"; $args->{term} = "($args->{term} OR " . $self->_flag_activity_exists($args, $fa, "$fa.who = $user_id", - Bugzilla::FlagActivity->newer_than_bugs_activity_sql($fa)) + Bugzilla::FlagActivity->after_cutoff_sql($fa)) . ')'; } } @@ -3793,7 +3793,7 @@ sub _flagtypes_changed { elsif ($operator eq 'changedby') { my $user_id = $value =~ /^\d+$/ ? $value : $self->_get_user_id($value); push @where, "$fa.who = $user_id", - Bugzilla::FlagActivity->newer_than_bugs_activity_sql($fa); + Bugzilla::FlagActivity->after_cutoff_sql($fa); } else { # changedto, changedfrom my $match = _flag_state_changed_sql($chart_id, $fa, $operator, $value) diff --git a/Bugzilla/Test/Util.pm b/Bugzilla/Test/Util.pm index d5b6a938c4..be29dd5224 100644 --- a/Bugzilla/Test/Util.pm +++ b/Bugzilla/Test/Util.pm @@ -12,8 +12,8 @@ use strict; use warnings; use base qw(Exporter); -our @EXPORT - = qw(create_user create_bug create_group create_oauth_client issue_api_key mock_useragent_tx); +our @EXPORT = qw(create_user create_bug create_group create_oauth_client + issue_api_key mock_useragent_tx set_flag_activity_cutoff); use Bugzilla::Group; use Bugzilla::User; @@ -107,6 +107,17 @@ sub issue_api_key { } } +# Flag changes up to $when are read from bugs_activity, later ones from +# flag_activity: see Bugzilla::FlagActivity->cutoff. checksetup sets it to the +# time it ran, after the fixed dates of most tests. +sub set_flag_activity_cutoff { + my ($when) = @_; + my $dbh = Bugzilla->dbh; + $dbh->do('DELETE FROM flag_activity_migration'); + $dbh->do('INSERT INTO flag_activity_migration (cutoff) VALUES (?)', undef, $when); + delete Bugzilla->request_cache->{flag_activity_cutoff}; +} + sub create_bug { state $check = compile_named( short_desc => Str, diff --git a/extensions/BMO/lib/Reports/UserActivity.pm b/extensions/BMO/lib/Reports/UserActivity.pm index 1dccd3e8b3..04b3c4322b 100644 --- a/extensions/BMO/lib/Reports/UserActivity.pm +++ b/extensions/BMO/lib/Reports/UserActivity.pm @@ -117,7 +117,9 @@ sub report { # Flag changes are in flag_activity, older ones in bugs_activity. CHAR(63) # is a question mark: a literal one would be taken for a placeholder by # the debug output below. - my $flag_newer = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('fsa'); + my $flag_newer = Bugzilla::FlagActivity->after_cutoff_sql('fsa'); + my $ba_before_cutoff + = Bugzilla::FlagActivity->bugs_activity_before_cutoff_sql('bugs_activity'); my $flag_last = Bugzilla::FlagActivity->last_of_request_sql('fsa'); my $flag_prev_id = Bugzilla::FlagActivity->previous_id_sql('fsa'); my $query = " @@ -142,6 +144,7 @@ sub report { WHERE profiles.login_name IN ($who_bits) AND CONVERT_TZ(bugs_activity.bug_when, 'UTC', 'America/Los_Angeles') >= ? AND CONVERT_TZ(bugs_activity.bug_when, 'UTC', 'America/Los_Angeles') <= ? + AND $ba_before_cutoff $activity_where UNION ALL diff --git a/extensions/UserProfile/lib/Util.pm b/extensions/UserProfile/lib/Util.pm index 8e64fa646d..b575d998a5 100644 --- a/extensions/UserProfile/lib/Util.pm +++ b/extensions/UserProfile/lib/Util.pm @@ -120,7 +120,7 @@ EOF # bugs touched my $flag_newer - = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('flag_activity'); + = Bugzilla::FlagActivity->after_cutoff_sql('flag_activity'); _update_statistics($statistics, 'touched', [($user_id) x 3], <newer_than_bugs_activity_sql('flag_activity'); + = Bugzilla::FlagActivity->after_cutoff_sql('flag_activity'); return Bugzilla->dbh->selectrow_array( <bugs_activity_before_cutoff_sql( + 'bugs_activity'); my $rows = $dbh->selectall_arrayref(< {}}, $user_id); SELECT products.name AS product, count(*) AS count FROM bugs_activity INNER JOIN bugs ON bugs.bug_id = bugs_activity.bug_id INNER JOIN products ON products.id = bugs.product_id - WHERE who = ? + WHERE who = ? AND $before GROUP BY bugs.product_id EOF map { $products{$_->{product}} += $_->{count} } @$rows; - # flag changes, skipping the older ones that are also in bugs_activity. As in + # flag changes after the cutoff, the older ones are in bugs_activity. As in # history, a request is one net change, and none if it left the flag as it was. - my $newer = Bugzilla::FlagActivity->newer_than_bugs_activity_sql('fa'); + my $newer = Bugzilla::FlagActivity->after_cutoff_sql('fa'); my $flag_last = Bugzilla::FlagActivity->last_of_request_sql('fa'); my $prev_id = Bugzilla::FlagActivity->previous_id_sql('fa'); $rows = $dbh->selectall_arrayref(< {}}, $user_id); diff --git a/t/bmo/bugmail-flag-events.t b/t/bmo/bugmail-flag-events.t index f194b38e31..7ebdea10c0 100644 --- a/t/bmo/bugmail-flag-events.t +++ b/t/bmo/bugmail-flag-events.t @@ -24,7 +24,8 @@ use Bugzilla; use Bugzilla::Constants; use Bugzilla::Bug; use Bugzilla::BugMail; -use Bugzilla::Test::Util qw(create_group create_user); +use Bugzilla::FlagActivity; +use Bugzilla::Test::Util qw(create_group create_user set_flag_activity_cutoff); BEGIN { Bugzilla->extensions } Bugzilla->usage_mode(USAGE_MODE_TEST); @@ -355,6 +356,11 @@ foreach my $later_first (0, 1) { # The mail of a new bug lists the flags set at creation, once, as bugs_activity # did: watchers see e.g. a needinfo requested on a new bug. { + # The mail reads the changes after the cutoff from flag_activity. The database + # is shared with the other test files: put it back after. + my $cutoff = Bugzilla::FlagActivity->cutoff; + set_flag_activity_cutoff('1999-12-31 00:00:00'); + my $bug = new_bug(assigned_to => $stranger->login); add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', '?', $requester, $requestee); @@ -366,6 +372,8 @@ foreach my $later_first (0, 1) { is(scalar @flags, 1, 'the new bug mail lists the flag set at creation once'); like($flags[0]{new}, qr/\?\(\Q@{[$requestee->login]}\E\)$/, 'as requested from the requestee'); + + set_flag_activity_cutoff($cutoff); } # The cc_list of a flag on a private attachment only reaches insiders. diff --git a/t/flag-activity-history.t b/t/flag-activity-history.t index 5ff5adf097..1c6dec738a 100644 --- a/t/flag-activity-history.t +++ b/t/flag-activity-history.t @@ -17,14 +17,14 @@ BEGIN { use Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo.test'); use Bugzilla::Test::MockDB; -use Bugzilla::Test::Util qw(create_bug create_user); +use Bugzilla::Test::Util qw(create_bug create_user set_flag_activity_cutoff); use Bugzilla::Bug; use Bugzilla::BugMail; use Bugzilla::User; use Test2::V0; -# Bug history and bugmail read the flag changes made before flag_activity -# existed from bugs_activity, and only the newer ones from flag_activity. +# Bug history and bugmail read the flag changes up to the cutoff from +# bugs_activity, and only the later ones from flag_activity. create_user('automation@bmo.tld', '*'); Bugzilla->set_user(Bugzilla::User->super_user); @@ -33,6 +33,7 @@ my $dbh = Bugzilla->dbh; my $setter = create_user('setter@bmo.test', '*'); my $alice = create_user('alice@bmo.test', '*'); my $bob = create_user('bob@bmo.test', '*'); +set_flag_activity_cutoff('2025-01-01 00:00:00'); sub new_bug { return create_bug( @@ -97,9 +98,15 @@ for my $row ( } # The last legacy change was also written to flag_activity, then the flag -# changed again once only flag_activity was written. +# changed again after the cutoff. A pod on the older code wrote that change to +# bugs_activity too. add_flag_activity($legacy_bug, '2020-01-03 10:00:00', 42, $setter, undef, '+'); add_flag_activity($legacy_bug, '2026-01-01 10:00:00', 42, $alice, undef, '-'); +$dbh->do( + 'INSERT INTO bugs_activity (bug_id, who, bug_when, fieldid, removed, added) + VALUES (?, ?, ?, ?, ?, ?)', undef, $legacy_bug, $alice->id, + '2026-01-01 10:00:00', $fieldid, 'review+', 'review-' +); my $expected = [ ['setter@bmo.test', '', 'review?(bob@bmo.test)'], diff --git a/t/flag-activity-search.t b/t/flag-activity-search.t index c681d566db..bd8d16cb85 100644 --- a/t/flag-activity-search.t +++ b/t/flag-activity-search.t @@ -17,13 +17,13 @@ BEGIN { use Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo.test'); use Bugzilla::Test::MockDB; -use Bugzilla::Test::Util qw(create_bug create_user); +use Bugzilla::Test::Util qw(create_bug create_user set_flag_activity_cutoff); use Bugzilla::Search; use Bugzilla::User; use Test2::V0; -# Search changed* on flags matches changes in flag_activity and the older -# ones that are only in bugs_activity. +# Search changed* on flags matches the changes up to the cutoff in +# bugs_activity, and the later ones in flag_activity. create_user('automation@bmo.tld', '*'); Bugzilla->set_user(Bugzilla::User->super_user); @@ -32,6 +32,7 @@ my $dbh = Bugzilla->dbh; my $alice = create_user('alice@bmo.test', '*'); my $bob = create_user('bob@bmo.test', '*'); my $carol = create_user('carol@bmo.test', '*'); +set_flag_activity_cutoff('2025-01-01 00:00:00'); sub new_bug { return create_bug( @@ -148,7 +149,7 @@ $dbh->do( $carol->id, $carol->id, $guessed_bug, '+' ); is(search(changedby => 'carol@bmo.test'), [], - 'changedby skips a flag_activity row older than bugs_activity'); + 'changedby skips a flag_activity row up to the cutoff'); is(search_anything(changedby => 'carol@bmo.test'), [], 'anything changedby skips it too'); diff --git a/t/flag-activity-start-rows.t b/t/flag-activity-start-rows.t index 414a2d41a3..66cf9ca882 100644 --- a/t/flag-activity-start-rows.t +++ b/t/flag-activity-start-rows.t @@ -18,7 +18,7 @@ BEGIN { use Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo.test'); use Bugzilla::Test::MockDB; use Bugzilla::Test::MockParams (mail_delivery_method => 'None'); -use Bugzilla::Test::Util qw(create_bug create_user); +use Bugzilla::Test::Util qw(create_bug create_user set_flag_activity_cutoff); use Bugzilla::Bug; use Bugzilla::Install::DB; use Bugzilla::User; @@ -62,13 +62,17 @@ $dbh->do( '2099-01-01 10:00:00', $fieldid, '', 'needinfo?(bob@bmo.test)' ); -# The index is in the schema of a new database; older ones don't have it yet. +# checksetup already ran on this database: forget it, so that it walks the +# flags again. sub add_start_rows { - $dbh->bz_drop_index('bugs_activity', 'bugs_activity_bug_field_when_idx'); + $dbh->do('DELETE FROM flag_activity_migration'); Bugzilla::Install::DB::_add_flag_activity_start_rows(); } add_start_rows() for 1 .. 2; + +# The flag was set before checksetup ran, the next change comes after. +set_flag_activity_cutoff('2099-01-01 10:30:00'); is( scalar $dbh->selectrow_array( 'SELECT COUNT(*) FROM flag_activity WHERE flag_id = ?', undef, $flag->id @@ -128,7 +132,7 @@ is( 'a flag without a modification date is dated at its creation' ); -# Once the index is there, checksetup doesn't walk the flags again. +# Once the cutoff is set, checksetup doesn't walk the flags again. $dbh->do('DELETE FROM flag_activity WHERE flag_id = ?', undef, $undated->id); Bugzilla::Install::DB::_add_flag_activity_start_rows(); is( diff --git a/t/flag-activity-stream.t b/t/flag-activity-stream.t index bf26484a3c..fa9b14065c 100644 --- a/t/flag-activity-stream.t +++ b/t/flag-activity-stream.t @@ -18,7 +18,7 @@ BEGIN { use Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo.test'); use Bugzilla::Test::MockDB; use Bugzilla::Test::MockParams (mail_delivery_method => 'None'); -use Bugzilla::Test::Util qw(create_bug create_user); +use Bugzilla::Test::Util qw(create_bug create_user set_flag_activity_cutoff); use Bugzilla::Bug; use Bugzilla::User; use Test2::V0; @@ -58,6 +58,7 @@ sub flag_activity_id { $bug->update('2099-01-01 10:00:00'); if ($legacy) { + set_flag_activity_cutoff('2099-01-01 10:00:00'); $dbh->do('DELETE FROM flag_activity WHERE bug_id = ?', undef, $bug_id); $dbh->do( 'INSERT INTO bugs_activity (bug_id, who, bug_when, fieldid, removed, added) diff --git a/t/flag-activity-user-stats.t b/t/flag-activity-user-stats.t index 089ff730d0..7d80fa9205 100644 --- a/t/flag-activity-user-stats.t +++ b/t/flag-activity-user-stats.t @@ -21,7 +21,7 @@ use Bugzilla::Test::MockParams ( mail_delivery_method => 'None', last_change_time_non_bot_skip_list => 'automation@bmo.tld', ); -use Bugzilla::Test::Util qw(create_bug create_user); +use Bugzilla::Test::Util qw(create_bug create_user set_flag_activity_cutoff); use Bugzilla::Bug; use Bugzilla::User; use Bugzilla::Extension::UserProfile::Util @@ -92,7 +92,8 @@ sub product_count { } is(product_count(), 1, 'a flag change counts in the activity by product'); -# An older change is in both tables: count it once. +# A pod on the older code writes a change after the cutoff to both tables: +# count it once. my ($fieldid) = $dbh->selectrow_array( "SELECT id FROM fielddefs WHERE name = 'flagtypes.name'"); @@ -104,8 +105,9 @@ $dbh->do( update_statistics_by_user($human->id); is(product_count(), 1, 'a change in both tables is counted once'); -# bugs_activity knows the bot made this change; flag_activity only has the -# setter, the human, as a guess. +# Up to the cutoff, bugs_activity knows the bot made this change; flag_activity +# only has the setter, the human, as a guess. +set_flag_activity_cutoff('2099-01-01 13:30:00'); $dbh->do( 'INSERT INTO bugs_activity (bug_id, who, bug_when, fieldid, removed, added) VALUES (?, ?, ?, ?, ?, ?)', undef, $bug_id, $bot->id, diff --git a/t/flag-activity-who.t b/t/flag-activity-who.t index 2b420947b6..417528da0e 100644 --- a/t/flag-activity-who.t +++ b/t/flag-activity-who.t @@ -18,7 +18,7 @@ BEGIN { use Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo.test'); use Bugzilla::Test::MockDB; use Bugzilla::Test::MockParams (mail_delivery_method => 'None'); -use Bugzilla::Test::Util qw(create_bug create_user); +use Bugzilla::Test::Util qw(create_bug create_user set_flag_activity_cutoff); use Bugzilla::Bug; use Bugzilla::Install::DB; use Bugzilla::User; @@ -34,6 +34,7 @@ my $dbh = Bugzilla->dbh; my $requester = create_user('requester@bmo.test', '*'); my $redirector = create_user('redirector@bmo.test', '*'); create_user($_, '*') for qw(bob@bmo.test carol@bmo.test); +set_flag_activity_cutoff('2025-01-01 00:00:00'); my $bug_id = create_bug( short_desc => 'Flag redirect test bug', From cc875d35cc4175eaa0a1d925849321753b493ea6 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 12 Nov 2026 20:13:47 +0100 Subject: [PATCH 39/50] Bug 2085398 - Use sql_string_concat for the flag names in GetBugActivity --- Bugzilla/Bug.pm | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Bugzilla/Bug.pm b/Bugzilla/Bug.pm index 12a828456b..d48bc8d106 100644 --- a/Bugzilla/Bug.pm +++ b/Bugzilla/Bug.pm @@ -4936,6 +4936,12 @@ sub GetBugActivity { my $flag_newer = Bugzilla::FlagActivity->after_cutoff_sql('fsa'); my $flag_last = Bugzilla::FlagActivity->last_of_request_sql('fsa'); my $flag_prev_id = Bugzilla::FlagActivity->previous_id_sql('fsa'); + my $flag_removed_req + = $dbh->sql_string_concat('ft.name', q{'?('}, 'preq.login_name', q{')'}); + my $flag_removed = $dbh->sql_string_concat('ft.name', 'prev.status'); + my $flag_added_req + = $dbh->sql_string_concat('ft.name', q{'?('}, 'req.login_name', q{')'}); + my $flag_added = $dbh->sql_string_concat('ft.name', 'fsa.status'); $query .= " UNION ALL SELECT 'flagtypes.name' AS field_name, @@ -4944,13 +4950,13 @@ sub GetBugActivity { $flag_bug_when AS bug_when, CASE WHEN prev.id IS NULL THEN '' WHEN prev.status = '?' AND preq.login_name IS NOT NULL - THEN CONCAT(ft.name, '?(', preq.login_name, ')') - ELSE CONCAT(ft.name, prev.status) + THEN $flag_removed_req + ELSE $flag_removed END AS removed, CASE WHEN fsa.status = 'X' THEN '' WHEN fsa.status = '?' AND req.login_name IS NOT NULL - THEN CONCAT(ft.name, '?(', req.login_name, ')') - ELSE CONCAT(ft.name, fsa.status) + THEN $flag_added_req + ELSE $flag_added END AS added, changer.login_name AS who, NULL AS comment_id From 715c65fe348006399aa131d326db0636c44aa716 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 12 Nov 2026 20:22:22 +0100 Subject: [PATCH 40/50] Bug 2085398 - Use sql_string_concat for the flag names in BugMail _get_diffs --- Bugzilla/BugMail.pm | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Bugzilla/BugMail.pm b/Bugzilla/BugMail.pm index 9258d62a6f..62972d1aee 100644 --- a/Bugzilla/BugMail.pm +++ b/Bugzilla/BugMail.pm @@ -1051,6 +1051,12 @@ sub _get_diffs { = Bugzilla::FlagActivity->bugs_activity_before_cutoff_sql('bugs_activity'); my $flag_last = Bugzilla::FlagActivity->last_of_request_sql('fsa'); my $flag_prev_id = Bugzilla::FlagActivity->previous_id_sql('fsa'); + my $flag_old_req + = $dbh->sql_string_concat('ft.name', q{'?('}, 'preq.login_name', q{')'}); + my $flag_old = $dbh->sql_string_concat('ft.name', 'prev.status'); + my $flag_new_req + = $dbh->sql_string_concat('ft.name', q{'?('}, 'req.login_name', q{')'}); + my $flag_new = $dbh->sql_string_concat('ft.name', 'fsa.status'); # If lastdiffed is NULL, then we don't limit the search on time. my $when_restriction = ''; @@ -1080,13 +1086,13 @@ sub _get_diffs { fsa.flag_when AS bug_when, CASE WHEN prev.id IS NULL THEN '' WHEN prev.status = '?' AND preq.login_name IS NOT NULL - THEN CONCAT(ft.name, '?(', preq.login_name, ')') - ELSE CONCAT(ft.name, prev.status) + THEN $flag_old_req + ELSE $flag_old END AS old, CASE WHEN fsa.status = 'X' THEN '' WHEN fsa.status = '?' AND req.login_name IS NOT NULL - THEN CONCAT(ft.name, '?(', req.login_name, ')') - ELSE CONCAT(ft.name, fsa.status) + THEN $flag_new_req + ELSE $flag_new END AS new, fsa.attachment_id AS attach_id, NULL AS comment_id, From dc040116027adb6f8eea83e1e62bd6bb0f7e53bd Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 12 Nov 2026 20:31:06 +0100 Subject: [PATCH 41/50] Bug 2085398 - Use sql_string_concat for the flag names in the user activity report --- extensions/BMO/lib/Reports/UserActivity.pm | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/extensions/BMO/lib/Reports/UserActivity.pm b/extensions/BMO/lib/Reports/UserActivity.pm index 04b3c4322b..b8690b5790 100644 --- a/extensions/BMO/lib/Reports/UserActivity.pm +++ b/extensions/BMO/lib/Reports/UserActivity.pm @@ -122,6 +122,12 @@ sub report { = Bugzilla::FlagActivity->bugs_activity_before_cutoff_sql('bugs_activity'); my $flag_last = Bugzilla::FlagActivity->last_of_request_sql('fsa'); my $flag_prev_id = Bugzilla::FlagActivity->previous_id_sql('fsa'); + my $flag_removed_req = $dbh->sql_string_concat('ft.name', 'prev.status', + q{'('}, 'preq.login_name', q{')'}); + my $flag_removed = $dbh->sql_string_concat('ft.name', 'prev.status'); + my $flag_added_req = $dbh->sql_string_concat('ft.name', 'fsa.status', + q{'('}, 'req.login_name', q{')'}); + my $flag_added = $dbh->sql_string_concat('ft.name', 'fsa.status'); my $query = " SELECT fielddefs.name, @@ -158,13 +164,13 @@ sub report { . " AS ts, CASE WHEN prev.id IS NULL THEN '' WHEN prev.status = CHAR(63) AND preq.login_name IS NOT NULL - THEN CONCAT(ft.name, prev.status, '(', preq.login_name, ')') - ELSE CONCAT(ft.name, prev.status) + THEN $flag_removed_req + ELSE $flag_removed END AS removed, CASE WHEN fsa.status = 'X' THEN '' WHEN fsa.status = CHAR(63) AND req.login_name IS NOT NULL - THEN CONCAT(ft.name, fsa.status, '(', req.login_name, ')') - ELSE CONCAT(ft.name, fsa.status) + THEN $flag_added_req + ELSE $flag_added END AS added, profiles.login_name, NULL AS comment_id, From c1c4690e63bf8dc2cd09b16b100498b979a08cf3 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 12 Nov 2026 20:46:43 +0100 Subject: [PATCH 42/50] Bug 2085398 - Run perltidy on the files this PR changes --- Bugzilla/Attachment.pm | 2 +- Bugzilla/Bug.pm | 282 +++++++++-------- Bugzilla/BugMail.pm | 202 ++++++------ Bugzilla/DB/Schema.pm | 94 +++--- Bugzilla/DB/Sqlite.pm | 2 +- Bugzilla/FlagActivity.pm | 10 +- Bugzilla/FlagType.pm | 7 +- Bugzilla/Install/DB.pm | 102 +++--- Bugzilla/Search.pm | 296 ++++++++++-------- Bugzilla/Test/Util.pm | 19 +- editusers.cgi | 18 +- extensions/BMO/lib/Reports/ReleaseTracking.pm | 10 +- extensions/BMO/lib/Reports/UserActivity.pm | 24 +- extensions/BugModal/lib/ActivityStream.pm | 21 +- extensions/TagNewUsers/Extension.pm | 2 +- extensions/UserProfile/lib/Util.pm | 12 +- qa/t/2_test_flags.t | 5 +- qa/t/4_test_user_delete_requestee.t | 17 +- t/bmo/bugmail-flag-events.t | 161 ++++++---- t/flag-activity-history.t | 22 +- t/flag-activity-search.t | 85 ++--- t/flag-activity-start-rows.t | 18 +- t/flag-activity-user-stats.t | 59 ++-- t/flag-activity-who.t | 2 +- t/flag-type-delete.t | 8 +- 25 files changed, 814 insertions(+), 666 deletions(-) diff --git a/Bugzilla/Attachment.pm b/Bugzilla/Attachment.pm index 1896b6ef7d..555629eb5c 100644 --- a/Bugzilla/Attachment.pm +++ b/Bugzilla/Attachment.pm @@ -115,7 +115,7 @@ use constant UPDATE_VALIDATORS => # and this one answer roughly the same question but fail in opposite # directions, so their memberships differ on purpose -- the reasoning is # written out at CSP_DOCUMENT_TYPES. - my %_SAFE_INLINE_TYPES = map { $_ => 1 } qw( +my %_SAFE_INLINE_TYPES = map { $_ => 1 } qw( image/png image/jpeg image/gif diff --git a/Bugzilla/Bug.pm b/Bugzilla/Bug.pm index d48bc8d106..a3a5f9db1a 100644 --- a/Bugzilla/Bug.pm +++ b/Bugzilla/Bug.pm @@ -35,8 +35,8 @@ use Bugzilla::BugUserLastVisit; use Date::Parse; use DateTime; use List::MoreUtils qw(firstidx uniq part any); -use List::Util qw(min max first); -use Storable qw(dclone); +use List::Util qw(min max first); +use Storable qw(dclone); use URI; use URI::QueryParam; use Scalar::Util qw(blessed weaken); @@ -74,7 +74,7 @@ use constant USE_MEMCACHED => 0; # This is a sub because it needs to call other subroutines. sub DB_COLUMNS { - my $dbh = Bugzilla->dbh; + my $dbh = Bugzilla->dbh; my @custom = grep { $_->type != FIELD_TYPE_MULTI_SELECT } Bugzilla->active_custom_fields({skip_extensions => 1}); my @custom_names = map { $_->name } @custom; @@ -108,7 +108,7 @@ sub DB_COLUMNS { status_whiteboard target_milestone version - ), 'reporter AS reporter_id', + ), 'reporter AS reporter_id', $dbh->sql_date_format('deadline', '%Y-%m-%d') . ' AS deadline', @custom_names ); @@ -219,7 +219,7 @@ sub UPDATE_COLUMNS { my @custom = grep { $_->type != FIELD_TYPE_MULTI_SELECT } Bugzilla->active_custom_fields({skip_extensions => 1}); my @custom_names = map { $_->name } @custom; - my @columns = qw( + my @columns = qw( alias assigned_to bug_file_loc @@ -391,7 +391,7 @@ sub initialize { sub object_cache_key { my $class = shift; - my $key = $class->SUPER::object_cache_key(@_) || return; + my $key = $class->SUPER::object_cache_key(@_) || return; return $key . ',' . Bugzilla->user->id; } @@ -498,7 +498,7 @@ sub match { # so include them in the list if they have been specified. if (exists $params->{"${field}_id"}) { my $current_ids = $params->{"${field}_id"}; - my @id_array = ref $current_ids ? @$current_ids : ($current_ids); + my @id_array = ref $current_ids ? @$current_ids : ($current_ids); push(@ids, @id_array); } @@ -606,9 +606,9 @@ sub possible_duplicates { my ($class, $params) = @_; my $short_desc = $params->{summary}; my $products = $params->{products} || []; - my $limit = $params->{limit} || MAX_POSSIBLE_DUPLICATES; + my $limit = $params->{limit} || MAX_POSSIBLE_DUPLICATES; $limit = MAX_POSSIBLE_DUPLICATES if $limit > MAX_POSSIBLE_DUPLICATES; - $products = [$products] if !ref($products) || ref($products) ne 'ARRAY'; + $products = [$products] if !ref($products) || ref($products) ne 'ARRAY'; my $orig_limit = $limit; detaint_natural($limit) @@ -641,7 +641,7 @@ sub possible_duplicates { foreach my $word (@words) { my ($term, $rel_term) = $dbh->sql_fulltext_search('bugs_fulltext.short_desc', $word); - push(@where, $term); + push(@where, $term); push(@relevance, $rel_term || $term); } @@ -841,7 +841,7 @@ sub create { if ($see_also) { my $see_also_array = $see_also; if (!ref $see_also_array) { - $see_also = trim($see_also); + $see_also = trim($see_also); $see_also_array = [split(/[\s,]+/, $see_also)]; } foreach my $value (@$see_also_array) { @@ -986,8 +986,8 @@ sub update { foreach my $field (qw(qa_contact assigned_to)) { if ($changes->{$field}) { my ($from, $to) = @{$changes->{$field}}; - $from = $old_bug->$field->login if $from; - $to = $self->$field->login if $to; + $from = $old_bug->$field->login if $from; + $to = $self->$field->login if $to; $changes->{$field} = [$from, $to]; } } @@ -1048,8 +1048,8 @@ sub update { my ($table, $field1, $field2) = @$rel; foreach my $pair ([$field1, $field2], [$field2, $field1]) { - my ($field, $other_field) = @$pair; - my ($removed, $added) = diff_arrays($old_bug->$field, $self->$field); + my ($field, $other_field) = @$pair; + my ($removed, $added) = diff_arrays($old_bug->$field, $self->$field); foreach my $id (@$removed) { $dbh->do("DELETE FROM $table WHERE $field = ? AND $other_field = ?", @@ -1105,10 +1105,8 @@ sub update { # we only audit when bugs protected with a secure-mail enabled group # are made public - if ( - !scalar @{$self->groups_in} && any { $old_groups{$_}->secure_mail } - @$removed_gr - ) + if (!scalar @{$self->groups_in} && any { $old_groups{$_}->secure_mail } + @$removed_gr) { Bugzilla->audit(sprintf( '%s made Bug %s public (%s)', @@ -1737,6 +1735,7 @@ sub _check_bug_type { my $type_required = Bugzilla->params->{'require_bug_type'}; if (defined $type && trim($type)) { + # Treat '--' type value same as empty if ($type_required && $type eq '--') { ThrowUserError('bug_type_required'); @@ -1756,7 +1755,8 @@ sub _check_bug_type { : undef; my $component = ($product && defined $params->{component}) - ? Bugzilla::Component->new({name => $params->{component}, product => $product, cache => 1}) + ? Bugzilla::Component->new( + {name => $params->{component}, product => $product, cache => 1}) : undef; return $component @@ -1826,7 +1826,7 @@ sub _check_component { $name || ThrowUserError("require_component"); my $product = blessed($invocant) ? $invocant->product_obj : $params->{product}; my $old_comp = blessed($invocant) ? $invocant->component : ''; - my $object = Bugzilla::Component->check({product => $product, name => $name}); + my $object = Bugzilla::Component->check({product => $product, name => $name}); if ($object->name ne $old_comp && !$object->is_active) { ThrowUserError('value_inactive', {class => ref($object), value => $name}); } @@ -1941,7 +1941,7 @@ sub _check_relationship { # And finally, check for dependency loops. my $bug_id = ref($invocant) ? $invocant->id : 0; - my %deps = validate_relationship($bug_id, $table, %deps_in); + my %deps = validate_relationship($bug_id, $table, %deps_in); return %deps; } @@ -2069,7 +2069,7 @@ sub _check_keywords { my $keyword_array = $keywords_in; if (!ref $keyword_array) { - $keywords_in = trim($keywords_in); + $keywords_in = trim($keywords_in); $keyword_array = [split(/[\s,]+/, $keywords_in)]; } @@ -2143,7 +2143,7 @@ sub _check_qa_contact { # the QA Contact for validity. if (!defined $id && $qa_contact) { $qa_contact = Bugzilla::User->check($qa_contact) if !ref $qa_contact; - $id = $qa_contact->id; + $id = $qa_contact->id; # create() checks this another way, so we don't have to run this # check during create(). @@ -2195,7 +2195,7 @@ sub _check_resolution { && (!$self->resolution || $resolution ne $self->resolution) && scalar @{$self->dependson}) { - my $dep_bugs = Bugzilla::Bug->new_from_list($self->dependson); + my $dep_bugs = Bugzilla::Bug->new_from_list($self->dependson); my $count_open = grep { $_->isopened } @$dep_bugs; if ($count_open) { ThrowUserError("still_unresolved_bugs", @@ -2619,7 +2619,7 @@ sub set_all { # (because currently there is no way to check for duplicate loops in that # situation). You also cannot set the alias of several bugs at once. if ($params->{other_bugs} and scalar @{$params->{other_bugs}} > 1) { - ThrowUserError('dupe_not_allowed') if exists $params->{dup_id}; + ThrowUserError('dupe_not_allowed') if exists $params->{dup_id}; ThrowUserError('multiple_alias_not_allowed') if defined $params->{alias}; } @@ -2723,9 +2723,9 @@ sub set_all { # glitch where e.g. cf_crash_signature could be stored on a product that # doesn't expose it. my @custom_fields - = grep { $_->type != FIELD_TYPE_EXTENSION } Bugzilla->active_custom_fields( - {product => $self->product_obj, component => $self->component_obj} - ); + = grep { $_->type != FIELD_TYPE_EXTENSION } + Bugzilla->active_custom_fields( + {product => $self->product_obj, component => $self->component_obj}); foreach my $field (@custom_fields) { my $fname = $field->name; if (exists $params->{$fname}) { @@ -2953,6 +2953,7 @@ sub _set_product { # themselves changed. if ($product_changed) { + # Try to set each value with the new product. # Have to set error_mode because Throw*Error calls exit() otherwise. # We will do this for both browser and non-browser clients. @@ -2985,7 +2986,7 @@ sub _set_product { my $verified = $params->{product_change_confirmed}; - # BMO - if everything is ok then we can skip the verfication page when using bug_modal +# BMO - if everything is ok then we can skip the verfication page when using bug_modal if ( (($params->{format} && $params->{format} eq 'modal') || !$verified) && $component_ok && $version_ok @@ -3055,8 +3056,9 @@ sub _set_product { } else { # If we are not in the browser, we just die if certain values are invalid. - $self->set_component($comp_name) if !$component_ok; + $self->set_component($comp_name) if !$component_ok; $self->set_target_milestone($tm_name) if !$milestone_ok; + # If version is not valid, then set to default version instead of dying. $self->set_version($product->default_version) if !$version_ok; } @@ -3087,8 +3089,9 @@ sub _set_product { # verify-new-product UI via _check_default_product_security_group() in the # BMO extension. my @pre_move_names = map { $_->name } @current_groups; - for my $sec_group ( - _target_security_group_when_dropping($product, \@pre_move_names, $dropped_group)) + for my $sec_group (_target_security_group_when_dropping( + $product, \@pre_move_names, $dropped_group + )) { $self->add_group($sec_group); } @@ -3125,7 +3128,7 @@ sub reset_qa_contact { sub set_remaining_time { $_[0]->set('remaining_time', $_[1]); } # Used only when closing a bug or moving between closed states. -sub _zero_remaining_time { $_[0]->{'remaining_time'} = 0; } +sub _zero_remaining_time { $_[0]->{'remaining_time'} = 0; } sub set_reporter_accessible { $_[0]->set('reporter_accessible', $_[1]); } sub set_resolution { @@ -3218,6 +3221,7 @@ sub set_bug_status { } sub set_type { + # The bug_type table column is nullable, but make sure to use the default type # in case it's not set $_[0]->set('bug_type', $_[1] || Bugzilla->params->{'default_bug_type'}); @@ -3269,7 +3273,7 @@ sub add_comment { $params ||= {}; # Fill out info that doesn't change and callers may not pass in - $params->{'bug_id'} = $self; + $params->{'bug_id'} = $self; $params->{'thetext'} = defined($comment) ? $comment : ''; # Validate all the entered data @@ -3336,7 +3340,7 @@ sub modify_keywords { @result = sort { lc($a->name) cmp lc($b->name) } @result; if ($any_changes) { - my $new = join(', ', (map { $_->name } @result)); + my $new = join(', ', (map { $_->name } @result)); my $can_change = $self->check_can_change_field('keywords', 0, 1); $can_change->{allowed} || ThrowUserError( 'illegal_change', @@ -3361,7 +3365,7 @@ sub add_group { # So we have to store and pass the name as entered by the user to # the error message, if we have it. my $group_name = blessed($group) ? $group->name : $group; - my $args = { + my $args = { name => $group_name, product => $self->product, bug_id => $self->id, @@ -3403,7 +3407,7 @@ sub remove_group { # See add_group() for the reason why we store the user input. my $group_name = blessed($group) ? $group->name : $group; - my $args = { + my $args = { name => $group_name, product => $self->product, bug_id => $self->id, @@ -3490,7 +3494,8 @@ sub add_see_also { # If this is a link to a local bug then save the # ref bug id for sending changes email. if (my $ref_bug = delete $field_values->{ref_bug}) { - my $ref_can_change = $ref_bug->check_can_change_field('see_also', '', $self->id); + my $ref_can_change + = $ref_bug->check_can_change_field('see_also', '', $self->id); if ( $class->isa('Bugzilla::BugUrl::Local') and !$skip_recursion and Bugzilla->user->can_see_bug($ref_bug->id) @@ -3726,8 +3731,8 @@ sub _resolve_ultimate_dup_id { # prevent them from marking this bug as a duplicate. return $last_dup if exists $dupes{$this_dup}; $dupes{$this_dup} = 1; - $last_dup = $this_dup; - $this_dup = $dbh->selectrow_array($sth, undef, $this_dup); + $last_dup = $this_dup; + $this_dup = $dbh->selectrow_array($sth, undef, $this_dup); } return $last_dup; @@ -3772,7 +3777,7 @@ sub any_flags_requesteeble { sub attachments { my ($self) = @_; return $self->{'attachments'} if exists $self->{'attachments'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; $self->{'attachments'} = Bugzilla::Attachment->get_attachments_by_bug($self, {preload => 1}); @@ -3782,7 +3787,7 @@ sub attachments { sub assigned_to { my ($self) = @_; return $self->{'assigned_to_obj'} if exists $self->{'assigned_to_obj'}; - $self->{'assigned_to'} = 0 if $self->{'error'}; + $self->{'assigned_to'} = 0 if $self->{'error'}; return $self->{'assigned_to_obj'} = new Bugzilla::User({id => $self->{'assigned_to'}, cache => 1}); } @@ -3790,7 +3795,7 @@ sub assigned_to { sub blocked { my ($self) = @_; return $self->{'blocked'} if exists $self->{'blocked'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; $self->{'blocked'} = list_relationship('dependencies', 'dependson', 'blocked', $self->bug_id); return $self->{'blocked'}; @@ -3820,7 +3825,7 @@ sub related_bugs { sub cc { my ($self) = @_; return $self->{'cc'} if exists $self->{'cc'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; my $dbh = Bugzilla->dbh; $self->{'cc'} = $dbh->selectcol_arrayref( @@ -3839,7 +3844,7 @@ sub cc { sub cc_users { my $self = shift; return $self->{'cc_users'} if exists $self->{'cc_users'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; my $dbh = Bugzilla->dbh; my $cc_ids = $dbh->selectcol_arrayref('SELECT who FROM cc WHERE bug_id = ?', @@ -3859,7 +3864,7 @@ sub component { sub component_obj { my ($self) = @_; return $self->{component_obj} if defined $self->{component_obj}; - return {} if $self->{error}; + return {} if $self->{error}; $self->{component_obj} = new Bugzilla::Component({id => $self->{component_id}, cache => 1}); return $self->{component_obj}; @@ -3868,7 +3873,7 @@ sub component_obj { sub classification_id { my ($self) = @_; return $self->{classification_id} if exists $self->{classification_id}; - return 0 if $self->{error}; + return 0 if $self->{error}; ($self->{classification_id}) = Bugzilla->dbh->selectrow_array( 'SELECT classification_id FROM products WHERE id = ?', @@ -3879,7 +3884,7 @@ sub classification_id { sub classification { my ($self) = @_; return $self->{classification} if exists $self->{classification}; - return '' if $self->{error}; + return '' if $self->{error}; ($self->{classification}) = Bugzilla->dbh->selectrow_array( 'SELECT name FROM classifications WHERE id = ?', @@ -3890,7 +3895,7 @@ sub classification { sub dependson { my ($self) = @_; return $self->{'dependson'} if exists $self->{'dependson'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; $self->{'dependson'} = list_relationship('dependencies', 'blocked', 'dependson', $self->bug_id); return $self->{'dependson'}; @@ -3905,7 +3910,7 @@ sub depends_on_obj { sub duplicates { my $self = shift; return $self->{duplicates} if exists $self->{duplicates}; - return [] if $self->{error}; + return [] if $self->{error}; $self->{duplicates} = Bugzilla::Bug->new_from_list($self->duplicate_ids); return $self->{duplicates}; } @@ -3913,7 +3918,7 @@ sub duplicates { sub duplicate_ids { my $self = shift; return $self->{duplicate_ids} if exists $self->{duplicate_ids}; - return [] if $self->{error}; + return [] if $self->{error}; my $dbh = Bugzilla->dbh; $self->{duplicate_ids} @@ -3925,7 +3930,7 @@ sub duplicate_ids { sub flag_types { my ($self) = @_; return $self->{'flag_types'} if exists $self->{'flag_types'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; my $vars = { target_type => 'bug', @@ -3962,7 +3967,7 @@ sub is_closed_for { return 0; } - my $past_time = DateTime->now()->subtract(%$duration)->epoch(); + my $past_time = DateTime->now()->subtract(%$duration)->epoch(); my $closed_time = str2time($self->cf_last_resolved); return $past_time > $closed_time; @@ -3977,7 +3982,7 @@ sub keywords { sub keyword_objects { my $self = shift; return $self->{'keyword_objects'} if defined $self->{'keyword_objects'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; my $dbh = Bugzilla->dbh; my $ids @@ -3996,14 +4001,15 @@ sub has_keyword { sub add_keyword { my ($self, $keyword) = @_; return if $self->has_keyword($keyword); - push @{$self->{'keyword_objects'}}, Bugzilla::Keyword->check({name => $keyword}); + push @{$self->{'keyword_objects'}}, + Bugzilla::Keyword->check({name => $keyword}); } sub del_keyword { my ($self, $keyword) = @_; $keyword = lc($keyword); my $index = -1; - my $i = 0; + my $i = 0; foreach my $keyword_obj (@{$self->keyword_objects}) { if (lc($keyword_obj->name) eq $keyword) { $index = $i; @@ -4073,11 +4079,12 @@ sub comment_count { sub counts { my ($self) = @_; - my $dbh = Bugzilla->dbh; - my $extra = !Bugzilla->user->is_insider ? 'AND isprivate = 0' : ''; - my $id = $self->id; + my $dbh = Bugzilla->dbh; + my $extra = !Bugzilla->user->is_insider ? 'AND isprivate = 0' : ''; + my $id = $self->id; - $self->{counts} ||= $dbh->selectrow_hashref("SELECT + $self->{counts} ||= $dbh->selectrow_hashref( + "SELECT (SELECT COUNT(*) FROM attachments WHERE bug_id = ? $extra) AS attachments, (SELECT COUNT(*) FROM cc WHERE bug_id = ?) AS cc, (SELECT COUNT(*) FROM longdescs WHERE bug_id = ? $extra) AS comments, @@ -4087,7 +4094,8 @@ sub counts { (SELECT COUNT(*) FROM regressions WHERE regressed_by = ?) AS regressed_by, (SELECT COUNT(*) FROM regressions WHERE regresses = ?) AS regressions, (SELECT COUNT(*) FROM duplicates WHERE dupe_of = ?) AS duplicates - ", undef, $id, $id, $id, $id, $id, $id, $id, $id, $id); + ", undef, $id, $id, $id, $id, $id, $id, $id, $id, $id + ); return $self->{counts}; } @@ -4126,7 +4134,7 @@ sub product_obj { sub qa_contact { my ($self) = @_; return $self->{'qa_contact_obj'} if exists $self->{'qa_contact_obj'}; - return undef if $self->{'error'}; + return undef if $self->{'error'}; if (Bugzilla->params->{'useqacontact'} && $self->{'qa_contact'}) { $self->{'qa_contact_obj'} @@ -4144,7 +4152,7 @@ sub qa_contact { sub regressed_by { my ($self) = @_; return $self->{'regressed_by'} if exists $self->{'regressed_by'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; $self->{'regressed_by'} = list_relationship('regressions', 'regresses', 'regressed_by', $self->bug_id); @@ -4160,7 +4168,7 @@ sub regressed_by_obj { sub regresses { my ($self) = @_; return $self->{'regresses'} if exists $self->{'regresses'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; $self->{'regresses'} = list_relationship('regressions', 'regressed_by', 'regresses', $self->bug_id); @@ -4216,7 +4224,7 @@ sub status { sub statuses_available { my $self = shift; - return [] if $self->{'error'}; + return [] if $self->{'error'}; return $self->{'statuses_available'} if defined $self->{'statuses_available'}; my @statuses = @{$self->status->can_change_to}; @@ -4275,7 +4283,7 @@ sub show_attachment_flags { sub groups { my $self = shift; return $self->{'groups'} if exists $self->{'groups'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; my $dbh = Bugzilla->dbh; my @groups; @@ -4294,7 +4302,8 @@ sub groups { . " THEN 1 ELSE 0 END," . " CASE WHEN groups.id IN($grouplist) THEN 1 ELSE 0 END," . " isactive, membercontrol, othercontrol" - . " FROM " . $dbh->quote_identifier('groups') + . " FROM " + . $dbh->quote_identifier('groups') . " LEFT JOIN bug_group_map" . " ON bug_group_map.group_id = groups.id" . " AND bug_id = ?" @@ -4319,10 +4328,12 @@ sub groups { # the user is a member of the group. if ( $ison - || ( $isactive + || ( + $isactive && $ingroup && ( ($membercontrol == CONTROLMAPDEFAULT) - || ($membercontrol == CONTROLMAPSHOWN))) + || ($membercontrol == CONTROLMAPSHOWN)) + ) ) { my $ismandatory = $isactive && ($membercontrol == CONTROLMAPMANDATORY); @@ -4393,7 +4404,7 @@ sub groups { sub groups_in { my $self = shift; return $self->{'groups_in'} if exists $self->{'groups_in'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; my $group_ids = Bugzilla->dbh->selectcol_arrayref( 'SELECT group_id FROM bug_group_map WHERE bug_id = ?', @@ -4454,7 +4465,7 @@ sub _target_security_group_when_dropping { sub user { my $self = shift; return $self->{'user'} if exists $self->{'user'}; - return {} if $self->{'error'}; + return {} if $self->{'error'}; my $user = Bugzilla->user; @@ -4480,7 +4491,7 @@ sub user { sub choices { my $self = shift; return $self->{'choices'} if exists $self->{'choices'}; - return {} if $self->{'error'}; + return {} if $self->{'error'}; my $user = Bugzilla->user; my @products = @{$user->get_enterable_products}; @@ -4561,8 +4572,7 @@ sub delta_ts_non_bot { # Grab the latest timestamp for comments, bug changes and flag changes that # was not made by one of the accounts in the bot skip list. if (my $bot_ids_string = $cache->{bot_ids_string}) { - my $flag_newer - = Bugzilla::FlagActivity->after_cutoff_sql('flag_activity'); + my $flag_newer = Bugzilla::FlagActivity->after_cutoff_sql('flag_activity'); ($self->{delta_ts_non_bot}) = $dbh->selectrow_array( "SELECT MAX(bug_when) FROM (SELECT MAX(bug_when) AS bug_when @@ -4576,8 +4586,7 @@ sub delta_ts_non_bot { SELECT MAX(flag_when) AS bug_when FROM flag_activity WHERE bug_id = ? AND who NOT IN ($bot_ids_string) - AND $flag_newer) tmp", - undef, ($self->id) x 3 + AND $flag_newer) tmp", undef, ($self->id) x 3 ); } @@ -4610,14 +4619,14 @@ sub to_hash { $hash->{assignee} = $self->assigned_to->to_hash; } if ($params->{blocks}) { - $hash->{blocks} =$self->blocked; + $hash->{blocks} = $self->blocked; } if ($params->{classification}) { - $hash->{classification} =$self->classification; + $hash->{classification} = $self->classification; } if ($params->{comments}) { my @result; - my $comments =$self->comments( + my $comments = $self->comments( {order => 'oldest_to_newest', after => $params->{new_since}}); foreach my $comment (@$comments) { next if $comment->is_private && !$user->is_insider; @@ -4626,32 +4635,32 @@ sub to_hash { $hash->{comments} = \@result; } if ($params->{component}) { - $hash->{component} =$self->component; + $hash->{component} = $self->component; } if ($params->{cc}) { $hash->{cc} = [map { $_->to_hash } @{$self->cc_users}]; } if ($params->{creation_time}) { - $hash->{creation_time} =$self->creation_ts; + $hash->{creation_time} = $self->creation_ts; } if ($params->{creator}) { $hash->{creator} = $self->reporter->to_hash; } if ($params->{depends_on}) { - $hash->{depends_on} =$self->dependson; + $hash->{depends_on} = $self->dependson; } if ($params->{description}) { - my $comment = Bugzilla::Comment->match({bug_id =>$self->id, LIMIT => 1})->[0]; + my $comment = Bugzilla::Comment->match({bug_id => $self->id, LIMIT => 1})->[0]; $hash->{description} = ($comment && (!$comment->is_private || Bugzilla->user->is_insider)) ? $comment->body : ''; } if ($params->{dupe_of}) { - $hash->{dupe_of} =$self->dup_id; + $hash->{dupe_of} = $self->dup_id; } if ($params->{duplicates}) { - $hash->{duplicates} =$self->duplicates; + $hash->{duplicates} = $self->duplicates; } if ($params->{groups}) { my @groups; @@ -4662,19 +4671,19 @@ sub to_hash { $hash->{groups} = \@groups; } if ($params->{is_open}) { - $hash->{is_open} =$self->status->is_open ? 1 : 0; + $hash->{is_open} = $self->status->is_open ? 1 : 0; } if ($params->{keywords}) { $hash->{keywords} = [map { $_->name } @{$self->keyword_objects}]; } if ($params->{last_change_time}) { - $hash->{last_change_time} =$self->delta_ts; + $hash->{last_change_time} = $self->delta_ts; } if ($params->{last_change_time_non_bot}) { - $hash->{last_change_time_non_bot} =$self->delta_ts_non_bot; + $hash->{last_change_time_non_bot} = $self->delta_ts_non_bot; } if ($params->{product}) { - $hash->{product} =$self->product; + $hash->{product} = $self->product; } if ($params->{qa_contact}) { if ($self->qa_contact) { @@ -4682,7 +4691,7 @@ sub to_hash { } } if ($params->{triage_owner}) { - my $triage_owner =$self->component_obj->triage_owner; + my $triage_owner = $self->component_obj->triage_owner; if ($triage_owner->login) { $hash->{triage_owner} = $triage_owner->to_hash; } @@ -4697,49 +4706,49 @@ sub to_hash { # Regressions if (Bugzilla->params->{use_regression_fields}) { if ($params->{regressed_by}) { - $hash->{regressed_by} =$self->regressed_by; + $hash->{regressed_by} = $self->regressed_by; } if ($params->{regressions}) { - $hash->{regressions} =$self->regresses; + $hash->{regressions} = $self->regresses; } } # Custom fields my @custom_fields = Bugzilla->active_custom_fields( { - product =>$self->product_obj, - component =>$self->component_obj, - bug_id =>$self->id + product => $self->product_obj, + component => $self->component_obj, + bug_id => $self->id }, ); foreach my $field (@custom_fields) { my $name = $field->name; next if !$params->{$name}; - $hash->{$name} =$self->$name; + $hash->{$name} = $self->$name; } # Timetracking fields if ($user->is_timetracker) { if ($params->{estimated_time}) { - $hash->{estimated_time} =$self->estimated_time; + $hash->{estimated_time} = $self->estimated_time; } if ($params->{remaining_time}) { - $hash->{remaining_time} =$self->remaining_time; + $hash->{remaining_time} = $self->remaining_time; } if ($params->{deadline}) { - $hash->{deadline} =$self->deadline; + $hash->{deadline} = $self->deadline; } if ($params->{actual_time}) { - $hash->{actual_time} =$self->actual_time; + $hash->{actual_time} = $self->actual_time; } } # Bug accessibility if ($params->{is_cc_accessible}) { - $hash->{is_cc_accessible} =$self->cclist_accessible ? 1 : 0; + $hash->{is_cc_accessible} = $self->cclist_accessible ? 1 : 0; } if ($params->{is_creator_accessible}) { - $hash->{is_creator_accessible} =$self->reporter_accessible ? 1 : 0; + $hash->{is_creator_accessible} = $self->reporter_accessible ? 1 : 0; } # Bug mentors @@ -4750,7 +4759,7 @@ sub to_hash { return $hash; } -# True if the current user is being reminded of +# True if the current user is being reminded of # this bug on a specific date. sub is_reminded { my ($self, $user) = @_; @@ -4792,8 +4801,8 @@ sub list_relationship { ORDER BY is_open DESC, $target_field" ); - my $bug_ids = $dbh->selectcol_arrayref( - $cache->{"${target_field}_sth_$exclude_resolved"}, + my $bug_ids + = $dbh->selectcol_arrayref($cache->{"${target_field}_sth_$exclude_resolved"}, undef, $bug_id); # If user has 'editbugs', show all bug ids. @@ -4857,7 +4866,8 @@ sub GetBugActivity { my $flag_suppjoins = ""; my $flag_suppwhere = ""; if (!$user->is_insider) { - $flag_suppjoins = "LEFT JOIN attachments fa_att ON fa_att.attach_id = fsa.attachment_id"; + $flag_suppjoins + = "LEFT JOIN attachments fa_att ON fa_att.attach_id = fsa.attachment_id"; $flag_suppwhere = "AND COALESCE(fa_att.isprivate, 0) = 0"; } @@ -4911,8 +4921,8 @@ sub GetBugActivity { $suppwhere = "AND longdescs.isprivate = 0"; } - $bug_when - = $dbh->sql_date_format('longdescs_tags_activity.bug_when', '%Y-%m-%d %H:%i:%s'); + $bug_when = $dbh->sql_date_format('longdescs_tags_activity.bug_when', + '%Y-%m-%d %H:%i:%s'); $query .= " UNION ALL SELECT 'comment_tag' AS field_name, @@ -4991,11 +5001,11 @@ sub GetBugActivity { Bugzilla::Hook::process( 'get_bug_activity', { - bug_id => $bug_id, - attach_id => $attach_id, - start_time => $starttime, + bug_id => $bug_id, + attach_id => $attach_id, + start_time => $starttime, include_comment_activity => $include_comment_activity, - list => \@$list + list => \@$list } ); @@ -5008,19 +5018,24 @@ sub GetBugActivity { my $incomplete_data = 0; foreach my $entry (@$list) { - my ($fieldname, $activity_id, $attachid, $when, $removed, $added, $who, $comment_id) + my ($fieldname, $activity_id, $attachid, $when, $removed, $added, $who, + $comment_id) = @$entry; my %change; my $activity_visible = 1; - my $last_change = @$changes[-1] || {}; + my $last_change = @$changes[-1] || {}; # Suppress any mid-air collision or duplicated change - if (( (exists $operation->{'when'} && $when eq $operation->{'when'}) - && $fieldname eq $last_change->{'fieldname'} - && $removed eq $last_change->{'removed'} - && $added eq $last_change->{'added'} - && (defined $attachid && $attachid eq $last_change->{'attachid'})) - || $removed eq $added ) + if ( + ( + (exists $operation->{'when'} && $when eq $operation->{'when'}) + && $fieldname eq $last_change->{'fieldname'} + && $removed eq $last_change->{'removed'} + && $added eq $last_change->{'added'} + && (defined $attachid && $attachid eq $last_change->{'attachid'}) + ) + || $removed eq $added + ) { next; } @@ -5033,10 +5048,7 @@ sub GetBugActivity { { $activity_visible = $user->is_timetracker; } - elsif ($fieldname eq 'longdescs.isprivate' - && !$user->is_insider - && $added) - { + elsif ($fieldname eq 'longdescs.isprivate' && !$user->is_insider && $added) { $activity_visible = 0; } else { @@ -5113,13 +5125,13 @@ sub GetBugActivity { } next if !@filtered_removed && !@filtered_added; $removed = join ', ', @filtered_removed; - $added = join', ', @filtered_added; + $added = join ', ', @filtered_added; } $operation->{'who'} = $who; $operation->{'when'} = $when; $operation->{'fieldname'} = $change{'fieldname'} = $fieldname; - $operation->{'attachid'} = $change{'attachid'} = $attachid; + $operation->{'attachid'} = $change{'attachid'} = $attachid; $change{'removed'} = $removed; $change{'added'} = $added; @@ -5173,7 +5185,10 @@ sub _join_activity_entries { return $current_change . $new_change; } else { - return $current_change . ($current_change && $new_change ? ' ' : '') . $new_change; + return + $current_change + . ($current_change && $new_change ? ' ' : '') + . $new_change; } } @@ -5192,7 +5207,7 @@ sub LogActivityEntry { if (length($removestr) > MAX_LINE_LENGTH) { my $commaposition = find_wrap_point($removed, MAX_LINE_LENGTH); $removestr = substr($removed, 0, $commaposition); - $removed = substr($removed, $commaposition); + $removed = substr($removed, $commaposition); } else { $removed = ""; # no more entries @@ -5200,7 +5215,7 @@ sub LogActivityEntry { if (length($addstr) > MAX_LINE_LENGTH) { my $commaposition = find_wrap_point($added, MAX_LINE_LENGTH); $addstr = substr($added, 0, $commaposition); - $added = substr($added, $commaposition); + $added = substr($added, $commaposition); } else { $added = ""; # no more entries @@ -5350,14 +5365,13 @@ sub check_can_change_field { # Allow anyone to enter/select the summary, component, bug type and platform # when filing a new bug, because some are required fields if (!$self->id) { - if ( - $field eq 'short_desc' + if ( $field eq 'short_desc' || $field eq 'component' || $field eq 'version' || $field eq 'rep_platform' || $field eq 'op_sys' - || ($field eq 'bug_type' && Bugzilla->params->{'require_bug_type'}) - ) { + || ($field eq 'bug_type' && Bugzilla->params->{'require_bug_type'})) + { return {allowed => 1}; } } diff --git a/Bugzilla/BugMail.pm b/Bugzilla/BugMail.pm index 62972d1aee..383fa1d436 100644 --- a/Bugzilla/BugMail.pm +++ b/Bugzilla/BugMail.pm @@ -26,7 +26,7 @@ use Bugzilla::Hook; use Date::Parse; use Date::Format; -use Scalar::Util qw(blessed); +use Scalar::Util qw(blessed); use List::MoreUtils qw(uniq firstidx); use Sys::Hostname; use Storable qw(dclone); @@ -66,7 +66,7 @@ sub Send { # Bugzilla::User objects of people in various roles. More than one person # can 'have' a role, if the person in that role has changed, or people are # watching. - my @assignees = ($bug->assigned_to); + my @assignees = ($bug->assigned_to); my @qa_contacts = $bug->qa_contact || (); my @ccs = @{$bug->cc_users}; @@ -215,6 +215,7 @@ sub Send { # other role on the bug. Flag-type cc_list addresses (admin-configured, # bypasses per-user opt-in) are added the same way notify() used to. foreach my $event (@flag_events) { + # notify() used to skip addressees who couldn't see a private # attachment; _flag_event_visible_to() matches that so a non-insider # doesn't get added as a recipient purely to be told about a flag on an @@ -222,16 +223,16 @@ sub Send { # body either). if ($event->{action} eq 'requested') { my $requestee_id = $event->{requestee_id}; - my $requestee - = $user_cache{$requestee_id} ||= Bugzilla::User->new({id => $requestee_id, cache => 1}); + my $requestee = $user_cache{$requestee_id} + ||= Bugzilla::User->new({id => $requestee_id, cache => 1}); $event->{requestee} = $requestee; $recipients{$requestee_id}->{+REL_FLAG_REQUESTEE} = BIT_DIRECT if _flag_event_visible_to($event, $requestee); } elsif ($event->{action} eq 'answered') { my $requester_id = $event->{requester_id}; - my $requester - = $user_cache{$requester_id} ||= Bugzilla::User->new({id => $requester_id, cache => 1}); + my $requester = $user_cache{$requester_id} + ||= Bugzilla::User->new({id => $requester_id, cache => 1}); $event->{requester} = $requester; # Don't add the requester as a recipient for clearing their own @@ -368,10 +369,8 @@ sub Send { $wants_mail = 1; } else { - $wants_mail = $user->wants_bug_mail( - $bug, $relationship, $start ? \@diffs : [], - $comments, $params->{dep_only}, $changer - ); + $wants_mail = $user->wants_bug_mail($bug, $relationship, $start ? \@diffs : [], + $comments, $params->{dep_only}, $changer); } if ($wants_mail) { $rels_which_want{$relationship} = $recipients{$user_id}->{$relationship}; @@ -395,9 +394,7 @@ sub Send { # Make sure the user isn't in the nomail list, and the dep check passed. # BMO: never send emails to nobody@mozilla.org, .bugs, or .tld addresses. # This check needs to happen after the bugmail_recipients hook. - if ( $user->email_enabled - && $dep_ok - && !is_fake_recipient_address($user->email)) + if ($user->email_enabled && $dep_ok && !is_fake_recipient_address($user->email)) { # Don't show summaries for bugs the user can't access, and @@ -435,7 +432,7 @@ sub Send { # the mail some other way (e.g. they're on the CC list). A cc_list # recipient who isn't the requestee/requester gets the event as a # generic 'set', so it isn't worded as "from you"/"your request". - my $user_cc_events = $flag_type_cc->{$user_id} || []; + my $user_cc_events = $flag_type_cc->{$user_id} || []; my @user_flag_events = map { my $event = $_; my $is_own @@ -443,10 +440,10 @@ sub Send { || ($event->{action} eq 'answered' && $event->{requester_id} == $user_id && $event->{setter}->id != $user_id); - !_flag_event_visible_to($event, $user) ? () - : $is_own ? $event + !_flag_event_visible_to($event, $user) ? () + : $is_own ? $event : (grep { $_ == $event } @$user_cc_events) ? {%$event, action => 'set'} - : (); + : (); } @flag_events; # Someone mailed only because of a flag (they have no other role on @@ -454,19 +451,17 @@ sub Send { # roles) gets the flag section and the new comments, as the old flag # mail did (so a requestee sees the question), but not every diff. my $flag_only = !grep { - $_ != REL_FLAG_REQUESTEE - && $_ != REL_FLAG_REQUESTER - && $_ != REL_FLAG_TYPE_CC + $_ != REL_FLAG_REQUESTEE && $_ != REL_FLAG_REQUESTER && $_ != REL_FLAG_TYPE_CC } keys %rels_which_want; my $sent_mail = sendMail({ - to => $user, - bug => $bug, - comments => $comments, - date => $date, - changer => $changer, - watchers => exists $watching{$user_id} ? $watching{$user_id} : undef, - diffs => $flag_only ? [] : \@diffs, + to => $user, + bug => $bug, + comments => $comments, + date => $date, + changer => $changer, + watchers => exists $watching{$user_id} ? $watching{$user_id} : undef, + diffs => $flag_only ? [] : \@diffs, rels_which_want => \%rels_which_want, referenced_bugs => $flag_only ? [] : $referenced_bugs, dep_only => $params->{dep_only}, @@ -497,14 +492,14 @@ sub Send { sub sendMail { my $params = shift; - my $user = $params->{to}; - my $bug = $params->{bug}; - my @send_comments = @{$params->{comments}}; - my $date = $params->{date}; - my $changer = $params->{changer}; - my $watchingRef = $params->{watchers}; - my @diffs = @{$params->{diffs}}; - my $relRef = $params->{rels_which_want}; + my $user = $params->{to}; + my $bug = $params->{bug}; + my @send_comments = @{$params->{comments}}; + my $date = $params->{date}; + my $changer = $params->{changer}; + my $watchingRef = $params->{watchers}; + my @diffs = @{$params->{diffs}}; + my $relRef = $params->{rels_which_want}; my $referenced_bugs = $params->{referenced_bugs}; my $dep_only = $params->{dep_only}; my $blocker_short_desc = $params->{blocker_short_desc}; @@ -545,6 +540,7 @@ sub sendMail { $add_diff = 1; } if ($add_diff) { + # Diffs with a blocker object are shared across per-recipient sendMail # calls. Clone the diff+blocker so hooks (e.g. SecureMail) can mutate # short_desc for one recipient without contaminating others. @@ -560,7 +556,10 @@ sub sendMail { @send_comments = grep { !$_->is_private } @send_comments; } - if (!scalar(@display_diffs) && !scalar(@send_comments) && !scalar(@$flag_events)) { + if ( !scalar(@display_diffs) + && !scalar(@send_comments) + && !scalar(@$flag_events)) + { # Whoops, no differences! return 0; @@ -620,8 +619,8 @@ sub sendMail { changedfieldnames => \@changedfieldnames, new_comments => \@send_comments, threadingmarker => build_thread_marker($bug->id, $user->id, !$bug->lastdiffed), - referenced_bugs => $referenced_bugs, - bugmailtype => $bugmailtype, + referenced_bugs => $referenced_bugs, + bugmailtype => $bugmailtype, blocker_short_desc => $blocker_short_desc, flag_events => $flag_events, }; @@ -670,19 +669,17 @@ sub enqueue { # way, dropping the attachment object (dequeue() re-fetches it by id so # it can also re-check visibility at send time). $vars->{flag_events} = [ - map { - { - action => $_->{action}, - attachment_id => $_->{attachment_id}, - requestee_id => $_->{requestee_id}, - requester_id => $_->{requester_id}, - status => $_->{status}, - type => _flatten_object($_->{type}), - setter => _flatten_object($_->{setter}), - requestee => _flatten_object($_->{requestee}), - requester => _flatten_object($_->{requester}), - } - } @{$vars->{flag_events}} + map { { + action => $_->{action}, + attachment_id => $_->{attachment_id}, + requestee_id => $_->{requestee_id}, + requester_id => $_->{requester_id}, + status => $_->{status}, + type => _flatten_object($_->{type}), + setter => _flatten_object($_->{setter}), + requestee => _flatten_object($_->{requestee}), + requester => _flatten_object($_->{requester}), + } } @{$vars->{flag_events}} ]; Bugzilla->job_queue->insert('bug_mail', {vars => $vars}); } @@ -702,24 +699,30 @@ sub dequeue { # stale if the bug was restricted or user access revoked between enqueue # and dequeue (TOCTOU). unless ($vars->{to_user}->can_see_bug($vars->{bug}->id)) { - WARN('dequeue: suppressing bugmail for user ' - . $vars->{to_user}->login . ' on bug ' . $vars->{bug}->id - . ' — bug no longer visible at send time'); + WARN( 'dequeue: suppressing bugmail for user ' + . $vars->{to_user}->login + . ' on bug ' + . $vars->{bug}->id + . ' — bug no longer visible at send time'); return; } # Drop if user lost insider/timetracker privileges between enqueue and dequeue. # Filtering at sendMail-time may have included content these privileges gated. if ($vars->{_to_user_was_insider} && !$vars->{to_user}->is_insider) { - WARN('dequeue: suppressing bugmail for user ' - . $vars->{to_user}->login . ' on bug ' . $vars->{bug}->id - . ' — insider access revoked between enqueue and dequeue'); + WARN( 'dequeue: suppressing bugmail for user ' + . $vars->{to_user}->login + . ' on bug ' + . $vars->{bug}->id + . ' — insider access revoked between enqueue and dequeue'); return; } if ($vars->{_to_user_was_timetracker} && !$vars->{to_user}->is_timetracker) { - WARN('dequeue: suppressing bugmail for user ' - . $vars->{to_user}->login . ' on bug ' . $vars->{bug}->id - . ' — timetracker access revoked between enqueue and dequeue'); + WARN( 'dequeue: suppressing bugmail for user ' + . $vars->{to_user}->login + . ' on bug ' + . $vars->{bug}->id + . ' — timetracker access revoked between enqueue and dequeue'); return; } @@ -731,18 +734,20 @@ sub dequeue { map { +{ %$_, - type => Bugzilla::FlagType->new_from_hash($_->{type}), - setter => Bugzilla::User->new_from_hash($_->{setter}), - requestee => $_->{requestee} ? Bugzilla::User->new_from_hash($_->{requestee}) : undef, - requester => $_->{requester} ? Bugzilla::User->new_from_hash($_->{requester}) : undef, + type => Bugzilla::FlagType->new_from_hash($_->{type}), + setter => Bugzilla::User->new_from_hash($_->{setter}), + requestee => $_->{requestee} ? Bugzilla::User->new_from_hash($_->{requestee}) + : undef, + requester => $_->{requester} ? Bugzilla::User->new_from_hash($_->{requester}) + : undef, attachment => $_->{attachment_id} - ? Bugzilla::Attachment->new({id => $_->{attachment_id}, cache => 1}) - : undef, + ? Bugzilla::Attachment->new({id => $_->{attachment_id}, cache => 1}) + : undef, } } @{$vars->{flag_events} || []} ]; - $vars->{flag_events} - = [grep { _flag_event_visible_to($_, $vars->{to_user}) } @{$vars->{flag_events}}]; + $vars->{flag_events} = [grep { _flag_event_visible_to($_, $vars->{to_user}) } + @{$vars->{flag_events}}]; $vars->{changer} = Bugzilla::User->new_from_hash($vars->{changer}); $vars->{new_comments} @@ -756,10 +761,13 @@ sub dequeue { # check may be stale if the blocker was restricted between enqueue # and dequeue (TOCTOU). Suppress the email to match enqueue behavior. unless ($vars->{to_user}->can_see_bug($diff->{blocker}->id)) { - WARN('dequeue: suppressing bugmail for user ' - . $vars->{to_user}->login . ' on bug ' . $vars->{bug}->id - . ' — blocker bug ' . $diff->{blocker}->id - . ' no longer visible at send time (TOCTOU)'); + WARN( 'dequeue: suppressing bugmail for user ' + . $vars->{to_user}->login + . ' on bug ' + . $vars->{bug}->id + . ' — blocker bug ' + . $diff->{blocker}->id + . ' no longer visible at send time (TOCTOU)'); return; } } @@ -789,11 +797,14 @@ sub dequeue { } } - # Drop dep/see_also diffs that re-stripping emptied (matches sendMail behaviour). - $vars->{diffs} = [grep { - !($_->{field_name} =~ /^(?:dependson|blocked|regress(?:ed_by|es)|see_also)$/) - || $_->{old} || $_->{new} - } @{$vars->{diffs}}]; + # Drop dep/see_also diffs that re-stripping emptied (matches sendMail behaviour). + $vars->{diffs} = [ + grep { + !($_->{field_name} =~ /^(?:dependson|blocked|regress(?:ed_by|es)|see_also)$/) + || $_->{old} + || $_->{new} + } @{$vars->{diffs}} + ]; # Recompute changed-field headers after dequeue-time diff stripping. $vars->{changedfields} = [uniq map { $_->{field_desc} } @{$vars->{diffs}}]; @@ -816,10 +827,15 @@ sub dequeue { } # If nothing remains to send, bail. - unless (@{$vars->{diffs}} || @{$vars->{new_comments}} || @{$vars->{flag_events}}) { - WARN('dequeue: suppressing bugmail for user ' - . $vars->{to_user}->login . ' on bug ' . $vars->{bug}->id - . ' — all diffs and comments stripped by dequeue-time visibility re-check'); + unless (@{$vars->{diffs}} + || @{$vars->{new_comments}} + || @{$vars->{flag_events}}) + { + WARN( 'dequeue: suppressing bugmail for user ' + . $vars->{to_user}->login + . ' on bug ' + . $vars->{bug}->id + . ' — all diffs and comments stripped by dequeue-time visibility re-check'); return; } @@ -835,7 +851,7 @@ sub _flatten_object { # the same objects are used for each recipient, so cache the flattened hash my $cache = Bugzilla->request_cache->{bugmail_flat_objects} ||= {}; - my $key = blessed($object) . '-' . $object->id; + my $key = blessed($object) . '-' . $object->id; return $cache->{$key} ||= $object->flatten_to_hash; } @@ -892,7 +908,10 @@ sub _generate_bugmail { # flag_events, and the dequeue-time TOCTOU re-check (bug 1883428). sub _flag_event_visible_to { my ($event, $user) = @_; - return !$event->{attachment} || !$event->{attachment}->isprivate || ($user && $user->is_insider); + return + !$event->{attachment} + || !$event->{attachment}->isprivate + || ($user && $user->is_insider); } # Find flag changes during this window, so sendMail() can add a note at the @@ -902,7 +921,7 @@ sub _get_flag_mail_events { my ($bug, $start, $end, $user_cache) = @_; my $dbh = Bugzilla->dbh; - my @args = ($bug->id); + my @args = ($bug->id); my $when_restriction = ''; if ($start) { $when_restriction = ' AND flag_when > ? AND flag_when <= ?'; @@ -937,7 +956,8 @@ sub _get_flag_mail_events { $event->{action} = 'requested'; $event->{requestee_id} = $row->{requestee_id}; } - elsif ($row->{status} eq '+' || $row->{status} eq '-' || $row->{status} eq 'X') { + elsif ($row->{status} eq '+' || $row->{status} eq '-' || $row->{status} eq 'X') + { # 'X' is a flag cleared without +/- (e.g. needinfo auto-cleared when # the requestee replies) -- notify.() treated that the same as an @@ -996,7 +1016,7 @@ sub _get_flag_type_cc { foreach my $cc (split(/[, ]+/, $cc_list)) { my $ccuser = Bugzilla::User->new({name => $cc, cache => 1}); - next if @bug_in_groups && (!$ccuser || !$ccuser->can_see_bug($bug->id)); + next if @bug_in_groups && (!$ccuser || !$ccuser->can_see_bug($bug->id)); next if $attachment_is_private && (!$ccuser || !$ccuser->is_insider); if ($ccuser) { @@ -1046,7 +1066,7 @@ sub _get_diffs { my $dbh = Bugzilla->dbh; my @args = ($bug->id); - my $flag_newer = Bugzilla::FlagActivity->after_cutoff_sql('fsa'); + my $flag_newer = Bugzilla::FlagActivity->after_cutoff_sql('fsa'); my $ba_before_cutoff = Bugzilla::FlagActivity->bugs_activity_before_cutoff_sql('bugs_activity'); my $flag_last = Bugzilla::FlagActivity->last_of_request_sql('fsa'); @@ -1107,8 +1127,8 @@ sub _get_diffs { AND $flag_newer AND $flag_last $flag_when_restriction - ORDER BY bug_when, field_desc, activity_id", {Slice => {}}, - @args, $bug->id, ($bug->lastdiffed ? ($bug->lastdiffed, $end) : ()) + ORDER BY bug_when, field_desc, activity_id", {Slice => {}}, @args, + $bug->id, ($bug->lastdiffed ? ($bug->lastdiffed, $end) : ()) ); # As in history, skip a request that left a flag as it was, such as a flag @@ -1148,7 +1168,7 @@ sub _get_diffs { } sub _get_new_bugmail_fields { - my $bug = shift; + my $bug = shift; my @fields = @{Bugzilla->fields({obsolete => 0, in_new_bugmail => 1})}; my @diffs; @@ -1216,7 +1236,7 @@ sub _bug_link_re { sub _parse_see_also { my (@links) = @_; my $bug_link_re = _bug_link_re(); - return grep { /^\d+$/ } map { /$bug_link_re/ ? int($1) : () } @links; + return grep {/^\d+$/} map { /$bug_link_re/ ? int($1) : () } @links; } 1; diff --git a/Bugzilla/DB/Schema.pm b/Bugzilla/DB/Schema.pm index c4cf819ad3..ed5daafe06 100644 --- a/Bugzilla/DB/Schema.pm +++ b/Bugzilla/DB/Schema.pm @@ -24,9 +24,9 @@ use Bugzilla::Hook; use Bugzilla::Util; use Bugzilla::Constants; -use Carp qw(confess); -use Digest::MD5 qw(md5_hex); -use Hash::Util qw(lock_value unlock_hash lock_keys unlock_keys); +use Carp qw(confess); +use Digest::MD5 qw(md5_hex); +use Hash::Util qw(lock_value unlock_hash lock_keys unlock_keys); use List::MoreUtils qw(firstidx natatime); use Try::Tiny; use Module::Runtime qw(require_module); @@ -426,21 +426,17 @@ use constant ABSTRACT_SCHEMA => { REFERENCES => {TABLE => 'longdescs', COLUMN => 'comment_id', DELETE => 'CASCADE'} }, - user_id => { + user_id => { TYPE => 'INT3', NOTNULL => 1, REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'} }, - reaction => {TYPE => 'varchar(24)', NOTNULL => 1}, + reaction => {TYPE => 'varchar(24)', NOTNULL => 1}, ], INDEXES => [ - longdescs_reactions_reaction_idx => { - FIELDS => ['comment_id', 'reaction'], - }, - longdescs_reactions_user_idx => { - FIELDS => ['comment_id', 'user_id', 'reaction'], - TYPE => 'UNIQUE', - }, + longdescs_reactions_reaction_idx => {FIELDS => ['comment_id', 'reaction'],}, + longdescs_reactions_user_idx => + {FIELDS => ['comment_id', 'user_id', 'reaction'], TYPE => 'UNIQUE',}, ], }, @@ -734,38 +730,38 @@ use constant ABSTRACT_SCHEMA => { flag_activity => { FIELDS => [ - id => {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1}, - flag_when => {TYPE => 'DATETIME', NOTNULL => 1}, - type_id => { + id => {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1}, + flag_when => {TYPE => 'DATETIME', NOTNULL => 1}, + type_id => { TYPE => 'INT2', NOTNULL => 1, REFERENCES => {TABLE => 'flagtypes', COLUMN => 'id', DELETE => 'CASCADE'}, }, - flag_id => {TYPE => 'INT3', NOTNULL => 1}, - setter_id => { + flag_id => {TYPE => 'INT3', NOTNULL => 1}, + setter_id => { TYPE => 'INT3', NOTNULL => 1, REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}, }, + # The user who made the change. Not always the setter: redirecting a # request keeps the original setter. - who => { + who => { TYPE => 'INT3', NOTNULL => 1, REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}, }, - requestee_id => { - TYPE => 'INT3', - REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}, - }, - bug_id => { + requestee_id => + {TYPE => 'INT3', REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'},}, + bug_id => { TYPE => 'INT3', NOTNULL => 1, REFERENCES => {TABLE => 'bugs', COLUMN => 'bug_id', DELETE => 'CASCADE'}, }, attachment_id => { TYPE => 'INT5', - REFERENCES => {TABLE => 'attachments', COLUMN => 'attach_id', DELETE => 'CASCADE'}, + REFERENCES => + {TABLE => 'attachments', COLUMN => 'attach_id', DELETE => 'CASCADE'}, }, status => {TYPE => 'CHAR(1)', NOTNULL => 1}, ], @@ -774,16 +770,15 @@ use constant ABSTRACT_SCHEMA => { flag_activity_type_id_idx => ['type_id'], flag_activity_bug_id_idx => ['bug_id'], flag_activity_status_when_idx => ['status', 'flag_when'], - flag_activity_who_idx => ['who', 'flag_when'], + flag_activity_who_idx => ['who', 'flag_when'], ], }, # One row: flag changes up to this time are read from bugs_activity, later # ones from flag_activity. # See Bugzilla::FlagActivity->cutoff. - flag_activity_migration => { - FIELDS => [cutoff => {TYPE => 'DATETIME', NOTNULL => 1},], - }, + flag_activity_migration => + {FIELDS => [cutoff => {TYPE => 'DATETIME', NOTNULL => 1},],}, # "flagtypes" defines the types of flags that can be set. flagtypes => { @@ -1057,7 +1052,7 @@ use constant ABSTRACT_SCHEMA => { mfa => {TYPE => 'varchar(8)', DEFAULT => "''"}, mfa_required_date => {TYPE => 'DATETIME'}, forget_after_date => {TYPE => 'DATETIME'}, - bounce_count => {TYPE => 'INT1', NOTNULL => 1, DEFAULT => 0}, + bounce_count => {TYPE => 'INT1', NOTNULL => 1, DEFAULT => 0}, modification_ts => {TYPE => 'DATETIME', NOTNULL => 1}, ], INDEXES => [ @@ -1281,16 +1276,16 @@ use constant ABSTRACT_SCHEMA => { REFERENCES => {TABLE => 'profiles', COLUMN => 'userid', DELETE => 'CASCADE'} }, ipaddr => {TYPE => 'varchar(40)'}, - lastused => {TYPE => 'DATETIME', NOTNULL => 1}, - id => {TYPE => 'INTSERIAL', NOTNULL => 1, PRIMARYKEY => 1}, - restrict_ipaddr => {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 0}, + lastused => {TYPE => 'DATETIME', NOTNULL => 1}, + id => {TYPE => 'INTSERIAL', NOTNULL => 1, PRIMARYKEY => 1}, + restrict_ipaddr => {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 0}, auth_method => {TYPE => 'varchar(40)'}, ], INDEXES => [ logincookies_lastused_idx => ['lastused'], logincookies_cookie_idx => {FIELDS => ['cookie'], TYPE => 'UNIQUE'}, ], - }, + }, login_failure => { FIELDS => [ @@ -1524,19 +1519,20 @@ use constant ABSTRACT_SCHEMA => { products => { FIELDS => [ - id => {TYPE => 'SMALLSERIAL', NOTNULL => 1, PRIMARYKEY => 1}, - name => {TYPE => 'varchar(64)', NOTNULL => 1}, + id => {TYPE => 'SMALLSERIAL', NOTNULL => 1, PRIMARYKEY => 1}, + name => {TYPE => 'varchar(64)', NOTNULL => 1}, classification_id => { TYPE => 'INT2', NOTNULL => 1, DEFAULT => '1', REFERENCES => {TABLE => 'classifications', COLUMN => 'id', DELETE => 'CASCADE'} }, - description => {TYPE => 'MEDIUMTEXT', NOTNULL => 1}, - isactive => {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 1}, - defaultmilestone => {TYPE => 'varchar(20)', NOTNULL => 1, DEFAULT => "'---'"}, - default_version => {TYPE => 'varchar(20)', NOTNULL => 1, DEFAULT => "'unspecified'"}, - allows_unconfirmed => {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'TRUE'}, + description => {TYPE => 'MEDIUMTEXT', NOTNULL => 1}, + isactive => {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 1}, + defaultmilestone => {TYPE => 'varchar(20)', NOTNULL => 1, DEFAULT => "'---'"}, + default_version => + {TYPE => 'varchar(20)', NOTNULL => 1, DEFAULT => "'unspecified'"}, + allows_unconfirmed => {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'TRUE'}, bug_description_template => {TYPE => 'MEDIUMTEXT'}, default_bug_type => {TYPE => 'varchar(20)'}, ], @@ -1839,7 +1835,7 @@ use constant ABSTRACT_SCHEMA => { jobid => {TYPE => 'INT4', NOTNULL => 1}, message => {TYPE => 'TEXT', NOTNULL => 1}, funcid => {TYPE => 'INT4', NOTNULL => 1, DEFAULT => 0}, - ], + ], INDEXES => [ ts_error_funcid_idx => [qw(funcid error_time)], ts_error_error_time_idx => ['error_time'], @@ -2045,8 +2041,8 @@ use constant ABSTRACT_SCHEMA => { bloomfilter_values => { FIELDS => [ - id => {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1}, - name => {TYPE => 'VARCHAR(64)', NOTNULL => 1}, + id => {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1}, + name => {TYPE => 'VARCHAR(64)', NOTNULL => 1}, value => {TYPE => 'VARCHAR(255)', NOTNULL => 1}, ], }, @@ -2393,8 +2389,7 @@ sub get_add_fks_sql { } else { foreach my $fk_string (@add) { - push(@sql, - "ALTER TABLE " . $dbh->quote_identifier($table) . " ADD $fk_string"); + push(@sql, "ALTER TABLE " . $dbh->quote_identifier($table) . " ADD $fk_string"); } } return @sql; @@ -2415,8 +2410,9 @@ sub get_drop_fk_sql { my ($self, $table, $column, $references) = @_; my $fk_name = $self->_get_fk_name($table, $column, $references); - return ( - "ALTER TABLE " . Bugzilla->dbh->quote_identifier($table) . " DROP CONSTRAINT $fk_name"); + return ("ALTER TABLE " + . Bugzilla->dbh->quote_identifier($table) + . " DROP CONSTRAINT $fk_name"); } sub convert_type { @@ -2778,9 +2774,7 @@ sub get_alter_column_ddl { # If we went from not being a PRIMARY KEY to being a PRIMARY KEY. if (!$old_def->{PRIMARYKEY} && $new_def->{PRIMARYKEY}) { push(@statements, - "ALTER TABLE " - . $dbh->quote_identifier($table) - . " ADD PRIMARY KEY ($column)"); + "ALTER TABLE " . $dbh->quote_identifier($table) . " ADD PRIMARY KEY ($column)"); } # If we went from being a PK to not being a PK diff --git a/Bugzilla/DB/Sqlite.pm b/Bugzilla/DB/Sqlite.pm index 3a72f76f10..1f775a11af 100644 --- a/Bugzilla/DB/Sqlite.pm +++ b/Bugzilla/DB/Sqlite.pm @@ -246,7 +246,7 @@ sub bz_setup_database { # If we created TheSchwartz tables with COLLATE bugzilla (during the # 4.1.x development series) re-create them without it. - my @tables = $self->bz_table_list(); + my @tables = $self->bz_table_list(); my @ts_tables = grep {/^ts_/} @tables; my $drop_ok; foreach my $table (@ts_tables) { diff --git a/Bugzilla/FlagActivity.pm b/Bugzilla/FlagActivity.pm index e0c4056a07..88bf7b9187 100644 --- a/Bugzilla/FlagActivity.pm +++ b/Bugzilla/FlagActivity.pm @@ -13,7 +13,7 @@ use warnings; use Bugzilla::Error; use Bugzilla::Field qw(get_field_id); -use Bugzilla::Util qw(trim datetime_from); +use Bugzilla::Util qw(trim datetime_from); use List::MoreUtils qw(none); use base qw( Bugzilla::Object ); @@ -94,9 +94,8 @@ sub status { return $_[0]->{status} } # bugs_activity rows must not be listed twice. sub cutoff { my $cache = Bugzilla->request_cache; - $cache->{flag_activity_cutoff} - //= Bugzilla->dbh->selectrow_array('SELECT cutoff FROM flag_activity_migration') - // '1970-01-01 00:00:00'; + $cache->{flag_activity_cutoff} //= Bugzilla->dbh->selectrow_array( + 'SELECT cutoff FROM flag_activity_migration') // '1970-01-01 00:00:00'; return $cache->{flag_activity_cutoff}; } @@ -111,7 +110,8 @@ sub after_cutoff_sql { sub bugs_activity_before_cutoff_sql { my ($class, $alias) = @_; my $field_id = get_field_id('flagtypes.name'); - return "($alias.fieldid <> $field_id OR $alias.bug_when <= " + return + "($alias.fieldid <> $field_id OR $alias.bug_when <= " . Bugzilla->dbh->quote($class->cutoff) . ')'; } diff --git a/Bugzilla/FlagType.pm b/Bugzilla/FlagType.pm index ce580ee2f4..83731e2124 100644 --- a/Bugzilla/FlagType.pm +++ b/Bugzilla/FlagType.pm @@ -546,7 +546,8 @@ sub request_group { sub has_activity { my $self = shift; my $dbh = Bugzilla->dbh; - $self->{'has_activity'} //= $dbh->selectrow_array( + $self->{'has_activity'} + //= $dbh->selectrow_array( 'SELECT 1 FROM flag_activity WHERE type_id = ? ' . $dbh->sql_limit(1), undef, $self->id) ? 1 : 0; return $self->{'has_activity'}; @@ -672,7 +673,7 @@ sub match { my $dbh = Bugzilla->dbh; # Depending on the criteria, we may have to append additional tables. - my $tables = [DB_TABLE]; + my $tables = [DB_TABLE]; my @criteria = sqlify_criteria($criteria, $tables); $tables = join(' ', @$tables); $criteria = join(' AND ', @criteria); @@ -700,7 +701,7 @@ sub count { my $dbh = Bugzilla->dbh; # Depending on the criteria, we may have to append additional tables. - my $tables = [DB_TABLE]; + my $tables = [DB_TABLE]; my @criteria = sqlify_criteria($criteria, $tables); $tables = join(' ', @$tables); $criteria = join(' AND ', @criteria); diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index 55cd455cd5..964e68e837 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -16,7 +16,7 @@ use warnings; use Bugzilla::Constants; use Bugzilla::Hook; -use Bugzilla::Install (); +use Bugzilla::Install (); use Bugzilla::Install::Util qw(indicate_progress install_string); use Bugzilla::Util; use Bugzilla::Series; @@ -72,8 +72,10 @@ sub update_fielddefs_definition { # If the largest fielddefs sortkey is less than 100, then # we're using the old sorting system, and we should convert # it to the new one before adding any new definitions. - if (!$dbh->selectrow_arrayref( - 'SELECT COUNT(id) FROM fielddefs WHERE sortkey >= 100')) + if ( + !$dbh->selectrow_arrayref( + 'SELECT COUNT(id) FROM fielddefs WHERE sortkey >= 100') + ) { print "Updating the sortkeys for the fielddefs table...\n"; my $field_ids @@ -754,8 +756,7 @@ sub update_table_definitions { $dbh->bz_add_column('profiles', 'password_change_reason', {TYPE => 'varchar(64)'}); - $dbh->bz_add_column('profiles', 'mfa', - {TYPE => 'varchar(8)',, DEFAULT => "''"}); + $dbh->bz_add_column('profiles', 'mfa', {TYPE => 'varchar(8)', DEFAULT => "''"}); $dbh->bz_add_column('profiles', 'mfa_required_date', {TYPE => 'DATETIME'}); @@ -790,7 +791,7 @@ sub update_table_definitions { {TYPE => 'MEDIUMTEXT'}); # Bug 1522341, 1541617, 1546788 - kohei.yoshino@gmail.com - $dbh->bz_add_column('bugs', 'bug_type', {TYPE => 'varchar(20)'}); + $dbh->bz_add_column('bugs', 'bug_type', {TYPE => 'varchar(20)'}); $dbh->bz_add_column('components', 'default_bug_type', {TYPE => 'varchar(20)'}); $dbh->bz_add_column('products', 'default_bug_type', {TYPE => 'varchar(20)'}); $dbh->bz_alter_column('bugs', 'bug_type', @@ -806,7 +807,8 @@ sub update_table_definitions { _populate_api_keys_creation_ts(); # Bug 1612290 - dkl@mozilla.com - $dbh->bz_add_column('profiles', 'bounce_count', {TYPE => 'INT1', NOTNULL => 1, DEFAULT => 0}); + $dbh->bz_add_column('profiles', 'bounce_count', + {TYPE => 'INT1', NOTNULL => 1, DEFAULT => 0}); # Bug 1588221 - dkl@mozilla.com $dbh->bz_alter_column('bugs_activity', 'attach_id', {TYPE => 'INT5'}); @@ -814,8 +816,9 @@ sub update_table_definitions { {TYPE => 'BIGSERIAL', NOTNULL => 1, PRIMARYKEY => 1}); $dbh->bz_alter_column('attach_data', 'id', {TYPE => 'INT5', NOTNULL => 1, PRIMARYKEY => 1}); - $dbh->bz_alter_column('flags', 'attach_id', {TYPE => 'INT5'}); - $dbh->bz_alter_column('user_request_log', 'attach_id', {TYPE => 'INT5', NOTNULL => 0}); + $dbh->bz_alter_column('flags', 'attach_id', {TYPE => 'INT5'}); + $dbh->bz_alter_column('user_request_log', 'attach_id', + {TYPE => 'INT5', NOTNULL => 0}); _populate_attachment_storage_class(); # Bug 1682404 - dkl@mozilla.com @@ -823,7 +826,7 @@ sub update_table_definitions { {TYPE => 'varchar(255)', NOTNULL => 1, DEFAULT => "'Mozilla'"}); # Bug 1697642 - dkl@mozilla.com - $dbh->bz_add_column('logincookies', 'auth_method',{TYPE => 'varchar(40)'}); + $dbh->bz_add_column('logincookies', 'auth_method', {TYPE => 'varchar(40)'}); # Bug 1761594 - dkl@mozilla.com $dbh->bz_add_column('oauth2_client', 'hostname', @@ -2111,7 +2114,7 @@ sub _convert_attachment_statuses_to_flags { while (($added, $who, $when) = $sth2->fetchrow_array()) { last if $added =~ /(^|[, ]+)\Q$status\E([, ]+|$)/; } - $who = $dbh->quote($who); # "NULL" by default if $who is undefined + $who = $dbh->quote($who); # "NULL" by default if $who is undefined $when = $when ? $dbh->quote($when) : "NOW()"; @@ -2383,7 +2386,7 @@ sub _copy_old_charts_into_database { # We also add a new query for "Open", so that migrated products get # the same set as new products (see editproducts.cgi.) my @openedstatuses = ("UNCONFIRMED", "NEW", "ASSIGNED", "REOPENED"); - my $query = join("&", map {"bug_status=$_"} @openedstatuses); + my $query = join("&", map {"bug_status=$_"} @openedstatuses); my $series = new Bugzilla::Series(undef, $product, $all_name, $open_name, undef, 1, $query_prod . $query, 1); @@ -2794,7 +2797,7 @@ sub _change_all_mysql_booleans_to_tinyint { my $quip_info_sth = $dbh->column_info(undef, undef, 'quips', '%'); my $quips_cols = $quip_info_sth->fetchall_hashref("COLUMN_NAME"); my $approved_col = $quips_cols->{'approved'}; - if ($approved_col->{TYPE_NAME} eq 'TINYINT' + if ( $approved_col->{TYPE_NAME} eq 'TINYINT' and defined $approved_col->{COLUMN_SIZE} and $approved_col->{COLUMN_SIZE} == 1) { @@ -2883,9 +2886,11 @@ sub _fix_attachments_submitter_id_idx { if ( $dbh->bz_index_info('attachments', 'attachments_submitter_id_idx') && ( - scalar(@{ - $dbh->bz_index_info('attachments', 'attachments_submitter_id_idx')->{FIELDS} - }) < 2 + scalar( + @{ + $dbh->bz_index_info('attachments', 'attachments_submitter_id_idx')->{FIELDS} + } + ) < 2 ) ) { @@ -3788,7 +3793,7 @@ sub _set_attachment_comment_type { my $text = $comments{$id}; next if $text !~ /^\Q$string\E(\d+)/; my $attachment_id = $1; - my @lines = split("\n", $text); + my @lines = split("\n", $text); if ($type == CMT_ATTACHMENT_CREATED) { # Now we have to remove the text up until we find a line that's @@ -3857,7 +3862,7 @@ sub _convert_flagtypes_fks_to_set_null { } sub _fix_decimal_types { - my $dbh = Bugzilla->dbh; + my $dbh = Bugzilla->dbh; my $type = {TYPE => 'decimal(7,2)', NOTNULL => 1, DEFAULT => '0'}; $dbh->bz_alter_column('bugs', 'estimated_time', $type); $dbh->bz_alter_column('bugs', 'remaining_time', $type); @@ -3866,7 +3871,7 @@ sub _fix_decimal_types { sub _fix_series_creator_fk { my $dbh = Bugzilla->dbh; - my $fk = $dbh->bz_fk_info('series', 'creator'); + my $fk = $dbh->bz_fk_info('series', 'creator'); if ($fk and $fk->{DELETE} eq 'SET NULL') { $fk->{DELETE} = 'CASCADE'; $dbh->bz_alter_fk('series', 'creator', $fk); @@ -4023,7 +4028,7 @@ sub _migrate_user_tags { indicate_progress({current => ++$current, total => $total, every => 25}); - my $uri = URI->new("buglist.cgi?$query", 'http'); + my $uri = URI->new("buglist.cgi?$query", 'http'); my $bug_id_list = $uri->query_param_delete('bug_id'); if (!$bug_id_list) { warn "No bug_id param for tag $name from user $user_id: $query"; @@ -4120,7 +4125,7 @@ sub _rename_tags_to_tag { sub _on_delete_set_null_for_audit_log_userid { my $dbh = Bugzilla->dbh; - my $fk = $dbh->bz_fk_info('audit_log', 'user_id'); + my $fk = $dbh->bz_fk_info('audit_log', 'user_id'); if ($fk and !defined $fk->{DELETE}) { $fk->{DELETE} = 'SET NULL'; $dbh->bz_alter_fk('audit_log', 'user_id', $fk); @@ -4191,7 +4196,7 @@ sub _fix_dependencies_dupes { sub _fix_flagclusions_indexes { my $dbh = Bugzilla->dbh; foreach my $table ('flaginclusions', 'flagexclusions') { - my $index = $table . '_type_id_idx'; + my $index = $table . '_type_id_idx'; my $idx_info = $dbh->bz_index_info($table, $index); if ($idx_info && $idx_info->{'TYPE'} ne 'UNIQUE') { @@ -4307,14 +4312,11 @@ sub _populate_oauth2_scopes { # if there are no scopes, then we're creating a database from scratch my ($scope_count) = $dbh->selectrow_array('SELECT COUNT(*) FROM oauth2_scope'); if (!$scope_count) { - $dbh->do( - "INSERT INTO oauth2_scope (id, name, description) VALUES " . - "(1, 'user:read', 'View basic account information such as email address.')" - ); - $dbh->do( - "INSERT INTO oauth2_scope (id, name, description) VALUES " . - "(2, 'api:modify', 'Read and modify data in Bugzilla using the Web API.')" + $dbh->do("INSERT INTO oauth2_scope (id, name, description) VALUES " + . "(1, 'user:read', 'View basic account information such as email address.')" ); + $dbh->do("INSERT INTO oauth2_scope (id, name, description) VALUES " + . "(2, 'api:modify', 'Read and modify data in Bugzilla using the Web API.')"); } # Bug 1658317 - dkl@mozilla - Update column names if this is an existing DB @@ -4332,7 +4334,9 @@ sub _populate_oauth2_scopes { } # Bug 1670319 - dkl@mozilla.com - Add new api:modify scope to scope list - my ($scope_found) = $dbh->selectrow_array("SELECT 1 FROM oauth2_scope WHERE name = 'api:modify'"); + my ($scope_found) + = $dbh->selectrow_array( + "SELECT 1 FROM oauth2_scope WHERE name = 'api:modify'"); if (!$scope_found) { $dbh->do("INSERT INTO oauth2_scope (name, description) VALUES " . "('api:modify', 'Read and modify data in Bugzilla using the Web API.')"); @@ -4407,7 +4411,9 @@ sub _populate_api_keys_creation_ts { # We do not have a way to tell when an API key was originally created # so we use the last_used timestamp as the initial creation value. - $dbh->do('UPDATE user_api_keys SET creation_ts = COALESCE(last_used, LOCALTIMESTAMP(0))'); + $dbh->do( + 'UPDATE user_api_keys SET creation_ts = COALESCE(last_used, LOCALTIMESTAMP(0))' + ); $dbh->bz_alter_column('user_api_keys', 'creation_ts', {TYPE => 'DATETIME', NOTNULL => 1}); @@ -4424,13 +4430,15 @@ sub _add_products_default_version { # Import the alphabetically last version for each product as the product's # default version, just like the Enter Bug page selecting the last one by # default before this migration. - my $sth = $dbh->prepare('SELECT product_id, MAX(value) AS version - FROM versions GROUP BY product_id HAVING version != "unspecified"'); + my $sth = $dbh->prepare( + 'SELECT product_id, MAX(value) AS version + FROM versions GROUP BY product_id HAVING version != "unspecified"' + ); $sth->execute(); while (my ($product_id, $version) = $sth->fetchrow_array()) { - $dbh->do('UPDATE products SET default_version = ? WHERE id = ?', undef, - ($version, $product_id)); + $dbh->do('UPDATE products SET default_version = ? WHERE id = ?', + undef, ($version, $product_id)); } } @@ -4438,7 +4446,8 @@ sub _populate_attachment_storage_class { my $dbh = Bugzilla->dbh; # Return if we have already made these changes - my $count = $dbh->selectrow_array('SELECT COUNT(id) FROM attachment_storage_class'); + my $count + = $dbh->selectrow_array('SELECT COUNT(id) FROM attachment_storage_class'); if (!$count) { $dbh->do( "INSERT INTO attachment_storage_class (id, storage_class) SELECT attachments.attach_id, 'database' FROM attachments ORDER BY attachments.attach_id" @@ -4513,11 +4522,13 @@ sub _migrate_profiles_modification_ts { } my $modification_ts = 0; + # IF we could not find anything then use todays date if (!$audit_log_when && !$profiles_act_when && !$creation_when) { $modification_ts = $now_when; } - # We used unix timestamps to make value comparison easier without using DateTime instance of each. + +# We used unix timestamps to make value comparison easier without using DateTime instance of each. else { $modification_ts = max($audit_log_when, $profiles_act_when, $creation_when); } @@ -4580,8 +4591,8 @@ sub _add_flag_activity_start_rows { FROM flags WHERE id > ? AND id <= ? AND NOT EXISTS (SELECT 1 FROM flag_activity - WHERE flag_activity.flag_id = flags.id)', - undef, $from, $from + $batch_size + WHERE flag_activity.flag_id = flags.id)', undef, + $from, $from + $batch_size ); } print "Added a flag_activity row for $added flags.\n" if $added > 0; @@ -4617,12 +4628,15 @@ sub _set_flag_activity_who_not_null { # MySQL can't make a column of a foreign key NOT NULL. Drop the key: # bz_setup_foreign_keys adds it back at the end of checksetup. $dbh->bz_drop_fk('flag_activity', 'who'); - $dbh->bz_alter_column('flag_activity', 'who', + $dbh->bz_alter_column( + 'flag_activity', + 'who', { - TYPE => 'INT3', - NOTNULL => 1, - REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'} - }); + TYPE => 'INT3', + NOTNULL => 1, + REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'} + } + ); } sub _remove_duo_recovery_codes { diff --git a/Bugzilla/Search.pm b/Bugzilla/Search.pm index d8cab17b34..121ce79dee 100644 --- a/Bugzilla/Search.pm +++ b/Bugzilla/Search.pm @@ -34,10 +34,10 @@ use Bugzilla::Util; use Data::Dumper; use DateTime; use List::MoreUtils qw(all any firstidx part uniq); -use POSIX qw(INT_MAX); -use Scalar::Util qw(blessed); -use Storable qw(dclone); -use Time::HiRes qw(gettimeofday tv_interval); +use POSIX qw(INT_MAX); +use Scalar::Util qw(blessed); +use Storable qw(dclone); +use Time::HiRes qw(gettimeofday tv_interval); # Description Of Boolean Charts # ----------------------------- @@ -110,9 +110,7 @@ use Time::HiRes qw(gettimeofday tv_interval); ############# # BMO - product aliases for searching -use constant PRODUCT_ALIASES => { - 'Fenix' => 'Firefox for Android', -}; +use constant PRODUCT_ALIASES => {'Fenix' => 'Firefox for Android',}; # When doing searches, NULL datetimes are treated as this date. use constant EMPTY_DATETIME => '1970-01-01 00:00:00'; @@ -295,7 +293,7 @@ use constant OPERATOR_FIELD_OVERRIDE => { changedby => \&_flagtypes_changed, everchanged => \&_flagtypes_changed, }, - longdesc => { + longdesc => { changedby => \&_long_desc_changedby, everchanged => \&_long_desc_everchanged, changedbefore => \&_long_desc_changedbefore_after, @@ -317,16 +315,16 @@ use constant OPERATOR_FIELD_OVERRIDE => { team_name => {_non_changed => \&_team_name_nonchanged}, # Count Fields - 'attachments.count' => RELATION_COUNT_OVERRIDE, - 'cc_count' => RELATION_COUNT_OVERRIDE, - 'see_also_count' => RELATION_COUNT_OVERRIDE, - 'keywords.count' => RELATION_COUNT_OVERRIDE, - 'blocked.count' => RELATION_COUNT_OVERRIDE, - 'dependson.count' => RELATION_COUNT_OVERRIDE, - 'regressed_by.count' => RELATION_COUNT_OVERRIDE, - 'regresses.count' => RELATION_COUNT_OVERRIDE, - 'dupe_count' => RELATION_COUNT_OVERRIDE, - 'longdescs.count' => { + 'attachments.count' => RELATION_COUNT_OVERRIDE, + 'cc_count' => RELATION_COUNT_OVERRIDE, + 'see_also_count' => RELATION_COUNT_OVERRIDE, + 'keywords.count' => RELATION_COUNT_OVERRIDE, + 'blocked.count' => RELATION_COUNT_OVERRIDE, + 'dependson.count' => RELATION_COUNT_OVERRIDE, + 'regressed_by.count' => RELATION_COUNT_OVERRIDE, + 'regresses.count' => RELATION_COUNT_OVERRIDE, + 'dupe_count' => RELATION_COUNT_OVERRIDE, + 'longdescs.count' => { changedby => \&_long_desc_changedby, everchanged => \&_long_desc_everchanged, changedbefore => \&_long_desc_changedbefore_after, @@ -427,7 +425,7 @@ use constant USER_FIELDS => { join => {table => 'longdescs', join => 'INNER'}, isprivate => 1, }, - qa_contact => {nullable => 1,}, + qa_contact => {nullable => 1,}, 'requestees.login_name' => {nullable => 1, field => 'requestee_id', join => {table => 'flags'},}, 'setters.login_name' => {field => 'setter_id', join => {table => 'flags'},}, @@ -496,7 +494,7 @@ sub COLUMN_JOINS { reporter => {from => 'reporter', to => 'userid', table => 'profiles', join => 'INNER',}, qa_contact => {from => 'qa_contact', to => 'userid', table => 'profiles',}, - component => + component => {from => 'component_id', to => 'id', table => 'components', join => 'INNER',}, team_name => {from => 'component_id', to => 'id', table => 'components', join => 'INNER',}, @@ -535,22 +533,22 @@ sub COLUMN_JOINS { to => 'id', }, }, - 'attachments.count' => {table => 'attachments',}, - 'cc_count' => {table => 'cc',}, - 'see_also_count' => {table => 'bug_see_also'}, - 'keywords.count' => {table => 'keywords',}, - 'longdescs.count' => {table => 'longdescs', join => 'INNER',}, - 'blocked' => {table => 'dependencies', to => 'dependson',}, - 'blocked.count' => {table => 'dependencies', to => 'dependson',}, - 'dependson' => {table => 'dependencies', to => 'blocked',}, - 'dependson.count' => {table => 'dependencies', to => 'blocked',}, - 'regressed_by' => {table => 'regressions', to => 'regresses',}, - 'regressed_by.count' => {table => 'regressions', to => 'regresses',}, - 'regresses' => {table => 'regressions', to => 'regressed_by',}, - 'regresses.count' => {table => 'regressions', to => 'regressed_by',}, - 'dupe_count' => {table => 'duplicates', to => 'dupe_of',}, - 'duplicates' => {table => 'duplicates', to => 'dupe_of',}, - last_visit_ts => { + 'attachments.count' => {table => 'attachments',}, + 'cc_count' => {table => 'cc',}, + 'see_also_count' => {table => 'bug_see_also'}, + 'keywords.count' => {table => 'keywords',}, + 'longdescs.count' => {table => 'longdescs', join => 'INNER',}, + 'blocked' => {table => 'dependencies', to => 'dependson',}, + 'blocked.count' => {table => 'dependencies', to => 'dependson',}, + 'dependson' => {table => 'dependencies', to => 'blocked',}, + 'dependson.count' => {table => 'dependencies', to => 'blocked',}, + 'regressed_by' => {table => 'regressions', to => 'regresses',}, + 'regressed_by.count' => {table => 'regressions', to => 'regresses',}, + 'regresses' => {table => 'regressions', to => 'regressed_by',}, + 'regresses.count' => {table => 'regressions', to => 'regressed_by',}, + 'dupe_count' => {table => 'duplicates', to => 'dupe_of',}, + 'duplicates' => {table => 'duplicates', to => 'dupe_of',}, + last_visit_ts => { as => 'bug_user_last_visit', table => 'bug_user_last_visit', extra => ['bug_user_last_visit.user_id = ' . $user->id], @@ -627,12 +625,13 @@ sub COLUMNS { 'DISTINCT ' . $dbh->sql_string_concat('map_flagtypes.name', 'map_flags.status') ), - 'keywords' => $dbh->sql_group_concat('DISTINCT map_keyworddefs.name'), - 'blocked' => $dbh->sql_group_concat('DISTINCT map_blocked.blocked'), - 'dependson' => $dbh->sql_group_concat('DISTINCT map_dependson.dependson'), - 'regressed_by' => $dbh->sql_group_concat('DISTINCT map_regressed_by.regressed_by'), - 'regresses' => $dbh->sql_group_concat('DISTINCT map_regresses.regresses'), - 'duplicates' => $dbh->sql_group_concat('DISTINCT map_duplicates.dupe'), + 'keywords' => $dbh->sql_group_concat('DISTINCT map_keyworddefs.name'), + 'blocked' => $dbh->sql_group_concat('DISTINCT map_blocked.blocked'), + 'dependson' => $dbh->sql_group_concat('DISTINCT map_dependson.dependson'), + 'regressed_by' => + $dbh->sql_group_concat('DISTINCT map_regressed_by.regressed_by'), + 'regresses' => $dbh->sql_group_concat('DISTINCT map_regresses.regresses'), + 'duplicates' => $dbh->sql_group_concat('DISTINCT map_duplicates.dupe'), 'attachments.count' => 'COUNT(DISTINCT map_attachments_count.attach_id)', 'cc_count' => 'COUNT(DISTINCT map_cc_count.who)', @@ -718,7 +717,7 @@ sub COLUMNS { sub REPORT_COLUMNS { my $invocant = shift; - my $user = blessed($invocant) ? $invocant->_user : Bugzilla->user; + my $user = blessed($invocant) ? $invocant->_user : Bugzilla->user; my $columns = dclone(blessed($invocant) ? $invocant->COLUMNS : COLUMNS); @@ -777,7 +776,7 @@ use constant GROUP_BY_SKIP => qw( # Note that the params argument may be modified by Bugzilla::Search sub new { my $invocant = shift; - my $class = ref($invocant) || $invocant; + my $class = ref($invocant) || $invocant; my $self = {@_}; bless($self, $class); @@ -793,7 +792,7 @@ sub new { # do it, because there's no way to know if we were passed a tied hash # or not. my $params_in = $self->_params; - my %params = map { $_ => $params_in->{$_} } keys %$params_in; + my %params = map { $_ => $params_in->{$_} } keys %$params_in; $self->{params} = \%params; return $self; @@ -827,7 +826,7 @@ sub data { # BMO - to avoid massive amounts of joins, if we're selecting a lot of # tracking flags, replace them with placeholders. the values will be # retrieved later and injected into the result. - my %tf_map = map { $_ => 1 } Bugzilla->tracking_flag_names; + my %tf_map = map { $_ => 1 } Bugzilla->tracking_flag_names; my @tf_selected = grep { exists $tf_map{$_} } @orig_fields; # MySQL has a limit of 61 joins, and we want to avoid massive amounts of joins @@ -846,7 +845,7 @@ sub data { my $sql = $self->_sql; # Do we just want bug IDs to pass to the 2nd query or all the data immediately? - my $func = $all_in_bugs_table ? 'selectall_arrayref' : 'selectcol_arrayref'; + my $func = $all_in_bugs_table ? 'selectall_arrayref' : 'selectcol_arrayref'; my $bug_ids = $self->_sql_execute($sql, $func); my @extra_data = ({sql => $sql, time => tv_interval($start_time)}); @@ -937,7 +936,7 @@ sub data { sub _sql { my ($self) = @_; return $self->{sql} if $self->{sql}; - my $dbh = Bugzilla->dbh; + my $dbh = Bugzilla->dbh; my $user_id = $self->{user}->id; my ($joins, $clause) = $self->_charts_to_conditions(); @@ -1057,7 +1056,7 @@ sub boolean_charts_to_custom_search { # We can just pick the last id in the array because they are sorted # numerically. my $last_id = ($self->_field_ids)[-1]; - my $count = defined($last_id) ? $last_id + 1 : 0; + my $count = defined($last_id) ? $last_id + 1 : 0; foreach my $param_set (@as_params) { foreach my $name (keys %$param_set) { my $value = $param_set->{$name}; @@ -1224,8 +1223,7 @@ sub _valid_order_columns { my ($self) = @_; return grep { $_ ne 'bug_list' } - map { (split_order_term($_))[0] } - $self->_valid_order; + map { (split_order_term($_))[0] } $self->_valid_order; } sub _validate_order_column { @@ -1268,7 +1266,7 @@ sub _special_order { my $name = $field->name; $special_order{$name} = { order => ["map_$name.sortkey", "map_$name.value"], - join => {table => $name, from => "bugs.$name", to => "value", join => 'INNER',} + join => {table => $name, from => "bugs.$name", to => "value", join => 'INNER',} }; } $self->{special_order} = \%special_order; @@ -1278,12 +1276,15 @@ sub _special_order { sub _sql_order_by { my ($self) = @_; if (!$self->{sql_order_by}) { + # Handle ordering by the list of bugs provided in the bug_id param if ($self->{order_by_bug_list}) { + # order_by_bug_list is guaranteed at this point to only contain a comma # separated list of integers return ("FIELD(bugs.bug_id,$self->{order_by_bug_list})"); } + # Otherwise order by columns my @order_by = map { $self->_translate_order_by_column($_) } $self->_valid_order; @@ -1509,7 +1510,7 @@ sub _translate_join { } my $table = $join_info->{table}; my $name = $join_info->{as}; - my $to = $join_info->{to} || "bug_id"; + my $to = $join_info->{to} || "bug_id"; my $join = $join_info->{join} || 'LEFT'; my @extra = @{$join_info->{extra} || []}; $name =~ s/\./_/g; @@ -1524,7 +1525,7 @@ sub _translate_join { } my $sql_table = $table eq 'groups' ? Bugzilla->dbh->quote_identifier($table) : $table; - my @join_sql = "$join JOIN $sql_table AS $name " + my @join_sql = "$join JOIN $sql_table AS $name " . " ON $from_table.$from = $name.$to$extra_condition"; return @join_sql; } @@ -1543,6 +1544,7 @@ sub _standard_where { my $userid = $self->_user->id; if ($self->{_no_security_check}) { + # If security checks have already been performed, there's no need to repeat # in sub-queries. return ""; @@ -1552,11 +1554,12 @@ sub _standard_where { my $term = 'security_map.group_id IS NULL'; if ($userid) { + # Authenticated; users directly associated with the bug have visibility. my @involved = ( - "bugs.reporter_accessible = 1 AND bugs.reporter = $userid", - 'bugs.cclist_accessible = 1 AND security_cc.who IS NOT NULL', - "bugs.assigned_to = $userid", + "bugs.reporter_accessible = 1 AND bugs.reporter = $userid", + 'bugs.cclist_accessible = 1 AND security_cc.who IS NOT NULL', + "bugs.assigned_to = $userid", ); if (Bugzilla->params->{'useqacontact'}) { push @involved, ("bugs.qa_contact = $userid"); @@ -1576,7 +1579,7 @@ sub _standard_where { sub _sql_where { my ($self, $main_clause) = @_; - my $where = ""; + my $where = ""; my $where_sql = $self->_standard_where(); $where .= '(' . $where_sql . ')' if $where_sql; @@ -1684,7 +1687,7 @@ sub _parse_basic_fields { my @values = $self->_param_array($param_name); next if !@values; my $default_op = $param_name eq 'content' ? 'matches' : 'anyexact'; - my $operator = $params->{"${param_name}_type"} || $default_op; + my $operator = $params->{"${param_name}_type"} || $default_op; # Fields that are displayed as multi-selects are passed as arrays, # so that they can properly search values that contain commas. @@ -1841,6 +1844,7 @@ sub _special_parse_email { my $email = trim($params->{"email$id"}); next if !$email; my $type = $params->{"emailtype$id"} || 'equals'; + # for backward compatibility $type = "equals" if $type eq "exact"; @@ -1941,19 +1945,19 @@ sub _boolean_charts { my @param_list = keys %$params; my @all_field_params = grep {/^field-?\d+/} @param_list; - my @chart_ids = map { /^field(-?\d+)/; $1 } @all_field_params; + my @chart_ids = map { /^field(-?\d+)/; $1 } @all_field_params; @chart_ids = sort { $a <=> $b } uniq @chart_ids; my $clause = new Bugzilla::Search::Clause(); foreach my $chart_id (@chart_ids) { my @all_and = grep {/^field$chart_id-\d+/} @param_list; - my @and_ids = map { /^field$chart_id-(\d+)/; $1 } @all_and; + my @and_ids = map { /^field$chart_id-(\d+)/; $1 } @all_and; @and_ids = sort { $a <=> $b } uniq @and_ids; my $and_clause = new Bugzilla::Search::Clause(); foreach my $and_id (@and_ids) { my @all_or = grep {/^field$chart_id-$and_id-\d+/} @param_list; - my @or_ids = map { /^field$chart_id-$and_id-(\d+)/; $1 } @all_or; + my @or_ids = map { /^field$chart_id-$and_id-(\d+)/; $1 } @all_or; @or_ids = sort { $a <=> $b } uniq @or_ids; my $or_clause = new Bugzilla::Search::Clause('OR'); @@ -2028,7 +2032,7 @@ sub _field_ids { my @param_list = keys %$params; my @field_params = grep {/^f\d+$/} @param_list; - my @field_ids = map { /(\d+)/; $1 } @field_params; + my @field_ids = map { /(\d+)/; $1 } @field_params; @field_ids = sort { $a <=> $b } @field_ids; return @field_ids; } @@ -2045,7 +2049,7 @@ sub _handle_chart { if (ref $value eq 'ARRAY') { # Trim input and ignore blank values. - @$value = map { trim($_) } @$value; + @$value = map { trim($_) } @$value; @$value = grep { defined $_ and $_ ne '' } @$value; return if !@$value; $string_value = join(',', @$value); @@ -2296,7 +2300,7 @@ sub _all_values { } else { @array = split($split_on, $all_values); - @array = map { trim($_) } @array; + @array = map { trim($_) } @array; @array = grep { defined $_ and $_ ne '' } @array; } @@ -2316,7 +2320,7 @@ sub _substring_terms { # split each term on spaces and commas anyway. my @words = split(/[\s,]+/, $args->{value}); @words = grep { defined $_ and $_ ne '' } @words; - @words = map { $dbh->quote($_) } @words; + @words = map { $dbh->quote($_) } @words; my @terms = map { $dbh->sql_iposition($_, $args->{full_field}) . " > 0" } @words; return @terms; @@ -2397,8 +2401,8 @@ sub _date_translate { sub SqlifyDate { my ($str) = @_; - my $fmt = "%Y-%m-%d %H:%M:%S"; - my $date = DateTime->now(); + my $fmt = "%Y-%m-%d %H:%M:%S"; + my $date = DateTime->now(); $str = "" if (!defined $str || lc($str) eq 'now'); if ($str eq "") { return $date->strftime('%Y-%m-%d 00:00:00'); @@ -2537,6 +2541,7 @@ sub _contact_exact_group { my $group_ids = Bugzilla::Group->flatten_group_membership($group->id); if ($field eq 'cc' && $chart_id eq '') { + # This is for the email1, email2, email3 fields from query.cgi. $chart_id = "CC$$sequence"; $args->{sequence}++; @@ -2553,8 +2558,8 @@ sub _contact_exact_group { $from = "$join_table_alias.who"; } - my $table = "user_group_map_$chart_id"; - my $join = { + my $table = "user_group_map_$chart_id"; + my $join = { table => 'user_group_map', as => $table, from => $from, @@ -2603,12 +2608,12 @@ sub _get_user_id { ###################################### sub _chart_resolution_parser { - my ($self, $args) = @_; + my ($self, $args) = @_; my ($value, $operator) = @$args{qw(value operator)}; # Treat `---` as empty if (trim($value) eq '---' && $operator =~ /^(?:not)?equals$/) { - $args->{value} = $args->{all_values} = $args->{quoted} = ''; + $args->{value} = $args->{all_values} = $args->{quoted} = ''; $args->{operator} = $operator eq 'equals' ? 'isempty' : 'isnotempty'; } } @@ -2652,7 +2657,7 @@ sub _user_nonchanged { # We want to copy $join so as not to modify USER_FIELDS. push(@$joins, {%$join, as => $as, extra => $extra}); my $search_field = USER_FIELDS->{$field}->{field}; - $args->{full_field} = "$as.$search_field"; + $args->{full_field} = "$as.$search_field"; $args->{enabled_field} = "$as.is_enabled"; } @@ -2677,7 +2682,7 @@ sub _user_nonchanged { join => (!$is_in_other_table and !$is_nullable) ? 'INNER' : undef, }; push(@$joins, $join); - $args->{full_field} = "$as.login_name"; + $args->{full_field} = "$as.login_name"; $args->{enabled_field} = "$as.is_enabled"; } @@ -2890,27 +2895,33 @@ sub _relation_count_changed { sub _relation_count_default { my ($self, $args) = @_; my ($chart_id, $field, $joins) = @$args{qw(chart_id field joins)}; - my $extra = !$self->_user->is_insider - && $field =~ /^(?:attachments|longdescs)\.count$/ ? 'WHERE isprivate = 0' : ''; + my $extra + = !$self->_user->is_insider && $field =~ /^(?:attachments|longdescs)\.count$/ + ? 'WHERE isprivate = 0' + : ''; my ($table, $column, $other_column) - = $field eq 'attachments.count' ? ('attachments', 'attach_id', 'bug_id') - : $field eq 'cc_count' ? ('cc', 'cc', 'bug_id') - : $field eq 'see_also_count' ? ('bug_see_also', 'id', 'bug_id') - : $field eq 'keywords.count' ? ('keywords', 'keywords', 'bug_id') - : $field eq 'longdescs.count' ? ('longdescs', 'comment_id', 'bug_id') - : $field eq 'blocked.count' ? ('dependencies', 'blocked', 'dependson') - : $field eq 'dependson.count' ? ('dependencies', 'dependson', 'blocked') - : $field eq 'regressed_by.count' ? ('regressions', 'regressed_by', 'regresses') + = $field eq 'attachments.count' ? ('attachments', 'attach_id', 'bug_id') + : $field eq 'cc_count' ? ('cc', 'cc', 'bug_id') + : $field eq 'see_also_count' ? ('bug_see_also', 'id', 'bug_id') + : $field eq 'keywords.count' ? ('keywords', 'keywords', 'bug_id') + : $field eq 'longdescs.count' ? ('longdescs', 'comment_id', 'bug_id') + : $field eq 'blocked.count' ? ('dependencies', 'blocked', 'dependson') + : $field eq 'dependson.count' ? ('dependencies', 'dependson', 'blocked') + : $field eq 'regressed_by.count' + ? ('regressions', 'regressed_by', 'regresses') : $field eq 'regresses.count' ? ('regressions', 'regresses', 'regressed_by') - : $field eq 'dupe_count' ? ('duplicates', 'dupe', 'dupe_of') - : undef; + : $field eq 'dupe_count' ? ('duplicates', 'dupe', 'dupe_of') + : undef; my $alias = "${column}_count_${chart_id}"; - push(@$joins, { - table => "(SELECT $other_column AS bug_id, COUNT(*) AS num" - . " FROM $table $extra GROUP BY $other_column)", - as => $alias, - }); + push( + @$joins, + { + table => "(SELECT $other_column AS bug_id, COUNT(*) AS num" + . " FROM $table $extra GROUP BY $other_column)", + as => $alias, + } + ); $args->{full_field} = "COALESCE(${alias}.num, 0)"; } @@ -3022,10 +3033,10 @@ sub _component_nonchanged { # Allow to search product/component pairs like "Core::General" with a simple # operator. Since product/component names may include spaces, other operators # like `anywords` won't work. - if ($args->{operator} =~ /^(?:(?:not)?equals)$/ + if ( $args->{operator} =~ /^(?:(?:not)?equals)$/ && $args->{value} =~ /^(?:(.+)\s*::\s*)?(.+)$/) { - $product = $1; + $product = $1; $args->{value} = $args->{all_values} = $2; $args->{quoted} = $dbh->quote($2); } @@ -3036,16 +3047,23 @@ sub _component_nonchanged { my $term = $args->{term}; if ($product) { + # Pass the complete condition and negative option to make sure both product # and component are included or excluded - $args->{term} = build_subselect('bugs.component_id', 'components.id', + $args->{term} = build_subselect( + 'bugs.component_id', + 'components.id', 'components JOIN products ON components.product_id = products.id', - 'products.name = ' . $dbh->quote($product) - . ' AND components.name = ' . $args->{quoted}, - $args->{operator} eq 'notequals'); - } else { - $args->{term} = build_subselect('bugs.component_id', 'components.id', - 'components', $term); + 'products.name = ' + . $dbh->quote($product) + . ' AND components.name = ' + . $args->{quoted}, + $args->{operator} eq 'notequals' + ); + } + else { + $args->{term} + = build_subselect('bugs.component_id', 'components.id', 'components', $term); } } @@ -3063,7 +3081,7 @@ sub _product_nonchanged { } } if ($aliased) { - $args->{value} = join(',', @{$args->{all_values}}); + $args->{value} = join(',', @{$args->{all_values}}); $args->{quoted} = Bugzilla->dbh->quote($args->{value}); } } @@ -3083,6 +3101,7 @@ sub _product_nonchanged { sub _team_name_nonchanged { my ($self, $args) = @_; + # This joins the right tables for us. $self->_add_extra_column('component'); $args->{full_field} = "map_component.team_name"; @@ -3170,8 +3189,11 @@ sub _owner_idle_time_greater_less { my $alias = "${activity_table}_$table"; "EXISTS (SELECT 1 FROM $activity_table $alias WHERE $alias.who = $owner AND $alias.$when > $quoted)" - } (['longdescs', 'bug_when'], ['bugs_activity', 'bug_when'], - ['flag_activity', 'flag_when']) + } ( + ['longdescs', 'bug_when'], + ['bugs_activity', 'bug_when'], + ['flag_activity', 'flag_when'] + ) ); $args->{term} = $operator =~ /greater/ ? "NOT ($active)" : "($active)"; @@ -3182,7 +3204,7 @@ sub _multiselect_negative { my ($field, $operator) = @$args{qw(field operator)}; $args->{operator} = $self->_reverse_operator($operator); - $args->{term} = $self->_multiselect_term($args, 1); + $args->{term} = $self->_multiselect_term($args, 1); } sub _multiselect_multiple { @@ -3240,9 +3262,9 @@ sub _flagtypes_nonchanged { } my $subselect_args = { - chart_id => $chart_id, - sequence => $chart_id, - field => 'flagtypes.name', + chart_id => $chart_id, + sequence => $chart_id, + field => 'flagtypes.name', full_field => $dbh->sql_string_concat("flagtypes_$chart_id.name", "flags_$chart_id.status"), operator => $operator, @@ -3318,7 +3340,7 @@ sub _multiselect_table { } elsif ($field eq 'longdesc') { $args->{_extra_where} = " AND isprivate = 0" if !$self->_user->is_insider; - $args->{full_field} = 'thetext'; + $args->{full_field} = 'thetext'; return "longdescs"; } elsif ($field eq 'longdescs.isprivate') { @@ -3360,7 +3382,7 @@ sub _multiselect_term { my $table = $self->_multiselect_table($args); $self->_do_operator_function($args); my $term = $args->{term}; - $term .= $args->{_extra_where} || ''; + $term .= $args->{_extra_where} || ''; my $select = $args->{_select_field} || 'bug_id'; return build_subselect("$args->{bugs_table}.bug_id", $select, $table, $term, $not); @@ -3374,7 +3396,7 @@ sub _multiselect_isempty { = @$args{qw(field operator joins chart_id)}; my $dbh = Bugzilla->dbh; $operator = $self->_reverse_operator($operator) if $not; - $not = $operator eq 'isnotempty' ? 'NOT' : ''; + $not = $operator eq 'isnotempty' ? 'NOT' : ''; if ($field eq 'keywords') { push @$joins, @@ -3633,9 +3655,11 @@ sub _changedbefore_changedafter { my $join = {table => 'bugs_activity', extra => []}; if ($field eq 'anything') { + # Handle special field name to find changes in any field $table = $join->{as} = "act_x_$chart_id"; - } else { + } + else { my $field_object = $self->_chart_fields->{$field} || ThrowCodeError("invalid_field_name", {field => $field}); @@ -3655,7 +3679,7 @@ sub _changedbefore_changedafter { } my $sql_operator = ($operator =~ /before/) ? '<=' : '>='; - my $sql_date = $dbh->quote(SqlifyDate($value)); + my $sql_date = $dbh->quote(SqlifyDate($value)); push(@{$join->{extra}}, "$table.bug_when $sql_operator $sql_date"); $args->{term} = "$table.bug_when IS NOT NULL"; @@ -3667,8 +3691,7 @@ sub _changedbefore_changedafter { my $fa = "flag_act_$chart_id"; $args->{term} = "($args->{term} OR " . $self->_flag_activity_exists($args, $fa, - "$fa.flag_when $sql_operator $sql_date") - . ')'; + "$fa.flag_when $sql_operator $sql_date") . ')'; } } @@ -3678,13 +3701,15 @@ sub _changedfrom_changedto { = @$args{qw(chart_id joins field operator quoted)}; my $table; - my $join = {table => 'bugs_activity', extra => []}; + my $join = {table => 'bugs_activity', extra => []}; my $column = ($operator =~ /from/) ? 'removed' : 'added'; if ($field eq 'anything') { + # Handle special field name to find changes in any field $table = $join->{as} = "act_x_$chart_id"; - } else { + } + else { my $field_object = $self->_chart_fields->{$field} || ThrowCodeError("invalid_field_name", {field => $field}); my $field_id = $field_object->id; @@ -3700,10 +3725,10 @@ sub _changedfrom_changedto { # Flag changes are in flag_activity. if ($field eq 'anything') { - my $fa = "flag_act_$chart_id"; - my $match - = _flag_state_changed_sql($chart_id, $fa, $operator, $args->{value}); - $args->{term} = "($args->{term} OR " + my $fa = "flag_act_$chart_id"; + my $match = _flag_state_changed_sql($chart_id, $fa, $operator, $args->{value}); + $args->{term} + = "($args->{term} OR " . $self->_flag_activity_exists($args, $fa, $match) . ')' if $match; } @@ -3718,9 +3743,11 @@ sub _changedby { my $join = {table => 'bugs_activity', extra => []}; if ($field eq 'anything') { + # Handle special field name to find changes in any field $table = $join->{as} = "act_x_$chart_id"; - } else { + } + else { my $field_object = $self->_chart_fields->{$field} || ThrowCodeError("invalid_field_name", {field => $field}); my $field_id = $field_object->id; @@ -3743,9 +3770,11 @@ sub _changedby { if ($field eq 'anything') { my $fa = "flag_act_$chart_id"; $args->{term} = "($args->{term} OR " - . $self->_flag_activity_exists($args, $fa, "$fa.who = $user_id", - Bugzilla::FlagActivity->after_cutoff_sql($fa)) - . ')'; + . $self->_flag_activity_exists( + $args, $fa, + "$fa.who = $user_id", + Bugzilla::FlagActivity->after_cutoff_sql($fa) + ) . ')'; } } @@ -3787,7 +3816,7 @@ sub _flagtypes_changed { if ($operator =~ /^changed(before|after)$/) { my $sql_operator = $1 eq 'before' ? '<=' : '>='; - my $sql_date = $dbh->quote(SqlifyDate($value)); + my $sql_date = $dbh->quote(SqlifyDate($value)); push @where, "$fa.flag_when $sql_operator $sql_date"; } elsif ($operator eq 'changedby') { @@ -3814,7 +3843,8 @@ sub _flag_activity_exists { push @where, "COALESCE((SELECT isprivate FROM attachments WHERE attach_id = $fa.attachment_id), 0) = 0"; } - return "EXISTS (SELECT 1 FROM flag_activity $fa WHERE " + return + "EXISTS (SELECT 1 FROM flag_activity $fa WHERE " . join(' AND ', @where) . ')'; } @@ -3825,6 +3855,7 @@ sub _flag_activity_exists { # (the state before this change), or undef if $value isn't a flag state. sub _flag_state_changed_sql { my ($chart_id, $fa, $operator, $value) = @_; + # As in history, a request counts as one net change. my $flag_last = Bugzilla::FlagActivity->last_of_request_sql($fa); if ($operator eq 'changedto') { @@ -3847,12 +3878,17 @@ sub _flag_state_match { = $value =~ /^\s*(.+?)([?+\-])(?:\(([^)]+)\))?\s*$/ or return; - my $sql = "$table.type_id IN (SELECT id FROM flagtypes WHERE name = " - . $dbh->quote($name) . ") AND $table.status = " . $dbh->quote($status); + my $sql + = "$table.type_id IN (SELECT id FROM flagtypes WHERE name = " + . $dbh->quote($name) + . ") AND $table.status = " + . $dbh->quote($status); if ($status eq '?') { - $sql .= $requestee + $sql + .= $requestee ? " AND $table.requestee_id = (SELECT userid FROM profiles" - . ' WHERE login_name = ' . $dbh->quote($requestee) . ')' + . ' WHERE login_name = ' + . $dbh->quote($requestee) . ')' : " AND $table.requestee_id IS NULL"; } return $sql; diff --git a/Bugzilla/Test/Util.pm b/Bugzilla/Test/Util.pm index be29dd5224..f229416dac 100644 --- a/Bugzilla/Test/Util.pm +++ b/Bugzilla/Test/Util.pm @@ -51,9 +51,7 @@ sub create_group { my ($group, %extra) = @_; require Bugzilla; return Bugzilla::Group->create({ - name => $group, - description => "$group Group Description", - %extra, + name => $group, description => "$group Group Description", %extra, }); } @@ -64,8 +62,10 @@ sub create_oauth_client { my $id = generate_random_password(20); my $secret = generate_random_password(40); - $dbh->do('INSERT INTO oauth2_client (client_id, description, secret, hostname) VALUES (?, ?, ?, ?)', - undef, $id, $description, $secret, hostname()); + $dbh->do( + 'INSERT INTO oauth2_client (client_id, description, secret, hostname) VALUES (?, ?, ?, ?)', + undef, $id, $description, $secret, hostname() + ); my $client_data = $dbh->selectrow_hashref('SELECT * FROM oauth2_client WHERE client_id = ?', @@ -78,10 +78,8 @@ sub create_oauth_client { if (!$scope_id) { die "Scope $scope not found"; } - $dbh->do( - 'INSERT INTO oauth2_client_scope (client_id, scope_id) VALUES (?, ?)', - undef, $client_data->{id}, $scope_id - ); + $dbh->do('INSERT INTO oauth2_client_scope (client_id, scope_id) VALUES (?, ?)', + undef, $client_data->{id}, $scope_id); } $client_data->{scopes} = $scopes; @@ -114,7 +112,8 @@ sub set_flag_activity_cutoff { my ($when) = @_; my $dbh = Bugzilla->dbh; $dbh->do('DELETE FROM flag_activity_migration'); - $dbh->do('INSERT INTO flag_activity_migration (cutoff) VALUES (?)', undef, $when); + $dbh->do('INSERT INTO flag_activity_migration (cutoff) VALUES (?)', + undef, $when); delete Bugzilla->request_cache->{flag_activity_cutoff}; } diff --git a/editusers.cgi b/editusers.cgi index 9761a92917..0a7ab330ea 100755 --- a/editusers.cgi +++ b/editusers.cgi @@ -333,16 +333,15 @@ elsif ($action eq 'update') { } if (@groups_added_to || @groups_removed_from) { - $otherUser->set_groups({add => \@groups_added_to, remove => \@groups_removed_from}); + $otherUser->set_groups( + {add => \@groups_added_to, remove => \@groups_removed_from}); } if (@groups_granted_rights_to_bless || @groups_denied_rights_to_bless) { - $otherUser->set_bless_groups( - { - add => \@groups_granted_rights_to_bless, - remove => \@groups_denied_rights_to_bless - } - ); + $otherUser->set_bless_groups({ + add => \@groups_granted_rights_to_bless, + remove => \@groups_denied_rights_to_bless + }); } my $changes = $otherUser->update(); @@ -391,14 +390,17 @@ elsif ($action eq 'del') { undef, $otherUserID); $vars->{'cc'} = $dbh->selectrow_array('SELECT COUNT(*) FROM cc WHERE who = ?', undef, $otherUserID); + # Flag changes are in flag_activity, not bugs_activity. $vars->{'bugs_activity'} = $dbh->selectrow_array( 'SELECT (SELECT COUNT(*) FROM bugs_activity WHERE who = ?) + (SELECT COUNT(*) FROM flag_activity WHERE who = ? OR setter_id = ?)', undef, ($otherUserID) x 3 ); + # The foreign key forbids deleting a requestee kept in the flag history. - $vars->{'flag_activity_requestee'} = $dbh->selectrow_array( + $vars->{'flag_activity_requestee'} + = $dbh->selectrow_array( 'SELECT COUNT(*) FROM flag_activity WHERE requestee_id = ?', undef, $otherUserID); $vars->{'component_cc'} diff --git a/extensions/BMO/lib/Reports/ReleaseTracking.pm b/extensions/BMO/lib/Reports/ReleaseTracking.pm index 3b6c20b023..d76a828fa0 100644 --- a/extensions/BMO/lib/Reports/ReleaseTracking.pm +++ b/extensions/BMO/lib/Reports/ReleaseTracking.pm @@ -15,8 +15,8 @@ use Bugzilla::Constants; use Bugzilla::Error; use Bugzilla::Field; use Bugzilla::FlagType; -use Bugzilla::Util qw(validate_date); -use JSON qw(-convert_blessed_universally); +use Bugzilla::Util qw(validate_date); +use JSON qw(-convert_blessed_universally); use List::MoreUtils qw(uniq); sub report { @@ -216,8 +216,8 @@ sub report { AND fa.type_id = ? AND fa.status = ? AND CONVERT_TZ(fa.flag_when, 'UTC', 'America/Los_Angeles') BETWEEN ? AND ?))"; push @params, $q->{field_id}, @range, - '%' . $q->{flag_name} . $q->{flag_status} . '%', - $type_id, $q->{flag_status}, @range; + '%' . $q->{flag_name} . $q->{flag_status} . '%', $type_id, $q->{flag_status}, + @range; } push @where, "(f.type_id = ?)"; @@ -282,7 +282,7 @@ sub report { } sub _parse_query { - my $q = shift; + my $q = shift; my @query = split(/:/, $q); my $query; diff --git a/extensions/BMO/lib/Reports/UserActivity.pm b/extensions/BMO/lib/Reports/UserActivity.pm index b8690b5790..85666c00c6 100644 --- a/extensions/BMO/lib/Reports/UserActivity.pm +++ b/extensions/BMO/lib/Reports/UserActivity.pm @@ -25,12 +25,12 @@ sub report { my @who = (); my $from = trim($input->{'from'} || ''); - my $to = trim($input->{'to'} || ''); + my $to = trim($input->{'to'} || ''); my $action = $input->{'action'} || ''; # fix non-breaking hyphens $from =~ s/\N{U+2011}/-/g; - $to =~ s/\N{U+2011}/-/g; + $to =~ s/\N{U+2011}/-/g; if ($from eq '') { my $dt = DateTime->now()->subtract('weeks' => 1); @@ -117,18 +117,19 @@ sub report { # Flag changes are in flag_activity, older ones in bugs_activity. CHAR(63) # is a question mark: a literal one would be taken for a placeholder by # the debug output below. - my $flag_newer = Bugzilla::FlagActivity->after_cutoff_sql('fsa'); + my $flag_newer = Bugzilla::FlagActivity->after_cutoff_sql('fsa'); my $ba_before_cutoff = Bugzilla::FlagActivity->bugs_activity_before_cutoff_sql('bugs_activity'); - my $flag_last = Bugzilla::FlagActivity->last_of_request_sql('fsa'); - my $flag_prev_id = Bugzilla::FlagActivity->previous_id_sql('fsa'); - my $flag_removed_req = $dbh->sql_string_concat('ft.name', 'prev.status', - q{'('}, 'preq.login_name', q{')'}); + my $flag_last = Bugzilla::FlagActivity->last_of_request_sql('fsa'); + my $flag_prev_id = Bugzilla::FlagActivity->previous_id_sql('fsa'); + my $flag_removed_req = $dbh->sql_string_concat('ft.name', 'prev.status', q{'('}, + 'preq.login_name', q{')'}); my $flag_removed = $dbh->sql_string_concat('ft.name', 'prev.status'); - my $flag_added_req = $dbh->sql_string_concat('ft.name', 'fsa.status', - q{'('}, 'req.login_name', q{')'}); + my $flag_added_req + = $dbh->sql_string_concat('ft.name', 'fsa.status', q{'('}, 'req.login_name', + q{')'}); my $flag_added = $dbh->sql_string_concat('ft.name', 'fsa.status'); - my $query = " + my $query = " SELECT fielddefs.name, bugs_activity.bug_id, @@ -160,8 +161,7 @@ sub report { fsa.bug_id, fsa.attachment_id AS attach_id, " - . $dbh->sql_date_format('fsa.flag_when', '%Y-%m-%d %H:%i:%s') - . " AS ts, + . $dbh->sql_date_format('fsa.flag_when', '%Y-%m-%d %H:%i:%s') . " AS ts, CASE WHEN prev.id IS NULL THEN '' WHEN prev.status = CHAR(63) AND preq.login_name IS NOT NULL THEN $flag_removed_req diff --git a/extensions/BugModal/lib/ActivityStream.pm b/extensions/BugModal/lib/ActivityStream.pm index 7e200826e4..7712ff019b 100644 --- a/extensions/BugModal/lib/ActivityStream.pm +++ b/extensions/BugModal/lib/ActivityStream.pm @@ -100,12 +100,12 @@ sub find_activity_id_for_flag { my $stream = $self->activity_stream; if (!exists $self->{activity_cache}->{flag}->{$flag_id}) { + # flag_activity records each change against the flag's object id, so we # can look up the exact timestamp without relying on flag-type name matching. my ($flag_when, $who) = Bugzilla->dbh->selectrow_array( "SELECT flag_when, who FROM flag_activity WHERE flag_id = ? - ORDER BY id DESC LIMIT 1", - undef, $flag_id + ORDER BY id DESC LIMIT 1", undef, $flag_id ); # flag_activity only logged review, feedback and needinfo before, so other @@ -117,15 +117,12 @@ sub find_activity_id_for_flag { = ($flag->modification_date, $flag->setter->id, $flag->type->name); } - SEARCH: foreach my $change_set (reverse @$stream) { + SEARCH: foreach my $change_set (reverse @$stream) { foreach my $activity (@{$change_set->{activity}}) { - next - unless $activity->{who}->id == $who && $activity->{when} eq $flag_when; + next unless $activity->{who}->id == $who && $activity->{when} eq $flag_when; foreach my $change (@{$activity->{changes}}) { next unless $change->{fieldname} eq 'flagtypes.name'; - next - if defined $flagtype_name - && $change->{flagtype_name} ne $flagtype_name; + next if defined $flagtype_name && $change->{flagtype_name} ne $flagtype_name; $self->{activity_cache}->{flag}->{$flag_id} = $change_set->{id}; last SEARCH; } @@ -217,8 +214,7 @@ sub _add_activities_to_stream { : Bugzilla::Bug::GetBugActivity($bug->id); # allow other extensions to alter history - Bugzilla::Hook::process('inline_history_activity', - {activity => $raw_activity}); + Bugzilla::Hook::process('inline_history_activity', {activity => $raw_activity}); my %attachment_cache; foreach my $attachment (@{$bug->attachments}) { @@ -262,7 +258,7 @@ sub _add_activities_to_stream { my $column = $dbh->bz_column_info($table, $field); if ($column && $column->{TYPE} eq 'BOOLEAN') { $change->{removed} = ''; - $change->{added} = $change->{added} ? 'true' : 'false'; + $change->{added} = $change->{added} ? 'true' : 'false'; } # load field object (only required for custom fields), and set the @@ -274,7 +270,8 @@ sub _add_activities_to_stream { } # identify buglist changes - if ($change->{fieldname} =~ /^(?:dependson|blocked|regress(?:ed_by|es)|dup_id|duplicates)$/ + if ($change->{fieldname} + =~ /^(?:dependson|blocked|regress(?:ed_by|es)|dup_id|duplicates)$/ || ($field_obj && $field_obj->type == FIELD_TYPE_BUG_ID)) { $change->{buglist} = 1; diff --git a/extensions/TagNewUsers/Extension.pm b/extensions/TagNewUsers/Extension.pm index e56b1a6bf6..214c99c3f9 100644 --- a/extensions/TagNewUsers/Extension.pm +++ b/extensions/TagNewUsers/Extension.pm @@ -204,7 +204,7 @@ sub _is_new { $self->{is_new} = 0; } else { - $self->{is_new} = (( $self->comment_count // 0) <= COMMENT_COUNT) + $self->{is_new} = (($self->comment_count // 0) <= COMMENT_COUNT) || ($self->creation_age <= PROFILE_AGE); } } diff --git a/extensions/UserProfile/lib/Util.pm b/extensions/UserProfile/lib/Util.pm index b575d998a5..96bf80bc83 100644 --- a/extensions/UserProfile/lib/Util.pm +++ b/extensions/UserProfile/lib/Util.pm @@ -119,8 +119,7 @@ EOF EOF # bugs touched - my $flag_newer - = Bugzilla::FlagActivity->after_cutoff_sql('flag_activity'); + my $flag_newer = Bugzilla::FlagActivity->after_cutoff_sql('flag_activity'); _update_statistics($statistics, 'touched', [($user_id) x 3], <after_cutoff_sql('flag_activity'); + my $flag_newer = Bugzilla::FlagActivity->after_cutoff_sql('flag_activity'); return Bugzilla->dbh->selectrow_array( <bugs_activity_before_cutoff_sql( - 'bugs_activity'); + my $before + = Bugzilla::FlagActivity->bugs_activity_before_cutoff_sql('bugs_activity'); my $rows = $dbh->selectall_arrayref(< {}}, $user_id); SELECT products.name AS product, count(*) AS count FROM bugs_activity @@ -404,7 +402,7 @@ sub _set_stats { if ($statistics->{$name}->{id}) { $dbh->do( "UPDATE $table SET count = ? WHERE user_id = ? AND $name_field = ?", - undef, $statistics->{$name}->{count}, + undef, $statistics->{$name}->{count}, $user_id, $name, ); } diff --git a/qa/t/2_test_flags.t b/qa/t/2_test_flags.t index 877b609a24..8d9a8a9888 100644 --- a/qa/t/2_test_flags.t +++ b/qa/t/2_test_flags.t @@ -474,13 +474,14 @@ ok(scalar(@flag_states) == 1 && $flag_states[0] eq '+', # Powerless users cannot set the flag to +, but setting it to ? is allowed. @flag_states = $sel->get_select_options("flag_type-$flagtype1_id"); -ok(scalar @flag_states == 2, "Two flag states available"); +ok(scalar @flag_states == 2, "Two flag states available"); ok(grep($_ eq '?', @flag_states), "Flag state '?' available"); # A powerless user cannot edit someone else's attachment flags. $sel->click_ok( "//a[contains(\@href,'/attachment.cgi?id=$attachment2_id&action=edit')]"); + # Wait a sec before the attachment overlay is loaded. sleep(1); $sel->is_element_present_ok( @@ -530,6 +531,7 @@ log_in($sel, $config, 'admin'); go_to_bug($sel, $bug1_id); $sel->click_ok( "//a[contains(\@href,'/attachment.cgi?id=${attachment3_id}&action=edit')]"); + # Wait a sec before the attachment overlay is loaded. sleep(1); $sel->is_element_present_ok( @@ -537,6 +539,7 @@ $sel->is_element_present_ok( ); $sel->select_ok('//select[@title="attachmentflag1"]', "label=+"); $sel->click_ok('//dialog[@id="att-overlay"]//input[@type="submit"]'); + # Wait a sec before the attachment is updated. sleep(1); $sel->wait_for_page_to_load_ok(WAIT_TIME); diff --git a/qa/t/4_test_user_delete_requestee.t b/qa/t/4_test_user_delete_requestee.t index 95f21ad89a..82e961d46f 100644 --- a/qa/t/4_test_user_delete_requestee.t +++ b/qa/t/4_test_user_delete_requestee.t @@ -24,11 +24,13 @@ set_parameters($sel, # flag_activity.requestee_id keeps a user from being deleted, so the # confirmation page must not offer it. my $dbh = Bugzilla->dbh; -$dbh->do("INSERT INTO profiles (login_name, realname, cryptpassword) - VALUES ('requestee\@selenium.bugzilla.org', 'Requestee', '*')"); +$dbh->do( + "INSERT INTO profiles (login_name, realname, cryptpassword) + VALUES ('requestee\@selenium.bugzilla.org', 'Requestee', '*')" +); my $user_id = $dbh->bz_last_key('profiles', 'userid'); -my ($admin_id) = $dbh->selectrow_array( - 'SELECT userid FROM profiles WHERE login_name = ?', +my ($admin_id) + = $dbh->selectrow_array('SELECT userid FROM profiles WHERE login_name = ?', undef, $config->{admin_user_login}); my ($bug_id) = $dbh->selectrow_array('SELECT MIN(bug_id) FROM bugs'); my ($type_id) = $dbh->selectrow_array('SELECT MIN(id) FROM flagtypes'); @@ -40,14 +42,15 @@ $dbh->do( ); # The page has its verdict only if there is also something safe to report. -$dbh->do('INSERT INTO cc (bug_id, who) VALUES (?, ?)', undef, $bug_id, $user_id); +$dbh->do('INSERT INTO cc (bug_id, who) VALUES (?, ?)', undef, $bug_id, + $user_id); $sel->open_ok("/editusers.cgi?action=del&userid=$user_id"); $sel->is_text_present_ok('named in the flag history'); $sel->is_text_present_ok('You cannot delete this user account'); -$dbh->do('DELETE FROM cc WHERE who = ?', undef, $user_id); +$dbh->do('DELETE FROM cc WHERE who = ?', undef, $user_id); $dbh->do('DELETE FROM flag_activity WHERE requestee_id = ?', undef, $user_id); -$dbh->do('DELETE FROM profiles WHERE userid = ?', undef, $user_id); +$dbh->do('DELETE FROM profiles WHERE userid = ?', undef, $user_id); logout($sel); diff --git a/t/bmo/bugmail-flag-events.t b/t/bmo/bugmail-flag-events.t index 7ebdea10c0..7356ecae76 100644 --- a/t/bmo/bugmail-flag-events.t +++ b/t/bmo/bugmail-flag-events.t @@ -90,7 +90,7 @@ sub add_activity { 'INSERT INTO flag_activity (flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, attachment_id, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', undef, $when, - $extra{type_id} // $type_id, $flag_id, $setter->id, $setter->id, + $extra{type_id} // $type_id, $flag_id, $setter->id, $setter->id, $requestee ? $requestee->id : undef, $bug->id, $extra{attachment_id}, $status ); } @@ -100,8 +100,8 @@ sub add_activity { sub flag_type { my ($name, $target_type, @cc) = @_; my ($id) - = $dbh->selectrow_array('SELECT id FROM flagtypes WHERE name = ?', - undef, $name); + = $dbh->selectrow_array('SELECT id FROM flagtypes WHERE name = ?', undef, + $name); return $id if $id; $dbh->do( 'INSERT INTO flagtypes (name, description, cc_list, target_type, @@ -165,13 +165,15 @@ sub sent_mails { = map { $next_flag_id++ } 1 .. 6; # Before the window. - add_activity($bug, $granted, '1999-12-31 23:00:00', '?', $requester, $requestee); + add_activity($bug, $granted, '1999-12-31 23:00:00', '?', $requester, + $requestee); add_activity($bug, $regranted, '1999-12-31 23:00:00', '+', $requestee); # In the window. - add_activity($bug, $asked, '2000-01-01 00:00:10', '?', $requester, $requestee); - add_activity($bug, $granted, '2000-01-01 00:00:20', '+', $requestee); - add_activity($bug, $auto_cleared, '2000-01-01 00:00:30', '?', $requester, $requestee); + add_activity($bug, $asked, '2000-01-01 00:00:10', '?', $requester, $requestee); + add_activity($bug, $granted, '2000-01-01 00:00:20', '+', $requestee); + add_activity($bug, $auto_cleared, '2000-01-01 00:00:30', + '?', $requester, $requestee); add_activity($bug, $auto_cleared, '2000-01-01 00:00:30', 'X', $requestee); add_activity($bug, $regranted, '2000-01-01 00:00:40', 'X', $requester); add_activity($bug, $no_requestee, '2000-01-01 00:00:50', '?', $requester); @@ -186,16 +188,20 @@ sub sent_mails { is_deeply( [map { [$_->{action}, $_->{status}] } @events], [ - ['requested', '?'], ['answered', '+'], ['requested', '?'], - ['answered', 'X'], ['set', 'X'], ['set', '?'], + ['requested', '?'], + ['answered', '+'], + ['requested', '?'], + ['answered', 'X'], + ['set', 'X'], + ['set', '?'], ], 'one event per in-window row, classified' ); is($events[0]{requestee_id}, $requestee->id, 'a request carries its requestee'); - is($events[1]{requester_id}, $requester->id, - "an answer finds its requester from a '?' before the window"); - is($events[3]{requester_id}, $requester->id, - "an 'X' auto-clear in the same second as its '?' is an answer"); + is($events[1]{requester_id}, + $requester->id, "an answer finds its requester from a '?' before the window"); + is($events[3]{requester_id}, + $requester->id, "an 'X' auto-clear in the same second as its '?' is an answer"); ok(!$events[4]{requester_id}, 'clearing an already granted flag has no requester to notify'); } @@ -203,26 +209,29 @@ sub sent_mails { # Send: a request reaches the requestee and the flag type cc_list only. { my $bug = new_bug(); - add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', '?', $requester, - $requestee); + add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', + '?', $requester, $requestee); my $sent = sent_to($bug, $requester); ok($sent->{$requestee->login}, 'requestee with no role on the bug is mailed'); ok($sent->{$type_cc->login}, 'flag type cc_list account is mailed'); - ok(!$sent->{$requester->login}, 'requester is not mailed about their own request'); - ok(!$sent->{$stranger->login}, 'unrelated user is not mailed'); + ok(!$sent->{$requester->login}, + 'requester is not mailed about their own request'); + ok(!$sent->{$stranger->login}, 'unrelated user is not mailed'); } # Send: an answer reaches the requester, not the person who answered. { my $bug = new_bug(); my $flag_id = $next_flag_id++; - add_activity($bug, $flag_id, '1999-12-31 23:00:00', '?', $requester, $requestee); + add_activity($bug, $flag_id, '1999-12-31 23:00:00', '?', $requester, + $requestee); add_activity($bug, $flag_id, '2000-01-01 00:00:10', '+', $requestee); my $sent = sent_to($bug, $requestee); ok($sent->{$requester->login}, 'requester is told their request was answered'); - ok(!$sent->{$requestee->login}, 'answerer is not mailed about their own answer'); + ok(!$sent->{$requestee->login}, + 'answerer is not mailed about their own answer'); } # An answer to a flag re-requested by someone else goes to the latest @@ -231,20 +240,20 @@ my $requester2 = create_user('flagmail-requester2@mozilla.example', '*'); foreach my $later_first (0, 1) { my $bug = new_bug(); my $flag_id = $next_flag_id++; - my @asks = ( - ['1999-12-31 23:00:00', $requester], - ['1999-12-31 23:30:00', $requester2], - ); + my @asks + = (['1999-12-31 23:00:00', $requester], ['1999-12-31 23:30:00', $requester2], + ); @asks = reverse @asks if $later_first; add_activity($bug, $flag_id, $_->[0], '?', $_->[1], $requestee) for @asks; add_activity($bug, $flag_id, '2000-01-01 00:00:10', '+', $requestee); - my $order = $later_first ? 'later request inserted first' : 'inserted in time order'; + my $order + = $later_first ? 'later request inserted first' : 'inserted in time order'; my @events = Bugzilla::BugMail::_get_flag_mail_events($bug, $window_start, $window_end, {}); - is($events[0]{requester_id}, $requester2->id, - "an answer goes to the latest requester ($order)"); + is($events[0]{requester_id}, + $requester2->id, "an answer goes to the latest requester ($order)"); my $sent = sent_to($bug, $requestee); ok($sent->{$requester2->login}, "the latest requester is mailed ($order)"); @@ -255,52 +264,63 @@ foreach my $later_first (0, 1) { # old flag mail had, whether the flag was requested or answered. { my $bug = new_bug(); - add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', '?', $requester, - $requestee); - my ($mail) = grep { $_->{to_user}->id == $requestee->id } - sent_mails($bug, $requester); - is($mail->{bugmailtype}, 'changed request', 'a request is marked as request mail'); + add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', + '?', $requester, $requestee); + my ($mail) + = grep { $_->{to_user}->id == $requestee->id } sent_mails($bug, $requester); + is( + $mail->{bugmailtype}, + 'changed request', + 'a request is marked as request mail' + ); $bug = new_bug(); my $flag_id = $next_flag_id++; - add_activity($bug, $flag_id, '1999-12-31 23:00:00', '?', $requester, $requestee); + add_activity($bug, $flag_id, '1999-12-31 23:00:00', '?', $requester, + $requestee); add_activity($bug, $flag_id, '2000-01-01 00:00:10', '+', $requestee); - ($mail) = grep { $_->{to_user}->id == $requester->id } - sent_mails($bug, $requestee); - is($mail->{bugmailtype}, 'changed request', 'an answer is marked as request mail'); + ($mail) + = grep { $_->{to_user}->id == $requester->id } sent_mails($bug, $requestee); + is( + $mail->{bugmailtype}, + 'changed request', + 'an answer is marked as request mail' + ); } # Send: cancelling your own request doesn't mail you. { my $bug = new_bug(); my $flag_id = $next_flag_id++; - add_activity($bug, $flag_id, '1999-12-31 23:00:00', '?', $requester, $requestee); + add_activity($bug, $flag_id, '1999-12-31 23:00:00', '?', $requester, + $requestee); add_activity($bug, $flag_id, '2000-01-01 00:00:10', 'X', $requester); my $sent = sent_to($bug, $requester); - ok(!$sent->{$requester->login}, 'requester is not mailed for cancelling their own request'); - ok($sent->{$type_cc->login}, 'flag type cc_list account is still mailed'); + ok(!$sent->{$requester->login}, + 'requester is not mailed for cancelling their own request'); + ok($sent->{$type_cc->login}, 'flag type cc_list account is still mailed'); } # Send: someone mailed only because of a flag gets the flag section and the # comments (so a requestee sees the question), but no diffs. { my $bug = new_bug(); - add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', '?', $requester, - $requestee); - my ($mail) = grep { $_->{to_user}->id == $requestee->id } - sent_mails($bug, $requester); + add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', + '?', $requester, $requestee); + my ($mail) + = grep { $_->{to_user}->id == $requestee->id } sent_mails($bug, $requester); - is(scalar @{$mail->{flag_events}}, 1, - 'requestee with no role on the bug gets the flag event'); + is(scalar @{$mail->{flag_events}}, + 1, 'requestee with no role on the bug gets the flag event'); ok(scalar @{$mail->{new_comments}}, 'and the comments'); ok(!@{$mail->{diffs}}, 'but no diffs'); $bug = new_bug(cc => [$requestee->login]); - add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', '?', $requester, - $requestee); - ($mail) = grep { $_->{to_user}->id == $requestee->id } - sent_mails($bug, $requester); + add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', + '?', $requester, $requestee); + ($mail) + = grep { $_->{to_user}->id == $requestee->id } sent_mails($bug, $requester); ok(scalar @{$mail->{new_comments}}, 'requestee who is also CCed gets the comments too'); @@ -316,14 +336,14 @@ foreach my $later_first (0, 1) { {}); my $sent = sent_to($bug, $requester); - ok($sent->{$type_cc->login}, "a '?' with no requestee reaches the flag type cc_list"); + ok($sent->{$type_cc->login}, + "a '?' with no requestee reaches the flag type cc_list"); ok(!$sent->{$requester->login} && !$sent->{$requestee->login}, 'and no requestee or requester'); - my ($accounts, $raw) - = Bugzilla::BugMail::_get_flag_type_cc($bug, \@events, {}); + my ($accounts, $raw) = Bugzilla::BugMail::_get_flag_type_cc($bug, \@events, {}); ok($accounts->{$type_cc->id}, 'cc_list account is an account recipient'); - ok($raw->{$raw_cc}, 'cc_list address with no account takes the raw mail path'); + ok($raw->{$raw_cc}, 'cc_list address with no account takes the raw mail path'); } # The cc_list address with no account gets the new public comments, as with @@ -362,16 +382,20 @@ foreach my $later_first (0, 1) { set_flag_activity_cutoff('1999-12-31 00:00:00'); my $bug = new_bug(assigned_to => $stranger->login); - add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', '?', $requester, - $requestee); + add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', + '?', $requester, $requestee); $dbh->do('UPDATE bugs SET lastdiffed = NULL WHERE bug_id = ?', undef, $bug->id); - my ($mail) = grep { $_->{to_user}->id == $stranger->id } + my ($mail) + = grep { $_->{to_user}->id == $stranger->id } sent_mails(Bugzilla::Bug->new($bug->id), $requester); my @flags = grep { $_->{field_name} eq 'flagtypes.name' } @{$mail->{diffs}}; is(scalar @flags, 1, 'the new bug mail lists the flag set at creation once'); - like($flags[0]{new}, qr/\?\(\Q@{[$requestee->login]}\E\)$/, - 'as requested from the requestee'); + like( + $flags[0]{new}, + qr/\?\(\Q@{[$requestee->login]}\E\)$/, + 'as requested from the requestee' + ); set_flag_activity_cutoff($cutoff); } @@ -383,8 +407,8 @@ foreach my $later_first (0, 1) { my $raw = 'flagmail-insider-list@mozilla.example'; my $group = group_with('flagmail-insiders', $insider); local Bugzilla->params->{insidergroup} = $group->name; - my $private_type = flag_type('flagmail-private', 'a', $insider->login, - $outsider->login, $raw); + my $private_type + = flag_type('flagmail-private', 'a', $insider->login, $outsider->login, $raw); my $bug = new_bug(); $dbh->do( @@ -395,11 +419,14 @@ foreach my $later_first (0, 1) { $bug->id, $window_start, $window_start, $requester->id ); my $attach_id = $dbh->bz_last_key('attachments', 'attach_id'); - add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', '?', $requester, - undef, type_id => $private_type, attachment_id => $attach_id); + add_activity( + $bug, $next_flag_id++, '2000-01-01 00:00:10', '?', $requester, undef, + type_id => $private_type, + attachment_id => $attach_id + ); my ($accounts, $raw_addresses) = flag_type_cc($bug); - ok($accounts->{$insider->id}, 'private attachment flag: insider is mailed'); + ok($accounts->{$insider->id}, 'private attachment flag: insider is mailed'); ok(!$accounts->{$outsider->id}, 'non-insider is not'); ok(!$raw_addresses->{$raw}, 'address with no account is not'); } @@ -410,18 +437,18 @@ foreach my $later_first (0, 1) { my $non_member = create_user('flagmail-nonmember@mozilla.example', '*'); my $raw = 'flagmail-group-list@mozilla.example'; my $group = group_with('flagmail-secure', $member); - my $secure_type = flag_type('flagmail-secure', 'b', $member->login, - $non_member->login, $raw); + my $secure_type + = flag_type('flagmail-secure', 'b', $member->login, $non_member->login, $raw); my $bug = new_bug(); $dbh->do('INSERT INTO bug_group_map (bug_id, group_id) VALUES (?, ?)', undef, $bug->id, $group->id); $bug = Bugzilla::Bug->new($bug->id); - add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', '?', $requester, - undef, type_id => $secure_type); + add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', + '?', $requester, undef, type_id => $secure_type); my ($accounts, $raw_addresses) = flag_type_cc($bug); - ok($accounts->{$member->id}, 'bug in a group: member is mailed'); + ok($accounts->{$member->id}, 'bug in a group: member is mailed'); ok(!$accounts->{$non_member->id}, 'non-member is not'); ok(!$raw_addresses->{$raw}, 'address with no account is not'); } diff --git a/t/flag-activity-history.t b/t/flag-activity-history.t index 1c6dec738a..fbdf8128ff 100644 --- a/t/flag-activity-history.t +++ b/t/flag-activity-history.t @@ -69,11 +69,11 @@ sub flag_changes { my @history = map { my $who = $_->{who}; map { [$who, $_->{removed}, $_->{added}] } - grep { $_->{fieldname} eq 'flagtypes.name' } @{$_->{changes}} + grep { $_->{fieldname} eq 'flagtypes.name' } + @{$_->{changes}} } @$operations; - $dbh->do('UPDATE bugs SET lastdiffed = NULL WHERE bug_id = ?', - undef, $bug_id); + $dbh->do('UPDATE bugs SET lastdiffed = NULL WHERE bug_id = ?', undef, $bug_id); my ($diffs) = Bugzilla::BugMail::_get_diffs(Bugzilla::Bug->new($bug_id), '2026-12-31 00:00:00', {}); my @mail = map { [$_->{who}->login, $_->{old}, $_->{new}] } @@ -121,12 +121,12 @@ is($mail, $expected, 'bugmail: each flag change once, with old value'); # A flag created and updated in one request has two rows in the same second: # history shows the request's net change, like bugs_activity did. my $same_second_bug = new_bug('same second'); -add_flag_activity($same_second_bug, '2026-02-01 10:00:00', 43, $alice, $bob->id, - '?'); -add_flag_activity($same_second_bug, '2026-02-01 10:00:00', 43, $alice, undef, - '+'); -add_flag_activity($same_second_bug, '2026-02-02 10:00:00', 43, $setter, undef, - '-'); +add_flag_activity($same_second_bug, '2026-02-01 10:00:00', + 43, $alice, $bob->id, '?'); +add_flag_activity($same_second_bug, '2026-02-01 10:00:00', + 43, $alice, undef, '+'); +add_flag_activity($same_second_bug, '2026-02-02 10:00:00', + 43, $setter, undef, '-'); $expected = [ ['alice@bmo.test', '', 'review+'], @@ -139,8 +139,8 @@ is($mail, $expected, 'bugmail: one net change per request'); # A flag created and removed in one request changes nothing: neither history # nor bugmail show it. my $no_change_bug = new_bug('no change'); -add_flag_activity($no_change_bug, '2026-03-01 10:00:00', 44, $alice, $bob->id, - '?'); +add_flag_activity($no_change_bug, '2026-03-01 10:00:00', + 44, $alice, $bob->id, '?'); add_flag_activity($no_change_bug, '2026-03-01 10:00:00', 44, $alice, undef, 'X'); diff --git a/t/flag-activity-search.t b/t/flag-activity-search.t index bd8d16cb85..54a326bf6c 100644 --- a/t/flag-activity-search.t +++ b/t/flag-activity-search.t @@ -66,8 +66,8 @@ for my $row (['2026-01-01 10:00:00', '?', $bob->id], 'INSERT INTO flag_activity (flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, status) - VALUES (?, ?, 42, ?, ?, ?, ?, ?)', undef, $row->[0], $review_id, - $alice->id, $alice->id, $row->[2], $current_bug, $row->[1] + VALUES (?, ?, 42, ?, ?, ?, ?, ?)', undef, $row->[0], $review_id, $alice->id, + $alice->id, $row->[2], $current_bug, $row->[1] ); } @@ -81,17 +81,17 @@ sub search { } is(search(changedto => 'review+'), [$legacy_bug, $current_bug], 'changedto'); -is(search(changedto => 'review?(bob@bmo.test)'), [$current_bug], - 'changedto with requestee'); -is(search(changedfrom => 'review?(bob@bmo.test)'), [$current_bug], - 'changedfrom uses the previous flag_activity row'); -is(search(changedfrom => 'review+'), [], 'changedfrom no match'); +is(search(changedto => 'review?(bob@bmo.test)'), + [$current_bug], 'changedto with requestee'); +is(search(changedfrom => 'review?(bob@bmo.test)'), + [$current_bug], 'changedfrom uses the previous flag_activity row'); +is(search(changedfrom => 'review+'), [], 'changedfrom no match'); is(search(changedbefore => '2021-01-01'), [$legacy_bug], 'changedbefore'); is(search(changedafter => '2025-01-01'), [$current_bug], 'changedafter'); -is(search(changedby => 'alice@bmo.test'), [$current_bug], - 'changedby flag_activity'); -is(search(changedby => 'bob@bmo.test'), [$legacy_bug], - 'changedby bugs_activity'); +is(search(changedby => 'alice@bmo.test'), + [$current_bug], 'changedby flag_activity'); +is(search(changedby => 'bob@bmo.test'), + [$legacy_bug], 'changedby bugs_activity'); is(search(everchanged => ''), [$legacy_bug, $current_bug], 'everchanged'); # Changes to any field include flag changes. @@ -103,16 +103,19 @@ sub search_anything { ); return [sort { $a <=> $b } map { $_->[0] } @{$search->data}]; } -is(search_anything(changedby => 'alice@bmo.test'), [$current_bug], - 'anything changedby flag_activity'); -is(search_anything(changedby => 'bob@bmo.test'), [$legacy_bug], - 'anything changedby bugs_activity'); -is(search_anything(changedafter => '2025-01-01'), [$current_bug], - 'anything changedafter'); -is(search_anything(changedto => 'review+'), [$legacy_bug, $current_bug], - 'anything changedto'); -is(search_anything(changedfrom => 'review?(bob@bmo.test)'), [$current_bug], - 'anything changedfrom'); +is(search_anything(changedby => 'alice@bmo.test'), + [$current_bug], 'anything changedby flag_activity'); +is(search_anything(changedby => 'bob@bmo.test'), + [$legacy_bug], 'anything changedby bugs_activity'); +is(search_anything(changedafter => '2025-01-01'), + [$current_bug], 'anything changedafter'); +is( + search_anything(changedto => 'review+'), + [$legacy_bug, $current_bug], + 'anything changedto' +); +is(search_anything(changedfrom => 'review?(bob@bmo.test)'), + [$current_bug], 'anything changedfrom'); # A flag requested and granted in one request is one net change, as in history. my $same_second_bug = new_bug('same second'); @@ -125,14 +128,17 @@ for my $row (['?', $alice->id], ['+', undef]) { $bob->id, $bob->id, $row->[1], $same_second_bug, $row->[0] ); } -is(search(changedto => 'review?(alice@bmo.test)'), [], - 'changedto skips a state within a request'); -is(search(changedfrom => 'review?(alice@bmo.test)'), [], - 'changedfrom skips a state within a request'); -is(search_anything(changedfrom => 'review?(alice@bmo.test)'), [], - 'anything changedfrom skips a state within a request'); -is(search(changedto => 'review+'), - [$legacy_bug, $current_bug, $same_second_bug], 'changedto the net change'); +is(search(changedto => 'review?(alice@bmo.test)'), + [], 'changedto skips a state within a request'); +is(search(changedfrom => 'review?(alice@bmo.test)'), + [], 'changedfrom skips a state within a request'); +is(search_anything(changedfrom => 'review?(alice@bmo.test)'), + [], 'anything changedfrom skips a state within a request'); +is( + search(changedto => 'review+'), + [$legacy_bug, $current_bug, $same_second_bug], + 'changedto the net change' +); # A legacy change also in flag_activity: bugs_activity knows who made it, the # flag_activity row only has the setter as a guess. @@ -145,13 +151,13 @@ $dbh->do( $dbh->do( 'INSERT INTO flag_activity (flag_when, type_id, flag_id, setter_id, who, requestee_id, bug_id, status) - VALUES (?, ?, 44, ?, ?, NULL, ?, ?)', undef, '2020-02-01 10:00:00', $review_id, - $carol->id, $carol->id, $guessed_bug, '+' + VALUES (?, ?, 44, ?, ?, NULL, ?, ?)', undef, '2020-02-01 10:00:00', + $review_id, $carol->id, $carol->id, $guessed_bug, '+' ); -is(search(changedby => 'carol@bmo.test'), [], - 'changedby skips a flag_activity row up to the cutoff'); -is(search_anything(changedby => 'carol@bmo.test'), [], - 'anything changedby skips it too'); +is(search(changedby => 'carol@bmo.test'), + [], 'changedby skips a flag_activity row up to the cutoff'); +is(search_anything(changedby => 'carol@bmo.test'), + [], 'anything changedby skips it too'); # An assignee whose only recent changes are flag changes isn't idle. my $owned_bug = create_bug( @@ -160,13 +166,18 @@ my $owned_bug = create_bug( bug_type => 'defect', assigned_to => 'alice@bmo.test', )->id; + sub idle { my ($operator) = @_; my $search = Bugzilla::Search->new( fields => ['bug_id'], params => { - f1 => 'owner_idle_time', o1 => $operator, v1 => '2025-12-01', - f2 => 'assigned_to', o2 => 'equals', v2 => 'alice@bmo.test', + f1 => 'owner_idle_time', + o1 => $operator, + v1 => '2025-12-01', + f2 => 'assigned_to', + o2 => 'equals', + v2 => 'alice@bmo.test', }, ); return [map { $_->[0] } @{$search->data}]; diff --git a/t/flag-activity-start-rows.t b/t/flag-activity-start-rows.t index 66cf9ca882..725f062e79 100644 --- a/t/flag-activity-start-rows.t +++ b/t/flag-activity-start-rows.t @@ -75,7 +75,8 @@ add_start_rows() for 1 .. 2; set_flag_activity_cutoff('2099-01-01 10:30:00'); is( scalar $dbh->selectrow_array( - 'SELECT COUNT(*) FROM flag_activity WHERE flag_id = ?', undef, $flag->id + 'SELECT COUNT(*) FROM flag_activity WHERE flag_id = ?', + undef, $flag->id ), 1, 'one starting row, however many times checksetup runs' @@ -87,7 +88,8 @@ sub flag_history { map { my $when = $_->{when}; map {"$when: $_->{removed} -> $_->{added}"} - grep { $_->{fieldname} eq 'flagtypes.name' } @{$_->{changes}} + grep { $_->{fieldname} eq 'flagtypes.name' } + @{$_->{changes}} } @$activity ]; } @@ -116,7 +118,8 @@ $bug = Bugzilla::Bug->new($bug_id); $bug->set_flags([], [{type_id => $needinfo_id, status => '?', requestee => 'bob@bmo.test'}]); $bug->update('2099-01-01 12:00:00'); -my ($undated) = grep { $_->status eq '?' } @{Bugzilla::Bug->new($bug_id)->flags}; +my ($undated) + = grep { $_->status eq '?' } @{Bugzilla::Bug->new($bug_id)->flags}; $dbh->do('DELETE FROM flag_activity WHERE flag_id = ?', undef, $undated->id); $dbh->do('UPDATE flags SET modification_date = NULL WHERE id = ?', undef, $undated->id); @@ -124,10 +127,12 @@ $dbh->do('UPDATE flags SET modification_date = NULL WHERE id = ?', add_start_rows(); is( scalar $dbh->selectrow_array( - 'SELECT flag_when FROM flag_activity WHERE flag_id = ?', undef, $undated->id + 'SELECT flag_when FROM flag_activity WHERE flag_id = ?', + undef, $undated->id ), scalar $dbh->selectrow_array( - 'SELECT creation_date FROM flags WHERE id = ?', undef, $undated->id + 'SELECT creation_date FROM flags WHERE id = ?', + undef, $undated->id ), 'a flag without a modification date is dated at its creation' ); @@ -137,7 +142,8 @@ $dbh->do('DELETE FROM flag_activity WHERE flag_id = ?', undef, $undated->id); Bugzilla::Install::DB::_add_flag_activity_start_rows(); is( scalar $dbh->selectrow_array( - 'SELECT COUNT(*) FROM flag_activity WHERE flag_id = ?', undef, $undated->id + 'SELECT COUNT(*) FROM flag_activity WHERE flag_id = ?', + undef, $undated->id ), 0, 'the start rows are only added once' diff --git a/t/flag-activity-user-stats.t b/t/flag-activity-user-stats.t index 7d80fa9205..d88440edeb 100644 --- a/t/flag-activity-user-stats.t +++ b/t/flag-activity-user-stats.t @@ -64,14 +64,23 @@ $bug->update('2099-01-01 11:00:00'); # flag changes are only in flag_activity. $dbh->do('DELETE FROM bugs_activity WHERE bug_id = ?', undef, $bug_id); -is(Bugzilla::Bug->new($bug_id)->delta_ts_non_bot, - '2099-01-01 10:00:00', 'the non-bot last change time counts flag changes'); +is( + Bugzilla::Bug->new($bug_id)->delta_ts_non_bot, + '2099-01-01 10:00:00', + 'the non-bot last change time counts flag changes' +); -is(last_user_activity($human->id), - '2099-01-01 10:00:00', 'the last user activity counts flag changes'); +is( + last_user_activity($human->id), + '2099-01-01 10:00:00', + 'the last user activity counts flag changes' +); -$dbh->do('UPDATE profiles SET last_statistics_ts = ? WHERE userid = ?', - undef, '2099-01-01 12:00:00', $human->id); +$dbh->do( + 'UPDATE profiles SET last_statistics_ts = ? WHERE userid = ?', + undef, '2099-01-01 12:00:00', + $human->id +); tag_for_recount_from_bug($bug_id); my ($last_statistics_ts) = $dbh->selectrow_array( @@ -82,11 +91,13 @@ is($last_statistics_ts, undef, 'a flag change tags the user for a recount'); update_statistics_by_user($human->id); my ($touched) = $dbh->selectrow_array( "SELECT count FROM profiles_statistics WHERE user_id = ? AND name = 'touched'", - undef, $human->id); + undef, $human->id +); is($touched, 1, 'a flag change counts as touching the bug'); sub product_count { - return scalar $dbh->selectrow_array( + return + scalar $dbh->selectrow_array( 'SELECT SUM(count) FROM profiles_statistics_products WHERE user_id = ?', undef, $human->id); } @@ -99,8 +110,8 @@ my ($fieldid) "SELECT id FROM fielddefs WHERE name = 'flagtypes.name'"); $dbh->do( 'INSERT INTO bugs_activity (bug_id, who, bug_when, fieldid, removed, added) - VALUES (?, ?, ?, ?, ?, ?)', undef, $bug_id, $human->id, - '2099-01-01 10:00:00', $fieldid, '', 'needinfo?(bob@bmo.test)' + VALUES (?, ?, ?, ?, ?, ?)', undef, $bug_id, $human->id, '2099-01-01 10:00:00', + $fieldid, '', 'needinfo?(bob@bmo.test)' ); update_statistics_by_user($human->id); is(product_count(), 1, 'a change in both tables is counted once'); @@ -110,17 +121,24 @@ is(product_count(), 1, 'a change in both tables is counted once'); set_flag_activity_cutoff('2099-01-01 13:30:00'); $dbh->do( 'INSERT INTO bugs_activity (bug_id, who, bug_when, fieldid, removed, added) - VALUES (?, ?, ?, ?, ?, ?)', undef, $bug_id, $bot->id, - '2099-01-01 13:00:00', $fieldid, 'needinfo?(bob@bmo.test)', '' + VALUES (?, ?, ?, ?, ?, ?)', undef, $bug_id, $bot->id, '2099-01-01 13:00:00', + $fieldid, 'needinfo?(bob@bmo.test)', '' ); $dbh->do( 'UPDATE flag_activity SET flag_when = ?, who = ? WHERE flag_when = ?', - undef, '2099-01-01 13:00:00', $human->id, '2099-01-01 11:00:00' + undef, '2099-01-01 13:00:00', + $human->id, '2099-01-01 11:00:00' +); +is( + Bugzilla::Bug->new($bug_id)->delta_ts_non_bot, + '2099-01-01 10:00:00', + 'the non-bot last change time skips a guessed who' +); +is( + last_user_activity($human->id), + '2099-01-01 10:00:00', + 'the last user activity skips a guessed who' ); -is(Bugzilla::Bug->new($bug_id)->delta_ts_non_bot, - '2099-01-01 10:00:00', 'the non-bot last change time skips a guessed who'); -is(last_user_activity($human->id), - '2099-01-01 10:00:00', 'the last user activity skips a guessed who'); # One request: a flag requested and granted, another requested and removed. sub add_request_rows { @@ -136,7 +154,10 @@ my $before = product_count(); add_request_rows(900, '?', '+'); add_request_rows(901, '?', 'X'); update_statistics_by_user($human->id); -is(product_count(), $before + 1, - 'the activity by product counts one net change per request'); +is( + product_count(), + $before + 1, + 'the activity by product counts one net change per request' +); done_testing; diff --git a/t/flag-activity-who.t b/t/flag-activity-who.t index 417528da0e..b58383729b 100644 --- a/t/flag-activity-who.t +++ b/t/flag-activity-who.t @@ -76,7 +76,7 @@ is( ); my ($operations) = Bugzilla::Bug::GetBugActivity($bug_id); -my ($redirect) = grep { +my ($redirect) = grep { grep { $_->{fieldname} eq 'flagtypes.name' && $_->{added} =~ /carol/ } @{$_->{changes}} } @$operations; diff --git a/t/flag-type-delete.t b/t/flag-type-delete.t index e8c3cc3c48..a910109579 100644 --- a/t/flag-type-delete.t +++ b/t/flag-type-delete.t @@ -38,7 +38,7 @@ my $unused = Bugzilla::FlagType->create({ description => 'A flag type that is never set', target_type => 'bug', }); -ok(lives { $unused->remove_from_db }, 'an unused flag type can be deleted'); +ok(lives { $unused->remove_from_db }, 'an unused flag type can be deleted'); ok(!Bugzilla::FlagType->new($unused->id), 'it is gone'); my $setter = create_user('setter@bmo.test', '*'); @@ -62,11 +62,13 @@ $dbh->do('DELETE FROM flags WHERE type_id = ?', undef, $needinfo->id); Bugzilla->set_user(Bugzilla::User->super_user); $needinfo = Bugzilla::FlagType->new($needinfo->id); -like(dies { $needinfo->remove_from_db }, qr/flag_type_has_activity/, +like(dies { $needinfo->remove_from_db }, + qr/flag_type_has_activity/, 'a flag type with flag_activity rows cannot be deleted'); ok( $dbh->selectrow_array( - 'SELECT COUNT(*) FROM flag_activity WHERE type_id = ?', undef, $needinfo->id + 'SELECT COUNT(*) FROM flag_activity WHERE type_id = ?', + undef, $needinfo->id ), 'its flag_activity rows are kept' ); From af5884e50f8a5c32e7f2634b275327aa20d17def Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 12 Nov 2026 20:52:06 +0100 Subject: [PATCH 43/50] Bug 2085398 - Move the _flag_state_match comment back above its sub --- Bugzilla/Search.pm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Bugzilla/Search.pm b/Bugzilla/Search.pm index 121ce79dee..6be44f1fcd 100644 --- a/Bugzilla/Search.pm +++ b/Bugzilla/Search.pm @@ -3848,9 +3848,6 @@ sub _flag_activity_exists { . join(' AND ', @where) . ')'; } -# SQL matching a flag_activity row against a history value like -# "review+" or "review?(user@example.com)". Returns nothing if the value -# isn't a flag state. # SQL condition on flag_activity row $fa for changedto or changedfrom $value # (the state before this change), or undef if $value isn't a flag state. sub _flag_state_changed_sql { @@ -3871,6 +3868,9 @@ sub _flag_state_changed_sql { WHERE $prev.id = $prev_id AND $match)"; } +# SQL matching a flag_activity row against a history value like +# "review+" or "review?(user@example.com)". Returns nothing if the value +# isn't a flag state. sub _flag_state_match { my ($table, $value) = @_; my $dbh = Bugzilla->dbh; From ed6ab6e4adde981761486b02276ae2698d1e11b3 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 12 Nov 2026 21:19:16 +0100 Subject: [PATCH 44/50] Bug 2085398 - Use loops instead of multi-statement map blocks --- Bugzilla/Search.pm | 24 ++++++++++++------------ t/flag-activity-history.t | 12 ++++++------ t/flag-activity-start-rows.t | 15 +++++++-------- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/Bugzilla/Search.pm b/Bugzilla/Search.pm index 6be44f1fcd..4b4613ed0a 100644 --- a/Bugzilla/Search.pm +++ b/Bugzilla/Search.pm @@ -3182,19 +3182,19 @@ sub _owner_idle_time_greater_less { # The assignee commented, changed a bug or changed a flag since $value. One # EXISTS each, as joins would multiply their rows. - my $active = join( - ' OR ', - map { - my ($activity_table, $when) = @$_; - my $alias = "${activity_table}_$table"; - "EXISTS (SELECT 1 FROM $activity_table $alias - WHERE $alias.who = $owner AND $alias.$when > $quoted)" - } ( - ['longdescs', 'bug_when'], - ['bugs_activity', 'bug_when'], - ['flag_activity', 'flag_when'] + my @exists; + foreach my $activity ( + ['longdescs', 'bug_when'], + ['bugs_activity', 'bug_when'], + ['flag_activity', 'flag_when'] ) - ); + { + my ($activity_table, $when) = @$activity; + my $alias = "${activity_table}_$table"; + push @exists, "EXISTS (SELECT 1 FROM $activity_table $alias + WHERE $alias.who = $owner AND $alias.$when > $quoted)"; + } + my $active = join(' OR ', @exists); $args->{term} = $operator =~ /greater/ ? "NOT ($active)" : "($active)"; } diff --git a/t/flag-activity-history.t b/t/flag-activity-history.t index fbdf8128ff..6853b23107 100644 --- a/t/flag-activity-history.t +++ b/t/flag-activity-history.t @@ -66,12 +66,12 @@ sub flag_changes { my ($bug_id) = @_; my ($operations) = Bugzilla::Bug::GetBugActivity($bug_id); - my @history = map { - my $who = $_->{who}; - map { [$who, $_->{removed}, $_->{added}] } - grep { $_->{fieldname} eq 'flagtypes.name' } - @{$_->{changes}} - } @$operations; + my @history; + foreach my $operation (@$operations) { + push @history, + map { [$operation->{who}, $_->{removed}, $_->{added}] } + grep { $_->{fieldname} eq 'flagtypes.name' } @{$operation->{changes}}; + } $dbh->do('UPDATE bugs SET lastdiffed = NULL WHERE bug_id = ?', undef, $bug_id); my ($diffs) = Bugzilla::BugMail::_get_diffs(Bugzilla::Bug->new($bug_id), diff --git a/t/flag-activity-start-rows.t b/t/flag-activity-start-rows.t index 725f062e79..79bd6701f1 100644 --- a/t/flag-activity-start-rows.t +++ b/t/flag-activity-start-rows.t @@ -84,14 +84,13 @@ is( sub flag_history { my ($activity) = Bugzilla::Bug::GetBugActivity($bug_id); - return [ - map { - my $when = $_->{when}; - map {"$when: $_->{removed} -> $_->{added}"} - grep { $_->{fieldname} eq 'flagtypes.name' } - @{$_->{changes}} - } @$activity - ]; + my @history; + foreach my $operation (@$activity) { + push @history, + map {"$operation->{when}: $_->{removed} -> $_->{added}"} + grep { $_->{fieldname} eq 'flagtypes.name' } @{$operation->{changes}}; + } + return \@history; } is( From ff7a278fc2502557ebf6d39ad200098c120af7fe Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 12 Nov 2026 21:53:45 +0100 Subject: [PATCH 45/50] Bug 2085398 - Skip flag_activity rows up to the cutoff in the owner idle time search --- Bugzilla/Search.pm | 9 +++++++-- t/flag-activity-search.t | 18 +++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Bugzilla/Search.pm b/Bugzilla/Search.pm index 4b4613ed0a..6532ff7f9a 100644 --- a/Bugzilla/Search.pm +++ b/Bugzilla/Search.pm @@ -3191,8 +3191,13 @@ sub _owner_idle_time_greater_less { { my ($activity_table, $when) = @$activity; my $alias = "${activity_table}_$table"; - push @exists, "EXISTS (SELECT 1 FROM $activity_table $alias - WHERE $alias.who = $owner AND $alias.$when > $quoted)"; + my $where = "$alias.who = $owner AND $alias.$when > $quoted"; + + # flag_activity rows up to the cutoff only have a guessed who: those + # changes are read from bugs_activity. + $where .= ' AND ' . Bugzilla::FlagActivity->after_cutoff_sql($alias) + if $activity_table eq 'flag_activity'; + push @exists, "EXISTS (SELECT 1 FROM $activity_table $alias WHERE $where)"; } my $active = join(' OR ', @exists); diff --git a/t/flag-activity-search.t b/t/flag-activity-search.t index 54a326bf6c..73fe45479d 100644 --- a/t/flag-activity-search.t +++ b/t/flag-activity-search.t @@ -168,16 +168,17 @@ my $owned_bug = create_bug( )->id; sub idle { - my ($operator) = @_; + my ($operator, $owner, $since) = @_; + $owner //= 'alice@bmo.test'; my $search = Bugzilla::Search->new( fields => ['bug_id'], params => { f1 => 'owner_idle_time', o1 => $operator, - v1 => '2025-12-01', + v1 => $since // '2025-12-01', f2 => 'assigned_to', o2 => 'equals', - v2 => 'alice@bmo.test', + v2 => $owner, }, ); return [map { $_->[0] } @{$search->data}]; @@ -185,4 +186,15 @@ sub idle { is(idle('lessthan'), [$owned_bug], 'owner_idle_time lessthan'); is(idle('greaterthan'), [], 'owner_idle_time greaterthan'); +# carol's only change since 2019 is the flag_activity row up to the cutoff +# above, whose who is a guess: she is idle. +my $carol_bug = create_bug( + short_desc => 'owned by carol', + comment => 'Flag search test bug', + bug_type => 'defect', + assigned_to => 'carol@bmo.test', +)->id; +is(idle('greaterthan', 'carol@bmo.test', '2019-01-01'), + [$carol_bug], 'owner_idle_time skips a flag_activity row up to the cutoff'); + done_testing; From 7ba522fac0304f28cb380faf3b540ca725f7291f Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 12 Nov 2026 21:54:20 +0100 Subject: [PATCH 46/50] Bug 2085398 - Count only the net change of a request in the release tracking report --- extensions/BMO/lib/Reports/ReleaseTracking.pm | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/extensions/BMO/lib/Reports/ReleaseTracking.pm b/extensions/BMO/lib/Reports/ReleaseTracking.pm index d76a828fa0..5650cb9bd8 100644 --- a/extensions/BMO/lib/Reports/ReleaseTracking.pm +++ b/extensions/BMO/lib/Reports/ReleaseTracking.pm @@ -14,6 +14,7 @@ use warnings; use Bugzilla::Constants; use Bugzilla::Error; use Bugzilla::Field; +use Bugzilla::FlagActivity; use Bugzilla::FlagType; use Bugzilla::Util qw(validate_date); use JSON qw(-convert_blessed_universally); @@ -205,6 +206,8 @@ sub report { my @range = ($q->{start_date} . ' 00:00:00', $q->{end_date} . ' 23:59:59'); # Flag changes are in flag_activity; older ones only in bugs_activity. + # As in history, only the net change of a request counts. + my $fa_last = Bugzilla::FlagActivity->last_of_request_sql('fa'); push @where, "(EXISTS ( SELECT 1 FROM bugs_activity a WHERE a.bug_id = b.bug_id AND a.fieldid = ? @@ -213,7 +216,7 @@ sub report { OR EXISTS ( SELECT 1 FROM flag_activity fa WHERE fa.bug_id = b.bug_id - AND fa.type_id = ? AND fa.status = ? + AND fa.type_id = ? AND fa.status = ? AND $fa_last AND CONVERT_TZ(fa.flag_when, 'UTC', 'America/Los_Angeles') BETWEEN ? AND ?))"; push @params, $q->{field_id}, @range, '%' . $q->{flag_name} . $q->{flag_status} . '%', $type_id, $q->{flag_status}, From 259baba060109f9c946db2fa7cca2cebb929948c Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 12 Nov 2026 21:54:32 +0100 Subject: [PATCH 47/50] Bug 2085398 - Use sql_limit in find_activity_id_for_flag --- extensions/BugModal/lib/ActivityStream.pm | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/extensions/BugModal/lib/ActivityStream.pm b/extensions/BugModal/lib/ActivityStream.pm index 7712ff019b..a852fc3f30 100644 --- a/extensions/BugModal/lib/ActivityStream.pm +++ b/extensions/BugModal/lib/ActivityStream.pm @@ -103,9 +103,10 @@ sub find_activity_id_for_flag { # flag_activity records each change against the flag's object id, so we # can look up the exact timestamp without relying on flag-type name matching. - my ($flag_when, $who) = Bugzilla->dbh->selectrow_array( + my $dbh = Bugzilla->dbh; + my ($flag_when, $who) = $dbh->selectrow_array( "SELECT flag_when, who FROM flag_activity WHERE flag_id = ? - ORDER BY id DESC LIMIT 1", undef, $flag_id + ORDER BY id DESC " . $dbh->sql_limit(1), undef, $flag_id ); # flag_activity only logged review, feedback and needinfo before, so other From 262c8aa5197c9eae5722cb3b798688ff4403f92c Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Thu, 12 Nov 2026 21:54:41 +0100 Subject: [PATCH 48/50] Bug 2085398 - Update the comments about where flag changes are stored --- Bugzilla/Flag.pm | 14 ++++++++------ Bugzilla/Install/DB.pm | 5 +++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Bugzilla/Flag.pm b/Bugzilla/Flag.pm index 7779512597..8b8f6a36cf 100644 --- a/Bugzilla/Flag.pm +++ b/Bugzilla/Flag.pm @@ -40,7 +40,7 @@ whose names start with _ or a re specifically noted as being private. =cut use Scalar::Util qw(blessed); -use Storable qw(dclone); +use Storable qw(dclone); use Bugzilla::FlagActivity; use Bugzilla::FlagType; @@ -61,7 +61,7 @@ use base qw(Bugzilla::Object Exporter); use constant DB_TABLE => 'flags'; use constant LIST_ORDER => 'id'; -# Flags are tracked in bugs_activity. +# Flag changes are tracked in flag_activity (see Bugzilla::FlagActivity). use constant AUDIT_CREATES => 0; use constant AUDIT_UPDATES => 0; use constant AUDIT_REMOVES => 0; @@ -327,6 +327,7 @@ sub set_flag { reason => $can_change->{reason} } ); + # Update (or delete) an existing flag. if ($params->{id}) { my $flag = $class->check({id => $params->{id}}); @@ -476,7 +477,7 @@ sub create { my ($class, $flag, $timestamp) = @_; $timestamp ||= Bugzilla->dbh->selectrow_array('SELECT LOCALTIMESTAMP(0)'); - my $params = {}; + my $params = {}; my @columns = grep { $_ ne 'id' } $class->_get_db_columns; # Some columns use date formatting so use alias instead @@ -569,7 +570,7 @@ sub update_flags { my ($class, $self, $old_self, $timestamp) = @_; my @old_summaries = $class->snapshot($old_self->flags); - my %old_flags = map { $_->id => $_ } @{$old_self->flags}; + my %old_flags = map { $_->id => $_ } @{$old_self->flags}; foreach my $new_flag (@{$self->flags}) { if (!$new_flag->id) { @@ -620,7 +621,7 @@ sub update_flags { } my @new_summaries = $class->snapshot($self->flags); - my @changes = $class->update_activity(\@old_summaries, \@new_summaries); + my @changes = $class->update_activity(\@old_summaries, \@new_summaries); Bugzilla::Hook::process( 'flag_end_of_update', @@ -699,7 +700,7 @@ sub force_retarget { foreach my $flag (@$flags) { # $bug is undefined when e.g. editing inclusion and exclusion lists. - my $obj = $flag->attachment || $bug || $flag->bug; + my $obj = $flag->attachment || $bug || $flag->bug; my $is_retargeted = $flag->retarget($obj); if ($is_retargeted) { $dbh->do('UPDATE flags SET type_id = ? WHERE id = ?', @@ -1049,6 +1050,7 @@ sub extract_flags_from_cgi { } } elsif (!$flag_type->is_multiplicable && scalar @$current_flags) { + # Update or delete the current flag if the type is not multiplicable. push(@new_flags, {id => @$current_flags[0]->id, status => $status}); } diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index ae28f9254c..0d61bc1405 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -4636,8 +4636,9 @@ sub _add_flag_activity_start_rows { 'INSERT INTO flag_activity_migration (cutoff) VALUES (LOCALTIMESTAMP(0))'); } -# Nullable for now: during the rolling deploy, pods still on the previous code -# insert rows without it. Bug 2085398 makes it NOT NULL once all pods write it. +# Added nullable: during the rolling deploy, pods still on the previous code +# inserted rows without it. _set_flag_activity_who_not_null makes it NOT NULL +# once all pods write it. sub _add_flag_activity_who { my $dbh = Bugzilla->dbh; From ca559221b46928a0e0165ec58526a159a7d62615 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Fri, 13 Nov 2026 14:44:39 +0100 Subject: [PATCH 49/50] Bug 2085398 - Remove perltidy-only changes to code this PR doesn't touch --- Bugzilla/Attachment.pm | 2 +- Bugzilla/Bug.pm | 273 +++++++++--------- Bugzilla/BugMail.pm | 196 ++++++------- Bugzilla/DB/Schema.pm | 87 +++--- Bugzilla/DB/Sqlite.pm | 2 +- Bugzilla/Flag.pm | 12 +- Bugzilla/FlagType.pm | 4 +- Bugzilla/Install/DB.pm | 71 ++--- Bugzilla/Search.pm | 249 +++++++--------- Bugzilla/Test/Util.pm | 16 +- editusers.cgi | 13 +- extensions/BMO/lib/Reports/ReleaseTracking.pm | 6 +- extensions/BMO/lib/Reports/UserActivity.pm | 4 +- extensions/BugModal/lib/ActivityStream.pm | 8 +- extensions/TagNewUsers/Extension.pm | 2 +- extensions/UserProfile/lib/Util.pm | 2 +- qa/t/2_test_flags.t | 5 +- t/bmo/bugmail-flag-events.t | 145 ++++------ 18 files changed, 501 insertions(+), 596 deletions(-) diff --git a/Bugzilla/Attachment.pm b/Bugzilla/Attachment.pm index 555629eb5c..1896b6ef7d 100644 --- a/Bugzilla/Attachment.pm +++ b/Bugzilla/Attachment.pm @@ -115,7 +115,7 @@ use constant UPDATE_VALIDATORS => # and this one answer roughly the same question but fail in opposite # directions, so their memberships differ on purpose -- the reasoning is # written out at CSP_DOCUMENT_TYPES. -my %_SAFE_INLINE_TYPES = map { $_ => 1 } qw( + my %_SAFE_INLINE_TYPES = map { $_ => 1 } qw( image/png image/jpeg image/gif diff --git a/Bugzilla/Bug.pm b/Bugzilla/Bug.pm index a3a5f9db1a..e484c521e0 100644 --- a/Bugzilla/Bug.pm +++ b/Bugzilla/Bug.pm @@ -35,8 +35,8 @@ use Bugzilla::BugUserLastVisit; use Date::Parse; use DateTime; use List::MoreUtils qw(firstidx uniq part any); -use List::Util qw(min max first); -use Storable qw(dclone); +use List::Util qw(min max first); +use Storable qw(dclone); use URI; use URI::QueryParam; use Scalar::Util qw(blessed weaken); @@ -74,7 +74,7 @@ use constant USE_MEMCACHED => 0; # This is a sub because it needs to call other subroutines. sub DB_COLUMNS { - my $dbh = Bugzilla->dbh; + my $dbh = Bugzilla->dbh; my @custom = grep { $_->type != FIELD_TYPE_MULTI_SELECT } Bugzilla->active_custom_fields({skip_extensions => 1}); my @custom_names = map { $_->name } @custom; @@ -108,7 +108,7 @@ sub DB_COLUMNS { status_whiteboard target_milestone version - ), 'reporter AS reporter_id', + ), 'reporter AS reporter_id', $dbh->sql_date_format('deadline', '%Y-%m-%d') . ' AS deadline', @custom_names ); @@ -219,7 +219,7 @@ sub UPDATE_COLUMNS { my @custom = grep { $_->type != FIELD_TYPE_MULTI_SELECT } Bugzilla->active_custom_fields({skip_extensions => 1}); my @custom_names = map { $_->name } @custom; - my @columns = qw( + my @columns = qw( alias assigned_to bug_file_loc @@ -391,7 +391,7 @@ sub initialize { sub object_cache_key { my $class = shift; - my $key = $class->SUPER::object_cache_key(@_) || return; + my $key = $class->SUPER::object_cache_key(@_) || return; return $key . ',' . Bugzilla->user->id; } @@ -498,7 +498,7 @@ sub match { # so include them in the list if they have been specified. if (exists $params->{"${field}_id"}) { my $current_ids = $params->{"${field}_id"}; - my @id_array = ref $current_ids ? @$current_ids : ($current_ids); + my @id_array = ref $current_ids ? @$current_ids : ($current_ids); push(@ids, @id_array); } @@ -606,9 +606,9 @@ sub possible_duplicates { my ($class, $params) = @_; my $short_desc = $params->{summary}; my $products = $params->{products} || []; - my $limit = $params->{limit} || MAX_POSSIBLE_DUPLICATES; + my $limit = $params->{limit} || MAX_POSSIBLE_DUPLICATES; $limit = MAX_POSSIBLE_DUPLICATES if $limit > MAX_POSSIBLE_DUPLICATES; - $products = [$products] if !ref($products) || ref($products) ne 'ARRAY'; + $products = [$products] if !ref($products) || ref($products) ne 'ARRAY'; my $orig_limit = $limit; detaint_natural($limit) @@ -641,7 +641,7 @@ sub possible_duplicates { foreach my $word (@words) { my ($term, $rel_term) = $dbh->sql_fulltext_search('bugs_fulltext.short_desc', $word); - push(@where, $term); + push(@where, $term); push(@relevance, $rel_term || $term); } @@ -841,7 +841,7 @@ sub create { if ($see_also) { my $see_also_array = $see_also; if (!ref $see_also_array) { - $see_also = trim($see_also); + $see_also = trim($see_also); $see_also_array = [split(/[\s,]+/, $see_also)]; } foreach my $value (@$see_also_array) { @@ -986,8 +986,8 @@ sub update { foreach my $field (qw(qa_contact assigned_to)) { if ($changes->{$field}) { my ($from, $to) = @{$changes->{$field}}; - $from = $old_bug->$field->login if $from; - $to = $self->$field->login if $to; + $from = $old_bug->$field->login if $from; + $to = $self->$field->login if $to; $changes->{$field} = [$from, $to]; } } @@ -1048,8 +1048,8 @@ sub update { my ($table, $field1, $field2) = @$rel; foreach my $pair ([$field1, $field2], [$field2, $field1]) { - my ($field, $other_field) = @$pair; - my ($removed, $added) = diff_arrays($old_bug->$field, $self->$field); + my ($field, $other_field) = @$pair; + my ($removed, $added) = diff_arrays($old_bug->$field, $self->$field); foreach my $id (@$removed) { $dbh->do("DELETE FROM $table WHERE $field = ? AND $other_field = ?", @@ -1105,8 +1105,10 @@ sub update { # we only audit when bugs protected with a secure-mail enabled group # are made public - if (!scalar @{$self->groups_in} && any { $old_groups{$_}->secure_mail } - @$removed_gr) + if ( + !scalar @{$self->groups_in} && any { $old_groups{$_}->secure_mail } + @$removed_gr + ) { Bugzilla->audit(sprintf( '%s made Bug %s public (%s)', @@ -1735,7 +1737,6 @@ sub _check_bug_type { my $type_required = Bugzilla->params->{'require_bug_type'}; if (defined $type && trim($type)) { - # Treat '--' type value same as empty if ($type_required && $type eq '--') { ThrowUserError('bug_type_required'); @@ -1755,8 +1756,7 @@ sub _check_bug_type { : undef; my $component = ($product && defined $params->{component}) - ? Bugzilla::Component->new( - {name => $params->{component}, product => $product, cache => 1}) + ? Bugzilla::Component->new({name => $params->{component}, product => $product, cache => 1}) : undef; return $component @@ -1826,7 +1826,7 @@ sub _check_component { $name || ThrowUserError("require_component"); my $product = blessed($invocant) ? $invocant->product_obj : $params->{product}; my $old_comp = blessed($invocant) ? $invocant->component : ''; - my $object = Bugzilla::Component->check({product => $product, name => $name}); + my $object = Bugzilla::Component->check({product => $product, name => $name}); if ($object->name ne $old_comp && !$object->is_active) { ThrowUserError('value_inactive', {class => ref($object), value => $name}); } @@ -1941,7 +1941,7 @@ sub _check_relationship { # And finally, check for dependency loops. my $bug_id = ref($invocant) ? $invocant->id : 0; - my %deps = validate_relationship($bug_id, $table, %deps_in); + my %deps = validate_relationship($bug_id, $table, %deps_in); return %deps; } @@ -2069,7 +2069,7 @@ sub _check_keywords { my $keyword_array = $keywords_in; if (!ref $keyword_array) { - $keywords_in = trim($keywords_in); + $keywords_in = trim($keywords_in); $keyword_array = [split(/[\s,]+/, $keywords_in)]; } @@ -2143,7 +2143,7 @@ sub _check_qa_contact { # the QA Contact for validity. if (!defined $id && $qa_contact) { $qa_contact = Bugzilla::User->check($qa_contact) if !ref $qa_contact; - $id = $qa_contact->id; + $id = $qa_contact->id; # create() checks this another way, so we don't have to run this # check during create(). @@ -2195,7 +2195,7 @@ sub _check_resolution { && (!$self->resolution || $resolution ne $self->resolution) && scalar @{$self->dependson}) { - my $dep_bugs = Bugzilla::Bug->new_from_list($self->dependson); + my $dep_bugs = Bugzilla::Bug->new_from_list($self->dependson); my $count_open = grep { $_->isopened } @$dep_bugs; if ($count_open) { ThrowUserError("still_unresolved_bugs", @@ -2619,7 +2619,7 @@ sub set_all { # (because currently there is no way to check for duplicate loops in that # situation). You also cannot set the alias of several bugs at once. if ($params->{other_bugs} and scalar @{$params->{other_bugs}} > 1) { - ThrowUserError('dupe_not_allowed') if exists $params->{dup_id}; + ThrowUserError('dupe_not_allowed') if exists $params->{dup_id}; ThrowUserError('multiple_alias_not_allowed') if defined $params->{alias}; } @@ -2723,9 +2723,9 @@ sub set_all { # glitch where e.g. cf_crash_signature could be stored on a product that # doesn't expose it. my @custom_fields - = grep { $_->type != FIELD_TYPE_EXTENSION } - Bugzilla->active_custom_fields( - {product => $self->product_obj, component => $self->component_obj}); + = grep { $_->type != FIELD_TYPE_EXTENSION } Bugzilla->active_custom_fields( + {product => $self->product_obj, component => $self->component_obj} + ); foreach my $field (@custom_fields) { my $fname = $field->name; if (exists $params->{$fname}) { @@ -2953,7 +2953,6 @@ sub _set_product { # themselves changed. if ($product_changed) { - # Try to set each value with the new product. # Have to set error_mode because Throw*Error calls exit() otherwise. # We will do this for both browser and non-browser clients. @@ -2986,7 +2985,7 @@ sub _set_product { my $verified = $params->{product_change_confirmed}; -# BMO - if everything is ok then we can skip the verfication page when using bug_modal + # BMO - if everything is ok then we can skip the verfication page when using bug_modal if ( (($params->{format} && $params->{format} eq 'modal') || !$verified) && $component_ok && $version_ok @@ -3056,9 +3055,8 @@ sub _set_product { } else { # If we are not in the browser, we just die if certain values are invalid. - $self->set_component($comp_name) if !$component_ok; + $self->set_component($comp_name) if !$component_ok; $self->set_target_milestone($tm_name) if !$milestone_ok; - # If version is not valid, then set to default version instead of dying. $self->set_version($product->default_version) if !$version_ok; } @@ -3089,9 +3087,8 @@ sub _set_product { # verify-new-product UI via _check_default_product_security_group() in the # BMO extension. my @pre_move_names = map { $_->name } @current_groups; - for my $sec_group (_target_security_group_when_dropping( - $product, \@pre_move_names, $dropped_group - )) + for my $sec_group ( + _target_security_group_when_dropping($product, \@pre_move_names, $dropped_group)) { $self->add_group($sec_group); } @@ -3128,7 +3125,7 @@ sub reset_qa_contact { sub set_remaining_time { $_[0]->set('remaining_time', $_[1]); } # Used only when closing a bug or moving between closed states. -sub _zero_remaining_time { $_[0]->{'remaining_time'} = 0; } +sub _zero_remaining_time { $_[0]->{'remaining_time'} = 0; } sub set_reporter_accessible { $_[0]->set('reporter_accessible', $_[1]); } sub set_resolution { @@ -3221,7 +3218,6 @@ sub set_bug_status { } sub set_type { - # The bug_type table column is nullable, but make sure to use the default type # in case it's not set $_[0]->set('bug_type', $_[1] || Bugzilla->params->{'default_bug_type'}); @@ -3273,7 +3269,7 @@ sub add_comment { $params ||= {}; # Fill out info that doesn't change and callers may not pass in - $params->{'bug_id'} = $self; + $params->{'bug_id'} = $self; $params->{'thetext'} = defined($comment) ? $comment : ''; # Validate all the entered data @@ -3340,7 +3336,7 @@ sub modify_keywords { @result = sort { lc($a->name) cmp lc($b->name) } @result; if ($any_changes) { - my $new = join(', ', (map { $_->name } @result)); + my $new = join(', ', (map { $_->name } @result)); my $can_change = $self->check_can_change_field('keywords', 0, 1); $can_change->{allowed} || ThrowUserError( 'illegal_change', @@ -3365,7 +3361,7 @@ sub add_group { # So we have to store and pass the name as entered by the user to # the error message, if we have it. my $group_name = blessed($group) ? $group->name : $group; - my $args = { + my $args = { name => $group_name, product => $self->product, bug_id => $self->id, @@ -3407,7 +3403,7 @@ sub remove_group { # See add_group() for the reason why we store the user input. my $group_name = blessed($group) ? $group->name : $group; - my $args = { + my $args = { name => $group_name, product => $self->product, bug_id => $self->id, @@ -3494,8 +3490,7 @@ sub add_see_also { # If this is a link to a local bug then save the # ref bug id for sending changes email. if (my $ref_bug = delete $field_values->{ref_bug}) { - my $ref_can_change - = $ref_bug->check_can_change_field('see_also', '', $self->id); + my $ref_can_change = $ref_bug->check_can_change_field('see_also', '', $self->id); if ( $class->isa('Bugzilla::BugUrl::Local') and !$skip_recursion and Bugzilla->user->can_see_bug($ref_bug->id) @@ -3731,8 +3726,8 @@ sub _resolve_ultimate_dup_id { # prevent them from marking this bug as a duplicate. return $last_dup if exists $dupes{$this_dup}; $dupes{$this_dup} = 1; - $last_dup = $this_dup; - $this_dup = $dbh->selectrow_array($sth, undef, $this_dup); + $last_dup = $this_dup; + $this_dup = $dbh->selectrow_array($sth, undef, $this_dup); } return $last_dup; @@ -3777,7 +3772,7 @@ sub any_flags_requesteeble { sub attachments { my ($self) = @_; return $self->{'attachments'} if exists $self->{'attachments'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; $self->{'attachments'} = Bugzilla::Attachment->get_attachments_by_bug($self, {preload => 1}); @@ -3787,7 +3782,7 @@ sub attachments { sub assigned_to { my ($self) = @_; return $self->{'assigned_to_obj'} if exists $self->{'assigned_to_obj'}; - $self->{'assigned_to'} = 0 if $self->{'error'}; + $self->{'assigned_to'} = 0 if $self->{'error'}; return $self->{'assigned_to_obj'} = new Bugzilla::User({id => $self->{'assigned_to'}, cache => 1}); } @@ -3795,7 +3790,7 @@ sub assigned_to { sub blocked { my ($self) = @_; return $self->{'blocked'} if exists $self->{'blocked'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; $self->{'blocked'} = list_relationship('dependencies', 'dependson', 'blocked', $self->bug_id); return $self->{'blocked'}; @@ -3825,7 +3820,7 @@ sub related_bugs { sub cc { my ($self) = @_; return $self->{'cc'} if exists $self->{'cc'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; my $dbh = Bugzilla->dbh; $self->{'cc'} = $dbh->selectcol_arrayref( @@ -3844,7 +3839,7 @@ sub cc { sub cc_users { my $self = shift; return $self->{'cc_users'} if exists $self->{'cc_users'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; my $dbh = Bugzilla->dbh; my $cc_ids = $dbh->selectcol_arrayref('SELECT who FROM cc WHERE bug_id = ?', @@ -3864,7 +3859,7 @@ sub component { sub component_obj { my ($self) = @_; return $self->{component_obj} if defined $self->{component_obj}; - return {} if $self->{error}; + return {} if $self->{error}; $self->{component_obj} = new Bugzilla::Component({id => $self->{component_id}, cache => 1}); return $self->{component_obj}; @@ -3873,7 +3868,7 @@ sub component_obj { sub classification_id { my ($self) = @_; return $self->{classification_id} if exists $self->{classification_id}; - return 0 if $self->{error}; + return 0 if $self->{error}; ($self->{classification_id}) = Bugzilla->dbh->selectrow_array( 'SELECT classification_id FROM products WHERE id = ?', @@ -3884,7 +3879,7 @@ sub classification_id { sub classification { my ($self) = @_; return $self->{classification} if exists $self->{classification}; - return '' if $self->{error}; + return '' if $self->{error}; ($self->{classification}) = Bugzilla->dbh->selectrow_array( 'SELECT name FROM classifications WHERE id = ?', @@ -3895,7 +3890,7 @@ sub classification { sub dependson { my ($self) = @_; return $self->{'dependson'} if exists $self->{'dependson'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; $self->{'dependson'} = list_relationship('dependencies', 'blocked', 'dependson', $self->bug_id); return $self->{'dependson'}; @@ -3910,7 +3905,7 @@ sub depends_on_obj { sub duplicates { my $self = shift; return $self->{duplicates} if exists $self->{duplicates}; - return [] if $self->{error}; + return [] if $self->{error}; $self->{duplicates} = Bugzilla::Bug->new_from_list($self->duplicate_ids); return $self->{duplicates}; } @@ -3918,7 +3913,7 @@ sub duplicates { sub duplicate_ids { my $self = shift; return $self->{duplicate_ids} if exists $self->{duplicate_ids}; - return [] if $self->{error}; + return [] if $self->{error}; my $dbh = Bugzilla->dbh; $self->{duplicate_ids} @@ -3930,7 +3925,7 @@ sub duplicate_ids { sub flag_types { my ($self) = @_; return $self->{'flag_types'} if exists $self->{'flag_types'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; my $vars = { target_type => 'bug', @@ -3967,7 +3962,7 @@ sub is_closed_for { return 0; } - my $past_time = DateTime->now()->subtract(%$duration)->epoch(); + my $past_time = DateTime->now()->subtract(%$duration)->epoch(); my $closed_time = str2time($self->cf_last_resolved); return $past_time > $closed_time; @@ -3982,7 +3977,7 @@ sub keywords { sub keyword_objects { my $self = shift; return $self->{'keyword_objects'} if defined $self->{'keyword_objects'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; my $dbh = Bugzilla->dbh; my $ids @@ -4001,15 +3996,14 @@ sub has_keyword { sub add_keyword { my ($self, $keyword) = @_; return if $self->has_keyword($keyword); - push @{$self->{'keyword_objects'}}, - Bugzilla::Keyword->check({name => $keyword}); + push @{$self->{'keyword_objects'}}, Bugzilla::Keyword->check({name => $keyword}); } sub del_keyword { my ($self, $keyword) = @_; $keyword = lc($keyword); my $index = -1; - my $i = 0; + my $i = 0; foreach my $keyword_obj (@{$self->keyword_objects}) { if (lc($keyword_obj->name) eq $keyword) { $index = $i; @@ -4079,12 +4073,11 @@ sub comment_count { sub counts { my ($self) = @_; - my $dbh = Bugzilla->dbh; - my $extra = !Bugzilla->user->is_insider ? 'AND isprivate = 0' : ''; - my $id = $self->id; + my $dbh = Bugzilla->dbh; + my $extra = !Bugzilla->user->is_insider ? 'AND isprivate = 0' : ''; + my $id = $self->id; - $self->{counts} ||= $dbh->selectrow_hashref( - "SELECT + $self->{counts} ||= $dbh->selectrow_hashref("SELECT (SELECT COUNT(*) FROM attachments WHERE bug_id = ? $extra) AS attachments, (SELECT COUNT(*) FROM cc WHERE bug_id = ?) AS cc, (SELECT COUNT(*) FROM longdescs WHERE bug_id = ? $extra) AS comments, @@ -4094,8 +4087,7 @@ sub counts { (SELECT COUNT(*) FROM regressions WHERE regressed_by = ?) AS regressed_by, (SELECT COUNT(*) FROM regressions WHERE regresses = ?) AS regressions, (SELECT COUNT(*) FROM duplicates WHERE dupe_of = ?) AS duplicates - ", undef, $id, $id, $id, $id, $id, $id, $id, $id, $id - ); + ", undef, $id, $id, $id, $id, $id, $id, $id, $id, $id); return $self->{counts}; } @@ -4134,7 +4126,7 @@ sub product_obj { sub qa_contact { my ($self) = @_; return $self->{'qa_contact_obj'} if exists $self->{'qa_contact_obj'}; - return undef if $self->{'error'}; + return undef if $self->{'error'}; if (Bugzilla->params->{'useqacontact'} && $self->{'qa_contact'}) { $self->{'qa_contact_obj'} @@ -4152,7 +4144,7 @@ sub qa_contact { sub regressed_by { my ($self) = @_; return $self->{'regressed_by'} if exists $self->{'regressed_by'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; $self->{'regressed_by'} = list_relationship('regressions', 'regresses', 'regressed_by', $self->bug_id); @@ -4168,7 +4160,7 @@ sub regressed_by_obj { sub regresses { my ($self) = @_; return $self->{'regresses'} if exists $self->{'regresses'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; $self->{'regresses'} = list_relationship('regressions', 'regressed_by', 'regresses', $self->bug_id); @@ -4224,7 +4216,7 @@ sub status { sub statuses_available { my $self = shift; - return [] if $self->{'error'}; + return [] if $self->{'error'}; return $self->{'statuses_available'} if defined $self->{'statuses_available'}; my @statuses = @{$self->status->can_change_to}; @@ -4283,7 +4275,7 @@ sub show_attachment_flags { sub groups { my $self = shift; return $self->{'groups'} if exists $self->{'groups'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; my $dbh = Bugzilla->dbh; my @groups; @@ -4302,8 +4294,7 @@ sub groups { . " THEN 1 ELSE 0 END," . " CASE WHEN groups.id IN($grouplist) THEN 1 ELSE 0 END," . " isactive, membercontrol, othercontrol" - . " FROM " - . $dbh->quote_identifier('groups') + . " FROM " . $dbh->quote_identifier('groups') . " LEFT JOIN bug_group_map" . " ON bug_group_map.group_id = groups.id" . " AND bug_id = ?" @@ -4328,12 +4319,10 @@ sub groups { # the user is a member of the group. if ( $ison - || ( - $isactive + || ( $isactive && $ingroup && ( ($membercontrol == CONTROLMAPDEFAULT) - || ($membercontrol == CONTROLMAPSHOWN)) - ) + || ($membercontrol == CONTROLMAPSHOWN))) ) { my $ismandatory = $isactive && ($membercontrol == CONTROLMAPMANDATORY); @@ -4404,7 +4393,7 @@ sub groups { sub groups_in { my $self = shift; return $self->{'groups_in'} if exists $self->{'groups_in'}; - return [] if $self->{'error'}; + return [] if $self->{'error'}; my $group_ids = Bugzilla->dbh->selectcol_arrayref( 'SELECT group_id FROM bug_group_map WHERE bug_id = ?', @@ -4465,7 +4454,7 @@ sub _target_security_group_when_dropping { sub user { my $self = shift; return $self->{'user'} if exists $self->{'user'}; - return {} if $self->{'error'}; + return {} if $self->{'error'}; my $user = Bugzilla->user; @@ -4491,7 +4480,7 @@ sub user { sub choices { my $self = shift; return $self->{'choices'} if exists $self->{'choices'}; - return {} if $self->{'error'}; + return {} if $self->{'error'}; my $user = Bugzilla->user; my @products = @{$user->get_enterable_products}; @@ -4619,14 +4608,14 @@ sub to_hash { $hash->{assignee} = $self->assigned_to->to_hash; } if ($params->{blocks}) { - $hash->{blocks} = $self->blocked; + $hash->{blocks} =$self->blocked; } if ($params->{classification}) { - $hash->{classification} = $self->classification; + $hash->{classification} =$self->classification; } if ($params->{comments}) { my @result; - my $comments = $self->comments( + my $comments =$self->comments( {order => 'oldest_to_newest', after => $params->{new_since}}); foreach my $comment (@$comments) { next if $comment->is_private && !$user->is_insider; @@ -4635,32 +4624,32 @@ sub to_hash { $hash->{comments} = \@result; } if ($params->{component}) { - $hash->{component} = $self->component; + $hash->{component} =$self->component; } if ($params->{cc}) { $hash->{cc} = [map { $_->to_hash } @{$self->cc_users}]; } if ($params->{creation_time}) { - $hash->{creation_time} = $self->creation_ts; + $hash->{creation_time} =$self->creation_ts; } if ($params->{creator}) { $hash->{creator} = $self->reporter->to_hash; } if ($params->{depends_on}) { - $hash->{depends_on} = $self->dependson; + $hash->{depends_on} =$self->dependson; } if ($params->{description}) { - my $comment = Bugzilla::Comment->match({bug_id => $self->id, LIMIT => 1})->[0]; + my $comment = Bugzilla::Comment->match({bug_id =>$self->id, LIMIT => 1})->[0]; $hash->{description} = ($comment && (!$comment->is_private || Bugzilla->user->is_insider)) ? $comment->body : ''; } if ($params->{dupe_of}) { - $hash->{dupe_of} = $self->dup_id; + $hash->{dupe_of} =$self->dup_id; } if ($params->{duplicates}) { - $hash->{duplicates} = $self->duplicates; + $hash->{duplicates} =$self->duplicates; } if ($params->{groups}) { my @groups; @@ -4671,19 +4660,19 @@ sub to_hash { $hash->{groups} = \@groups; } if ($params->{is_open}) { - $hash->{is_open} = $self->status->is_open ? 1 : 0; + $hash->{is_open} =$self->status->is_open ? 1 : 0; } if ($params->{keywords}) { $hash->{keywords} = [map { $_->name } @{$self->keyword_objects}]; } if ($params->{last_change_time}) { - $hash->{last_change_time} = $self->delta_ts; + $hash->{last_change_time} =$self->delta_ts; } if ($params->{last_change_time_non_bot}) { - $hash->{last_change_time_non_bot} = $self->delta_ts_non_bot; + $hash->{last_change_time_non_bot} =$self->delta_ts_non_bot; } if ($params->{product}) { - $hash->{product} = $self->product; + $hash->{product} =$self->product; } if ($params->{qa_contact}) { if ($self->qa_contact) { @@ -4691,7 +4680,7 @@ sub to_hash { } } if ($params->{triage_owner}) { - my $triage_owner = $self->component_obj->triage_owner; + my $triage_owner =$self->component_obj->triage_owner; if ($triage_owner->login) { $hash->{triage_owner} = $triage_owner->to_hash; } @@ -4706,49 +4695,49 @@ sub to_hash { # Regressions if (Bugzilla->params->{use_regression_fields}) { if ($params->{regressed_by}) { - $hash->{regressed_by} = $self->regressed_by; + $hash->{regressed_by} =$self->regressed_by; } if ($params->{regressions}) { - $hash->{regressions} = $self->regresses; + $hash->{regressions} =$self->regresses; } } # Custom fields my @custom_fields = Bugzilla->active_custom_fields( { - product => $self->product_obj, - component => $self->component_obj, - bug_id => $self->id + product =>$self->product_obj, + component =>$self->component_obj, + bug_id =>$self->id }, ); foreach my $field (@custom_fields) { my $name = $field->name; next if !$params->{$name}; - $hash->{$name} = $self->$name; + $hash->{$name} =$self->$name; } # Timetracking fields if ($user->is_timetracker) { if ($params->{estimated_time}) { - $hash->{estimated_time} = $self->estimated_time; + $hash->{estimated_time} =$self->estimated_time; } if ($params->{remaining_time}) { - $hash->{remaining_time} = $self->remaining_time; + $hash->{remaining_time} =$self->remaining_time; } if ($params->{deadline}) { - $hash->{deadline} = $self->deadline; + $hash->{deadline} =$self->deadline; } if ($params->{actual_time}) { - $hash->{actual_time} = $self->actual_time; + $hash->{actual_time} =$self->actual_time; } } # Bug accessibility if ($params->{is_cc_accessible}) { - $hash->{is_cc_accessible} = $self->cclist_accessible ? 1 : 0; + $hash->{is_cc_accessible} =$self->cclist_accessible ? 1 : 0; } if ($params->{is_creator_accessible}) { - $hash->{is_creator_accessible} = $self->reporter_accessible ? 1 : 0; + $hash->{is_creator_accessible} =$self->reporter_accessible ? 1 : 0; } # Bug mentors @@ -4759,7 +4748,7 @@ sub to_hash { return $hash; } -# True if the current user is being reminded of +# True if the current user is being reminded of # this bug on a specific date. sub is_reminded { my ($self, $user) = @_; @@ -4801,8 +4790,8 @@ sub list_relationship { ORDER BY is_open DESC, $target_field" ); - my $bug_ids - = $dbh->selectcol_arrayref($cache->{"${target_field}_sth_$exclude_resolved"}, + my $bug_ids = $dbh->selectcol_arrayref( + $cache->{"${target_field}_sth_$exclude_resolved"}, undef, $bug_id); # If user has 'editbugs', show all bug ids. @@ -4921,8 +4910,8 @@ sub GetBugActivity { $suppwhere = "AND longdescs.isprivate = 0"; } - $bug_when = $dbh->sql_date_format('longdescs_tags_activity.bug_when', - '%Y-%m-%d %H:%i:%s'); + $bug_when + = $dbh->sql_date_format('longdescs_tags_activity.bug_when', '%Y-%m-%d %H:%i:%s'); $query .= " UNION ALL SELECT 'comment_tag' AS field_name, @@ -5001,11 +4990,11 @@ sub GetBugActivity { Bugzilla::Hook::process( 'get_bug_activity', { - bug_id => $bug_id, - attach_id => $attach_id, - start_time => $starttime, + bug_id => $bug_id, + attach_id => $attach_id, + start_time => $starttime, include_comment_activity => $include_comment_activity, - list => \@$list + list => \@$list } ); @@ -5018,24 +5007,19 @@ sub GetBugActivity { my $incomplete_data = 0; foreach my $entry (@$list) { - my ($fieldname, $activity_id, $attachid, $when, $removed, $added, $who, - $comment_id) + my ($fieldname, $activity_id, $attachid, $when, $removed, $added, $who, $comment_id) = @$entry; my %change; my $activity_visible = 1; - my $last_change = @$changes[-1] || {}; + my $last_change = @$changes[-1] || {}; # Suppress any mid-air collision or duplicated change - if ( - ( - (exists $operation->{'when'} && $when eq $operation->{'when'}) - && $fieldname eq $last_change->{'fieldname'} - && $removed eq $last_change->{'removed'} - && $added eq $last_change->{'added'} - && (defined $attachid && $attachid eq $last_change->{'attachid'}) - ) - || $removed eq $added - ) + if (( (exists $operation->{'when'} && $when eq $operation->{'when'}) + && $fieldname eq $last_change->{'fieldname'} + && $removed eq $last_change->{'removed'} + && $added eq $last_change->{'added'} + && (defined $attachid && $attachid eq $last_change->{'attachid'})) + || $removed eq $added ) { next; } @@ -5048,7 +5032,10 @@ sub GetBugActivity { { $activity_visible = $user->is_timetracker; } - elsif ($fieldname eq 'longdescs.isprivate' && !$user->is_insider && $added) { + elsif ($fieldname eq 'longdescs.isprivate' + && !$user->is_insider + && $added) + { $activity_visible = 0; } else { @@ -5125,13 +5112,13 @@ sub GetBugActivity { } next if !@filtered_removed && !@filtered_added; $removed = join ', ', @filtered_removed; - $added = join ', ', @filtered_added; + $added = join', ', @filtered_added; } $operation->{'who'} = $who; $operation->{'when'} = $when; $operation->{'fieldname'} = $change{'fieldname'} = $fieldname; - $operation->{'attachid'} = $change{'attachid'} = $attachid; + $operation->{'attachid'} = $change{'attachid'} = $attachid; $change{'removed'} = $removed; $change{'added'} = $added; @@ -5185,10 +5172,7 @@ sub _join_activity_entries { return $current_change . $new_change; } else { - return - $current_change - . ($current_change && $new_change ? ' ' : '') - . $new_change; + return $current_change . ($current_change && $new_change ? ' ' : '') . $new_change; } } @@ -5207,7 +5191,7 @@ sub LogActivityEntry { if (length($removestr) > MAX_LINE_LENGTH) { my $commaposition = find_wrap_point($removed, MAX_LINE_LENGTH); $removestr = substr($removed, 0, $commaposition); - $removed = substr($removed, $commaposition); + $removed = substr($removed, $commaposition); } else { $removed = ""; # no more entries @@ -5215,7 +5199,7 @@ sub LogActivityEntry { if (length($addstr) > MAX_LINE_LENGTH) { my $commaposition = find_wrap_point($added, MAX_LINE_LENGTH); $addstr = substr($added, 0, $commaposition); - $added = substr($added, $commaposition); + $added = substr($added, $commaposition); } else { $added = ""; # no more entries @@ -5365,13 +5349,14 @@ sub check_can_change_field { # Allow anyone to enter/select the summary, component, bug type and platform # when filing a new bug, because some are required fields if (!$self->id) { - if ( $field eq 'short_desc' + if ( + $field eq 'short_desc' || $field eq 'component' || $field eq 'version' || $field eq 'rep_platform' || $field eq 'op_sys' - || ($field eq 'bug_type' && Bugzilla->params->{'require_bug_type'})) - { + || ($field eq 'bug_type' && Bugzilla->params->{'require_bug_type'}) + ) { return {allowed => 1}; } } diff --git a/Bugzilla/BugMail.pm b/Bugzilla/BugMail.pm index 383fa1d436..bfcd5afc6b 100644 --- a/Bugzilla/BugMail.pm +++ b/Bugzilla/BugMail.pm @@ -26,7 +26,7 @@ use Bugzilla::Hook; use Date::Parse; use Date::Format; -use Scalar::Util qw(blessed); +use Scalar::Util qw(blessed); use List::MoreUtils qw(uniq firstidx); use Sys::Hostname; use Storable qw(dclone); @@ -66,7 +66,7 @@ sub Send { # Bugzilla::User objects of people in various roles. More than one person # can 'have' a role, if the person in that role has changed, or people are # watching. - my @assignees = ($bug->assigned_to); + my @assignees = ($bug->assigned_to); my @qa_contacts = $bug->qa_contact || (); my @ccs = @{$bug->cc_users}; @@ -215,7 +215,6 @@ sub Send { # other role on the bug. Flag-type cc_list addresses (admin-configured, # bypasses per-user opt-in) are added the same way notify() used to. foreach my $event (@flag_events) { - # notify() used to skip addressees who couldn't see a private # attachment; _flag_event_visible_to() matches that so a non-insider # doesn't get added as a recipient purely to be told about a flag on an @@ -223,16 +222,16 @@ sub Send { # body either). if ($event->{action} eq 'requested') { my $requestee_id = $event->{requestee_id}; - my $requestee = $user_cache{$requestee_id} - ||= Bugzilla::User->new({id => $requestee_id, cache => 1}); + my $requestee + = $user_cache{$requestee_id} ||= Bugzilla::User->new({id => $requestee_id, cache => 1}); $event->{requestee} = $requestee; $recipients{$requestee_id}->{+REL_FLAG_REQUESTEE} = BIT_DIRECT if _flag_event_visible_to($event, $requestee); } elsif ($event->{action} eq 'answered') { my $requester_id = $event->{requester_id}; - my $requester = $user_cache{$requester_id} - ||= Bugzilla::User->new({id => $requester_id, cache => 1}); + my $requester + = $user_cache{$requester_id} ||= Bugzilla::User->new({id => $requester_id, cache => 1}); $event->{requester} = $requester; # Don't add the requester as a recipient for clearing their own @@ -369,8 +368,10 @@ sub Send { $wants_mail = 1; } else { - $wants_mail = $user->wants_bug_mail($bug, $relationship, $start ? \@diffs : [], - $comments, $params->{dep_only}, $changer); + $wants_mail = $user->wants_bug_mail( + $bug, $relationship, $start ? \@diffs : [], + $comments, $params->{dep_only}, $changer + ); } if ($wants_mail) { $rels_which_want{$relationship} = $recipients{$user_id}->{$relationship}; @@ -394,7 +395,9 @@ sub Send { # Make sure the user isn't in the nomail list, and the dep check passed. # BMO: never send emails to nobody@mozilla.org, .bugs, or .tld addresses. # This check needs to happen after the bugmail_recipients hook. - if ($user->email_enabled && $dep_ok && !is_fake_recipient_address($user->email)) + if ( $user->email_enabled + && $dep_ok + && !is_fake_recipient_address($user->email)) { # Don't show summaries for bugs the user can't access, and @@ -432,7 +435,7 @@ sub Send { # the mail some other way (e.g. they're on the CC list). A cc_list # recipient who isn't the requestee/requester gets the event as a # generic 'set', so it isn't worded as "from you"/"your request". - my $user_cc_events = $flag_type_cc->{$user_id} || []; + my $user_cc_events = $flag_type_cc->{$user_id} || []; my @user_flag_events = map { my $event = $_; my $is_own @@ -440,10 +443,10 @@ sub Send { || ($event->{action} eq 'answered' && $event->{requester_id} == $user_id && $event->{setter}->id != $user_id); - !_flag_event_visible_to($event, $user) ? () - : $is_own ? $event + !_flag_event_visible_to($event, $user) ? () + : $is_own ? $event : (grep { $_ == $event } @$user_cc_events) ? {%$event, action => 'set'} - : (); + : (); } @flag_events; # Someone mailed only because of a flag (they have no other role on @@ -451,17 +454,19 @@ sub Send { # roles) gets the flag section and the new comments, as the old flag # mail did (so a requestee sees the question), but not every diff. my $flag_only = !grep { - $_ != REL_FLAG_REQUESTEE && $_ != REL_FLAG_REQUESTER && $_ != REL_FLAG_TYPE_CC + $_ != REL_FLAG_REQUESTEE + && $_ != REL_FLAG_REQUESTER + && $_ != REL_FLAG_TYPE_CC } keys %rels_which_want; my $sent_mail = sendMail({ - to => $user, - bug => $bug, - comments => $comments, - date => $date, - changer => $changer, - watchers => exists $watching{$user_id} ? $watching{$user_id} : undef, - diffs => $flag_only ? [] : \@diffs, + to => $user, + bug => $bug, + comments => $comments, + date => $date, + changer => $changer, + watchers => exists $watching{$user_id} ? $watching{$user_id} : undef, + diffs => $flag_only ? [] : \@diffs, rels_which_want => \%rels_which_want, referenced_bugs => $flag_only ? [] : $referenced_bugs, dep_only => $params->{dep_only}, @@ -492,14 +497,14 @@ sub Send { sub sendMail { my $params = shift; - my $user = $params->{to}; - my $bug = $params->{bug}; - my @send_comments = @{$params->{comments}}; - my $date = $params->{date}; - my $changer = $params->{changer}; - my $watchingRef = $params->{watchers}; - my @diffs = @{$params->{diffs}}; - my $relRef = $params->{rels_which_want}; + my $user = $params->{to}; + my $bug = $params->{bug}; + my @send_comments = @{$params->{comments}}; + my $date = $params->{date}; + my $changer = $params->{changer}; + my $watchingRef = $params->{watchers}; + my @diffs = @{$params->{diffs}}; + my $relRef = $params->{rels_which_want}; my $referenced_bugs = $params->{referenced_bugs}; my $dep_only = $params->{dep_only}; my $blocker_short_desc = $params->{blocker_short_desc}; @@ -540,7 +545,6 @@ sub sendMail { $add_diff = 1; } if ($add_diff) { - # Diffs with a blocker object are shared across per-recipient sendMail # calls. Clone the diff+blocker so hooks (e.g. SecureMail) can mutate # short_desc for one recipient without contaminating others. @@ -556,10 +560,7 @@ sub sendMail { @send_comments = grep { !$_->is_private } @send_comments; } - if ( !scalar(@display_diffs) - && !scalar(@send_comments) - && !scalar(@$flag_events)) - { + if (!scalar(@display_diffs) && !scalar(@send_comments) && !scalar(@$flag_events)) { # Whoops, no differences! return 0; @@ -619,8 +620,8 @@ sub sendMail { changedfieldnames => \@changedfieldnames, new_comments => \@send_comments, threadingmarker => build_thread_marker($bug->id, $user->id, !$bug->lastdiffed), - referenced_bugs => $referenced_bugs, - bugmailtype => $bugmailtype, + referenced_bugs => $referenced_bugs, + bugmailtype => $bugmailtype, blocker_short_desc => $blocker_short_desc, flag_events => $flag_events, }; @@ -669,17 +670,19 @@ sub enqueue { # way, dropping the attachment object (dequeue() re-fetches it by id so # it can also re-check visibility at send time). $vars->{flag_events} = [ - map { { - action => $_->{action}, - attachment_id => $_->{attachment_id}, - requestee_id => $_->{requestee_id}, - requester_id => $_->{requester_id}, - status => $_->{status}, - type => _flatten_object($_->{type}), - setter => _flatten_object($_->{setter}), - requestee => _flatten_object($_->{requestee}), - requester => _flatten_object($_->{requester}), - } } @{$vars->{flag_events}} + map { + { + action => $_->{action}, + attachment_id => $_->{attachment_id}, + requestee_id => $_->{requestee_id}, + requester_id => $_->{requester_id}, + status => $_->{status}, + type => _flatten_object($_->{type}), + setter => _flatten_object($_->{setter}), + requestee => _flatten_object($_->{requestee}), + requester => _flatten_object($_->{requester}), + } + } @{$vars->{flag_events}} ]; Bugzilla->job_queue->insert('bug_mail', {vars => $vars}); } @@ -699,30 +702,24 @@ sub dequeue { # stale if the bug was restricted or user access revoked between enqueue # and dequeue (TOCTOU). unless ($vars->{to_user}->can_see_bug($vars->{bug}->id)) { - WARN( 'dequeue: suppressing bugmail for user ' - . $vars->{to_user}->login - . ' on bug ' - . $vars->{bug}->id - . ' — bug no longer visible at send time'); + WARN('dequeue: suppressing bugmail for user ' + . $vars->{to_user}->login . ' on bug ' . $vars->{bug}->id + . ' — bug no longer visible at send time'); return; } # Drop if user lost insider/timetracker privileges between enqueue and dequeue. # Filtering at sendMail-time may have included content these privileges gated. if ($vars->{_to_user_was_insider} && !$vars->{to_user}->is_insider) { - WARN( 'dequeue: suppressing bugmail for user ' - . $vars->{to_user}->login - . ' on bug ' - . $vars->{bug}->id - . ' — insider access revoked between enqueue and dequeue'); + WARN('dequeue: suppressing bugmail for user ' + . $vars->{to_user}->login . ' on bug ' . $vars->{bug}->id + . ' — insider access revoked between enqueue and dequeue'); return; } if ($vars->{_to_user_was_timetracker} && !$vars->{to_user}->is_timetracker) { - WARN( 'dequeue: suppressing bugmail for user ' - . $vars->{to_user}->login - . ' on bug ' - . $vars->{bug}->id - . ' — timetracker access revoked between enqueue and dequeue'); + WARN('dequeue: suppressing bugmail for user ' + . $vars->{to_user}->login . ' on bug ' . $vars->{bug}->id + . ' — timetracker access revoked between enqueue and dequeue'); return; } @@ -734,20 +731,18 @@ sub dequeue { map { +{ %$_, - type => Bugzilla::FlagType->new_from_hash($_->{type}), - setter => Bugzilla::User->new_from_hash($_->{setter}), - requestee => $_->{requestee} ? Bugzilla::User->new_from_hash($_->{requestee}) - : undef, - requester => $_->{requester} ? Bugzilla::User->new_from_hash($_->{requester}) - : undef, + type => Bugzilla::FlagType->new_from_hash($_->{type}), + setter => Bugzilla::User->new_from_hash($_->{setter}), + requestee => $_->{requestee} ? Bugzilla::User->new_from_hash($_->{requestee}) : undef, + requester => $_->{requester} ? Bugzilla::User->new_from_hash($_->{requester}) : undef, attachment => $_->{attachment_id} - ? Bugzilla::Attachment->new({id => $_->{attachment_id}, cache => 1}) - : undef, + ? Bugzilla::Attachment->new({id => $_->{attachment_id}, cache => 1}) + : undef, } } @{$vars->{flag_events} || []} ]; - $vars->{flag_events} = [grep { _flag_event_visible_to($_, $vars->{to_user}) } - @{$vars->{flag_events}}]; + $vars->{flag_events} + = [grep { _flag_event_visible_to($_, $vars->{to_user}) } @{$vars->{flag_events}}]; $vars->{changer} = Bugzilla::User->new_from_hash($vars->{changer}); $vars->{new_comments} @@ -761,13 +756,10 @@ sub dequeue { # check may be stale if the blocker was restricted between enqueue # and dequeue (TOCTOU). Suppress the email to match enqueue behavior. unless ($vars->{to_user}->can_see_bug($diff->{blocker}->id)) { - WARN( 'dequeue: suppressing bugmail for user ' - . $vars->{to_user}->login - . ' on bug ' - . $vars->{bug}->id - . ' — blocker bug ' - . $diff->{blocker}->id - . ' no longer visible at send time (TOCTOU)'); + WARN('dequeue: suppressing bugmail for user ' + . $vars->{to_user}->login . ' on bug ' . $vars->{bug}->id + . ' — blocker bug ' . $diff->{blocker}->id + . ' no longer visible at send time (TOCTOU)'); return; } } @@ -797,14 +789,11 @@ sub dequeue { } } - # Drop dep/see_also diffs that re-stripping emptied (matches sendMail behaviour). - $vars->{diffs} = [ - grep { - !($_->{field_name} =~ /^(?:dependson|blocked|regress(?:ed_by|es)|see_also)$/) - || $_->{old} - || $_->{new} - } @{$vars->{diffs}} - ]; + # Drop dep/see_also diffs that re-stripping emptied (matches sendMail behaviour). + $vars->{diffs} = [grep { + !($_->{field_name} =~ /^(?:dependson|blocked|regress(?:ed_by|es)|see_also)$/) + || $_->{old} || $_->{new} + } @{$vars->{diffs}}]; # Recompute changed-field headers after dequeue-time diff stripping. $vars->{changedfields} = [uniq map { $_->{field_desc} } @{$vars->{diffs}}]; @@ -827,15 +816,10 @@ sub dequeue { } # If nothing remains to send, bail. - unless (@{$vars->{diffs}} - || @{$vars->{new_comments}} - || @{$vars->{flag_events}}) - { - WARN( 'dequeue: suppressing bugmail for user ' - . $vars->{to_user}->login - . ' on bug ' - . $vars->{bug}->id - . ' — all diffs and comments stripped by dequeue-time visibility re-check'); + unless (@{$vars->{diffs}} || @{$vars->{new_comments}} || @{$vars->{flag_events}}) { + WARN('dequeue: suppressing bugmail for user ' + . $vars->{to_user}->login . ' on bug ' . $vars->{bug}->id + . ' — all diffs and comments stripped by dequeue-time visibility re-check'); return; } @@ -851,7 +835,7 @@ sub _flatten_object { # the same objects are used for each recipient, so cache the flattened hash my $cache = Bugzilla->request_cache->{bugmail_flat_objects} ||= {}; - my $key = blessed($object) . '-' . $object->id; + my $key = blessed($object) . '-' . $object->id; return $cache->{$key} ||= $object->flatten_to_hash; } @@ -908,10 +892,7 @@ sub _generate_bugmail { # flag_events, and the dequeue-time TOCTOU re-check (bug 1883428). sub _flag_event_visible_to { my ($event, $user) = @_; - return - !$event->{attachment} - || !$event->{attachment}->isprivate - || ($user && $user->is_insider); + return !$event->{attachment} || !$event->{attachment}->isprivate || ($user && $user->is_insider); } # Find flag changes during this window, so sendMail() can add a note at the @@ -921,7 +902,7 @@ sub _get_flag_mail_events { my ($bug, $start, $end, $user_cache) = @_; my $dbh = Bugzilla->dbh; - my @args = ($bug->id); + my @args = ($bug->id); my $when_restriction = ''; if ($start) { $when_restriction = ' AND flag_when > ? AND flag_when <= ?'; @@ -956,8 +937,7 @@ sub _get_flag_mail_events { $event->{action} = 'requested'; $event->{requestee_id} = $row->{requestee_id}; } - elsif ($row->{status} eq '+' || $row->{status} eq '-' || $row->{status} eq 'X') - { + elsif ($row->{status} eq '+' || $row->{status} eq '-' || $row->{status} eq 'X') { # 'X' is a flag cleared without +/- (e.g. needinfo auto-cleared when # the requestee replies) -- notify.() treated that the same as an @@ -1016,7 +996,7 @@ sub _get_flag_type_cc { foreach my $cc (split(/[, ]+/, $cc_list)) { my $ccuser = Bugzilla::User->new({name => $cc, cache => 1}); - next if @bug_in_groups && (!$ccuser || !$ccuser->can_see_bug($bug->id)); + next if @bug_in_groups && (!$ccuser || !$ccuser->can_see_bug($bug->id)); next if $attachment_is_private && (!$ccuser || !$ccuser->is_insider); if ($ccuser) { @@ -1168,7 +1148,7 @@ sub _get_diffs { } sub _get_new_bugmail_fields { - my $bug = shift; + my $bug = shift; my @fields = @{Bugzilla->fields({obsolete => 0, in_new_bugmail => 1})}; my @diffs; @@ -1236,7 +1216,7 @@ sub _bug_link_re { sub _parse_see_also { my (@links) = @_; my $bug_link_re = _bug_link_re(); - return grep {/^\d+$/} map { /$bug_link_re/ ? int($1) : () } @links; + return grep { /^\d+$/ } map { /$bug_link_re/ ? int($1) : () } @links; } 1; diff --git a/Bugzilla/DB/Schema.pm b/Bugzilla/DB/Schema.pm index af6b5203ae..d77d05b69c 100644 --- a/Bugzilla/DB/Schema.pm +++ b/Bugzilla/DB/Schema.pm @@ -24,9 +24,9 @@ use Bugzilla::Hook; use Bugzilla::Util; use Bugzilla::Constants; -use Carp qw(confess); -use Digest::MD5 qw(md5_hex); -use Hash::Util qw(lock_value unlock_hash lock_keys unlock_keys); +use Carp qw(confess); +use Digest::MD5 qw(md5_hex); +use Hash::Util qw(lock_value unlock_hash lock_keys unlock_keys); use List::MoreUtils qw(firstidx natatime); use Try::Tiny; use Module::Runtime qw(require_module); @@ -426,17 +426,21 @@ use constant ABSTRACT_SCHEMA => { REFERENCES => {TABLE => 'longdescs', COLUMN => 'comment_id', DELETE => 'CASCADE'} }, - user_id => { + user_id => { TYPE => 'INT3', NOTNULL => 1, REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'} }, - reaction => {TYPE => 'varchar(24)', NOTNULL => 1}, + reaction => {TYPE => 'varchar(24)', NOTNULL => 1}, ], INDEXES => [ - longdescs_reactions_reaction_idx => {FIELDS => ['comment_id', 'reaction'],}, - longdescs_reactions_user_idx => - {FIELDS => ['comment_id', 'user_id', 'reaction'], TYPE => 'UNIQUE',}, + longdescs_reactions_reaction_idx => { + FIELDS => ['comment_id', 'reaction'], + }, + longdescs_reactions_user_idx => { + FIELDS => ['comment_id', 'user_id', 'reaction'], + TYPE => 'UNIQUE', + }, ], }, @@ -730,20 +734,19 @@ use constant ABSTRACT_SCHEMA => { flag_activity => { FIELDS => [ - id => {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1}, - flag_when => {TYPE => 'DATETIME', NOTNULL => 1}, - type_id => { + id => {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1}, + flag_when => {TYPE => 'DATETIME', NOTNULL => 1}, + type_id => { TYPE => 'INT2', NOTNULL => 1, REFERENCES => {TABLE => 'flagtypes', COLUMN => 'id', DELETE => 'CASCADE'}, }, - flag_id => {TYPE => 'INT3', NOTNULL => 1}, - setter_id => { + flag_id => {TYPE => 'INT3', NOTNULL => 1}, + setter_id => { TYPE => 'INT3', NOTNULL => 1, REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}, }, - # The user who made the change. Not always the setter: redirecting a # request keeps the original setter. who => { @@ -751,17 +754,18 @@ use constant ABSTRACT_SCHEMA => { NOTNULL => 1, REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}, }, - requestee_id => - {TYPE => 'INT3', REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'},}, - bug_id => { + requestee_id => { + TYPE => 'INT3', + REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}, + }, + bug_id => { TYPE => 'INT3', NOTNULL => 1, REFERENCES => {TABLE => 'bugs', COLUMN => 'bug_id', DELETE => 'CASCADE'}, }, attachment_id => { TYPE => 'INT5', - REFERENCES => - {TABLE => 'attachments', COLUMN => 'attach_id', DELETE => 'CASCADE'}, + REFERENCES => {TABLE => 'attachments', COLUMN => 'attach_id', DELETE => 'CASCADE'}, }, status => {TYPE => 'CHAR(1)', NOTNULL => 1}, ], @@ -770,7 +774,7 @@ use constant ABSTRACT_SCHEMA => { flag_activity_type_id_idx => ['type_id'], flag_activity_bug_id_idx => ['bug_id'], flag_activity_status_when_idx => ['status', 'flag_when'], - flag_activity_who_idx => ['who', 'flag_when'], + flag_activity_who_idx => ['who', 'flag_when'], ], }, @@ -1052,7 +1056,7 @@ use constant ABSTRACT_SCHEMA => { mfa => {TYPE => 'varchar(8)', DEFAULT => "''"}, mfa_required_date => {TYPE => 'DATETIME'}, forget_after_date => {TYPE => 'DATETIME'}, - bounce_count => {TYPE => 'INT1', NOTNULL => 1, DEFAULT => 0}, + bounce_count => {TYPE => 'INT1', NOTNULL => 1, DEFAULT => 0}, modification_ts => {TYPE => 'DATETIME', NOTNULL => 1}, ], INDEXES => [ @@ -1276,16 +1280,16 @@ use constant ABSTRACT_SCHEMA => { REFERENCES => {TABLE => 'profiles', COLUMN => 'userid', DELETE => 'CASCADE'} }, ipaddr => {TYPE => 'varchar(40)'}, - lastused => {TYPE => 'DATETIME', NOTNULL => 1}, - id => {TYPE => 'INTSERIAL', NOTNULL => 1, PRIMARYKEY => 1}, - restrict_ipaddr => {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 0}, + lastused => {TYPE => 'DATETIME', NOTNULL => 1}, + id => {TYPE => 'INTSERIAL', NOTNULL => 1, PRIMARYKEY => 1}, + restrict_ipaddr => {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 0}, auth_method => {TYPE => 'varchar(40)'}, ], INDEXES => [ logincookies_lastused_idx => ['lastused'], logincookies_cookie_idx => {FIELDS => ['cookie'], TYPE => 'UNIQUE'}, ], - }, + }, login_failure => { FIELDS => [ @@ -1519,20 +1523,19 @@ use constant ABSTRACT_SCHEMA => { products => { FIELDS => [ - id => {TYPE => 'SMALLSERIAL', NOTNULL => 1, PRIMARYKEY => 1}, - name => {TYPE => 'varchar(64)', NOTNULL => 1}, + id => {TYPE => 'SMALLSERIAL', NOTNULL => 1, PRIMARYKEY => 1}, + name => {TYPE => 'varchar(64)', NOTNULL => 1}, classification_id => { TYPE => 'INT2', NOTNULL => 1, DEFAULT => '1', REFERENCES => {TABLE => 'classifications', COLUMN => 'id', DELETE => 'CASCADE'} }, - description => {TYPE => 'MEDIUMTEXT', NOTNULL => 1}, - isactive => {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 1}, - defaultmilestone => {TYPE => 'varchar(20)', NOTNULL => 1, DEFAULT => "'---'"}, - default_version => - {TYPE => 'varchar(20)', NOTNULL => 1, DEFAULT => "'unspecified'"}, - allows_unconfirmed => {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'TRUE'}, + description => {TYPE => 'MEDIUMTEXT', NOTNULL => 1}, + isactive => {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 1}, + defaultmilestone => {TYPE => 'varchar(20)', NOTNULL => 1, DEFAULT => "'---'"}, + default_version => {TYPE => 'varchar(20)', NOTNULL => 1, DEFAULT => "'unspecified'"}, + allows_unconfirmed => {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'TRUE'}, bug_description_template => {TYPE => 'MEDIUMTEXT'}, default_bug_type => {TYPE => 'varchar(20)'}, ], @@ -1835,7 +1838,7 @@ use constant ABSTRACT_SCHEMA => { jobid => {TYPE => 'INT4', NOTNULL => 1}, message => {TYPE => 'TEXT', NOTNULL => 1}, funcid => {TYPE => 'INT4', NOTNULL => 1, DEFAULT => 0}, - ], + ], INDEXES => [ ts_error_funcid_idx => [qw(funcid error_time)], ts_error_error_time_idx => ['error_time'], @@ -2041,8 +2044,8 @@ use constant ABSTRACT_SCHEMA => { bloomfilter_values => { FIELDS => [ - id => {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1}, - name => {TYPE => 'VARCHAR(64)', NOTNULL => 1}, + id => {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1}, + name => {TYPE => 'VARCHAR(64)', NOTNULL => 1}, value => {TYPE => 'VARCHAR(255)', NOTNULL => 1}, ], }, @@ -2389,7 +2392,8 @@ sub get_add_fks_sql { } else { foreach my $fk_string (@add) { - push(@sql, "ALTER TABLE " . $dbh->quote_identifier($table) . " ADD $fk_string"); + push(@sql, + "ALTER TABLE " . $dbh->quote_identifier($table) . " ADD $fk_string"); } } return @sql; @@ -2410,9 +2414,8 @@ sub get_drop_fk_sql { my ($self, $table, $column, $references) = @_; my $fk_name = $self->_get_fk_name($table, $column, $references); - return ("ALTER TABLE " - . Bugzilla->dbh->quote_identifier($table) - . " DROP CONSTRAINT $fk_name"); + return ( + "ALTER TABLE " . Bugzilla->dbh->quote_identifier($table) . " DROP CONSTRAINT $fk_name"); } sub convert_type { @@ -2774,7 +2777,9 @@ sub get_alter_column_ddl { # If we went from not being a PRIMARY KEY to being a PRIMARY KEY. if (!$old_def->{PRIMARYKEY} && $new_def->{PRIMARYKEY}) { push(@statements, - "ALTER TABLE " . $dbh->quote_identifier($table) . " ADD PRIMARY KEY ($column)"); + "ALTER TABLE " + . $dbh->quote_identifier($table) + . " ADD PRIMARY KEY ($column)"); } # If we went from being a PK to not being a PK diff --git a/Bugzilla/DB/Sqlite.pm b/Bugzilla/DB/Sqlite.pm index e45d1909b8..32f3851ccc 100644 --- a/Bugzilla/DB/Sqlite.pm +++ b/Bugzilla/DB/Sqlite.pm @@ -248,7 +248,7 @@ sub bz_setup_database { # If we created TheSchwartz tables with COLLATE bugzilla (during the # 4.1.x development series) re-create them without it. - my @tables = $self->bz_table_list(); + my @tables = $self->bz_table_list(); my @ts_tables = grep {/^ts_/} @tables; my $drop_ok; foreach my $table (@ts_tables) { diff --git a/Bugzilla/Flag.pm b/Bugzilla/Flag.pm index 8b8f6a36cf..a350cc0181 100644 --- a/Bugzilla/Flag.pm +++ b/Bugzilla/Flag.pm @@ -40,7 +40,7 @@ whose names start with _ or a re specifically noted as being private. =cut use Scalar::Util qw(blessed); -use Storable qw(dclone); +use Storable qw(dclone); use Bugzilla::FlagActivity; use Bugzilla::FlagType; @@ -327,7 +327,6 @@ sub set_flag { reason => $can_change->{reason} } ); - # Update (or delete) an existing flag. if ($params->{id}) { my $flag = $class->check({id => $params->{id}}); @@ -477,7 +476,7 @@ sub create { my ($class, $flag, $timestamp) = @_; $timestamp ||= Bugzilla->dbh->selectrow_array('SELECT LOCALTIMESTAMP(0)'); - my $params = {}; + my $params = {}; my @columns = grep { $_ ne 'id' } $class->_get_db_columns; # Some columns use date formatting so use alias instead @@ -570,7 +569,7 @@ sub update_flags { my ($class, $self, $old_self, $timestamp) = @_; my @old_summaries = $class->snapshot($old_self->flags); - my %old_flags = map { $_->id => $_ } @{$old_self->flags}; + my %old_flags = map { $_->id => $_ } @{$old_self->flags}; foreach my $new_flag (@{$self->flags}) { if (!$new_flag->id) { @@ -621,7 +620,7 @@ sub update_flags { } my @new_summaries = $class->snapshot($self->flags); - my @changes = $class->update_activity(\@old_summaries, \@new_summaries); + my @changes = $class->update_activity(\@old_summaries, \@new_summaries); Bugzilla::Hook::process( 'flag_end_of_update', @@ -700,7 +699,7 @@ sub force_retarget { foreach my $flag (@$flags) { # $bug is undefined when e.g. editing inclusion and exclusion lists. - my $obj = $flag->attachment || $bug || $flag->bug; + my $obj = $flag->attachment || $bug || $flag->bug; my $is_retargeted = $flag->retarget($obj); if ($is_retargeted) { $dbh->do('UPDATE flags SET type_id = ? WHERE id = ?', @@ -1050,7 +1049,6 @@ sub extract_flags_from_cgi { } } elsif (!$flag_type->is_multiplicable && scalar @$current_flags) { - # Update or delete the current flag if the type is not multiplicable. push(@new_flags, {id => @$current_flags[0]->id, status => $status}); } diff --git a/Bugzilla/FlagType.pm b/Bugzilla/FlagType.pm index 83731e2124..c162495ae8 100644 --- a/Bugzilla/FlagType.pm +++ b/Bugzilla/FlagType.pm @@ -673,7 +673,7 @@ sub match { my $dbh = Bugzilla->dbh; # Depending on the criteria, we may have to append additional tables. - my $tables = [DB_TABLE]; + my $tables = [DB_TABLE]; my @criteria = sqlify_criteria($criteria, $tables); $tables = join(' ', @$tables); $criteria = join(' AND ', @criteria); @@ -701,7 +701,7 @@ sub count { my $dbh = Bugzilla->dbh; # Depending on the criteria, we may have to append additional tables. - my $tables = [DB_TABLE]; + my $tables = [DB_TABLE]; my @criteria = sqlify_criteria($criteria, $tables); $tables = join(' ', @$tables); $criteria = join(' AND ', @criteria); diff --git a/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index 0d61bc1405..cf927bf593 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -16,7 +16,7 @@ use warnings; use Bugzilla::Constants; use Bugzilla::Hook; -use Bugzilla::Install (); +use Bugzilla::Install (); use Bugzilla::Install::Util qw(indicate_progress install_string); use Bugzilla::Util; use Bugzilla::Series; @@ -72,10 +72,8 @@ sub update_fielddefs_definition { # If the largest fielddefs sortkey is less than 100, then # we're using the old sorting system, and we should convert # it to the new one before adding any new definitions. - if ( - !$dbh->selectrow_arrayref( - 'SELECT COUNT(id) FROM fielddefs WHERE sortkey >= 100') - ) + if (!$dbh->selectrow_arrayref( + 'SELECT COUNT(id) FROM fielddefs WHERE sortkey >= 100')) { print "Updating the sortkeys for the fielddefs table...\n"; my $field_ids @@ -795,7 +793,7 @@ sub update_table_definitions { {TYPE => 'MEDIUMTEXT'}); # Bug 1522341, 1541617, 1546788 - kohei.yoshino@gmail.com - $dbh->bz_add_column('bugs', 'bug_type', {TYPE => 'varchar(20)'}); + $dbh->bz_add_column('bugs', 'bug_type', {TYPE => 'varchar(20)'}); $dbh->bz_add_column('components', 'default_bug_type', {TYPE => 'varchar(20)'}); $dbh->bz_add_column('products', 'default_bug_type', {TYPE => 'varchar(20)'}); $dbh->bz_alter_column('bugs', 'bug_type', @@ -811,8 +809,7 @@ sub update_table_definitions { _populate_api_keys_creation_ts(); # Bug 1612290 - dkl@mozilla.com - $dbh->bz_add_column('profiles', 'bounce_count', - {TYPE => 'INT1', NOTNULL => 1, DEFAULT => 0}); + $dbh->bz_add_column('profiles', 'bounce_count', {TYPE => 'INT1', NOTNULL => 1, DEFAULT => 0}); # Bug 1588221 - dkl@mozilla.com $dbh->bz_alter_column('bugs_activity', 'attach_id', {TYPE => 'INT5'}); @@ -820,9 +817,8 @@ sub update_table_definitions { {TYPE => 'BIGSERIAL', NOTNULL => 1, PRIMARYKEY => 1}); $dbh->bz_alter_column('attach_data', 'id', {TYPE => 'INT5', NOTNULL => 1, PRIMARYKEY => 1}); - $dbh->bz_alter_column('flags', 'attach_id', {TYPE => 'INT5'}); - $dbh->bz_alter_column('user_request_log', 'attach_id', - {TYPE => 'INT5', NOTNULL => 0}); + $dbh->bz_alter_column('flags', 'attach_id', {TYPE => 'INT5'}); + $dbh->bz_alter_column('user_request_log', 'attach_id', {TYPE => 'INT5', NOTNULL => 0}); _populate_attachment_storage_class(); # Bug 1682404 - dkl@mozilla.com @@ -830,7 +826,7 @@ sub update_table_definitions { {TYPE => 'varchar(255)', NOTNULL => 1, DEFAULT => "'Mozilla'"}); # Bug 1697642 - dkl@mozilla.com - $dbh->bz_add_column('logincookies', 'auth_method', {TYPE => 'varchar(40)'}); + $dbh->bz_add_column('logincookies', 'auth_method',{TYPE => 'varchar(40)'}); # Bug 1761594 - dkl@mozilla.com $dbh->bz_add_column('oauth2_client', 'hostname', @@ -2118,7 +2114,7 @@ sub _convert_attachment_statuses_to_flags { while (($added, $who, $when) = $sth2->fetchrow_array()) { last if $added =~ /(^|[, ]+)\Q$status\E([, ]+|$)/; } - $who = $dbh->quote($who); # "NULL" by default if $who is undefined + $who = $dbh->quote($who); # "NULL" by default if $who is undefined $when = $when ? $dbh->quote($when) : "NOW()"; @@ -2390,7 +2386,7 @@ sub _copy_old_charts_into_database { # We also add a new query for "Open", so that migrated products get # the same set as new products (see editproducts.cgi.) my @openedstatuses = ("UNCONFIRMED", "NEW", "ASSIGNED", "REOPENED"); - my $query = join("&", map {"bug_status=$_"} @openedstatuses); + my $query = join("&", map {"bug_status=$_"} @openedstatuses); my $series = new Bugzilla::Series(undef, $product, $all_name, $open_name, undef, 1, $query_prod . $query, 1); @@ -2801,7 +2797,7 @@ sub _change_all_mysql_booleans_to_tinyint { my $quip_info_sth = $dbh->column_info(undef, undef, 'quips', '%'); my $quips_cols = $quip_info_sth->fetchall_hashref("COLUMN_NAME"); my $approved_col = $quips_cols->{'approved'}; - if ( $approved_col->{TYPE_NAME} eq 'TINYINT' + if ($approved_col->{TYPE_NAME} eq 'TINYINT' and defined $approved_col->{COLUMN_SIZE} and $approved_col->{COLUMN_SIZE} == 1) { @@ -2890,11 +2886,9 @@ sub _fix_attachments_submitter_id_idx { if ( $dbh->bz_index_info('attachments', 'attachments_submitter_id_idx') && ( - scalar( - @{ - $dbh->bz_index_info('attachments', 'attachments_submitter_id_idx')->{FIELDS} - } - ) < 2 + scalar(@{ + $dbh->bz_index_info('attachments', 'attachments_submitter_id_idx')->{FIELDS} + }) < 2 ) ) { @@ -3797,7 +3791,7 @@ sub _set_attachment_comment_type { my $text = $comments{$id}; next if $text !~ /^\Q$string\E(\d+)/; my $attachment_id = $1; - my @lines = split("\n", $text); + my @lines = split("\n", $text); if ($type == CMT_ATTACHMENT_CREATED) { # Now we have to remove the text up until we find a line that's @@ -3866,7 +3860,7 @@ sub _convert_flagtypes_fks_to_set_null { } sub _fix_decimal_types { - my $dbh = Bugzilla->dbh; + my $dbh = Bugzilla->dbh; my $type = {TYPE => 'decimal(7,2)', NOTNULL => 1, DEFAULT => '0'}; $dbh->bz_alter_column('bugs', 'estimated_time', $type); $dbh->bz_alter_column('bugs', 'remaining_time', $type); @@ -3875,7 +3869,7 @@ sub _fix_decimal_types { sub _fix_series_creator_fk { my $dbh = Bugzilla->dbh; - my $fk = $dbh->bz_fk_info('series', 'creator'); + my $fk = $dbh->bz_fk_info('series', 'creator'); if ($fk and $fk->{DELETE} eq 'SET NULL') { $fk->{DELETE} = 'CASCADE'; $dbh->bz_alter_fk('series', 'creator', $fk); @@ -4032,7 +4026,7 @@ sub _migrate_user_tags { indicate_progress({current => ++$current, total => $total, every => 25}); - my $uri = URI->new("buglist.cgi?$query", 'http'); + my $uri = URI->new("buglist.cgi?$query", 'http'); my $bug_id_list = $uri->query_param_delete('bug_id'); if (!$bug_id_list) { warn "No bug_id param for tag $name from user $user_id: $query"; @@ -4129,7 +4123,7 @@ sub _rename_tags_to_tag { sub _on_delete_set_null_for_audit_log_userid { my $dbh = Bugzilla->dbh; - my $fk = $dbh->bz_fk_info('audit_log', 'user_id'); + my $fk = $dbh->bz_fk_info('audit_log', 'user_id'); if ($fk and !defined $fk->{DELETE}) { $fk->{DELETE} = 'SET NULL'; $dbh->bz_alter_fk('audit_log', 'user_id', $fk); @@ -4200,7 +4194,7 @@ sub _fix_dependencies_dupes { sub _fix_flagclusions_indexes { my $dbh = Bugzilla->dbh; foreach my $table ('flaginclusions', 'flagexclusions') { - my $index = $table . '_type_id_idx'; + my $index = $table . '_type_id_idx'; my $idx_info = $dbh->bz_index_info($table, $index); if ($idx_info && $idx_info->{'TYPE'} ne 'UNIQUE') { @@ -4368,9 +4362,7 @@ sub _populate_oauth2_scopes { } # Bug 1670319 - dkl@mozilla.com - Add new api:modify scope to scope list - my ($scope_found) - = $dbh->selectrow_array( - "SELECT 1 FROM oauth2_scope WHERE name = 'api:modify'"); + my ($scope_found) = $dbh->selectrow_array("SELECT 1 FROM oauth2_scope WHERE name = 'api:modify'"); if (!$scope_found) { $dbh->do("INSERT INTO oauth2_scope (name, description) VALUES " . "('api:modify', 'Read and modify data in Bugzilla using the Web API.')"); @@ -4445,9 +4437,7 @@ sub _populate_api_keys_creation_ts { # We do not have a way to tell when an API key was originally created # so we use the last_used timestamp as the initial creation value. - $dbh->do( - 'UPDATE user_api_keys SET creation_ts = COALESCE(last_used, LOCALTIMESTAMP(0))' - ); + $dbh->do('UPDATE user_api_keys SET creation_ts = COALESCE(last_used, LOCALTIMESTAMP(0))'); $dbh->bz_alter_column('user_api_keys', 'creation_ts', {TYPE => 'DATETIME', NOTNULL => 1}); @@ -4464,15 +4454,13 @@ sub _add_products_default_version { # Import the alphabetically last version for each product as the product's # default version, just like the Enter Bug page selecting the last one by # default before this migration. - my $sth = $dbh->prepare( - 'SELECT product_id, MAX(value) AS version - FROM versions GROUP BY product_id HAVING version != "unspecified"' - ); + my $sth = $dbh->prepare('SELECT product_id, MAX(value) AS version + FROM versions GROUP BY product_id HAVING version != "unspecified"'); $sth->execute(); while (my ($product_id, $version) = $sth->fetchrow_array()) { - $dbh->do('UPDATE products SET default_version = ? WHERE id = ?', - undef, ($version, $product_id)); + $dbh->do('UPDATE products SET default_version = ? WHERE id = ?', undef, + ($version, $product_id)); } } @@ -4480,8 +4468,7 @@ sub _populate_attachment_storage_class { my $dbh = Bugzilla->dbh; # Return if we have already made these changes - my $count - = $dbh->selectrow_array('SELECT COUNT(id) FROM attachment_storage_class'); + my $count = $dbh->selectrow_array('SELECT COUNT(id) FROM attachment_storage_class'); if (!$count) { $dbh->do( "INSERT INTO attachment_storage_class (id, storage_class) SELECT attachments.attach_id, 'database' FROM attachments ORDER BY attachments.attach_id" @@ -4556,13 +4543,11 @@ sub _migrate_profiles_modification_ts { } my $modification_ts = 0; - # IF we could not find anything then use todays date if (!$audit_log_when && !$profiles_act_when && !$creation_when) { $modification_ts = $now_when; } - -# We used unix timestamps to make value comparison easier without using DateTime instance of each. + # We used unix timestamps to make value comparison easier without using DateTime instance of each. else { $modification_ts = max($audit_log_when, $profiles_act_when, $creation_when); } diff --git a/Bugzilla/Search.pm b/Bugzilla/Search.pm index 6532ff7f9a..487a7a58c3 100644 --- a/Bugzilla/Search.pm +++ b/Bugzilla/Search.pm @@ -34,10 +34,10 @@ use Bugzilla::Util; use Data::Dumper; use DateTime; use List::MoreUtils qw(all any firstidx part uniq); -use POSIX qw(INT_MAX); -use Scalar::Util qw(blessed); -use Storable qw(dclone); -use Time::HiRes qw(gettimeofday tv_interval); +use POSIX qw(INT_MAX); +use Scalar::Util qw(blessed); +use Storable qw(dclone); +use Time::HiRes qw(gettimeofday tv_interval); # Description Of Boolean Charts # ----------------------------- @@ -110,7 +110,9 @@ use Time::HiRes qw(gettimeofday tv_interval); ############# # BMO - product aliases for searching -use constant PRODUCT_ALIASES => {'Fenix' => 'Firefox for Android',}; +use constant PRODUCT_ALIASES => { + 'Fenix' => 'Firefox for Android', +}; # When doing searches, NULL datetimes are treated as this date. use constant EMPTY_DATETIME => '1970-01-01 00:00:00'; @@ -315,16 +317,16 @@ use constant OPERATOR_FIELD_OVERRIDE => { team_name => {_non_changed => \&_team_name_nonchanged}, # Count Fields - 'attachments.count' => RELATION_COUNT_OVERRIDE, - 'cc_count' => RELATION_COUNT_OVERRIDE, - 'see_also_count' => RELATION_COUNT_OVERRIDE, - 'keywords.count' => RELATION_COUNT_OVERRIDE, - 'blocked.count' => RELATION_COUNT_OVERRIDE, - 'dependson.count' => RELATION_COUNT_OVERRIDE, - 'regressed_by.count' => RELATION_COUNT_OVERRIDE, - 'regresses.count' => RELATION_COUNT_OVERRIDE, - 'dupe_count' => RELATION_COUNT_OVERRIDE, - 'longdescs.count' => { + 'attachments.count' => RELATION_COUNT_OVERRIDE, + 'cc_count' => RELATION_COUNT_OVERRIDE, + 'see_also_count' => RELATION_COUNT_OVERRIDE, + 'keywords.count' => RELATION_COUNT_OVERRIDE, + 'blocked.count' => RELATION_COUNT_OVERRIDE, + 'dependson.count' => RELATION_COUNT_OVERRIDE, + 'regressed_by.count' => RELATION_COUNT_OVERRIDE, + 'regresses.count' => RELATION_COUNT_OVERRIDE, + 'dupe_count' => RELATION_COUNT_OVERRIDE, + 'longdescs.count' => { changedby => \&_long_desc_changedby, everchanged => \&_long_desc_everchanged, changedbefore => \&_long_desc_changedbefore_after, @@ -425,7 +427,7 @@ use constant USER_FIELDS => { join => {table => 'longdescs', join => 'INNER'}, isprivate => 1, }, - qa_contact => {nullable => 1,}, + qa_contact => {nullable => 1,}, 'requestees.login_name' => {nullable => 1, field => 'requestee_id', join => {table => 'flags'},}, 'setters.login_name' => {field => 'setter_id', join => {table => 'flags'},}, @@ -494,7 +496,7 @@ sub COLUMN_JOINS { reporter => {from => 'reporter', to => 'userid', table => 'profiles', join => 'INNER',}, qa_contact => {from => 'qa_contact', to => 'userid', table => 'profiles',}, - component => + component => {from => 'component_id', to => 'id', table => 'components', join => 'INNER',}, team_name => {from => 'component_id', to => 'id', table => 'components', join => 'INNER',}, @@ -533,22 +535,22 @@ sub COLUMN_JOINS { to => 'id', }, }, - 'attachments.count' => {table => 'attachments',}, - 'cc_count' => {table => 'cc',}, - 'see_also_count' => {table => 'bug_see_also'}, - 'keywords.count' => {table => 'keywords',}, - 'longdescs.count' => {table => 'longdescs', join => 'INNER',}, - 'blocked' => {table => 'dependencies', to => 'dependson',}, - 'blocked.count' => {table => 'dependencies', to => 'dependson',}, - 'dependson' => {table => 'dependencies', to => 'blocked',}, - 'dependson.count' => {table => 'dependencies', to => 'blocked',}, - 'regressed_by' => {table => 'regressions', to => 'regresses',}, - 'regressed_by.count' => {table => 'regressions', to => 'regresses',}, - 'regresses' => {table => 'regressions', to => 'regressed_by',}, - 'regresses.count' => {table => 'regressions', to => 'regressed_by',}, - 'dupe_count' => {table => 'duplicates', to => 'dupe_of',}, - 'duplicates' => {table => 'duplicates', to => 'dupe_of',}, - last_visit_ts => { + 'attachments.count' => {table => 'attachments',}, + 'cc_count' => {table => 'cc',}, + 'see_also_count' => {table => 'bug_see_also'}, + 'keywords.count' => {table => 'keywords',}, + 'longdescs.count' => {table => 'longdescs', join => 'INNER',}, + 'blocked' => {table => 'dependencies', to => 'dependson',}, + 'blocked.count' => {table => 'dependencies', to => 'dependson',}, + 'dependson' => {table => 'dependencies', to => 'blocked',}, + 'dependson.count' => {table => 'dependencies', to => 'blocked',}, + 'regressed_by' => {table => 'regressions', to => 'regresses',}, + 'regressed_by.count' => {table => 'regressions', to => 'regresses',}, + 'regresses' => {table => 'regressions', to => 'regressed_by',}, + 'regresses.count' => {table => 'regressions', to => 'regressed_by',}, + 'dupe_count' => {table => 'duplicates', to => 'dupe_of',}, + 'duplicates' => {table => 'duplicates', to => 'dupe_of',}, + last_visit_ts => { as => 'bug_user_last_visit', table => 'bug_user_last_visit', extra => ['bug_user_last_visit.user_id = ' . $user->id], @@ -625,13 +627,12 @@ sub COLUMNS { 'DISTINCT ' . $dbh->sql_string_concat('map_flagtypes.name', 'map_flags.status') ), - 'keywords' => $dbh->sql_group_concat('DISTINCT map_keyworddefs.name'), - 'blocked' => $dbh->sql_group_concat('DISTINCT map_blocked.blocked'), - 'dependson' => $dbh->sql_group_concat('DISTINCT map_dependson.dependson'), - 'regressed_by' => - $dbh->sql_group_concat('DISTINCT map_regressed_by.regressed_by'), - 'regresses' => $dbh->sql_group_concat('DISTINCT map_regresses.regresses'), - 'duplicates' => $dbh->sql_group_concat('DISTINCT map_duplicates.dupe'), + 'keywords' => $dbh->sql_group_concat('DISTINCT map_keyworddefs.name'), + 'blocked' => $dbh->sql_group_concat('DISTINCT map_blocked.blocked'), + 'dependson' => $dbh->sql_group_concat('DISTINCT map_dependson.dependson'), + 'regressed_by' => $dbh->sql_group_concat('DISTINCT map_regressed_by.regressed_by'), + 'regresses' => $dbh->sql_group_concat('DISTINCT map_regresses.regresses'), + 'duplicates' => $dbh->sql_group_concat('DISTINCT map_duplicates.dupe'), 'attachments.count' => 'COUNT(DISTINCT map_attachments_count.attach_id)', 'cc_count' => 'COUNT(DISTINCT map_cc_count.who)', @@ -717,7 +718,7 @@ sub COLUMNS { sub REPORT_COLUMNS { my $invocant = shift; - my $user = blessed($invocant) ? $invocant->_user : Bugzilla->user; + my $user = blessed($invocant) ? $invocant->_user : Bugzilla->user; my $columns = dclone(blessed($invocant) ? $invocant->COLUMNS : COLUMNS); @@ -776,7 +777,7 @@ use constant GROUP_BY_SKIP => qw( # Note that the params argument may be modified by Bugzilla::Search sub new { my $invocant = shift; - my $class = ref($invocant) || $invocant; + my $class = ref($invocant) || $invocant; my $self = {@_}; bless($self, $class); @@ -792,7 +793,7 @@ sub new { # do it, because there's no way to know if we were passed a tied hash # or not. my $params_in = $self->_params; - my %params = map { $_ => $params_in->{$_} } keys %$params_in; + my %params = map { $_ => $params_in->{$_} } keys %$params_in; $self->{params} = \%params; return $self; @@ -826,7 +827,7 @@ sub data { # BMO - to avoid massive amounts of joins, if we're selecting a lot of # tracking flags, replace them with placeholders. the values will be # retrieved later and injected into the result. - my %tf_map = map { $_ => 1 } Bugzilla->tracking_flag_names; + my %tf_map = map { $_ => 1 } Bugzilla->tracking_flag_names; my @tf_selected = grep { exists $tf_map{$_} } @orig_fields; # MySQL has a limit of 61 joins, and we want to avoid massive amounts of joins @@ -845,7 +846,7 @@ sub data { my $sql = $self->_sql; # Do we just want bug IDs to pass to the 2nd query or all the data immediately? - my $func = $all_in_bugs_table ? 'selectall_arrayref' : 'selectcol_arrayref'; + my $func = $all_in_bugs_table ? 'selectall_arrayref' : 'selectcol_arrayref'; my $bug_ids = $self->_sql_execute($sql, $func); my @extra_data = ({sql => $sql, time => tv_interval($start_time)}); @@ -936,7 +937,7 @@ sub data { sub _sql { my ($self) = @_; return $self->{sql} if $self->{sql}; - my $dbh = Bugzilla->dbh; + my $dbh = Bugzilla->dbh; my $user_id = $self->{user}->id; my ($joins, $clause) = $self->_charts_to_conditions(); @@ -1056,7 +1057,7 @@ sub boolean_charts_to_custom_search { # We can just pick the last id in the array because they are sorted # numerically. my $last_id = ($self->_field_ids)[-1]; - my $count = defined($last_id) ? $last_id + 1 : 0; + my $count = defined($last_id) ? $last_id + 1 : 0; foreach my $param_set (@as_params) { foreach my $name (keys %$param_set) { my $value = $param_set->{$name}; @@ -1223,7 +1224,8 @@ sub _valid_order_columns { my ($self) = @_; return grep { $_ ne 'bug_list' } - map { (split_order_term($_))[0] } $self->_valid_order; + map { (split_order_term($_))[0] } + $self->_valid_order; } sub _validate_order_column { @@ -1266,7 +1268,7 @@ sub _special_order { my $name = $field->name; $special_order{$name} = { order => ["map_$name.sortkey", "map_$name.value"], - join => {table => $name, from => "bugs.$name", to => "value", join => 'INNER',} + join => {table => $name, from => "bugs.$name", to => "value", join => 'INNER',} }; } $self->{special_order} = \%special_order; @@ -1276,15 +1278,12 @@ sub _special_order { sub _sql_order_by { my ($self) = @_; if (!$self->{sql_order_by}) { - # Handle ordering by the list of bugs provided in the bug_id param if ($self->{order_by_bug_list}) { - # order_by_bug_list is guaranteed at this point to only contain a comma # separated list of integers return ("FIELD(bugs.bug_id,$self->{order_by_bug_list})"); } - # Otherwise order by columns my @order_by = map { $self->_translate_order_by_column($_) } $self->_valid_order; @@ -1510,7 +1509,7 @@ sub _translate_join { } my $table = $join_info->{table}; my $name = $join_info->{as}; - my $to = $join_info->{to} || "bug_id"; + my $to = $join_info->{to} || "bug_id"; my $join = $join_info->{join} || 'LEFT'; my @extra = @{$join_info->{extra} || []}; $name =~ s/\./_/g; @@ -1525,7 +1524,7 @@ sub _translate_join { } my $sql_table = $table eq 'groups' ? Bugzilla->dbh->quote_identifier($table) : $table; - my @join_sql = "$join JOIN $sql_table AS $name " + my @join_sql = "$join JOIN $sql_table AS $name " . " ON $from_table.$from = $name.$to$extra_condition"; return @join_sql; } @@ -1544,7 +1543,6 @@ sub _standard_where { my $userid = $self->_user->id; if ($self->{_no_security_check}) { - # If security checks have already been performed, there's no need to repeat # in sub-queries. return ""; @@ -1554,12 +1552,11 @@ sub _standard_where { my $term = 'security_map.group_id IS NULL'; if ($userid) { - # Authenticated; users directly associated with the bug have visibility. my @involved = ( - "bugs.reporter_accessible = 1 AND bugs.reporter = $userid", - 'bugs.cclist_accessible = 1 AND security_cc.who IS NOT NULL', - "bugs.assigned_to = $userid", + "bugs.reporter_accessible = 1 AND bugs.reporter = $userid", + 'bugs.cclist_accessible = 1 AND security_cc.who IS NOT NULL', + "bugs.assigned_to = $userid", ); if (Bugzilla->params->{'useqacontact'}) { push @involved, ("bugs.qa_contact = $userid"); @@ -1579,7 +1576,7 @@ sub _standard_where { sub _sql_where { my ($self, $main_clause) = @_; - my $where = ""; + my $where = ""; my $where_sql = $self->_standard_where(); $where .= '(' . $where_sql . ')' if $where_sql; @@ -1687,7 +1684,7 @@ sub _parse_basic_fields { my @values = $self->_param_array($param_name); next if !@values; my $default_op = $param_name eq 'content' ? 'matches' : 'anyexact'; - my $operator = $params->{"${param_name}_type"} || $default_op; + my $operator = $params->{"${param_name}_type"} || $default_op; # Fields that are displayed as multi-selects are passed as arrays, # so that they can properly search values that contain commas. @@ -1844,7 +1841,6 @@ sub _special_parse_email { my $email = trim($params->{"email$id"}); next if !$email; my $type = $params->{"emailtype$id"} || 'equals'; - # for backward compatibility $type = "equals" if $type eq "exact"; @@ -1945,19 +1941,19 @@ sub _boolean_charts { my @param_list = keys %$params; my @all_field_params = grep {/^field-?\d+/} @param_list; - my @chart_ids = map { /^field(-?\d+)/; $1 } @all_field_params; + my @chart_ids = map { /^field(-?\d+)/; $1 } @all_field_params; @chart_ids = sort { $a <=> $b } uniq @chart_ids; my $clause = new Bugzilla::Search::Clause(); foreach my $chart_id (@chart_ids) { my @all_and = grep {/^field$chart_id-\d+/} @param_list; - my @and_ids = map { /^field$chart_id-(\d+)/; $1 } @all_and; + my @and_ids = map { /^field$chart_id-(\d+)/; $1 } @all_and; @and_ids = sort { $a <=> $b } uniq @and_ids; my $and_clause = new Bugzilla::Search::Clause(); foreach my $and_id (@and_ids) { my @all_or = grep {/^field$chart_id-$and_id-\d+/} @param_list; - my @or_ids = map { /^field$chart_id-$and_id-(\d+)/; $1 } @all_or; + my @or_ids = map { /^field$chart_id-$and_id-(\d+)/; $1 } @all_or; @or_ids = sort { $a <=> $b } uniq @or_ids; my $or_clause = new Bugzilla::Search::Clause('OR'); @@ -2032,7 +2028,7 @@ sub _field_ids { my @param_list = keys %$params; my @field_params = grep {/^f\d+$/} @param_list; - my @field_ids = map { /(\d+)/; $1 } @field_params; + my @field_ids = map { /(\d+)/; $1 } @field_params; @field_ids = sort { $a <=> $b } @field_ids; return @field_ids; } @@ -2049,7 +2045,7 @@ sub _handle_chart { if (ref $value eq 'ARRAY') { # Trim input and ignore blank values. - @$value = map { trim($_) } @$value; + @$value = map { trim($_) } @$value; @$value = grep { defined $_ and $_ ne '' } @$value; return if !@$value; $string_value = join(',', @$value); @@ -2300,7 +2296,7 @@ sub _all_values { } else { @array = split($split_on, $all_values); - @array = map { trim($_) } @array; + @array = map { trim($_) } @array; @array = grep { defined $_ and $_ ne '' } @array; } @@ -2320,7 +2316,7 @@ sub _substring_terms { # split each term on spaces and commas anyway. my @words = split(/[\s,]+/, $args->{value}); @words = grep { defined $_ and $_ ne '' } @words; - @words = map { $dbh->quote($_) } @words; + @words = map { $dbh->quote($_) } @words; my @terms = map { $dbh->sql_iposition($_, $args->{full_field}) . " > 0" } @words; return @terms; @@ -2401,8 +2397,8 @@ sub _date_translate { sub SqlifyDate { my ($str) = @_; - my $fmt = "%Y-%m-%d %H:%M:%S"; - my $date = DateTime->now(); + my $fmt = "%Y-%m-%d %H:%M:%S"; + my $date = DateTime->now(); $str = "" if (!defined $str || lc($str) eq 'now'); if ($str eq "") { return $date->strftime('%Y-%m-%d 00:00:00'); @@ -2541,7 +2537,6 @@ sub _contact_exact_group { my $group_ids = Bugzilla::Group->flatten_group_membership($group->id); if ($field eq 'cc' && $chart_id eq '') { - # This is for the email1, email2, email3 fields from query.cgi. $chart_id = "CC$$sequence"; $args->{sequence}++; @@ -2558,8 +2553,8 @@ sub _contact_exact_group { $from = "$join_table_alias.who"; } - my $table = "user_group_map_$chart_id"; - my $join = { + my $table = "user_group_map_$chart_id"; + my $join = { table => 'user_group_map', as => $table, from => $from, @@ -2608,12 +2603,12 @@ sub _get_user_id { ###################################### sub _chart_resolution_parser { - my ($self, $args) = @_; + my ($self, $args) = @_; my ($value, $operator) = @$args{qw(value operator)}; # Treat `---` as empty if (trim($value) eq '---' && $operator =~ /^(?:not)?equals$/) { - $args->{value} = $args->{all_values} = $args->{quoted} = ''; + $args->{value} = $args->{all_values} = $args->{quoted} = ''; $args->{operator} = $operator eq 'equals' ? 'isempty' : 'isnotempty'; } } @@ -2657,7 +2652,7 @@ sub _user_nonchanged { # We want to copy $join so as not to modify USER_FIELDS. push(@$joins, {%$join, as => $as, extra => $extra}); my $search_field = USER_FIELDS->{$field}->{field}; - $args->{full_field} = "$as.$search_field"; + $args->{full_field} = "$as.$search_field"; $args->{enabled_field} = "$as.is_enabled"; } @@ -2682,7 +2677,7 @@ sub _user_nonchanged { join => (!$is_in_other_table and !$is_nullable) ? 'INNER' : undef, }; push(@$joins, $join); - $args->{full_field} = "$as.login_name"; + $args->{full_field} = "$as.login_name"; $args->{enabled_field} = "$as.is_enabled"; } @@ -2895,33 +2890,27 @@ sub _relation_count_changed { sub _relation_count_default { my ($self, $args) = @_; my ($chart_id, $field, $joins) = @$args{qw(chart_id field joins)}; - my $extra - = !$self->_user->is_insider && $field =~ /^(?:attachments|longdescs)\.count$/ - ? 'WHERE isprivate = 0' - : ''; + my $extra = !$self->_user->is_insider + && $field =~ /^(?:attachments|longdescs)\.count$/ ? 'WHERE isprivate = 0' : ''; my ($table, $column, $other_column) - = $field eq 'attachments.count' ? ('attachments', 'attach_id', 'bug_id') - : $field eq 'cc_count' ? ('cc', 'cc', 'bug_id') - : $field eq 'see_also_count' ? ('bug_see_also', 'id', 'bug_id') - : $field eq 'keywords.count' ? ('keywords', 'keywords', 'bug_id') - : $field eq 'longdescs.count' ? ('longdescs', 'comment_id', 'bug_id') - : $field eq 'blocked.count' ? ('dependencies', 'blocked', 'dependson') - : $field eq 'dependson.count' ? ('dependencies', 'dependson', 'blocked') - : $field eq 'regressed_by.count' - ? ('regressions', 'regressed_by', 'regresses') + = $field eq 'attachments.count' ? ('attachments', 'attach_id', 'bug_id') + : $field eq 'cc_count' ? ('cc', 'cc', 'bug_id') + : $field eq 'see_also_count' ? ('bug_see_also', 'id', 'bug_id') + : $field eq 'keywords.count' ? ('keywords', 'keywords', 'bug_id') + : $field eq 'longdescs.count' ? ('longdescs', 'comment_id', 'bug_id') + : $field eq 'blocked.count' ? ('dependencies', 'blocked', 'dependson') + : $field eq 'dependson.count' ? ('dependencies', 'dependson', 'blocked') + : $field eq 'regressed_by.count' ? ('regressions', 'regressed_by', 'regresses') : $field eq 'regresses.count' ? ('regressions', 'regresses', 'regressed_by') - : $field eq 'dupe_count' ? ('duplicates', 'dupe', 'dupe_of') - : undef; + : $field eq 'dupe_count' ? ('duplicates', 'dupe', 'dupe_of') + : undef; my $alias = "${column}_count_${chart_id}"; - push( - @$joins, - { - table => "(SELECT $other_column AS bug_id, COUNT(*) AS num" - . " FROM $table $extra GROUP BY $other_column)", - as => $alias, - } - ); + push(@$joins, { + table => "(SELECT $other_column AS bug_id, COUNT(*) AS num" + . " FROM $table $extra GROUP BY $other_column)", + as => $alias, + }); $args->{full_field} = "COALESCE(${alias}.num, 0)"; } @@ -3033,10 +3022,10 @@ sub _component_nonchanged { # Allow to search product/component pairs like "Core::General" with a simple # operator. Since product/component names may include spaces, other operators # like `anywords` won't work. - if ( $args->{operator} =~ /^(?:(?:not)?equals)$/ + if ($args->{operator} =~ /^(?:(?:not)?equals)$/ && $args->{value} =~ /^(?:(.+)\s*::\s*)?(.+)$/) { - $product = $1; + $product = $1; $args->{value} = $args->{all_values} = $2; $args->{quoted} = $dbh->quote($2); } @@ -3047,23 +3036,16 @@ sub _component_nonchanged { my $term = $args->{term}; if ($product) { - # Pass the complete condition and negative option to make sure both product # and component are included or excluded - $args->{term} = build_subselect( - 'bugs.component_id', - 'components.id', + $args->{term} = build_subselect('bugs.component_id', 'components.id', 'components JOIN products ON components.product_id = products.id', - 'products.name = ' - . $dbh->quote($product) - . ' AND components.name = ' - . $args->{quoted}, - $args->{operator} eq 'notequals' - ); - } - else { - $args->{term} - = build_subselect('bugs.component_id', 'components.id', 'components', $term); + 'products.name = ' . $dbh->quote($product) + . ' AND components.name = ' . $args->{quoted}, + $args->{operator} eq 'notequals'); + } else { + $args->{term} = build_subselect('bugs.component_id', 'components.id', + 'components', $term); } } @@ -3081,7 +3063,7 @@ sub _product_nonchanged { } } if ($aliased) { - $args->{value} = join(',', @{$args->{all_values}}); + $args->{value} = join(',', @{$args->{all_values}}); $args->{quoted} = Bugzilla->dbh->quote($args->{value}); } } @@ -3101,7 +3083,6 @@ sub _product_nonchanged { sub _team_name_nonchanged { my ($self, $args) = @_; - # This joins the right tables for us. $self->_add_extra_column('component'); $args->{full_field} = "map_component.team_name"; @@ -3209,7 +3190,7 @@ sub _multiselect_negative { my ($field, $operator) = @$args{qw(field operator)}; $args->{operator} = $self->_reverse_operator($operator); - $args->{term} = $self->_multiselect_term($args, 1); + $args->{term} = $self->_multiselect_term($args, 1); } sub _multiselect_multiple { @@ -3267,9 +3248,9 @@ sub _flagtypes_nonchanged { } my $subselect_args = { - chart_id => $chart_id, - sequence => $chart_id, - field => 'flagtypes.name', + chart_id => $chart_id, + sequence => $chart_id, + field => 'flagtypes.name', full_field => $dbh->sql_string_concat("flagtypes_$chart_id.name", "flags_$chart_id.status"), operator => $operator, @@ -3345,7 +3326,7 @@ sub _multiselect_table { } elsif ($field eq 'longdesc') { $args->{_extra_where} = " AND isprivate = 0" if !$self->_user->is_insider; - $args->{full_field} = 'thetext'; + $args->{full_field} = 'thetext'; return "longdescs"; } elsif ($field eq 'longdescs.isprivate') { @@ -3387,7 +3368,7 @@ sub _multiselect_term { my $table = $self->_multiselect_table($args); $self->_do_operator_function($args); my $term = $args->{term}; - $term .= $args->{_extra_where} || ''; + $term .= $args->{_extra_where} || ''; my $select = $args->{_select_field} || 'bug_id'; return build_subselect("$args->{bugs_table}.bug_id", $select, $table, $term, $not); @@ -3401,7 +3382,7 @@ sub _multiselect_isempty { = @$args{qw(field operator joins chart_id)}; my $dbh = Bugzilla->dbh; $operator = $self->_reverse_operator($operator) if $not; - $not = $operator eq 'isnotempty' ? 'NOT' : ''; + $not = $operator eq 'isnotempty' ? 'NOT' : ''; if ($field eq 'keywords') { push @$joins, @@ -3660,11 +3641,9 @@ sub _changedbefore_changedafter { my $join = {table => 'bugs_activity', extra => []}; if ($field eq 'anything') { - # Handle special field name to find changes in any field $table = $join->{as} = "act_x_$chart_id"; - } - else { + } else { my $field_object = $self->_chart_fields->{$field} || ThrowCodeError("invalid_field_name", {field => $field}); @@ -3684,7 +3663,7 @@ sub _changedbefore_changedafter { } my $sql_operator = ($operator =~ /before/) ? '<=' : '>='; - my $sql_date = $dbh->quote(SqlifyDate($value)); + my $sql_date = $dbh->quote(SqlifyDate($value)); push(@{$join->{extra}}, "$table.bug_when $sql_operator $sql_date"); $args->{term} = "$table.bug_when IS NOT NULL"; @@ -3706,15 +3685,13 @@ sub _changedfrom_changedto { = @$args{qw(chart_id joins field operator quoted)}; my $table; - my $join = {table => 'bugs_activity', extra => []}; + my $join = {table => 'bugs_activity', extra => []}; my $column = ($operator =~ /from/) ? 'removed' : 'added'; if ($field eq 'anything') { - # Handle special field name to find changes in any field $table = $join->{as} = "act_x_$chart_id"; - } - else { + } else { my $field_object = $self->_chart_fields->{$field} || ThrowCodeError("invalid_field_name", {field => $field}); my $field_id = $field_object->id; @@ -3748,11 +3725,9 @@ sub _changedby { my $join = {table => 'bugs_activity', extra => []}; if ($field eq 'anything') { - # Handle special field name to find changes in any field $table = $join->{as} = "act_x_$chart_id"; - } - else { + } else { my $field_object = $self->_chart_fields->{$field} || ThrowCodeError("invalid_field_name", {field => $field}); my $field_id = $field_object->id; diff --git a/Bugzilla/Test/Util.pm b/Bugzilla/Test/Util.pm index f229416dac..9d8109967c 100644 --- a/Bugzilla/Test/Util.pm +++ b/Bugzilla/Test/Util.pm @@ -51,7 +51,9 @@ sub create_group { my ($group, %extra) = @_; require Bugzilla; return Bugzilla::Group->create({ - name => $group, description => "$group Group Description", %extra, + name => $group, + description => "$group Group Description", + %extra, }); } @@ -62,10 +64,8 @@ sub create_oauth_client { my $id = generate_random_password(20); my $secret = generate_random_password(40); - $dbh->do( - 'INSERT INTO oauth2_client (client_id, description, secret, hostname) VALUES (?, ?, ?, ?)', - undef, $id, $description, $secret, hostname() - ); + $dbh->do('INSERT INTO oauth2_client (client_id, description, secret, hostname) VALUES (?, ?, ?, ?)', + undef, $id, $description, $secret, hostname()); my $client_data = $dbh->selectrow_hashref('SELECT * FROM oauth2_client WHERE client_id = ?', @@ -78,8 +78,10 @@ sub create_oauth_client { if (!$scope_id) { die "Scope $scope not found"; } - $dbh->do('INSERT INTO oauth2_client_scope (client_id, scope_id) VALUES (?, ?)', - undef, $client_data->{id}, $scope_id); + $dbh->do( + 'INSERT INTO oauth2_client_scope (client_id, scope_id) VALUES (?, ?)', + undef, $client_data->{id}, $scope_id + ); } $client_data->{scopes} = $scopes; diff --git a/editusers.cgi b/editusers.cgi index 0a7ab330ea..f14122a440 100755 --- a/editusers.cgi +++ b/editusers.cgi @@ -333,15 +333,16 @@ elsif ($action eq 'update') { } if (@groups_added_to || @groups_removed_from) { - $otherUser->set_groups( - {add => \@groups_added_to, remove => \@groups_removed_from}); + $otherUser->set_groups({add => \@groups_added_to, remove => \@groups_removed_from}); } if (@groups_granted_rights_to_bless || @groups_denied_rights_to_bless) { - $otherUser->set_bless_groups({ - add => \@groups_granted_rights_to_bless, - remove => \@groups_denied_rights_to_bless - }); + $otherUser->set_bless_groups( + { + add => \@groups_granted_rights_to_bless, + remove => \@groups_denied_rights_to_bless + } + ); } my $changes = $otherUser->update(); diff --git a/extensions/BMO/lib/Reports/ReleaseTracking.pm b/extensions/BMO/lib/Reports/ReleaseTracking.pm index 5650cb9bd8..2345f15894 100644 --- a/extensions/BMO/lib/Reports/ReleaseTracking.pm +++ b/extensions/BMO/lib/Reports/ReleaseTracking.pm @@ -16,8 +16,8 @@ use Bugzilla::Error; use Bugzilla::Field; use Bugzilla::FlagActivity; use Bugzilla::FlagType; -use Bugzilla::Util qw(validate_date); -use JSON qw(-convert_blessed_universally); +use Bugzilla::Util qw(validate_date); +use JSON qw(-convert_blessed_universally); use List::MoreUtils qw(uniq); sub report { @@ -285,7 +285,7 @@ sub report { } sub _parse_query { - my $q = shift; + my $q = shift; my @query = split(/:/, $q); my $query; diff --git a/extensions/BMO/lib/Reports/UserActivity.pm b/extensions/BMO/lib/Reports/UserActivity.pm index 85666c00c6..f7e4c1fccd 100644 --- a/extensions/BMO/lib/Reports/UserActivity.pm +++ b/extensions/BMO/lib/Reports/UserActivity.pm @@ -25,12 +25,12 @@ sub report { my @who = (); my $from = trim($input->{'from'} || ''); - my $to = trim($input->{'to'} || ''); + my $to = trim($input->{'to'} || ''); my $action = $input->{'action'} || ''; # fix non-breaking hyphens $from =~ s/\N{U+2011}/-/g; - $to =~ s/\N{U+2011}/-/g; + $to =~ s/\N{U+2011}/-/g; if ($from eq '') { my $dt = DateTime->now()->subtract('weeks' => 1); diff --git a/extensions/BugModal/lib/ActivityStream.pm b/extensions/BugModal/lib/ActivityStream.pm index a852fc3f30..4723c907c5 100644 --- a/extensions/BugModal/lib/ActivityStream.pm +++ b/extensions/BugModal/lib/ActivityStream.pm @@ -215,7 +215,8 @@ sub _add_activities_to_stream { : Bugzilla::Bug::GetBugActivity($bug->id); # allow other extensions to alter history - Bugzilla::Hook::process('inline_history_activity', {activity => $raw_activity}); + Bugzilla::Hook::process('inline_history_activity', + {activity => $raw_activity}); my %attachment_cache; foreach my $attachment (@{$bug->attachments}) { @@ -259,7 +260,7 @@ sub _add_activities_to_stream { my $column = $dbh->bz_column_info($table, $field); if ($column && $column->{TYPE} eq 'BOOLEAN') { $change->{removed} = ''; - $change->{added} = $change->{added} ? 'true' : 'false'; + $change->{added} = $change->{added} ? 'true' : 'false'; } # load field object (only required for custom fields), and set the @@ -271,8 +272,7 @@ sub _add_activities_to_stream { } # identify buglist changes - if ($change->{fieldname} - =~ /^(?:dependson|blocked|regress(?:ed_by|es)|dup_id|duplicates)$/ + if ($change->{fieldname} =~ /^(?:dependson|blocked|regress(?:ed_by|es)|dup_id|duplicates)$/ || ($field_obj && $field_obj->type == FIELD_TYPE_BUG_ID)) { $change->{buglist} = 1; diff --git a/extensions/TagNewUsers/Extension.pm b/extensions/TagNewUsers/Extension.pm index 214c99c3f9..e56b1a6bf6 100644 --- a/extensions/TagNewUsers/Extension.pm +++ b/extensions/TagNewUsers/Extension.pm @@ -204,7 +204,7 @@ sub _is_new { $self->{is_new} = 0; } else { - $self->{is_new} = (($self->comment_count // 0) <= COMMENT_COUNT) + $self->{is_new} = (( $self->comment_count // 0) <= COMMENT_COUNT) || ($self->creation_age <= PROFILE_AGE); } } diff --git a/extensions/UserProfile/lib/Util.pm b/extensions/UserProfile/lib/Util.pm index 96bf80bc83..1a97086010 100644 --- a/extensions/UserProfile/lib/Util.pm +++ b/extensions/UserProfile/lib/Util.pm @@ -402,7 +402,7 @@ sub _set_stats { if ($statistics->{$name}->{id}) { $dbh->do( "UPDATE $table SET count = ? WHERE user_id = ? AND $name_field = ?", - undef, $statistics->{$name}->{count}, + undef, $statistics->{$name}->{count}, $user_id, $name, ); } diff --git a/qa/t/2_test_flags.t b/qa/t/2_test_flags.t index 8d9a8a9888..877b609a24 100644 --- a/qa/t/2_test_flags.t +++ b/qa/t/2_test_flags.t @@ -474,14 +474,13 @@ ok(scalar(@flag_states) == 1 && $flag_states[0] eq '+', # Powerless users cannot set the flag to +, but setting it to ? is allowed. @flag_states = $sel->get_select_options("flag_type-$flagtype1_id"); -ok(scalar @flag_states == 2, "Two flag states available"); +ok(scalar @flag_states == 2, "Two flag states available"); ok(grep($_ eq '?', @flag_states), "Flag state '?' available"); # A powerless user cannot edit someone else's attachment flags. $sel->click_ok( "//a[contains(\@href,'/attachment.cgi?id=$attachment2_id&action=edit')]"); - # Wait a sec before the attachment overlay is loaded. sleep(1); $sel->is_element_present_ok( @@ -531,7 +530,6 @@ log_in($sel, $config, 'admin'); go_to_bug($sel, $bug1_id); $sel->click_ok( "//a[contains(\@href,'/attachment.cgi?id=${attachment3_id}&action=edit')]"); - # Wait a sec before the attachment overlay is loaded. sleep(1); $sel->is_element_present_ok( @@ -539,7 +537,6 @@ $sel->is_element_present_ok( ); $sel->select_ok('//select[@title="attachmentflag1"]', "label=+"); $sel->click_ok('//dialog[@id="att-overlay"]//input[@type="submit"]'); - # Wait a sec before the attachment is updated. sleep(1); $sel->wait_for_page_to_load_ok(WAIT_TIME); diff --git a/t/bmo/bugmail-flag-events.t b/t/bmo/bugmail-flag-events.t index 7356ecae76..0c8ab88515 100644 --- a/t/bmo/bugmail-flag-events.t +++ b/t/bmo/bugmail-flag-events.t @@ -100,8 +100,8 @@ sub add_activity { sub flag_type { my ($name, $target_type, @cc) = @_; my ($id) - = $dbh->selectrow_array('SELECT id FROM flagtypes WHERE name = ?', undef, - $name); + = $dbh->selectrow_array('SELECT id FROM flagtypes WHERE name = ?', + undef, $name); return $id if $id; $dbh->do( 'INSERT INTO flagtypes (name, description, cc_list, target_type, @@ -165,15 +165,13 @@ sub sent_mails { = map { $next_flag_id++ } 1 .. 6; # Before the window. - add_activity($bug, $granted, '1999-12-31 23:00:00', '?', $requester, - $requestee); + add_activity($bug, $granted, '1999-12-31 23:00:00', '?', $requester, $requestee); add_activity($bug, $regranted, '1999-12-31 23:00:00', '+', $requestee); # In the window. - add_activity($bug, $asked, '2000-01-01 00:00:10', '?', $requester, $requestee); - add_activity($bug, $granted, '2000-01-01 00:00:20', '+', $requestee); - add_activity($bug, $auto_cleared, '2000-01-01 00:00:30', - '?', $requester, $requestee); + add_activity($bug, $asked, '2000-01-01 00:00:10', '?', $requester, $requestee); + add_activity($bug, $granted, '2000-01-01 00:00:20', '+', $requestee); + add_activity($bug, $auto_cleared, '2000-01-01 00:00:30', '?', $requester, $requestee); add_activity($bug, $auto_cleared, '2000-01-01 00:00:30', 'X', $requestee); add_activity($bug, $regranted, '2000-01-01 00:00:40', 'X', $requester); add_activity($bug, $no_requestee, '2000-01-01 00:00:50', '?', $requester); @@ -188,20 +186,16 @@ sub sent_mails { is_deeply( [map { [$_->{action}, $_->{status}] } @events], [ - ['requested', '?'], - ['answered', '+'], - ['requested', '?'], - ['answered', 'X'], - ['set', 'X'], - ['set', '?'], + ['requested', '?'], ['answered', '+'], ['requested', '?'], + ['answered', 'X'], ['set', 'X'], ['set', '?'], ], 'one event per in-window row, classified' ); is($events[0]{requestee_id}, $requestee->id, 'a request carries its requestee'); - is($events[1]{requester_id}, - $requester->id, "an answer finds its requester from a '?' before the window"); - is($events[3]{requester_id}, - $requester->id, "an 'X' auto-clear in the same second as its '?' is an answer"); + is($events[1]{requester_id}, $requester->id, + "an answer finds its requester from a '?' before the window"); + is($events[3]{requester_id}, $requester->id, + "an 'X' auto-clear in the same second as its '?' is an answer"); ok(!$events[4]{requester_id}, 'clearing an already granted flag has no requester to notify'); } @@ -209,29 +203,26 @@ sub sent_mails { # Send: a request reaches the requestee and the flag type cc_list only. { my $bug = new_bug(); - add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', - '?', $requester, $requestee); + add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', '?', $requester, + $requestee); my $sent = sent_to($bug, $requester); ok($sent->{$requestee->login}, 'requestee with no role on the bug is mailed'); ok($sent->{$type_cc->login}, 'flag type cc_list account is mailed'); - ok(!$sent->{$requester->login}, - 'requester is not mailed about their own request'); - ok(!$sent->{$stranger->login}, 'unrelated user is not mailed'); + ok(!$sent->{$requester->login}, 'requester is not mailed about their own request'); + ok(!$sent->{$stranger->login}, 'unrelated user is not mailed'); } # Send: an answer reaches the requester, not the person who answered. { my $bug = new_bug(); my $flag_id = $next_flag_id++; - add_activity($bug, $flag_id, '1999-12-31 23:00:00', '?', $requester, - $requestee); + add_activity($bug, $flag_id, '1999-12-31 23:00:00', '?', $requester, $requestee); add_activity($bug, $flag_id, '2000-01-01 00:00:10', '+', $requestee); my $sent = sent_to($bug, $requestee); ok($sent->{$requester->login}, 'requester is told their request was answered'); - ok(!$sent->{$requestee->login}, - 'answerer is not mailed about their own answer'); + ok(!$sent->{$requestee->login}, 'answerer is not mailed about their own answer'); } # An answer to a flag re-requested by someone else goes to the latest @@ -240,20 +231,20 @@ my $requester2 = create_user('flagmail-requester2@mozilla.example', '*'); foreach my $later_first (0, 1) { my $bug = new_bug(); my $flag_id = $next_flag_id++; - my @asks - = (['1999-12-31 23:00:00', $requester], ['1999-12-31 23:30:00', $requester2], - ); + my @asks = ( + ['1999-12-31 23:00:00', $requester], + ['1999-12-31 23:30:00', $requester2], + ); @asks = reverse @asks if $later_first; add_activity($bug, $flag_id, $_->[0], '?', $_->[1], $requestee) for @asks; add_activity($bug, $flag_id, '2000-01-01 00:00:10', '+', $requestee); - my $order - = $later_first ? 'later request inserted first' : 'inserted in time order'; + my $order = $later_first ? 'later request inserted first' : 'inserted in time order'; my @events = Bugzilla::BugMail::_get_flag_mail_events($bug, $window_start, $window_end, {}); - is($events[0]{requester_id}, - $requester2->id, "an answer goes to the latest requester ($order)"); + is($events[0]{requester_id}, $requester2->id, + "an answer goes to the latest requester ($order)"); my $sent = sent_to($bug, $requestee); ok($sent->{$requester2->login}, "the latest requester is mailed ($order)"); @@ -264,63 +255,52 @@ foreach my $later_first (0, 1) { # old flag mail had, whether the flag was requested or answered. { my $bug = new_bug(); - add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', - '?', $requester, $requestee); - my ($mail) - = grep { $_->{to_user}->id == $requestee->id } sent_mails($bug, $requester); - is( - $mail->{bugmailtype}, - 'changed request', - 'a request is marked as request mail' - ); + add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', '?', $requester, + $requestee); + my ($mail) = grep { $_->{to_user}->id == $requestee->id } + sent_mails($bug, $requester); + is($mail->{bugmailtype}, 'changed request', 'a request is marked as request mail'); $bug = new_bug(); my $flag_id = $next_flag_id++; - add_activity($bug, $flag_id, '1999-12-31 23:00:00', '?', $requester, - $requestee); + add_activity($bug, $flag_id, '1999-12-31 23:00:00', '?', $requester, $requestee); add_activity($bug, $flag_id, '2000-01-01 00:00:10', '+', $requestee); - ($mail) - = grep { $_->{to_user}->id == $requester->id } sent_mails($bug, $requestee); - is( - $mail->{bugmailtype}, - 'changed request', - 'an answer is marked as request mail' - ); + ($mail) = grep { $_->{to_user}->id == $requester->id } + sent_mails($bug, $requestee); + is($mail->{bugmailtype}, 'changed request', 'an answer is marked as request mail'); } # Send: cancelling your own request doesn't mail you. { my $bug = new_bug(); my $flag_id = $next_flag_id++; - add_activity($bug, $flag_id, '1999-12-31 23:00:00', '?', $requester, - $requestee); + add_activity($bug, $flag_id, '1999-12-31 23:00:00', '?', $requester, $requestee); add_activity($bug, $flag_id, '2000-01-01 00:00:10', 'X', $requester); my $sent = sent_to($bug, $requester); - ok(!$sent->{$requester->login}, - 'requester is not mailed for cancelling their own request'); - ok($sent->{$type_cc->login}, 'flag type cc_list account is still mailed'); + ok(!$sent->{$requester->login}, 'requester is not mailed for cancelling their own request'); + ok($sent->{$type_cc->login}, 'flag type cc_list account is still mailed'); } # Send: someone mailed only because of a flag gets the flag section and the # comments (so a requestee sees the question), but no diffs. { my $bug = new_bug(); - add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', - '?', $requester, $requestee); - my ($mail) - = grep { $_->{to_user}->id == $requestee->id } sent_mails($bug, $requester); + add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', '?', $requester, + $requestee); + my ($mail) = grep { $_->{to_user}->id == $requestee->id } + sent_mails($bug, $requester); - is(scalar @{$mail->{flag_events}}, - 1, 'requestee with no role on the bug gets the flag event'); + is(scalar @{$mail->{flag_events}}, 1, + 'requestee with no role on the bug gets the flag event'); ok(scalar @{$mail->{new_comments}}, 'and the comments'); ok(!@{$mail->{diffs}}, 'but no diffs'); $bug = new_bug(cc => [$requestee->login]); - add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', - '?', $requester, $requestee); - ($mail) - = grep { $_->{to_user}->id == $requestee->id } sent_mails($bug, $requester); + add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', '?', $requester, + $requestee); + ($mail) = grep { $_->{to_user}->id == $requestee->id } + sent_mails($bug, $requester); ok(scalar @{$mail->{new_comments}}, 'requestee who is also CCed gets the comments too'); @@ -336,14 +316,14 @@ foreach my $later_first (0, 1) { {}); my $sent = sent_to($bug, $requester); - ok($sent->{$type_cc->login}, - "a '?' with no requestee reaches the flag type cc_list"); + ok($sent->{$type_cc->login}, "a '?' with no requestee reaches the flag type cc_list"); ok(!$sent->{$requester->login} && !$sent->{$requestee->login}, 'and no requestee or requester'); - my ($accounts, $raw) = Bugzilla::BugMail::_get_flag_type_cc($bug, \@events, {}); + my ($accounts, $raw) + = Bugzilla::BugMail::_get_flag_type_cc($bug, \@events, {}); ok($accounts->{$type_cc->id}, 'cc_list account is an account recipient'); - ok($raw->{$raw_cc}, 'cc_list address with no account takes the raw mail path'); + ok($raw->{$raw_cc}, 'cc_list address with no account takes the raw mail path'); } # The cc_list address with no account gets the new public comments, as with @@ -407,8 +387,8 @@ foreach my $later_first (0, 1) { my $raw = 'flagmail-insider-list@mozilla.example'; my $group = group_with('flagmail-insiders', $insider); local Bugzilla->params->{insidergroup} = $group->name; - my $private_type - = flag_type('flagmail-private', 'a', $insider->login, $outsider->login, $raw); + my $private_type = flag_type('flagmail-private', 'a', $insider->login, + $outsider->login, $raw); my $bug = new_bug(); $dbh->do( @@ -419,14 +399,11 @@ foreach my $later_first (0, 1) { $bug->id, $window_start, $window_start, $requester->id ); my $attach_id = $dbh->bz_last_key('attachments', 'attach_id'); - add_activity( - $bug, $next_flag_id++, '2000-01-01 00:00:10', '?', $requester, undef, - type_id => $private_type, - attachment_id => $attach_id - ); + add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', '?', $requester, + undef, type_id => $private_type, attachment_id => $attach_id); my ($accounts, $raw_addresses) = flag_type_cc($bug); - ok($accounts->{$insider->id}, 'private attachment flag: insider is mailed'); + ok($accounts->{$insider->id}, 'private attachment flag: insider is mailed'); ok(!$accounts->{$outsider->id}, 'non-insider is not'); ok(!$raw_addresses->{$raw}, 'address with no account is not'); } @@ -437,18 +414,18 @@ foreach my $later_first (0, 1) { my $non_member = create_user('flagmail-nonmember@mozilla.example', '*'); my $raw = 'flagmail-group-list@mozilla.example'; my $group = group_with('flagmail-secure', $member); - my $secure_type - = flag_type('flagmail-secure', 'b', $member->login, $non_member->login, $raw); + my $secure_type = flag_type('flagmail-secure', 'b', $member->login, + $non_member->login, $raw); my $bug = new_bug(); $dbh->do('INSERT INTO bug_group_map (bug_id, group_id) VALUES (?, ?)', undef, $bug->id, $group->id); $bug = Bugzilla::Bug->new($bug->id); - add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', - '?', $requester, undef, type_id => $secure_type); + add_activity($bug, $next_flag_id++, '2000-01-01 00:00:10', '?', $requester, + undef, type_id => $secure_type); my ($accounts, $raw_addresses) = flag_type_cc($bug); - ok($accounts->{$member->id}, 'bug in a group: member is mailed'); + ok($accounts->{$member->id}, 'bug in a group: member is mailed'); ok(!$accounts->{$non_member->id}, 'non-member is not'); ok(!$raw_addresses->{$raw}, 'address with no account is not'); } From 7f1ecfc277aabd8d1ed2bff8ff0f8d991246f092 Mon Sep 17 00:00:00 2001 From: Xavier L'Hour Date: Fri, 13 Nov 2026 15:00:58 +0100 Subject: [PATCH 50/50] Bug 2085398 - Load the last flag_activity row of all the bug's flags in one query in the activity stream --- extensions/BugModal/lib/ActivityStream.pm | 14 +++++++---- t/flag-activity-stream.t | 30 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/extensions/BugModal/lib/ActivityStream.pm b/extensions/BugModal/lib/ActivityStream.pm index 4723c907c5..06b52f4774 100644 --- a/extensions/BugModal/lib/ActivityStream.pm +++ b/extensions/BugModal/lib/ActivityStream.pm @@ -103,11 +103,15 @@ sub find_activity_id_for_flag { # flag_activity records each change against the flag's object id, so we # can look up the exact timestamp without relying on flag-type name matching. - my $dbh = Bugzilla->dbh; - my ($flag_when, $who) = $dbh->selectrow_array( - "SELECT flag_when, who FROM flag_activity WHERE flag_id = ? - ORDER BY id DESC " . $dbh->sql_limit(1), undef, $flag_id - ); + # The last row of every flag of the bug is loaded in one query. + if (!$self->{flag_activity_last}) { + my $rows = Bugzilla->dbh->selectall_arrayref( + 'SELECT flag_id, flag_when, who FROM flag_activity + WHERE bug_id = ? ORDER BY id', undef, $self->id + ); + $self->{flag_activity_last} = {map { $_->[0] => [$_->[1], $_->[2]] } @$rows}; + } + my ($flag_when, $who) = @{$self->{flag_activity_last}->{$flag_id} // []}; # flag_activity only logged review, feedback and needinfo before, so other # flags may have no row: match them as before, on the setter, the diff --git a/t/flag-activity-stream.t b/t/flag-activity-stream.t index fa9b14065c..91ea019fd1 100644 --- a/t/flag-activity-stream.t +++ b/t/flag-activity-stream.t @@ -77,4 +77,34 @@ like(flag_activity_id(0), qr/^a\d+_\d+$/, like(flag_activity_id(1), qr/^a\d+_\d+$/, 'a flag without flag_activity rows is linked to its bugs_activity change'); +# Two flags of the same bug, set at different times, are each linked to their +# own change. The cutoff set by the case above is moved back, so that both +# changes are read from flag_activity. +{ + set_flag_activity_cutoff('2000-01-01 00:00:00'); + Bugzilla->set_user(Bugzilla::User->super_user); + my $bug_id = create_bug( + short_desc => 'Flag activity stream test bug, two flags', + comment => 'Bug for the flag activity stream tests', + bug_type => 'defect', + assigned_to => 'nobody@mozilla.org', + )->id; + + Bugzilla->set_user($setter); + foreach my $when ('2099-01-01 10:00:00', '2099-01-02 10:00:00') { + my $bug = Bugzilla::Bug->new($bug_id); + $bug->set_flags([], + [{type_id => $needinfo_id, status => '?', requestee => 'bob@bmo.test'}]); + $bug->update($when); + } + + my $bug = Bugzilla::Bug->new($bug_id); + my @ids = map { $bug->find_activity_id_for_flag($_) } + sort { $a->id <=> $b->id } @{$bug->flags}; + is(scalar @ids, 2, 'the bug has two flags'); + like($ids[0], qr/^a\d+_\d+$/, 'the first flag is linked to a change'); + like($ids[1], qr/^a\d+_\d+$/, 'the second flag is linked to a change'); + isnt($ids[0], $ids[1], 'each flag is linked to its own change'); +} + done_testing;