summaryrefslogtreecommitdiffstats
path: root/scripts/movebugs.pl
blob: 55a43d943508dd9017387a575c20cee1a657cb6a (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/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 strict;
use warnings;
use lib qw(. lib local/lib/perl5);


use Cwd 'abs_path';
use File::Basename;

use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::FlagType;
use Bugzilla::Hook;
use Bugzilla::Util;

Bugzilla->usage_mode(USAGE_MODE_CMDLINE);

if (scalar @ARGV < 4) {
    die <<USAGE;
Usage: movebugs.pl <old-product> <old-component> <new-product> <new-component>

Eg. movebugs.pl mozilla.org bmo bugzilla.mozilla.org admin
Will move all bugs in the mozilla.org:bmo component to the
bugzilla.mozilla.org:admin component.

The new product must have matching versions, milestones, and flags from the old
product (will be validated by this script).
USAGE
}

my ($old_product, $old_component, $new_product, $new_component) = @ARGV;

my $dbh = Bugzilla->dbh;

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

my $old_component_id = $dbh->selectrow_array(
    "SELECT id FROM components WHERE name=? AND product_id=?",
    undef, $old_component, $old_product_id);
$old_component_id
    or die "Can't find component ID for '$old_component'.\n";

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

my $new_component_id = $dbh->selectrow_array(
    "SELECT id FROM components WHERE name=? AND product_id=?",
    undef, $new_component, $new_product_id);
$new_component_id
    or die "Can't find component ID for '$new_component'.\n";

my $product_field_id = $dbh->selectrow_array(
    "SELECT id FROM fielddefs WHERE name = 'product'");
$product_field_id
    or die "Can't find field ID for 'product' field\n";
my $component_field_id = $dbh->selectrow_array(
    "SELECT id FROM fielddefs WHERE name = 'component'");
$component_field_id
    or die "Can't find field ID for 'component' field\n";

my $nobody = Bugzilla->params->{'nobody_user'};
my $user_id = $dbh->selectrow_array(
    "SELECT userid FROM profiles WHERE login_name=?",
    undef, $nobody);
$user_id
    or die "Can't find user ID for '$nobody'\n";

$dbh->bz_start_transaction();

# build list of bugs
my $ra_ids = $dbh->selectcol_arrayref(
    "SELECT bug_id FROM bugs WHERE product_id=? AND component_id=?",
    undef, $old_product_id, $old_component_id);
my $bug_count = scalar @$ra_ids;
$bug_count
    or die "No bugs were found in '$old_component'\n";
my $where_sql = 'bug_id IN (' . join(',', @$ra_ids) . ')';

# check versions
my @missing_versions;
my $ra_versions = $dbh->selectcol_arrayref(
    "SELECT DISTINCT version FROM bugs WHERE $where_sql");
foreach my $version (@$ra_versions) {
    my $has_version = $dbh->selectrow_array(
        "SELECT 1 FROM versions WHERE product_id=? AND value=?",
        undef, $new_product_id, $version);
    push @missing_versions, $version unless $has_version;
}

# check milestones
my @missing_milestones;
my $ra_milestones = $dbh->selectcol_arrayref(
    "SELECT DISTINCT target_milestone FROM bugs WHERE $where_sql");
foreach my $milestone (@$ra_milestones) {
    my $has_milestone = $dbh->selectrow_array(
        "SELECT 1 FROM milestones WHERE product_id=? AND value=?",
        undef, $new_product_id, $milestone);
    push @missing_milestones, $milestone unless $has_milestone;
}

# check flags
my @missing_flags;
my $ra_old_types = $dbh->selectcol_arrayref(
    "SELECT DISTINCT type_id
       FROM flags
            INNER JOIN flagtypes ON flagtypes.id = flags.type_id
      WHERE $where_sql");
my $ra_new_types =
    Bugzilla::FlagType::match({ product_id   => $new_product_id,
                                component_id => $new_component_id });
foreach my $old_type (@$ra_old_types) {
    unless (grep { $_->id == $old_type } @$ra_new_types) {
        my $flagtype = Bugzilla::FlagType->new($old_type);
        push @missing_flags, $flagtype->name . ' (' . $flagtype->target_type . ')';
    }
}

# show missing
my $missing_error = '';
if (@missing_versions) {
    $missing_error .= "'$new_product' is missing the following version(s):\n  " .
        join("\n  ", @missing_versions) . "\n";
}
if (@missing_milestones) {
    $missing_error .= "'$new_product' is missing the following milestone(s):\n  " .
        join("\n  ", @missing_milestones) . "\n";
}
if (@missing_flags) {
    $missing_error .= "'$new_product'::'$new_component' is missing the following flag(s):\n  " .
        join("\n  ", @missing_flags) . "\n";
}
die $missing_error if $missing_error;

# confirmation
print <<EOF;
About to move $bug_count bugs
From '$old_product' : '$old_component'
To '$new_product' : '$new_component'

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

print "Moving $bug_count bugs from $old_product:$old_component to $new_product:$new_component\n";

# update bugs
$dbh->do(
    "UPDATE bugs SET product_id=?, component_id=? WHERE $where_sql",
    undef, $new_product_id, $new_component_id);

# touch bugs
$dbh->do("UPDATE bugs SET delta_ts=NOW() WHERE $where_sql");
$dbh->do("UPDATE bugs SET lastdiffed=NOW() WHERE $where_sql");

# update bugs_activity
$dbh->do(
    "INSERT INTO bugs_activity(bug_id, who, bug_when, fieldid, removed, added)
          SELECT bug_id, ?, delta_ts, ?, ?, ?  FROM bugs WHERE $where_sql",
    undef,
    $user_id, $product_field_id, $old_product, $new_product);
$dbh->do(
    "INSERT INTO bugs_activity(bug_id, who, bug_when, fieldid, removed, added)
          SELECT bug_id, ?, delta_ts, ?, ?, ?  FROM bugs WHERE $where_sql",
    undef,
    $user_id, $component_field_id, $old_component, $new_component);

Bugzilla::Hook::process('reorg_move_bugs', { bug_ids => $ra_ids } );

$dbh->bz_commit_transaction();

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

# It's complex to determine which items now need to be flushed from memcached.
# As this is expected to be a rare event, we just flush the entire cache.
Bugzilla->memcached->clear_all();