summaryrefslogtreecommitdiffstats
path: root/extensions/Review/lib/WebService.pm
blob: 4cb3d48d84873d8dbab76d0481f0631d5045c33b (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
# 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::WebService;

use strict;
use warnings;

use base qw(Bugzilla::WebService);

use Bugzilla::Bug;
use Bugzilla::Component;
use Bugzilla::Error;

sub suggestions {
    my ($self, $params) = @_;
    my $dbh = Bugzilla->switch_to_shadow_db();

    my ($bug, $product, $component);
    if (exists $params->{bug_id}) {
        $bug = Bugzilla::Bug->check($params->{bug_id});
        $product = $bug->product_obj;
        $component = $bug->component_obj;
    }
    elsif (exists $params->{product}) {
        $product = Bugzilla::Product->check($params->{product});
        if (exists $params->{component}) {
            $component = Bugzilla::Component->check({
                product => $product, name => $params->{component}
            });
        }
    }
    else {
        ThrowUserError("reviewer_suggestions_param_required");
    }

    my @reviewers;
    if ($bug) {
        # we always need to be authentiated to perform user matching
        my $user = Bugzilla->user;
        if (!$user->id) {
            Bugzilla->set_user(Bugzilla::User->check({ name => 'nobody@mozilla.org' }));
            push @reviewers, @{ $bug->mentors };
            Bugzilla->set_user($user);
        } else {
            push @reviewers, @{ $bug->mentors };
        }
    }
    if ($component) {
        push @reviewers, @{ $component->reviewers_objs };
    }
    if (!@{ $component->reviewers_objs }) {
        push @reviewers, @{ $product->reviewers_objs };
    }

    my @result;
    foreach my $reviewer (@reviewers) {
        push @result, {
            id    => $self->type('int', $reviewer->id),
            email => $self->type('email', $reviewer->login),
            name  => $self->type('string', $reviewer->name),
            review_count => $self->type('int', $reviewer->review_count),
        };
    }
    return \@result;
}

sub rest_resources {
    return [
        # bug-id
        qr{^/review/suggestions/(\d+)$}, {
            GET => {
                method => 'suggestions',
                params => sub {
                    return { bug_id => $_[0] };
                },
            },
        },
        # product/component
        qr{^/review/suggestions/([^/]+)/(.+)$}, {
            GET => {
                method => 'suggestions',
                params => sub {
                    return { product => $_[0], component => $_[1] };
                },
            },
        },
        # just product
        qr{^/review/suggestions/([^/]+)$}, {
            GET => {
                method => 'suggestions',
                params => sub {
                    return { product => $_[0] };
                },
            },
        },
        # named parameters
        qr{^/review/suggestions$}, {
            GET => {
                method => 'suggestions',
            },
        },
    ];
};

1;

__END__
=head1 NAME

Bugzilla::Extension::Review::WebService - Functions for the Mozilla specific
'review' flag optimisations.

=head1 METHODS

See L<Bugzilla::WebService> for a description of how parameters are passed,
and what B<STABLE>, B<UNSTABLE>, and B<EXPERIMENTAL> mean.

Although the data input and output is the same for JSONRPC, XMLRPC and REST,
the directions for how to access the data via REST is noted in each method
where applicable.

=head2 suggestions

B<EXPERIMENTAL>

=over

=item B<Description>

Returns the list of suggestions for reviewers.

=item B<REST>

GET /rest/review/suggestions/C<bug-id>

GET /rest/review/suggestions/C<product-name>

GET /rest/review/suggestions/C<product-name>/C<component-name>

GET /rest/review/suggestions?product=C<product-name>

GET /rest/review/suggestions?product=C<product-name>&component=C<component-name>

The returned data format is the same as below.

=item B<Params>

Query by Bug:

=over

=over

=item C<bug_id> (integer) - The bug ID.

=back

=back

Query by Product or Component:

=over

=over

=item C<product> (string) - The product name.

=item C<component> (string) - The component name (optional).  If providing a C<component>, a C<product> must also be provided.

=back

=back

=item B<Returns>

An array of hashes with the following keys/values:

=over

=item C<id> (integer) - The user's ID.

=item C<email> (string) - The user's email address (aka login).

=item C<name> (string) - The user's display name (may not match the Bugzilla "real name").

=item C<review_count> (string) - The number of "review" and "feedback" requests in the user's queue.

=back

=back