diff --git a/extensions/PhabBugz/lib/Feed.pm b/extensions/PhabBugz/lib/Feed.pm index 707cc8cba5..eeb8d39218 100644 --- a/extensions/PhabBugz/lib/Feed.pm +++ b/extensions/PhabBugz/lib/Feed.pm @@ -35,8 +35,10 @@ use Bugzilla::Extension::PhabBugz::Policy; use Bugzilla::Extension::PhabBugz::Revision; use Bugzilla::Extension::PhabBugz::User; use Bugzilla::Extension::PhabBugz::Util qw( + cc_triage_owner create_revision_attachment get_bug_role_phids + get_revision_attachment is_attachment_phab_revision request set_attachment_approval_flags @@ -732,6 +734,11 @@ sub process_revision_change { # Always make sure secure-revison and proper bmo- project tags are on the revision. $revision->set_private_project_tags($set_project_names); + # When a revision is first attached to a private bug, CC the triage + # owner so they can see the revision. This is only done once so that + # removing themselves from the CC list sticks. + cc_triage_owner($bug) if !get_revision_attachment($bug, $revision); + # Subscriber list of the private revision should always match # the bug roles such as assignee, qa contact, and cc members. my $subscribers = get_bug_role_phids($bug); diff --git a/extensions/PhabBugz/lib/Util.pm b/extensions/PhabBugz/lib/Util.pm index dd17cf6dd3..1c09f9cf80 100644 --- a/extensions/PhabBugz/lib/Util.pm +++ b/extensions/PhabBugz/lib/Util.pm @@ -31,9 +31,11 @@ use Mojo::JSON qw(encode_json); use base qw(Exporter); our @EXPORT = qw( + cc_triage_owner create_revision_attachment get_attachment_revisions get_bug_role_phids + get_revision_attachment intersect is_attachment_phab_revision is_auto_assign_excluded @@ -199,23 +201,37 @@ sub set_attachment_approval_flags { $attachment->set_flags(\@old_flags, \@new_flags); } -sub create_revision_attachment { - state $check = compile(Bug, Revision, Str, User); - my ($bug, $revision, $timestamp, $submitter) = $check->(@_); - +sub _revision_uri { + my ($revision) = @_; my $phab_base_uri = Bugzilla->params->{phabricator_base_uri}; ThrowUserError('invalid_phabricator_uri') unless $phab_base_uri; + return $phab_base_uri . 'D' . $revision->id; +} + +# Returns the bug's attachment pointing at the given revision, or undef if the +# revision has not been attached to this bug yet. +sub get_revision_attachment { + state $check = compile(Bug, Revision); + my ($bug, $revision) = $check->(@_); - my $revision_uri = $phab_base_uri . "D" . $revision->id; + my $revision_uri = _revision_uri($revision); + return + first { trim($_->data) eq $revision_uri } + grep { is_attachment_phab_revision($_) } @{$bug->attachments}; +} + +sub create_revision_attachment { + state $check = compile(Bug, Revision, Str, User); + my ($bug, $revision, $timestamp, $submitter) = $check->(@_); # Check for previous attachment with same revision id. # If one matches then return it instead. This is fine as # BMO does not contain actual diff content. - my @review_attachments - = grep { is_attachment_phab_revision($_) } @{$bug->attachments}; - my $attachment = first { trim($_->data) eq $revision_uri } @review_attachments; + my $attachment = get_revision_attachment($bug, $revision); if (!defined $attachment) { + my $revision_uri = _revision_uri($revision); + # No attachment is present, so we can now create new one if (!$timestamp) { @@ -269,6 +285,37 @@ sub intersect { return grep { exists($e{$_}) } @{$list2}; } +# Adds the component triage owner to the CC list of a private bug so that they +# become a subscriber of its private revisions. Triage owners can see private +# bugs in their component only while they are members of the +# mozilla-employee-confidential group (see Bugzilla::User::visible_bugs), so +# the same restriction applies here. The CC change is not saved; the caller is +# responsible for calling $bug->update. +sub cc_triage_owner { + state $check = compile(Bug); + my ($bug) = $check->(@_); + + return if !@{$bug->groups_in}; + + my $triage_owner = $bug->component_obj->triage_owner; + return + if !$triage_owner + || !$triage_owner->is_enabled + || !$triage_owner->is_employee_confidential; + + try { + $bug->add_cc($triage_owner); + INFO('Added triage owner ' . $triage_owner->login . ' to CC list'); + } + catch { + WARN( 'Unable to add triage owner ' + . $triage_owner->login + . ' to CC list of bug ' + . $bug->id + . ": $_"); + }; +} + sub get_bug_role_phids { state $check = compile(Bug); my ($bug) = $check->(@_); @@ -338,11 +385,7 @@ sub set_bug_assignee { # created and persists across a commandeer, so it identifies the original # (possibly excluded, e.g. Hackbot) author even after the revision author # has changed. - my $revision_uri = Bugzilla->params->{phabricator_base_uri} . 'D' . $revision->id; - my $attachment = first { - is_attachment_phab_revision($_) && trim($_->data) eq $revision_uri - } - @{$bug->attachments}; + my $attachment = get_revision_attachment($bug, $revision); my $submitter = $revision->author->bugzilla_user; diff --git a/extensions/PhabBugz/t/bmo/feed-stories.t b/extensions/PhabBugz/t/bmo/feed-stories.t index c7bf454a01..b7645530c7 100644 --- a/extensions/PhabBugz/t/bmo/feed-stories.t +++ b/extensions/PhabBugz/t/bmo/feed-stories.t @@ -31,9 +31,12 @@ use Mojo::JSON qw(encode_json); use Test2::Tools::Mock qw(mock); use Test::More; +use Bugzilla::Component; use Bugzilla::Extension::PhabBugz::Constants; use Bugzilla::Extension::PhabBugz::Feed; use Bugzilla::Extension::PhabBugz::Util qw(is_attachment_phab_revision); +use Bugzilla::Group; +use Bugzilla::Product; use PhabTest::Conduit qw(:all); use PhabTest::Env; use PhabTest::Fixtures qw(:all); @@ -353,6 +356,90 @@ sub phab_attachments { ); } +# Triage owner of a private bug is CC'd (and so subscribed) when a revision +# is first attached, if they are in mozilla-employee-confidential +SKIP: { + my $confidential + = Bugzilla::Group->new({name => 'mozilla-employee-confidential'}); + skip 'mozilla-employee-confidential group required', 9 if !$confidential; + + my $group = $env->bug_group('triage'); + $conduit->add_project(project_obj(name => 'bmo-' . $group->name)); + $env->add_to_group($author, $group); + + my $owner = $env->bmo_user('triage-owner'); + my $owner_phab = $env->link_user($owner); + $env->add_to_group($owner, $confidential); + my $outsider = $env->bmo_user('triage-outsider'); + $env->link_user($outsider); + + # Every bug from $env->bug is in Firefox :: General, so point its triage + # owner at our user for this block only. + my $component = Bugzilla::Component->new({ + product => Bugzilla::Product->new({name => 'Firefox'}), + name => 'General' + }); + my $set_triage_owner = sub { + my ($login) = @_; + my $restore = Bugzilla->set_user($env->admin, scope_guard => 1); + my $fresh = Bugzilla::Component->new($component->id); + $fresh->set_triage_owner($login); + $fresh->update; + }; + my $original_owner = $component->triage_owner; + my $restore_owner + = sub { $set_triage_owner->($original_owner ? $original_owner->login : '') }; + $env->on_teardown($restore_owner); + $set_triage_owner->($owner->login); + + my $is_cc = sub { + my ($bug, $user) = @_; + return grep { $_->id == $user->id } @{Bugzilla::Bug->new($bug->id)->cc_users}; + }; + + my $bug = $env->bug(groups => [$group]); + start_scenario(); + my $raw = revision(bug_id => $bug->id); + story(object_phid => $raw->{phid}, text => updated_text($raw)); + ok(!run_feed(), 'feed_query for a new revision on a private bug'); + ok($is_cc->($bug, $owner), 'confidential triage owner CC\'d'); + my ($subscribers_set) + = grep { $_->{type} eq 'subscribers.set' } + $conduit->transactions_for($raw->{phid}); + ok( + (grep { $_ eq $owner_phab->{phid} } @{$subscribers_set->{value} // []}), + 'confidential triage owner subscribed to the revision' + ); + + # Removing themselves from the CC list sticks on later revision changes + $dbh->do('DELETE FROM cc WHERE bug_id = ? AND who = ?', + undef, $bug->id, $owner->id); + start_scenario(); + story(object_phid => $raw->{phid}, text => updated_text($raw)); + ok(!run_feed(), 'second story for the same revision'); + ok(!$is_cc->($bug, $owner), 'triage owner not CC\'d again'); + + # A triage owner outside mozilla-employee-confidential is not CC'd + $set_triage_owner->($outsider->login); + my $other_bug = $env->bug(groups => [$group]); + start_scenario(); + my $other_raw = revision(bug_id => $other_bug->id); + story(object_phid => $other_raw->{phid}, text => updated_text($other_raw)); + ok(!run_feed(), 'feed_query with a non-confidential triage owner'); + ok(!$is_cc->($other_bug, $outsider), 'non-confidential triage owner not CC\'d'); + + # Public bugs are left alone + $set_triage_owner->($owner->login); + my $public_bug = $env->bug; + start_scenario(); + my $public_raw = revision(bug_id => $public_bug->id); + story(object_phid => $public_raw->{phid}, text => updated_text($public_raw)); + ok(!run_feed(), 'feed_query for a revision on a public bug'); + ok(!$is_cc->($public_bug, $owner), 'triage owner not CC\'d on a public bug'); + + $restore_owner->(); +} + # Invalid bug id, or a bug the author cannot see { start_scenario();