summaryrefslogtreecommitdiffstats
path: root/scripts/reset_default_user.pl
blob: 2888df48cbe08d84792ea1ea6ecaf89066952cf8 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/perl -wT
# 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 FindBin qw($RealBin);
use lib ("$RealBin/..", "$RealBin/../lib", "$RealBin/../local/lib/perl5");

use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::User;
use Bugzilla::Field;
use Bugzilla::Util qw(trick_taint);

use Getopt::Long;

Bugzilla->usage_mode(USAGE_MODE_CMDLINE);

my $dbh = Bugzilla->dbh;

my $field_name = "";
my $product    = "";
my $component  = "";
my $help       = "";
my %user_cache = ();

my $result = GetOptions('field=s' => \$field_name,
                        'product=s'   => \$product,
                        'component=s' => \$component,
                        'help|h'      => \$help);

sub usage {
    print <<USAGE;
Usage: reset_default_user.pl --field <fieldname> --product <product> [--component <component>] [--help]

This script will load all bugs matching the product, and optionally component,
and reset the default user value back to the default value for the component.
Valid field names are assigned_to and qa_contact.
USAGE
}

if (!$product || $help
    || ($field_name ne 'assigned_to' && $field_name ne 'qa_contact'))
{
    usage();
    exit(1);
}

# We will need these for entering into bugs_activity
my $who   = Bugzilla::User->new({ name => 'nobody@mozilla.org' });
my $field = Bugzilla::Field->new({ name => $field_name });

trick_taint($product);
my $product_id = $dbh->selectrow_array(
    "SELECT id FROM products WHERE name = ?",
    undef, $product);
$product_id or die "Can't find product ID for '$product'.\n";

my $component_id;
my $default_user_id;
if ($component) {
    trick_taint($component);
    my $colname = $field->name eq 'qa_contact'
                  ? 'initialqacontact'
                  : 'initialowner';
    ($component_id, $default_user_id) = $dbh->selectrow_array(
        "SELECT id, $colname FROM components " .
        "WHERE name = ? AND product_id = ?",
        undef, $component, $product_id);
    $component_id or die "Can't find component ID for '$component'.\n";
    $user_cache{$default_user_id} ||= Bugzilla::User->new($default_user_id);
}

# build list of bugs
my $bugs_query = "SELECT bug_id, qa_contact, component_id " .
                 "FROM bugs WHERE product_id = ?";
my @args = ($product_id);

if ($component_id) {
    $bugs_query .= " AND component_id = ? AND qa_contact != ?";
    push(@args, $component_id, $default_user_id);
}

my $bugs = $dbh->selectall_arrayref($bugs_query, {Slice => {}}, @args);
my $bug_count = scalar @$bugs;
$bug_count
    or die "No bugs were found.\n";

# confirmation
print <<EOF;
About to reset $field_name for $bug_count bugs.

Press <Ctrl-C> to stop or <Enter> to continue...
EOF
getc();

$dbh->bz_start_transaction();

foreach my $bug (@$bugs) {
    my $bug_id      = $bug->{bug_id};
    my $old_user_id = $bug->{$field->name};
    my $old_comp_id = $bug->{component_id};

    # If only changing one component, we already have the default user id
    my $new_user_id;
    if ($default_user_id) {
        $new_user_id = $default_user_id;
    }
    else {
        my $colname = $field->name eq 'qa_contact'
                      ? 'initialqacontact'
                      : 'initialowner';
        $new_user_id = $dbh->selectrow_array(
            "SELECT $colname FROM components WHERE id = ?",
            undef, $old_comp_id);
    }

    if ($old_user_id != $new_user_id) {
        print "Resetting " . $field->name . " for bug $bug_id ...";

        # Use the cached version if already exists
        my $old_user = $user_cache{$old_user_id} ||= Bugzilla::User->new($old_user_id);
        my $new_user = $user_cache{$new_user_id} ||= Bugzilla::User->new($new_user_id);

        my $timestamp = $dbh->selectrow_array('SELECT LOCALTIMESTAMP(0)');

        $dbh->do("UPDATE bugs SET " . $field->name . " = ? WHERE bug_id = ?",
                 undef, $new_user_id, $bug_id);
        $dbh->do("INSERT INTO bugs_activity(bug_id, who, bug_when, fieldid, removed, added) " .
                 "VALUES (?, ?, ?, ?, ?, ?)",
                 undef, $bug_id, $who->id, $timestamp, $field->id, $old_user->login, $new_user->login);
        $dbh->do("UPDATE bugs SET delta_ts = ?, lastdiffed = ? WHERE bug_id = ?",
                 undef, $timestamp, $timestamp, $bug_id);

        Bugzilla->memcached->clear({ table => 'bugs', id => $bug_id });

        print "done.\n";
    }
}

$dbh->bz_commit_transaction();