summaryrefslogtreecommitdiffstats
path: root/extensions/TrackingFlags/bin/bug_825946.pl
blob: 8a340175b613d535a3b7c2d29f415f713494557f (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
#!/usr/bin/perl
# 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 5.10.1;

use lib qw(. lib local/lib/perl5);

BEGIN {
  use Bugzilla;
  Bugzilla->extensions;
}

use Bugzilla::Constants qw( USAGE_MODE_CMDLINE );
use Bugzilla::Extension::TrackingFlags::Flag;
use Bugzilla::User;
use Bugzilla::Bug qw(LogActivityEntry);

Bugzilla->usage_mode(USAGE_MODE_CMDLINE);
my $dbh = Bugzilla->dbh;
my $user = Bugzilla::User->check({name => Bugzilla->params->{'nobody_user'}});

my $tf_vis = $dbh->selectall_arrayref(<<SQL);
    SELECT
        tracking_flag_id,
        product_id,
        component_id
    FROM
        tracking_flags_visibility
SQL

my $tf_bugs = $dbh->selectall_arrayref(<<SQL);
    SELECT
        tf.name,
        tf_bugs.value,
        bugs.bug_id,
        tf_bugs.tracking_flag_id,
        bugs.product_id,
        bugs.component_id
    FROM
        tracking_flags_bugs AS tf_bugs
    JOIN bugs USING (bug_id)
    JOIN tracking_flags AS tf ON tf.id = tf_bugs.tracking_flag_id
SQL

my %visible;
foreach my $row (@$tf_vis) {
  my ($tracking_flag_id, $product_id, $component_id) = @$row;
  $visible{$tracking_flag_id}{$product_id}{$component_id // 'ALL'} = 1;
}

my %bugs = map { $_->[0] => 1 } @$tf_bugs;
my $bugs = keys %bugs;

printf "About to check %d tracking flags on %d bugs\n", @$tf_bugs + 0, $bugs;
print "Press <Ctrl-C> to stop or <Enter> to continue...\n";
getc();

my $removed = 0;
$dbh->bz_start_transaction();
my ($timestamp) = $dbh->selectrow_array('SELECT LOCALTIMESTAMP(0)');
foreach my $tf_bug (@$tf_bugs) {
  my ($flag_name, $value, $bug_id, $tf_id, $product_id, $component_id) = @$tf_bug;
  unless ($visible{$tf_id}{$product_id}{$component_id}
    || $visible{$tf_id}{$product_id}{ALL})
  {
    $dbh->do(
      "DELETE FROM tracking_flags_bugs WHERE tracking_flag_id = ? AND bug_id = ?",
      undef, $tf_id, $bug_id);
    LogActivityEntry($bug_id, $flag_name, $value, '---', $user->id, $timestamp);
    $removed++;
  }
}
$dbh->bz_commit_transaction();

print "Removed $removed tracking flags\n";
print "Done.\n";