summaryrefslogtreecommitdiffstats
path: root/extensions/BMO/bin/migrate-github-pull-requests.pl
blob: c39778a4a0a90f69c0f7792b8132c043bcc4aeec (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
#!/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);

use Bugzilla;
BEGIN { Bugzilla->extensions() }

use Bugzilla::Extension::BMO::Data;
use Bugzilla::Field;
use Bugzilla::Install::Util qw(indicate_progress);
use Bugzilla::User;
use Bugzilla::Util qw(trim);

my $dbh = Bugzilla->dbh;
my $nobody = Bugzilla::User->check({ name => Bugzilla->params->{'nobody_user'} });
my $field = Bugzilla::Field->check({ name => 'attachments.mimetype' });

# grab list of suitable attachments

my $sql = <<EOF;
SELECT attachments.attach_id,
       attachments.bug_id,
       attachments.mimetype,
       attach_data.thedata
  FROM attachments
       INNER JOIN attach_data ON attach_data.id = attachments.attach_id
 WHERE ispatch = 0
       AND mimetype = 'text/plain'
       AND thedata IS NOT NULL
       AND LENGTH(thedata) > 0
       AND LENGTH(thedata) <= 256
EOF
print "Searching for suitable attachments..\n";
my $attachments = $dbh->selectall_arrayref($sql, { Slice => {} });
my ($current, $total, $updated) = (1, scalar(@$attachments), 0);

die "No suitable attachments found\n" unless $total;
print "About to check $total attachments for github pull requests, and\n";
print "update content-type if required.\n";
print "Press <enter> to start, or ^C to cancel...\n";
<>;

foreach my $attachment (@$attachments) {
    indicate_progress({ current => $current++, total => $total, every => 25 });

    # check payload
    my $url = trim($attachment->{thedata});
    next if $url =~ /\s/;
    next unless $url =~ m#^https://github\.com/[^/]+/[^/]+/pull/\d+\/?$#i;

    $dbh->bz_start_transaction;

    # set content-type
    $dbh->do(
        "UPDATE attachments SET mimetype = ? WHERE attach_id = ?",
        undef,
        'text/x-github-pull-request', $attachment->{attach_id}
    );

    # insert into bugs_activity
    my $timestamp = $dbh->selectrow_array('SELECT LOCALTIMESTAMP(0)');
    $dbh->do(
        "INSERT INTO bugs_activity(bug_id, who, bug_when, fieldid, removed, added)
                     VALUES (?, ?, ?, ?, ?, ?)",
        undef,
        $attachment->{bug_id}, $nobody->id, $timestamp, $field->id,
        $attachment->{mimetype}, 'text/x-github-pull-request'
    );
    $dbh->do(
        "UPDATE bugs SET delta_ts = ?, lastdiffed = ? WHERE bug_id = ?",
        undef,
        $timestamp, $timestamp, $attachment->{bug_id}
    );

    $dbh->bz_commit_transaction;
    $updated++;
}

print "Attachments updated: $updated\n";