summaryrefslogtreecommitdiffstats
path: root/scripts/undo.pl
blob: 852c786fd361512865f5a96decf8771e83a7e4a4 (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
193
194
195
196
197
198
199
200
201
202
203
#!/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 5.10.1;
use strict;
use warnings;

use File::Basename qw(dirname);
use File::Spec::Functions qw(catfile catdir rel2abs);
use Cwd qw(realpath);
BEGIN {
    require lib;
    my $dir = rel2abs(catdir(dirname(__FILE__), '..'));
    lib->import($dir, catdir($dir, 'lib'), catdir($dir, qw(local lib perl5)));
}

use Bugzilla;
BEGIN { Bugzilla->extensions };
use Bugzilla::Extension::UserProfile::Util qw(tag_for_recount_from_bug);

use Try::Tiny;

# not involved with kmag
my $query = q{
    resolution = 'INACTIVE'
    AND NOT (
        triage_owner.login_name = 'mak77@bonardo.net'
        AND bugs.priority IN ('P4', 'P5', '--')
    )
    AND NOT (
        product.name = 'Toolkit' AND component.name = 'Add-ons Manager'
    )
    AND NOT (
        product.name = 'Toolkit' AND component.name LIKE 'WebExtensions%'
    )
    AND NOT (
        product.name = 'Core' AND component.name = 'XPConnect'
    )
};
undo($query);

sub undo {
    my @args = @_;
    my $changes = get_changes(@args);
    my $comments = get_comments(@args);

    my %action;
    while ($_ = $changes->()) {
        push @{ $action{$_->{bug_id}}{$_->{bug_when}}{remove_activities} }, { id => $_->{change_id} };
        $action{ $_->{bug_id} }{ $_->{bug_when} }{change}{ $_->{field_name} } = {
            replace => $_->{added},
            with    => $_->{removed},
        };
    }

    while ($_ = $comments->()) {
        push @{ $action{ $_->{bug_id} }{$_->{bug_when}}{remove_comments} }, {
            id => $_->{comment_id},
        };
    }

    my $dbh = Bugzilla->dbh;
    my @bug_ids = sort { $b <=> $a } keys %action;
    say 'Found ', 0 + @bug_ids, ' bugs';
    foreach my $bug_id (@bug_ids) {
        $dbh->bz_start_transaction;
        say "Fix $bug_id";
        try {
            my ($delta_ts) = $dbh->selectrow_array(
                'SELECT delta_ts FROM bugs WHERE bug_id = ?',
                undef,
                $bug_id);
            my ($previous_last_ts) = $dbh->selectrow_array(
                q{
                    SELECT MAX(bug_when) FROM (
                        SELECT bug_when FROM bugs_activity WHERE bug_id = ? AND bug_when < ?
                        UNION
                        SELECT bug_when FROM longdescs WHERE bug_id = ? AND bug_when < ?
                        UNION
                        SELECT creation_ts AS bug_when FROM bugs WHERE bug_id = ?
                    ) as changes ORDER BY bug_when DESC
                },
                undef,
                $bug_id, $delta_ts,
                $bug_id, $delta_ts,
                $bug_id,
            );
            die 'cannot find previous last updated time' unless $previous_last_ts;
            my $action = delete $action{$bug_id}{$delta_ts};
            if (keys %{ $action{$bug_id}{$delta_ts}}) {
                die "skipping because more than one change\n";
            }
            elsif (!$action) {
                die "skipping because most recent change newer than automation change\n";
            }
            foreach my $field (keys %{ $action->{change} }) {
                my $change = $action->{change}{$field};
                if ($field eq 'cf_last_resolved' && !$change->{with}) {
                    $change->{with} = undef;
                }
                my $did = $dbh->do(
                    "UPDATE bugs SET $field = ? WHERE bug_id = ? AND $field = ?",
                    undef, $change->{with}, $bug_id, $change->{replace},
                );
                die "Failed to set $field to $change->{with}" unless $did;
            }
            $dbh->do('UPDATE bugs SET delta_ts = ?, lastdiffed = ? WHERE bug_id = ?', undef,
                $previous_last_ts, $previous_last_ts, $bug_id );
            my $del_comments = $dbh->prepare('DELETE FROM longdescs WHERE comment_id = ?');
            my $del_activity = $dbh->prepare('DELETE FROM bugs_activity WHERE id = ?');
            foreach my $c (@{ $action->{remove_comments}}) {
                $del_comments->execute($c->{id}) or die "failed to delete comment $c->{id}";
            }
            foreach my $a (@{ $action->{remove_activities}}) {
                $del_activity->execute($a->{id}) or die "failed to delete comment $a->{id}";
            }
            tag_for_recount_from_bug($bug_id);
            $dbh->bz_commit_transaction;
            sleep 1;
        } catch {
            chomp $_;
            say "Error updating $bug_id: $_";
            $dbh->bz_rollback_transaction;
        };
    }
    say 'Done.';
}

sub get_changes {
    my ($where, @bind) = @_;

    my $sql = qq{
        SELECT
            BA.id AS change_id,
            BA.bug_id,
            FD.name AS field_name,
            BA.removed,
            BA.added,
            BA.bug_when
        FROM
            bugs_activity AS BA
                JOIN
            fielddefs AS FD ON BA.fieldid = FD.id
                JOIN
            profiles AS changer ON changer.userid = BA.who
                JOIN
            (SELECT
                bug_id
            FROM
                bugs
            JOIN products AS product ON product.id = product_id
            JOIN components AS component ON component.id = component_id
            LEFT JOIN profiles AS triage_owner ON triage_owner.userid = component.triage_owner_id
            WHERE
                $where
            ) target_bugs ON BA.bug_id = target_bugs.bug_id
        WHERE
            changer.login_name = 'automation\@bmo.tld'
                AND BA.bug_when BETWEEN '2018-05-22' AND '2018-05-26'
    };
    my $sth = Bugzilla->dbh->prepare($sql);
    $sth->execute(@bind);

    return sub { $sth->fetchrow_hashref };
}

sub get_comments {
    my ($where, @bind) = @_;

    my $sql = qq{
        SELECT
            C.comment_id AS comment_id,
            C.bug_id AS bug_id,
            C.bug_when
        FROM
            longdescs AS C
                JOIN
            profiles AS commenter ON commenter.userid = C.who
                JOIN
            (SELECT
                bug_id
            FROM
                bugs
            JOIN products AS product ON product.id = product_id
            JOIN components AS component ON component.id = component_id
            LEFT JOIN profiles AS triage_owner ON triage_owner.userid = component.triage_owner_id
            WHERE
                $where
            ) target_bugs ON C.bug_id = target_bugs.bug_id
        WHERE
            commenter.login_name = 'automation\@bmo.tld'
                AND C.bug_when BETWEEN '2018-05-22' AND '2018-05-26'
    };
    my $sth = Bugzilla->dbh->prepare($sql);
    $sth->execute(@bind);

    return sub { $sth->fetchrow_hashref };
}