summaryrefslogtreecommitdiffstats
path: root/scripts/eject-users-from-groups.pl
blob: c67238705e183a5e6cc631c634080097f0fef447 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/perl -w
# 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 5.10.1;
use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/..", "$FindBin::Bin/../lib", "$FindBin::Bin/../local/lib/perl5";
use Getopt::Long;

use Bugzilla;
use Bugzilla::Constants;

BEGIN { Bugzilla->extensions }

Bugzilla->usage_mode(USAGE_MODE_CMDLINE);

my $dbh = Bugzilla->dbh;
my @remove_group_names;
my $nobody_name = 'nobody@mozilla.org';
my $admin_name = 'automation@bmo.tld';

GetOptions(
    'nobody=s' => \$nobody_name,
    'admin=s'  => \$admin_name,
    'group|G=s@' => \@remove_group_names,
);
my @user_names = @ARGV;

unless (@remove_group_names) {
    die "usage: $0 [--admin=$admin_name] [--nobody=$nobody_name] ",
      "-G legal -G finance dylan\@mozilla.com bob\@example.net\n";
}

$dbh->bz_start_transaction();
my ($timestamp) = $dbh->selectrow_array('SELECT LOCALTIMESTAMP(0)');
my $admin_user  = Bugzilla::User->check({ name => $admin_name });
my $nobody_user = Bugzilla::User->check({ name => $nobody_name });
Bugzilla->set_user($admin_user);

my @remove_groups = map { Bugzilla::Group->check({name => $_}) } @remove_group_names;

foreach my $user_name (@user_names) {
    my $user = Bugzilla::User->check({name => $user_name});
    say 'Working on ', $user->identity;

    $user->force_bug_dissociation($nobody_user, \@remove_groups, $timestamp);
}

$dbh->bz_commit_transaction();