summaryrefslogtreecommitdiffstats
path: root/extensions/Push/lib/Connector/ReviewBoard.pm
blob: 1c657a728ce97f5f52b3002aa003316da2f8fced (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
# 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.

package Bugzilla::Extension::Push::Connector::ReviewBoard;

use 5.10.1;
use strict;
use warnings;

use base 'Bugzilla::Extension::Push::Connector::Base';

use Bugzilla::Bug;
use Bugzilla::BugMail;
use Bugzilla::Component;
use Bugzilla::Constants;
use Bugzilla::Extension::Push::Constants;
use Bugzilla::Extension::Push::Util;
use Bugzilla::Group;
use Bugzilla::Product;
use Bugzilla::User;
use Bugzilla::Util qw( trim );

use constant RB_CONTENT_TYPE => 'text/x-review-board-request';
use constant AUTOMATION_USER => 'automation@bmo.tld';

sub options {
    return (
        {
            name     => 'product',
            label    => 'Product to create bugs in',
            type     => 'string',
            default  => 'Developer Services',
            required => 1,
            validate => sub {
                Bugzilla::Product->new({ name => $_[0] })
                    || die "Invalid Product ($_[0])\n";
            },
        },
        {
            name     => 'component',
            label    => 'Component to create bugs in',
            type     => 'string',
            default  => 'MozReview',
            required => 1,
            validate => sub {
                my ($component, $config) = @_;
                    my $product = Bugzilla::Product->new({ name => $config->{product} })
                        || die "Invalid Product (" . $config->{product} . ")\n";
                    Bugzilla::Component->new({ product => $product, name => $component })
                        || die "Invalid Component ($component)\n";
            },
        },
        {
            name     => 'version',
            label    => "The bug's version",
            type     => 'string',
            default  => 'Production',
            required => 1,
            validate => sub {
                my ($version, $config) = @_;
                    my $product = Bugzilla::Product->new({ name => $config->{product} })
                        || die "Invalid Product (" . $config->{product} . ")\n";
                    Bugzilla::Version->new({ product => $product, name => $version })
                        || die "Invalid Version ($version)\n";
            },
        },
        {
            name     => 'group',
            label    => 'Security group',
            type     => 'string',
            default  => 'mozilla-employee-confidential',
            required => 1,
            validate => sub {
                Bugzilla::Group->new({ name => $_[0] })
                    || die "Invalid Group ($_[0])\n";
            },
        },
        {
            name     => 'cc',
            label    => 'Comma separated list of users to CC',
            type     => 'string',
            default  => '',
            required => 1,
            validate => sub {
                foreach my $login (map { trim($_) } split(',', $_[0])) {
                    Bugzilla::User->new({ name => $login })
                        || die "Invalid User ($login)\n";
                }
            },
        },
    );
}

sub should_send {
    my ($self, $message) = @_;

    if ($message->routing_key =~ /^(?:attachment|bug)\.modify:.*\bis_private\b/) {
        my $payload = $message->payload_decoded();
        my $target  = $payload->{event}->{target};

        if ($target ne 'bug' && exists $payload->{$target}->{bug}) {
            return 0 if $payload->{$target}->{bug}->{is_private};
            return 0 if $payload->{$target}->{content_type} ne RB_CONTENT_TYPE;
        }

        return $payload->{$target}->{is_private} ? 1 : 0;
    }
    else {
        # We're not interested in the message.
        return 0;
    }
}

sub send {
    my ($self, $message) = @_;
    my $logger = Bugzilla->push_ext->logger;
    my $config = $self->config;

    eval {
        my $payload = $message->payload_decoded();
        my $target  = $payload->{event}->{target};

        # load attachments
        my $bug_id = $target eq 'bug' ? $payload->{bug}->{id} : $payload->{attachment}->{bug}->{id};
        my $attach_id = $target eq 'attachment' ? $payload->{attachment}->{id} : undef;
        Bugzilla->set_user(Bugzilla::User->super_user);
        my $bug = Bugzilla::Bug->new({ id => $bug_id, cache => 1 });
        Bugzilla->logout;

        # create a bug if there are any mozreview attachments
        my @reviews = grep { $_->contenttype eq RB_CONTENT_TYPE } @{ $bug->attachments };
        if (@reviews) {

            # build comment
            my $comment = $target eq 'bug'
                ? "Bug $bug_id has MozReview reviews and is no longer public."
                : "MozReview attachment $attach_id on Bug $bug_id is no longer public.";
            $comment .= "\n\n";
            foreach my $attachment (@reviews) {
                $comment .= $attachment->data . "\n";
            }

            # create bug
            my $user = Bugzilla::User->new({ name => AUTOMATION_USER, cache => 1 });
            die "Invalid User: " . AUTOMATION_USER . "\n" unless $user;
            Bugzilla->set_user($user);
            my $new_bug = Bugzilla::Bug->create({
                short_desc   => "[SECURITY] Bug $bug_id is no longer public",
                product      => $config->{product},
                component    => $config->{component},
                bug_severity => 'normal',
                groups       => [ map { trim($_) } split(',', $config->{group}) ],
                op_sys       => 'Unspecified',
                rep_platform => 'Unspecified',
                version      => $config->{version},
                cc           => [ map { trim($_) } split(',', $config->{cc}) ],
                comment      => $comment,
            });
            Bugzilla::BugMail::Send($new_bug->id, { changer => Bugzilla->user });
            Bugzilla->logout;

            $logger->info("Created bug " . $new_bug->id);
        }
    };
    my $error = $@;
    Bugzilla->logout;
    if ($error) {
        return (PUSH_RESULT_TRANSIENT, clean_error($error));
    }

    return PUSH_RESULT_OK;
}

1;