Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions extensions/Needinfo/Extension.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ use constant MAX_MENTIONS => 10;
# block_needinfo values that only accept needinfo requests from members of a
# group (directly or through a group nested in it), keyed to that group.
use constant NEEDINFO_ALLOWED_GROUPS =>
{editbugs => 'editbugs', hackbot_needinfo => 'hackbot_needinfo_allowed'};
{editbugs => 'editbugs', hackbot_needinfo => 'hackbot-users'};

# Name the hackbot group was first created under, renamed in place by
# _setup_hackbot_users_group so its members and nested groups carry over.
use constant OLD_HACKBOT_GROUP => 'hackbot_needinfo_allowed';

BEGIN {
*Bugzilla::User::needinfo_blocked = \&_user_needinfo_blocked;
Expand Down Expand Up @@ -83,25 +87,49 @@ sub install_update_db {
# Users who set block_needinfo to 'hackbot_needinfo' only accept needinfo
# requests from members of this group. Admins add groups (such as
# mozilla-employee-confidential) to it rather than individual users.
# It is a bug group in no products: PhabBugz syncs every active bug group to
# a Phabricator project, so Hackbot can see the same membership there, while
# no product offers it for restricting bugs.
# Called from install_before_final_checks rather than install_update_db:
# other extensions (e.g. SecureMail) add group columns in their own
# install_update_db, and Group->create needs those on a fresh database.
sub _create_hackbot_needinfo_group {
sub _setup_hackbot_users_group {
my $name = NEEDINFO_ALLOWED_GROUPS->{hackbot_needinfo};
return if Bugzilla::Group->new({name => $name});

if (my $group = Bugzilla::Group->new({name => OLD_HACKBOT_GROUP})) {
print 'Renaming ' . OLD_HACKBOT_GROUP . " group to $name ...\n";
_migrate_old_hackbot_group($group, $name);
return;
}

print "Creating $name group ...\n";
Bugzilla::Group->create({
name => $name,
description => 'Users who may needinfo people that only accept '
. 'needinfo requests from hackbot-permitted users',
isbuggroup => 0,
isbuggroup => 1,
Comment thread
dklawren marked this conversation as resolved.
Resolved
});
}

# Renames the group in place and makes it a bug group. isbuggroup is not
# among Bugzilla::Group's UPDATE_COLUMNS, so that flag is set directly.
sub _migrate_old_hackbot_group {
my ($group, $name) = @_;
my $dbh = Bugzilla->dbh;
my $groups = $dbh->quote_identifier('groups');

$dbh->bz_start_transaction();
$group->set_name($name);
$group->update();
$dbh->do("UPDATE $groups SET isbuggroup = 1 WHERE id = ?", undef, $group->id);
$dbh->bz_commit_transaction();
Bugzilla->memcached->clear_config();
}

sub install_before_final_checks {
my ($self, $args) = @_;
_create_hackbot_needinfo_group();
_setup_hackbot_users_group();
add_setting({
name => 'block_needinfo',
options => ['on', 'editbugs', 'hackbot_needinfo', 'off'],
Expand Down
28 changes: 23 additions & 5 deletions extensions/Needinfo/t/bmo/block-needinfo-hackbot.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# defined by the Mozilla Public License, v. 2.0.

# A user whose block_needinfo setting is 'hackbot_needinfo' only accepts
# needinfo requests from members of the hackbot_needinfo_allowed group,
# needinfo requests from members of the hackbot-users group,
# whether they are in it directly or through a group nested in it.

use strict;
Expand Down Expand Up @@ -36,10 +36,28 @@ my $dbh = Bugzilla->dbh;
Bugzilla->error_mode(ERROR_MODE_DIE);
Bugzilla->set_user(Bugzilla::User->super_user);

my $hackbot_group = Bugzilla::Group->new({name => 'hackbot_needinfo_allowed'});
ok($hackbot_group, 'checksetup created hackbot_needinfo_allowed');
my $hackbot_group = Bugzilla::Group->new({name => 'hackbot-users'});
ok($hackbot_group, 'checksetup created hackbot-users');
ok($hackbot_group->is_bug_group, 'hackbot-users is a bug group');
ok($hackbot_group->is_active, 'hackbot-users is active, so PhabBugz syncs it');
is(
$dbh->selectrow_array(
'SELECT COUNT(*) FROM group_control_map WHERE group_id = ?', undef,
$hackbot_group->id
),
0,
'hackbot-users is in no products'
);

# The migration renames an existing non-bug group in place, keeping its id.
my $old_group = create_group("$prefix-old", isbuggroup => 0);
Bugzilla::Extension::Needinfo::_migrate_old_hackbot_group($old_group,
"$prefix-new");
my $migrated = Bugzilla::Group->new({name => "$prefix-new"});
is($migrated && $migrated->id, $old_group->id, 'migration renames in place');
ok($migrated && $migrated->is_bug_group, 'migration makes it a bug group');

# A group nested in hackbot_needinfo_allowed, standing in for groups such as
# A group nested in hackbot-users, standing in for groups such as
# mozilla-employee-confidential.
my $nested_group = create_group("$prefix-staff", isbuggroup => 0);
$dbh->do(
Expand Down Expand Up @@ -85,7 +103,7 @@ ok(blocked_for($users{outsider}), 'blocked from a non-member');

Bugzilla->set_user($users{outsider});
eval { Bugzilla::Extension::Needinfo::_check_requestee($requestee) };
like($@, qr/hackbot_needinfo_allowed/, 'non-member gets the hackbot error');
like($@, qr/hackbot-users/, 'non-member gets the hackbot error');

Bugzilla->set_user($users{direct});
eval { Bugzilla::Extension::Needinfo::_check_requestee($requestee) };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
[% ELSIF error == "needinfo_blocked_hackbot" %]
[% title = "Needinfo Request Blocked" %]
[% requestee.identity FILTER html %] only accepts "needinfo" requests from
users in the <em>hackbot_needinfo_allowed</em> group.
users in the <em>hackbot-users</em> group.
[% END %]