summaryrefslogtreecommitdiffstats
path: root/extensions/BMO/lib/WebService.pm
blob: a9387b3302a3178a9b0c8f509223c1ed28fef1bb (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
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis, 
# WITHOUT WARRANTY OF ANY KIND,  either express or implied. See the License for
# the specific language governing rights and limitations under the License.
#
# The Original Code is the BMO Bugzilla Extension.
#
# The Initial Developer of the Original Code is Mozilla Foundation.  Portions created
# by the Initial Developer are Copyright (C) 2011 the Mozilla Foundation. All
# Rights Reserved.
#
# Contributor(s):
#   Dave Lawrence <dkl@mozilla.com>

package Bugzilla::Extension::BMO::WebService;

use strict;
use warnings;

use base qw(Bugzilla::WebService);

use Bugzilla::Constants;
use Bugzilla::Error;
use Bugzilla::Util qw(detaint_natural trick_taint);
use Bugzilla::WebService::Util qw(validate);
use Bugzilla::Field;

sub getBugsConfirmer {
    my ($self, $params) = validate(@_, 'names');
    my $dbh = Bugzilla->dbh;

    defined($params->{names}) 
        || ThrowCodeError('params_required',
               { function => 'BMO.getBugsConfirmer', params => ['names'] });

    my @user_objects = map { Bugzilla::User->check($_) } @{ $params->{names} };

    # start filtering to remove duplicate user ids
    @user_objects = values %{{ map { $_->id => $_ } @user_objects }};

    my $fieldid = get_field_id('bug_status');

    my $query = "SELECT DISTINCT bugs_activity.bug_id
                   FROM bugs_activity
                        LEFT JOIN bug_group_map 
                        ON bugs_activity.bug_id = bug_group_map.bug_id
                  WHERE bugs_activity.fieldid = ?
                        AND bugs_activity.added = 'NEW'
                        AND bugs_activity.removed = 'UNCONFIRMED'
                        AND bugs_activity.who = ?
                        AND bug_group_map.bug_id IS NULL
               ORDER BY bugs_activity.bug_id";

    my %users;
    foreach my $user (@user_objects) {
        my $bugs = $dbh->selectcol_arrayref($query, undef, $fieldid, $user->id);
        $users{$user->login} = $bugs;
    }

    return \%users;
}

sub getBugsVerifier {
    my ($self, $params) = validate(@_, 'names');
    my $dbh = Bugzilla->dbh;

    defined($params->{names}) 
        || ThrowCodeError('params_required',
               { function => 'BMO.getBugsVerifier', params => ['names'] });

    my @user_objects = map { Bugzilla::User->check($_) } @{ $params->{names} };

    # start filtering to remove duplicate user ids
    @user_objects = values %{{ map { $_->id => $_ } @user_objects }};

    my $fieldid = get_field_id('bug_status');

    my $query = "SELECT DISTINCT bugs_activity.bug_id
                   FROM bugs_activity
                        LEFT JOIN bug_group_map 
                        ON bugs_activity.bug_id = bug_group_map.bug_id
                  WHERE bugs_activity.fieldid = ?
                        AND bugs_activity.removed = 'RESOLVED'
                        AND bugs_activity.added = 'VERIFIED'
                        AND bugs_activity.who = ?
                        AND bug_group_map.bug_id IS NULL
               ORDER BY bugs_activity.bug_id";

    my %users;
    foreach my $user (@user_objects) {
        my $bugs = $dbh->selectcol_arrayref($query, undef, $fieldid, $user->id);
        $users{$user->login} = $bugs;
    }

    return \%users;
}

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

    my $search = $params->{'search'};
    $search || ThrowCodeError('param_required',
        { function => 'Bug.prod_comp_search', param => 'search' });

    my $limit = detaint_natural($params->{'limit'}) 
                ? $dbh->sql_limit($params->{'limit'}) 
                : '';

    # We do this in the DB directly as we want it to be fast and
    # not have the overhead of loading full product objects
 
    # All products which the user has "Entry" access to.
    my $enterable_ids = $dbh->selectcol_arrayref(
           'SELECT products.id FROM products
         LEFT JOIN group_control_map
                   ON group_control_map.product_id = products.id
                      AND group_control_map.entry != 0
                      AND group_id NOT IN (' . $user->groups_as_string . ')
            WHERE group_id IS NULL
                  AND products.isactive = 1');

    if (scalar @$enterable_ids) {
        # And all of these products must have at least one component
        # and one version.
        $enterable_ids = $dbh->selectcol_arrayref(
            'SELECT DISTINCT products.id FROM products
              WHERE ' . $dbh->sql_in('products.id', $enterable_ids) .
              ' AND products.id IN (SELECT DISTINCT components.product_id
                                      FROM components
                                     WHERE components.isactive = 1)
                AND products.id IN (SELECT DISTINCT versions.product_id
                                      FROM versions
                                     WHERE versions.isactive = 1)');
    }

    return { products => [] } if !scalar @$enterable_ids;

    my @list;
    foreach my $word (split(/[\s,]+/, $search)) {
        if ($word ne "") {
            my $sql_word = $dbh->quote($word);
            trick_taint($sql_word);
            # XXX CONCAT_WS is MySQL specific
            my $field = "CONCAT_WS(' ', products.name, components.name, components.description)";
            push(@list, $dbh->sql_iposition($sql_word, $field) . " > 0");
        }
    }

    my $products = $dbh->selectall_arrayref("
        SELECT products.name AS product,
               components.name AS component
          FROM products 
               INNER JOIN components ON products.id = components.product_id
         WHERE (" . join(" AND ", @list) . ")
               AND products.id IN (" . join(",", @$enterable_ids) . ")
      ORDER BY products.name $limit", 
        { Slice => {} });

    # To help mozilla staff file bmo administration bugs into the right
    # component, sort bmo in front of bugzilla.
    if ($user->in_group('mozilla-corporation') || $user->in_group('mozilla-foundation')) {
        $products = [
            sort {
                return 1 if $a->{product} eq 'Bugzilla'
                            && $b->{product} eq 'bugzilla.mozilla.org';
                return -1 if $b->{product} eq 'Bugzilla'
                             && $a->{product} eq 'bugzilla.mozilla.org';
                return lc($a->{product}) cmp lc($b->{product})
                       || lc($a->{component}) cmp lc($b->{component});
            } @$products
        ];
    }

    return { products => $products };
}

1;

__END__

=head1 NAME

Bugzilla::Extension::BMO::Webservice - The BMO WebServices API

=head1 DESCRIPTION

This module contains API methods that are useful to user's of bugzilla.mozilla.org.

=head1 METHODS

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

=head2 getBugsConfirmer

B<UNSTABLE>

=over

=item B<Description>

This method returns public bug ids that a given user has confirmed (changed from 
C<UNCONFIRMED> to C<NEW>).

=item B<Params>

You pass a field called C<names> that is a list of Bugzilla login names to find bugs for.

=over

=item C<names> (array) - An array of strings representing Bugzilla login names. 

=back

=item B<Returns>

=over

A hash of Bugzilla login names. Each name points to an array of bug ids that the user has confirmed.

=back

=item B<Errors>

=over

=back

=item B<History>

=over

=item Added in BMO Bugzilla B<4.0>.

=back

=back

=head2 getBugsVerifier

B<UNSTABLE>

=over

=item B<Description>

This method returns public bug ids that a given user has verified (changed from
C<RESOLVED> to C<VERIFIED>). 

=item B<Params>

You pass a field called C<names> that is a list of Bugzilla login names to find bugs for.

=over

=item C<names> (array) - An array of strings representing Bugzilla login names. 

=back

=item B<Returns>

=over

A hash of Bugzilla login names. Each name points to an array of bug ids that the user has verified.

=back

=item B<Errors>

=over

=back

=item B<History>

=over

=item Added in BMO Bugzilla B<4.0>.

=back

=back