summaryrefslogtreecommitdiffstats
path: root/extensions/Review/Extension.pm
blob: b495c9ecdf7f77b0a5e0f6be24e746dd044622ef (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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
# 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::Extension::Review::Util;
use Bugzilla::Install::Filesystem;
use Bugzilla::User;
use Bugzilla::Util qw(clean_text);

use constant UNAVAILABLE_RE => qr/\b(?:unavailable|pto|away)\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::mentors               = \&_bug_mentors;
    *Bugzilla::Bug::is_mentor             = \&_bug_is_mentor;
    *Bugzilla::User::review_count         = \&_user_review_count;
}

#
# monkey-patched methods
#

sub _product_reviewers         { _reviewers($_[0],      'product',   $_[1])   }
sub _product_reviewers_objs    { _reviewers_objs($_[0], 'product',   $_[1])   }
sub _component_reviewers       { _reviewers($_[0],      'component', $_[1]) }
sub _component_reviewers_objs  { _reviewers_objs($_[0], 'component', $_[1]) }

sub _reviewers {
    my ($object, $type, $include_disabled) = @_;
    return join(', ', map { $_->login } @{ _reviewers_objs($object, $type, $include_disabled) });
}

sub _reviewers_objs {
    my ($object, $type, $include_disabled) = @_;
    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 = map { $user_map{$_} } @$user_ids;
        if (!$include_disabled) {
            @reviewers = grep { $_->is_enabled
                                && $_->name !~ UNAVAILABLE_RE } @reviewers;
        }
        $object->{reviewers} = \@reviewers;
    }
    return $object->{reviewers};
}

sub _bug_mentors {
    my ($self) = @_;
    # extract the mentors from the status_whiteboard
    # when the mentors gets its own field, this will be easier
    if (!exists $self->{mentors}) {
        my @mentors;
        my $whiteboard = $self->status_whiteboard;
        my $logout = 0;
        while ($whiteboard =~ /\[mentor=([^\]]+)\]/g) {
            my $mentor_string = $1;
            my $user;
            if ($mentor_string =~ /\@/) {
                # assume it's a full username if it contains an @
                $user = Bugzilla::User->new({ name => $mentor_string });
            } else {
                # otherwise assume it's a : prefixed nick.  only works if a
                # single user matches.

                # we need to be logged in to do user searching
                if (!Bugzilla->user->id) {
                    Bugzilla->set_user(Bugzilla::User->check({ name => 'nobody@mozilla.org' }));
                    $logout = 1;
                }

                foreach my $query ("*:$mentor_string*", "*$mentor_string*") {
                    my $matches = Bugzilla::User::match($query, 2);
                    if ($matches && scalar(@$matches) == 1) {
                        $user = $matches->[0];
                        last;
                    }
                }
            }
            push @mentors, $user if $user;
        }
        Bugzilla->logout_request() if $logout;
        $self->{mentors} = \@mentors;
    }
    return $self->{mentors};
}

sub _bug_is_mentor {
    my ($self, $user) = @_;
    my $user_id = ($user || Bugzilla->user)->id;
    return (grep { $_->id == $user_id} @{ $self->mentors }) ? 1 : 0;
}

sub _user_review_count {
    my ($self) = @_;
    if (!exists $self->{review_count}) {
        my $dbh = Bugzilla->dbh;
        ($self->{review_count}) = $dbh->selectrow_array(
            "SELECT COUNT(*)
               FROM flags
                    INNER JOIN flagtypes ON flagtypes.id = flags.type_id
              WHERE flags.requestee_id = ?
                    AND " . $dbh->sql_in('flagtypes.name', [ "'review'", "'feedback'" ]),
            undef,
            $self->id,
        );
    }
    return $self->{review_count};
}

#
# 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';
    }
    elsif ($class->isa('Bugzilla::User')) {
        push @$columns, qw(review_request_count feedback_request_count needinfo_request_count);
    }
}

sub object_update_columns {
    my ($self, $args) = @_;
    my ($object, $columns) = @$args{qw(object columns)};
    if ($object->isa('Bugzilla::Product')) {
        push @$columns, 'reviewer_required';
    }
    elsif ($object->isa('Bugzilla::User')) {
        push @$columns, qw(review_request_count feedback_request_count needinfo_request_count);
    }
}

#
# 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)};

    if ($object->isa('Bugzilla::Product') || $object->isa('Bugzilla::Component')) {
        my ($new, $new_users) = _new_reviewers_from_input();
        _update_reviewers($object, [], $new_users);
    }
    elsif (_is_countable_flag($object) && $object->requestee_id && $object->status eq '?') {
        _adjust_request_count($object, +1);
    }
}

sub object_end_of_update {
    my ($self, $args) = @_;
    my ($object, $old_object, $changes) = @$args{qw(object old_object changes)};

    if ($object->isa('Bugzilla::Product') || $object->isa('Bugzilla::Component')) {
        my ($new, $new_users) = _new_reviewers_from_input();
        my $old = $old_object->reviewers(1);
        if ($old ne $new) {
            _update_reviewers($object, $old_object->reviewers_objs(1), $new_users);
            $changes->{reviewers} = [ $old ? $old : undef, $new ? $new : undef ];
        }
    }
    elsif (_is_countable_flag($object)) {
        my ($old_status, $new_status) = ($old_object->status, $object->status);
        if ($old_status ne '?' && $new_status eq '?') {
            # setting flag to ?
            _adjust_request_count($object, +1);
        }
        elsif ($old_status eq '?' && $new_status ne '?') {
            # setting flag from ?
            _adjust_request_count($old_object, -1);
        }
        elsif ($old_object->requestee_id && !$object->requestee_id) {
            # removing requestee
            _adjust_request_count($old_object, -1);
        }
        elsif (!$old_object->requestee_id && $object->requestee_id) {
            # setting requestee
            _adjust_request_count($object, +1);
        }
        elsif ($old_object->requestee_id && $object->requestee_id
               && $old_object->requestee_id != $object->requestee_id)
        {
            # changing requestee
            _adjust_request_count($old_object, -1);
            _adjust_request_count($object, +1);
        }
    }
}

sub object_before_delete {
    my ($self, $args) = @_;
    my $object = $args->{object};

    if (_is_countable_flag($object) && $object->requestee_id && $object->status eq '?') {
        _adjust_request_count($object, -1);
    }
}

sub _is_countable_flag {
    my ($object) = @_;
    return unless $object->isa('Bugzilla::Flag');
    my $type_name = $object->type->name;
    return $type_name eq 'review' || $type_name eq 'feedback' || $type_name eq 'needinfo';
}

sub _adjust_request_count {
    my ($flag, $add) = @_;
    return unless my $requestee_id = $flag->requestee_id;
    my $field = $flag->type->name . '_request_count';

    # update the current user's object so things are display correctly on the
    # post-processing page
    my $user = Bugzilla->user;
    if ($requestee_id == $user->id) {
        $user->{$field} += $add;
    }

    # update database directly to avoid creating audit_log entries
    $add = $add == -1 ? ' - 1' : ' + 1';
    Bugzilla->dbh->do(
        "UPDATE profiles SET $field = $field $add WHERE userid = ?",
        undef,
        $requestee_id
    );
    Bugzilla->memcached->clear({ table => 'profiles', id => $requestee_id });
}

sub _new_reviewers_from_input {
    if (!Bugzilla->input_params->{reviewers}) {
        return ('', []);
    }
    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 ($object, $new_flags) = @$args{qw(object new_flags)};
    my $bug = $object->isa('Bugzilla::Attachment') ? $object->bug : $object;
    return unless $bug->product_obj->reviewer_required;

    foreach my $orig_change (@$new_flags) {
        my $change = $orig_change; # work on a copy
        $change =~ s/^[^:]+://;
        my $reviewer = '';
        if ($change =~ s/\(([^\)]+)\)$//) {
            $reviewer = $1;
        }
        my ($name, $value) = $change =~ /^(.+)(.)$/;

        if ($name eq 'review' && $value eq '?' && $reviewer eq '') {
            ThrowUserError('reviewer_required');
        }
    }
}

#
# web service / pages
#

sub webservice {
    my ($self,  $args) = @_;
    my $dispatch = $args->{dispatch};
    $dispatch->{Review} = "Bugzilla::Extension::Review::WebService";
}

sub page_before_template {
    my ($self, $args) = @_;

    if ($args->{page_id} eq 'review_suggestions.html') {
        $self->review_suggestions_report($args);
    }
    elsif ($args->{page_id} eq 'review_requests_rebuild.html') {
        $self->review_requests_rebuild($args);
    }
}

sub review_suggestions_report {
    my ($self, $args) = @_;

    my $user = Bugzilla->login(LOGIN_REQUIRED);
    my $products = [];
    my @products = sort { lc($a->name) cmp lc($b->name) }
                   @{ Bugzilla->user->get_accessible_products };
    foreach my $product_obj (@products) {
        my $has_reviewers = 0;
        my $product = {
            name       => $product_obj->name,
            components => [],
            reviewers  => $product_obj->reviewers_objs(1),
        };
        $has_reviewers = scalar @{ $product->{reviewers} };

        foreach my $component_obj (@{ $product_obj->components }) {
            my $component = {
                name       => $component_obj->name,
                reviewers  => $component_obj->reviewers_objs(1),
            };
            if (@{ $component->{reviewers} }) {
                push @{ $product->{components} }, $component;
                $has_reviewers = 1;
            }
        }

        if ($has_reviewers) {
            push @$products, $product;
        }
    }
    $args->{vars}->{products} = $products;
}

sub review_requests_rebuild {
    my ($self, $args) = @_;

    Bugzilla->user->in_group('admin')
        || ThrowUserError('auth_failure', { group  => 'admin',
                                            action => 'run',
                                            object => 'review_requests_rebuild' });
    if (Bugzilla->cgi->param('rebuild')) {
        my $processed_users = 0;
        rebuild_review_counters(sub {
            my ($count, $total) = @_;
            $processed_users = $total;
        });
        $args->{vars}->{rebuild} = 1;
        $args->{vars}->{total}   = $processed_users;
    }
}

#
# 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',
            },
        ],
    };

    $args->{'schema'}->{'flag_state_activity'} = {
        FIELDS => [
            id => {
                TYPE       => 'MEDIUMSERIAL',
                NOTNULL    => 1,
                PRIMARYKEY => 1,
            },

            flag_when => {
                TYPE    => 'DATETIME',
                NOTNULL => 1,
            },

            type_id => {
                TYPE       => 'INT2',
                NOTNULL    => 1,
                REFERENCES => {
                    TABLE  => 'flagtypes',
                    COLUMN => 'id',
                    DELETE => 'CASCADE'
                }
            },

            flag_id => {
                TYPE    => 'INT3',
                NOTNULL => 1,
            },

            setter_id => {
                TYPE       => 'INT3',
                NOTNULL    => 1,
                REFERENCES => {
                    TABLE  => 'profiles',
                    COLUMN => 'userid',
                },
            },

            requestee_id => {
                TYPE       => 'INT3',
                REFERENCES => {
                    TABLE  => 'profiles',
                    COLUMN => 'userid',
                },
            },

            bug_id => {
                TYPE       => 'INT3',
                NOTNULL    => 1,
                REFERENCES => {
                    TABLE  => 'bugs',
                    COLUMN => 'bug_id',
                    DELETE => 'CASCADE'
                }
            },

            attachment_id => {
                TYPE       => 'INT3',
                REFERENCES => {
                    TABLE  => 'attachments',
                    COLUMN => 'attach_id',
                    DELETE => 'CASCADE'
                }
            },

            status => {
                TYPE    => 'CHAR(1)',
                NOTNULL => 1,
            },
        ],
    };

}

sub install_update_db {
    my $dbh = Bugzilla->dbh;
    $dbh->bz_add_column(
        'products',
        'reviewer_required', { TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE' }
    );
    $dbh->bz_add_column(
        'profiles',
        'review_request_count', { TYPE => 'INT2', NOTNULL => 1, DEFAULT => 0 }
    );
    $dbh->bz_add_column(
        'profiles',
        'feedback_request_count', { TYPE => 'INT2', NOTNULL => 1, DEFAULT => 0 }
    );
    $dbh->bz_add_column(
        'profiles',
        'needinfo_request_count', { TYPE => 'INT2', NOTNULL => 1, DEFAULT => 0 }
    );
}

sub install_filesystem {
    my ($self, $args) = @_;
    my $files = $args->{files};
    my $extensions_dir = bz_locations()->{extensionsdir};
    $files->{"$extensions_dir/Review/bin/review_requests_rebuild.pl"} = {
        perms => Bugzilla::Install::Filesystem::OWNER_EXECUTE
    };
}


__PACKAGE__->NAME;