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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
# 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::Review;
use strict;
use warnings;
use base qw(Bugzilla::Extension);
our $VERSION = '1';
use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::Error;
use Bugzilla::User;
use Bugzilla::Util qw(clean_text);
use constant UNAVAILABLE_RE => qr/\b(?:unavailable|pto)\b/i;
#
# monkey-patched methods
#
BEGIN {
*Bugzilla::Product::reviewers = \&_product_reviewers;
*Bugzilla::Product::reviewers_objs = \&_product_reviewers_objs;
*Bugzilla::Product::reviewer_required = \&_product_reviewer_required;
*Bugzilla::Component::reviewers = \&_component_reviewers;
*Bugzilla::Component::reviewers_objs = \&_component_reviewers_objs;
*Bugzilla::Bug::mentor = \&_bug_mentor;
}
#
# reviewers
#
sub _product_reviewers { _reviewers($_[0], 'product') }
sub _product_reviewers_objs { _reviewers_objs($_[0], 'product') }
sub _component_reviewers { _reviewers($_[0], 'component') }
sub _component_reviewers_objs { _reviewers_objs($_[0], 'component') }
sub _reviewers {
my ($object, $type) = @_;
return join(', ', map { $_->login } @{ _reviewers_objs($object, $type) });
}
sub _reviewers_objs {
my ($object, $type) = @_;
if (!$object->{reviewers}) {
my $dbh = Bugzilla->dbh;
my $user_ids = $dbh->selectcol_arrayref(
"SELECT user_id FROM ${type}_reviewers WHERE ${type}_id = ? ORDER BY sortkey",
undef,
$object->id,
);
# new_from_list always sorts according to the object's definition,
# so we have to reorder the list
my $users = Bugzilla::User->new_from_list($user_ids);
my %user_map = map { $_->id => $_ } @$users;
my @reviewers =
grep { $_->name !~ UNAVAILABLE_RE }
map { $user_map{$_} } @$user_ids;
$object->{reviewers} = \@reviewers;
}
return $object->{reviewers};
}
sub _bug_mentor {
my ($self) = @_;
# extract the mentor from the status_whiteboard
# when the mentor gets its own field, this will be easier
if (!exists $self->{mentor}) {
my $mentor;
if ($self->status_whiteboard =~ /\[mentor=([^\]]+)\]/) {
my $mentor_string = $1;
if ($mentor_string =~ /\@/) {
# assume it's a full username if it contains an @
$mentor = Bugzilla::User->new({ name => $mentor_string });
} else {
# otherwise assume it's a : prefixed nick. only works if a
# single user matches.
my $matches = Bugzilla::User::match("*:$mentor_string*", 2);
if ($matches && scalar(@$matches) == 1) {
$mentor = $matches->[0];
}
}
}
$self->{mentor} = $mentor;
}
return $self->{mentor};
}
#
# reviewer-required
#
sub _product_reviewer_required { $_[0]->{reviewer_required} }
sub object_columns {
my ($self, $args) = @_;
my ($class, $columns) = @$args{qw(class columns)};
if ($class->isa('Bugzilla::Product')) {
push @$columns, 'reviewer_required';
}
}
sub object_update_columns {
my ($self, $args) = @_;
my ($object, $columns) = @$args{qw(object columns)};
if ($object->isa('Bugzilla::Product')) {
push @$columns, 'reviewer_required';
}
}
#
# create/update
#
sub object_before_create {
my ($self, $args) = @_;
my ($class, $params) = @$args{qw(class params)};
return unless $class->isa('Bugzilla::Product');
$params->{reviewer_required} = Bugzilla->cgi->param('reviewer_required') ? 1 : 0;
}
sub object_end_of_set_all {
my ($self, $args) = @_;
my ($object, $params) = @$args{qw(object params)};
return unless $object->isa('Bugzilla::Product');
$object->set('reviewer_required', Bugzilla->cgi->param('reviewer_required') ? 1 : 0);
}
sub object_end_of_create {
my ($self, $args) = @_;
my ($object, $params) = @$args{qw(object params)};
return unless $object->isa('Bugzilla::Product') || $object->isa('Bugzilla::Component');;
my ($new, $new_users) = _new_reviewers_from_input();
_update_reviewers($object, [], $new_users);
}
sub object_end_of_update {
my ($self, $args) = @_;
my ($object, $old_object, $changes) = @$args{qw(object old_object changes)};
return unless $object->isa('Bugzilla::Product') || $object->isa('Bugzilla::Component');;
my ($new, $new_users) = _new_reviewers_from_input();
my $old = $old_object->reviewers;
if ($old ne $new) {
_update_reviewers($object, $old_object->reviewers_objs, $new_users);
$changes->{reviewers} = [ $old ? $old : undef, $new ? $new : undef ];
}
}
sub _new_reviewers_from_input {
if (!Bugzilla->input_params->{reviewers}) {
return (undef, []);
}
Bugzilla::User::match_field({ 'reviewers' => {'type' => 'multi'} });
my $new = Bugzilla->input_params->{reviewers};
$new = [ $new ] unless ref($new);
my $new_users = [];
foreach my $login (@$new) {
push @$new_users, Bugzilla::User->check($login);
}
$new = join(', ', @$new);
return ($new, $new_users);
}
sub _update_reviewers {
my ($object, $old_users, $new_users) = @_;
my $dbh = Bugzilla->dbh;
my $type = $object->isa('Bugzilla::Product') ? 'product' : 'component';
# remove deleted users
foreach my $old_user (@$old_users) {
if (!grep { $_->id == $old_user->id } @$new_users) {
$dbh->do(
"DELETE FROM ${type}_reviewers WHERE ${type}_id=? AND user_id=?",
undef,
$object->id, $old_user->id,
);
}
}
# add new users
foreach my $new_user (@$new_users) {
if (!grep { $_->id == $new_user->id } @$old_users) {
$dbh->do(
"INSERT INTO ${type}_reviewers(${type}_id,user_id) VALUES (?,?)",
undef,
$object->id, $new_user->id,
);
}
}
# and update the sortkey for all users
for (my $i = 0; $i < scalar(@$new_users); $i++) {
$dbh->do(
"UPDATE ${type}_reviewers SET sortkey=? WHERE ${type}_id=? AND user_id=?",
undef,
($i + 1) * 10, $object->id, $new_users->[$i]->id,
);
}
}
# bugzilla's handling of requestee matching when creating bugs is "if it's
# wrong, or matches too many, default to empty", which breaks mandatory
# reviewer requirements. instead we just throw an error.
sub post_bug_attachment_flags {
my ($self, $args) = @_;
my $bug = $args->{bug};
my $cgi = Bugzilla->cgi;
# extract the set flag-types
my @flagtype_ids = map { /^flag_type-(\d+)$/ ? $1 : () } $cgi->param();
@flagtype_ids = grep { $cgi->param("flag_type-$_") ne 'X' } @flagtype_ids;
return unless scalar(@flagtype_ids);
# find valid review flagtypes
my $flag_types = Bugzilla::FlagType::match({
product_id => $bug->product_id,
component_id => $bug->component_id,
is_active => 1
});
foreach my $flag_type (@$flag_types) {
next unless $flag_type->name eq 'review'
&& $flag_type->target_type eq 'attachment';
my $type_id = $flag_type->id;
next unless scalar(grep { $_ == $type_id } @flagtype_ids);
my $reviewers = clean_text($cgi->param("requestee_type-$type_id") || '');
if ($reviewers eq '' && $bug->product_obj->reviewer_required) {
ThrowUserError('reviewer_required');
}
foreach my $reviewer (split(/[,;]+/, $reviewers)) {
# search on the reviewer
my $users = Bugzilla::User::match($reviewer, 2, 1);
# no matches
if (scalar(@$users) == 0) {
ThrowUserError('user_match_failed', { name => $reviewer });
}
# more than one match, throw error
if (scalar(@$users) > 1) {
ThrowUserError('user_match_too_many', { fields => [ 'review' ] });
}
}
}
}
sub flag_end_of_update {
my ($self, $args) = @_;
my ($attachment, $changes) = @$args{qw(object changes)};
if (exists $changes->{'flag.review'}
&& $changes->{'flag.review'}->[1] eq '?'
&& $attachment->bug->product_obj->reviewer_required)
{
ThrowUserError('reviewer_required');
}
}
#
# installation
#
sub db_schema_abstract_schema {
my ($self, $args) = @_;
$args->{'schema'}->{'product_reviewers'} = {
FIELDS => [
id => {
TYPE => 'MEDIUMSERIAL',
NOTNULL => 1,
PRIMARYKEY => 1,
},
user_id => {
TYPE => 'INT3',
NOTNULL => 1,
REFERENCES => {
TABLE => 'profiles',
COLUMN => 'userid',
DELETE => 'CASCADE',
}
},
display_name => {
TYPE => 'VARCHAR(64)',
},
product_id => {
TYPE => 'INT2',
NOTNULL => 1,
REFERENCES => {
TABLE => 'products',
COLUMN => 'id',
DELETE => 'CASCADE',
}
},
sortkey => {
TYPE => 'INT2',
NOTNULL => 1,
DEFAULT => 0,
},
],
INDEXES => [
product_reviewers_idx => {
FIELDS => [ 'user_id', 'product_id' ],
TYPE => 'UNIQUE',
},
],
};
$args->{'schema'}->{'component_reviewers'} = {
FIELDS => [
id => {
TYPE => 'MEDIUMSERIAL',
NOTNULL => 1,
PRIMARYKEY => 1,
},
user_id => {
TYPE => 'INT3',
NOTNULL => 1,
REFERENCES => {
TABLE => 'profiles',
COLUMN => 'userid',
DELETE => 'CASCADE',
}
},
display_name => {
TYPE => 'VARCHAR(64)',
},
component_id => {
TYPE => 'INT2',
NOTNULL => 1,
REFERENCES => {
TABLE => 'components',
COLUMN => 'id',
DELETE => 'CASCADE',
}
},
sortkey => {
TYPE => 'INT2',
NOTNULL => 1,
DEFAULT => 0,
},
],
INDEXES => [
component_reviewers_idx => {
FIELDS => [ 'user_id', 'component_id' ],
TYPE => 'UNIQUE',
},
],
};
}
sub install_update_db {
my $dbh = Bugzilla->dbh;
$dbh->bz_add_column(
'products',
'reviewer_required',
{
TYPE => 'BOOLEAN',
NOTNULL => 1,
DEFAULT => 'FALSE',
}
);
}
__PACKAGE__->NAME;
|