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..e484c521e0 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; @@ -1221,6 +1222,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] : ''; @@ -4556,29 +4558,25 @@ 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 + 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 + 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) + AND $flag_newer) 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}; @@ -4854,9 +4852,25 @@ 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'); + 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. @@ -4879,6 +4893,7 @@ sub GetBugActivity { INNER JOIN profiles ON profiles.userid = bugs_activity.who WHERE bugs_activity.bug_id = ? + AND $ba_before_cutoff $attachpart $suppwhere "; @@ -4916,6 +4931,50 @@ sub GetBugActivity { push @args, $bug_id; } + my $flag_bug_when = $dbh->sql_date_format('fsa.flag_when', '%Y-%m-%d %H:%i:%s'); + 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, + fsa.id 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 $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 $flag_added_req + ELSE $flag_added + END AS added, + changer.login_name AS who, + NULL AS comment_id + FROM flag_activity fsa + 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.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 + "; + push @args, $bug_id; + push @args, $attach_id if $attach_id; + $query .= ") AS OUTER_TABLE"; # Only consider changes since $starttime, if given. diff --git a/Bugzilla/BugMail.pm b/Bugzilla/BugMail.pm index ec35d20907..bfcd5afc6b 100644 --- a/Bugzilla/BugMail.pm +++ b/Bugzilla/BugMail.pm @@ -18,6 +18,7 @@ use Bugzilla::Util; use Bugzilla::Bug; use Bugzilla::Attachment; use Bugzilla::Comment; +use Bugzilla::FlagActivity; use Bugzilla::FlagType; use Bugzilla::Logging; use Bugzilla::Mailer; @@ -1044,12 +1045,25 @@ 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->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_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 = ''; + 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); } @@ -1058,15 +1072,50 @@ 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 WHERE bugs_activity.bug_id = ? + AND $ba_before_cutoff $when_restriction - ORDER BY bugs_activity.bug_when, fielddefs.description", {Slice => {}}, - @args + 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 $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 $flag_new_req + ELSE $flag_new + END AS new, + fsa.attachment_id AS attach_id, + NULL AS comment_id, + 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.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, 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/Bugzilla/DB/Schema.pm b/Bugzilla/DB/Schema.pm index 77cf05127a..d77d05b69c 100644 --- a/Bugzilla/DB/Schema.pm +++ b/Bugzilla/DB/Schema.pm @@ -748,9 +748,10 @@ use constant ABSTRACT_SCHEMA => { REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}, }, # The user who made the change. Not always the setter: redirecting a - # request keeps the original setter. Nullable until all pods write it. - who => { + # request keeps the original setter. + who => { TYPE => 'INT3', + NOTNULL => 1, REFERENCES => {TABLE => 'profiles', COLUMN => 'userid'}, }, requestee_id => { @@ -777,6 +778,12 @@ 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/DB/Sqlite.pm b/Bugzilla/DB/Sqlite.pm index df774a663f..32f3851ccc 100644 --- a/Bugzilla/DB/Sqlite.pm +++ b/Bugzilla/DB/Sqlite.pm @@ -227,6 +227,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/Bugzilla/Flag.pm b/Bugzilla/Flag.pm index 7779512597..a350cc0181 100644 --- a/Bugzilla/Flag.pm +++ b/Bugzilla/Flag.pm @@ -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; diff --git a/Bugzilla/FlagActivity.pm b/Bugzilla/FlagActivity.pm index 4ed09aa76d..88bf7b9187 100644 --- a/Bugzilla/FlagActivity.pm +++ b/Bugzilla/FlagActivity.pm @@ -12,7 +12,8 @@ use strict; use warnings; use Bugzilla::Error; -use Bugzilla::Util qw(trim datetime_from); +use Bugzilla::Field qw(get_field_id); +use Bugzilla::Util qw(trim datetime_from); use List::MoreUtils qw(none); use base qw( Bugzilla::Object ); @@ -85,6 +86,61 @@ sub requestee_id { return $_[0]->{requestee_id} } sub attachment_id { return $_[0]->{attachment_id} } sub status { return $_[0]->{status} } +# 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) = @_; + 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 +# 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/Bugzilla/FlagType.pm b/Bugzilla/FlagType.pm index bf09a72e89..c162495ae8 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,25 @@ 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/Bugzilla/Install/DB.pm b/Bugzilla/Install/DB.pm index 71e5300dee..cf927bf593 100644 --- a/Bugzilla/Install/DB.pm +++ b/Bugzilla/Install/DB.pm @@ -754,8 +754,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'}); @@ -852,9 +851,18 @@ sub update_table_definitions { # Before _migrate_flag_state_activity, which copies into this column. _add_flag_activity_who(); + # Bug 2085398 - xavier.lhour@gmail.com + # After _add_flag_activity_who, which fills the rows without it. + _set_flag_activity_who_not_null(); + # 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(); @@ -4332,14 +4340,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 @@ -4579,8 +4584,46 @@ sub _migrate_flag_state_activity { $dbh->bz_drop_table('flag_state_activity'); } -# 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. +# 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 is older than the cutoff. +sub _add_flag_activity_start_rows { + my $dbh = Bugzilla->dbh; + + # 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; + + 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 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 + 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; + + # See Bugzilla::FlagActivity->cutoff. + $dbh->do( + 'INSERT INTO flag_activity_migration (cutoff) VALUES (LOCALTIMESTAMP(0))'); +} + +# 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; @@ -4597,6 +4640,25 @@ sub _add_flag_activity_who { $dbh->do('UPDATE flag_activity SET who = setter_id WHERE who IS NULL'); } +# Every pod writes who since bug 2088705 was deployed. +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', + 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 5247e51f06..487a7a58c3 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; @@ -285,8 +286,16 @@ use constant OPERATOR_FIELD_OVERRIDE => { dependson => MULTI_SELECT_OVERRIDE, duplicates => MULTI_SELECT_OVERRIDE, keywords => MULTI_SELECT_OVERRIDE, - 'flagtypes.name' => {_non_changed => \&_flagtypes_nonchanged,}, - longdesc => { + '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, changedbefore => \&_long_desc_changedbefore_after, @@ -3145,38 +3154,35 @@ 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 @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"; + my $where = "$alias.who = $owner AND $alias.$when > $quoted"; - 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"] - }; - - push(@$joins, $comments_join, $activity_join); - - if ($operator =~ /greater/) { - $args->{term} = "$ld_table.who IS NULL AND $act_table.who IS NULL"; - } - else { - $args->{term} = "($ld_table.who IS NOT NULL OR $act_table.who IS NOT NULL)"; + # 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); + + $args->{term} = $operator =~ /greater/ ? "NOT ($active)" : "($active)"; } sub _multiselect_negative { @@ -3663,6 +3669,14 @@ 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 { @@ -3690,6 +3704,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 { @@ -3721,6 +3745,17 @@ 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", + Bugzilla::FlagActivity->after_cutoff_sql($fa) + ) . ')'; + } } sub _changed_security_check { @@ -3745,6 +3780,100 @@ sub _changed_security_check { } } +# Flag changes are recorded in flag_activity. Older changes are only in +# bugs_activity, so match either. +sub _flagtypes_changed { + my ($self, $args) = @_; + my $dbh = Bugzilla->dbh; + $self->_convert_everchanged($args) if $args->{operator} eq 'everchanged'; + 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; + + 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.who = $user_id", + Bugzilla::FlagActivity->after_cutoff_sql($fa); + } + else { # changedto, changedfrom + my $match = _flag_state_changed_sql($chart_id, $fa, $operator, $value) + or return; + push @where, $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"; + } + return + "EXISTS (SELECT 1 FROM flag_activity $fa WHERE " + . join(' AND ', @where) . ')'; +} + +# 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) = @_; + + # As in history, a request counts as one net change. + 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 $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 "$flag_last AND EXISTS ( + SELECT 1 FROM flag_activity $prev + 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; + 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/Bugzilla/Test/Util.pm b/Bugzilla/Test/Util.pm index d5b6a938c4..9d8109967c 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,18 @@ 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/editusers.cgi b/editusers.cgi index 9446ff2922..f14122a440 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; @@ -390,8 +391,18 @@ 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 = ?', + + # 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( + 'SELECT COUNT(*) FROM flag_activity WHERE requestee_id = ?', undef, $otherUserID); $vars->{'component_cc'} = $dbh->selectrow_array('SELECT COUNT(*) FROM component_cc WHERE user_id = ?', @@ -521,13 +532,20 @@ 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 => $flag->setter_id, + who => $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; diff --git a/extensions/BMO/lib/Reports/ReleaseTracking.pm b/extensions/BMO/lib/Reports/ReleaseTracking.pm index fa798a237f..2345f15894 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); @@ -195,29 +196,33 @@ 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. + # 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 = ? + 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.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}, + @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..f7e4c1fccd 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; @@ -55,6 +56,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 +65,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 +99,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,7 +114,22 @@ sub report { $comment_filter = 'AND longdescs.isprivate = 0'; } - my $query = " + # 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 $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_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, bugs_activity.bug_id, @@ -130,10 +151,51 @@ 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 + 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 = CHAR(63) AND preq.login_name IS NOT NULL + 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 $flag_added_req + ELSE $flag_added + 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.who + LEFT JOIN profiles req + ON req.userid = fsa.requestee_id + LEFT JOIN flag_activity prev + 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 + SELECT 'comment_tag' AS name, longdescs_tags_activity.bug_id, @@ -243,6 +305,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' diff --git a/extensions/BugModal/lib/ActivityStream.pm b/extensions/BugModal/lib/ActivityStream.pm index 5745511763..06b52f4774 100644 --- a/extensions/BugModal/lib/ActivityStream.pm +++ b/extensions/BugModal/lib/ActivityStream.pm @@ -96,35 +96,48 @@ 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; - - # unfortunately bugs_activity treats all flag changes as the same field, so - # we don't have an object_id to match on + 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. + # 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 + # 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); + } - 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 $activity->{who}->id == $who && $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'; + next if defined $flagtype_name && $change->{flagtype_name} ne $flagtype_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 + $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 diff --git a/extensions/Needinfo/t/bmo/mentions-on-create.t b/extensions/Needinfo/t/bmo/mentions-on-create.t index 8a578ba77e..916aa326e4 100644 --- a/extensions/Needinfo/t/bmo/mentions-on-create.t +++ b/extensions/Needinfo/t/bmo/mentions-on-create.t @@ -203,13 +203,12 @@ is_deeply( is_deeply(cc_list($bug_id), [$alice->login, $bob->login], '... and CC them'); my ($creation_ts, $flag_when) = $dbh->selectrow_array( - "SELECT bugs.creation_ts, bugs_activity.bug_when + "SELECT bugs.creation_ts, flag_activity.flag_when FROM bugs - INNER JOIN bugs_activity ON bugs_activity.bug_id = bugs.bug_id - INNER JOIN fielddefs ON fielddefs.id = bugs_activity.fieldid - WHERE bugs.bug_id = ? AND fielddefs.name = 'flagtypes.name' - AND bugs_activity.added LIKE 'needinfo?%' " . $dbh->sql_limit(1), - undef, $bug_id + INNER JOIN flag_activity ON flag_activity.bug_id = bugs.bug_id + WHERE bugs.bug_id = ? AND flag_activity.type_id = ? + AND flag_activity.status = ? " . $dbh->sql_limit(1), undef, $bug_id, + $needinfo->id, '?' ); ok($flag_when, 'the needinfo requests are in the bug activity'); is($flag_when, $creation_ts, '... at the creation time, like form flags'); diff --git a/extensions/TagNewUsers/Extension.pm b/extensions/TagNewUsers/Extension.pm index ba8f294469..e56b1a6bf6 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..1a97086010 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) = @_; @@ -118,7 +119,8 @@ EOF EOF # bugs touched - _update_statistics($statistics, 'touched', [$user_id, $user_id], <after_cutoff_sql('flag_activity'); + _update_statistics($statistics, 'touched', [($user_id) x 3], <dbh; # get a list of all users associated with this bug - my $user_ids - = $dbh->selectcol_arrayref(<selectcol_arrayref(<after_cutoff_sql('flag_activity'); return Bugzilla->dbh->selectrow_array( - < ? + UNION ALL + SELECT MAX(flag_when) AS bug_when + FROM flag_activity + WHERE who = ? AND $flag_newer ) tmp EOF } @@ -281,13 +297,34 @@ sub _activity_by_product { my %products; - # changes + # changes, without the flag changes after the cutoff, counted below + 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 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 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->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); + 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 + 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/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/qa/t/4_test_user_delete_requestee.t b/qa/t/4_test_user_delete_requestee.t new file mode 100644 index 0000000000..82e961d46f --- /dev/null +++ b/qa/t/4_test_user_delete_requestee.t @@ -0,0 +1,56 @@ +# 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/t/bmo/bugmail-flag-events.t b/t/bmo/bugmail-flag-events.t index 4f13367206..0c8ab88515 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); @@ -86,10 +87,10 @@ sub new_bug { sub add_activity { my ($bug, $flag_id, $when, $status, $setter, $requestee, %extra) = @_; $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, attachment_id, status) - VALUES (?, ?, ?, ?, ?, ?, ?, ?)', undef, $when, - $extra{type_id} // $type_id, $flag_id, $setter->id, + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', undef, $when, + $extra{type_id} // $type_id, $flag_id, $setter->id, $setter->id, $requestee ? $requestee->id : undef, $bug->id, $extra{attachment_id}, $status ); } @@ -352,6 +353,33 @@ foreach my $later_first (0, 1) { unlike($raw[0], qr/private reasons/, 'but not the private ones'); } +# 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); + $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); + 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' + ); + + set_flag_activity_cutoff($cutoff); +} + # The cc_list of a flag on a private attachment only reaches insiders. { my $insider = create_user('flagmail-insider@mozilla.example', '*'); diff --git a/t/flag-activity-history.t b/t/flag-activity-history.t new file mode 100644 index 0000000000..6853b23107 --- /dev/null +++ b/t/flag-activity-history.t @@ -0,0 +1,151 @@ +#!/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 set_flag_activity_cutoff); +use Bugzilla::Bug; +use Bugzilla::BugMail; +use Bugzilla::User; +use Test2::V0; + +# 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); +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( + 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( + "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 ($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, + status) + VALUES (?, ?, ?, ?, ?, ?, ?, ?)', undef, $when, $review_id, $flag_id, + $who->id, $who->id, $requestee_id, $bug_id, $status + ); +} + +# [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; + 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), + '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)', ''], + ['2020-01-03 10:00:00', '', 'review+'], + ) +{ + $dbh->do( + 'INSERT INTO bugs_activity (bug_id, who, bug_when, fieldid, removed, added) + 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 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)'], + ['setter@bmo.test', 'review?(bob@bmo.test)', ''], + ['setter@bmo.test', '', 'review+'], + ['alice@bmo.test', 'review+', 'review-'], +]; +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'); + +# 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; diff --git a/t/flag-activity-search.t b/t/flag-activity-search.t new file mode 100644 index 0000000000..73fe45479d --- /dev/null +++ b/t/flag-activity-search.t @@ -0,0 +1,200 @@ +#!/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 set_flag_activity_cutoff); +use Bugzilla::Search; +use Bugzilla::User; +use Test2::V0; + +# 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); +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( + 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, who, requestee_id, bug_id, + status) + VALUES (?, ?, 42, ?, ?, ?, ?, ?)', undef, $row->[0], $review_id, $alice->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'); + +# 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'); +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'); +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' +); + +# 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 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( + short_desc => 'owned', + comment => 'Flag search test bug', + bug_type => 'defect', + assigned_to => 'alice@bmo.test', +)->id; + +sub idle { + my ($operator, $owner, $since) = @_; + $owner //= 'alice@bmo.test'; + my $search = Bugzilla::Search->new( + fields => ['bug_id'], + params => { + f1 => 'owner_idle_time', + o1 => $operator, + v1 => $since // '2025-12-01', + f2 => 'assigned_to', + o2 => 'equals', + v2 => $owner, + }, + ); + return [map { $_->[0] } @{$search->data}]; +} +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; diff --git a/t/flag-activity-start-rows.t b/t/flag-activity-start-rows.t new file mode 100644 index 0000000000..79bd6701f1 --- /dev/null +++ b/t/flag-activity-start-rows.t @@ -0,0 +1,151 @@ +#!/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 set_flag_activity_cutoff); +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)' +); + +# checksetup already ran on this database: forget it, so that it walks the +# flags again. +sub add_start_rows { + $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 + ), + 1, + 'one starting row, however many times checksetup runs' +); + +sub flag_history { + my ($activity) = Bugzilla::Bug::GetBugActivity($bug_id); + my @history; + foreach my $operation (@$activity) { + push @history, + map {"$operation->{when}: $_->{removed} -> $_->{added}"} + grep { $_->{fieldname} eq 'flagtypes.name' } @{$operation->{changes}}; + } + return \@history; +} + +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' +); + +# 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); + +add_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' +); + +# 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( + 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; diff --git a/t/flag-activity-stream.t b/t/flag-activity-stream.t new file mode 100644 index 0000000000..91ea019fd1 --- /dev/null +++ b/t/flag-activity-stream.t @@ -0,0 +1,110 @@ +#!/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 set_flag_activity_cutoff); +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) { + 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) + 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'); + +# 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; diff --git a/t/flag-activity-user-stats.t b/t/flag-activity-user-stats.t new file mode 100644 index 0000000000..d88440edeb --- /dev/null +++ b/t/flag-activity-user-stats.t @@ -0,0 +1,163 @@ +#!/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 set_flag_activity_cutoff); +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'); + +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'); + +# 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'"); +$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'); + +# 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, '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' +); + +# 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; diff --git a/t/flag-activity-who.t b/t/flag-activity-who.t index 2dc6109712..b58383729b 100644 --- a/t/flag-activity-who.t +++ b/t/flag-activity-who.t @@ -18,15 +18,14 @@ 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; use Test2::V0; -# Redirecting a request keeps the original setter, but flag_activity.who -# records the user who redirected it. The column stays nullable while pods on -# the previous code still insert rows without it. +# 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); @@ -35,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', @@ -75,22 +75,28 @@ is( 'who is the user who made each change' ); -# A pod on the previous code inserts a row without who: it's saved, and the -# next checksetup fills it with the setter. -$dbh->do( - 'INSERT INTO flag_activity (flag_when, type_id, flag_id, setter_id, bug_id, - status) - VALUES (?, ?, ?, ?, ?, ?)', undef, '2026-01-01 12:00:00', $needinfo_id, - $flag->id, $requester->id, $bug_id, 'X' -); -Bugzilla::Install::DB::_add_flag_activity_who(); -my ($who) - = $dbh->selectrow_array( - "SELECT who FROM flag_activity WHERE flag_id = ? AND status = 'X'", +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'); + +# A database left by bug 2088705: who nullable, with rows that pods on the +# older code inserted without it. The next checksetup fills them and makes the +# column NOT NULL. +$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); -is($who, $requester->id, - 'a row inserted without who gets the setter on the next checksetup'); -ok(!$dbh->bz_column_info('flag_activity', 'who')->{NOTNULL}, - 'the column stays nullable'); +Bugzilla::Install::DB->update_table_definitions({}); +is( + scalar $dbh->selectrow_array( + 'SELECT COUNT(*) FROM flag_activity WHERE who IS NULL'), + 0, + 'the rows without who are filled' +); +ok($dbh->bz_column_info('flag_activity', 'who')->{NOTNULL}, + 'and the column is NOT NULL'); done_testing; diff --git a/t/flag-type-delete.t b/t/flag-type-delete.t new file mode 100644 index 0000000000..a910109579 --- /dev/null +++ b/t/flag-type-delete.t @@ -0,0 +1,76 @@ +#!/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/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 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'} %]