-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathBug.pm
More file actions
5169 lines (3508 loc) · 135 KB
/
Bug.pm
File metadata and controls
5169 lines (3508 loc) · 135 KB
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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::WebService::Bug;
use 5.10.1;
use strict;
use warnings;
use base qw(Bugzilla::WebService);
use Bugzilla::Comment;
use Bugzilla::Comment::TagWeights;
use Bugzilla::Constants;
use Bugzilla::Error;
use Bugzilla::Field;
use Bugzilla::WebService::Constants;
use Bugzilla::WebService::Util
qw(extract_flags filter filter_wants validate translate);
use Bugzilla::Bug;
use Bugzilla::BugMail;
use Bugzilla::Util qw(trim detaint_natural remote_ip);
use Bugzilla::Version;
use Bugzilla::Milestone;
use Bugzilla::Status;
use Bugzilla::Token qw(issue_hash_token);
use Bugzilla::Search;
use Bugzilla::Product;
use Bugzilla::FlagType;
use Bugzilla::Search::Quicksearch;
use List::Util qw(max);
use List::MoreUtils qw(uniq);
use Storable qw(dclone);
use Types::Standard -all;
use Type::Utils;
#############
# Constants #
#############
use constant PRODUCT_SPECIFIC_FIELDS => qw(version target_milestone component);
sub DATE_FIELDS {
my $fields = {
comments => ['new_since'],
create => [],
history => ['new_since'],
search => ['last_change_time', 'creation_time'],
update => []
};
# Add date related custom fields
foreach my $field (Bugzilla->active_custom_fields({skip_extensions => 1})) {
next
unless ($field->type == FIELD_TYPE_DATETIME
|| $field->type == FIELD_TYPE_DATE);
push(@{$fields->{create}}, $field->name);
push(@{$fields->{update}}, $field->name);
}
return $fields;
}
use constant BASE64_FIELDS => {add_attachment => ['data'],};
use constant READ_ONLY => qw(
attachments
comments
fields
get
history
legal_values
search
);
use constant PUBLIC_METHODS => qw(
add_attachment
add_comment
attachments
comments
create
fields
get
history
legal_values
possible_duplicates
render_comment
search
search_comment_tags
update
update_attachment
update_comment_tags
update_see_also
);
use constant ATTACHMENT_MAPPED_SETTERS =>
{file_name => 'filename', summary => 'description',};
use constant ATTACHMENT_MAPPED_RETURNS => {
description => 'summary',
ispatch => 'is_patch',
isprivate => 'is_private',
isobsolete => 'is_obsolete',
filename => 'file_name',
mimetype => 'content_type',
};
our $api_field_types = sub {
Bugzilla::request_cache->{api_field_types} ||= {
%{{map { $_ => 'double' } Bugzilla::Bug::NUMERIC_COLUMNS()}},
%{{map { $_ => 'dateTime' } Bugzilla::Bug::DATE_COLUMNS()}},
};
};
our $api_field_names = sub {
Bugzilla::request_cache->{api_field_types} ||= do {
my $tmp = {reverse %{Bugzilla::Bug::FIELD_MAP()}};
# This doesn't normally belong in FIELD_MAP, but we do want to translate
# "bug_group" back into "groups".
$tmp->{'bug_group'} = 'groups';
$tmp;
};
};
######################################################
# Add aliases here for old method name compatibility #
######################################################
BEGIN {
# In 3.0, get was called get_bugs
*get_bugs = \&get;
# Before 3.4rc1, "history" was get_history.
*get_history = \&history;
}
###########
# Methods #
###########
sub fields {
my ($self, $params) = validate(@_, 'ids', 'names');
Bugzilla->switch_to_shadow_db();
my @fields;
if (defined $params->{ids}) {
my $ids = $params->{ids};
foreach my $id (@$ids) {
my $loop_field = Bugzilla::Field->check({id => $id});
push(@fields, $loop_field);
}
}
if (defined $params->{names}) {
my $names = $params->{names};
foreach my $field_name (@$names) {
my $loop_field = Bugzilla::Field->check($field_name);
# Don't push in duplicate fields if we also asked for this field
# in "ids".
if (!grep($_->id == $loop_field->id, @fields)) {
push(@fields, $loop_field);
}
}
}
if (!defined $params->{ids} and !defined $params->{names}) {
@fields = @{Bugzilla->fields({obsolete => 0})};
}
my @fields_out;
foreach my $field (@fields) {
my $visibility_field
= $field->visibility_field ? $field->visibility_field->name : undef;
my $vis_values = $field->visibility_values;
my $value_field = $field->value_field ? $field->value_field->name : undef;
my (@values, $has_values);
if ( ($field->is_select and $field->name ne 'product')
or grep($_ eq $field->name, PRODUCT_SPECIFIC_FIELDS)
or $field->name eq 'keywords')
{
$has_values = 1;
@values = @{$self->_legal_field_values({field => $field})};
}
if (grep($_ eq $field->name, PRODUCT_SPECIFIC_FIELDS)) {
$value_field = 'product';
}
my %field_data = (
id => $self->type('int', $field->id),
type => $self->type('int', $field->type),
is_custom => $self->type('boolean', $field->custom),
name => $self->type('string', $field->name),
display_name => $self->type('string', $field->description),
is_mandatory => $self->type('boolean', $field->is_mandatory),
is_on_bug_entry => $self->type('boolean', $field->enter_bug),
visibility_field => $self->type('string', $visibility_field),
visibility_values => [map { $self->type('string', $_->name) } @$vis_values],
);
if ($has_values) {
$field_data{value_field} = $self->type('string', $value_field);
$field_data{values} = \@values;
}
push(@fields_out, filter $params, \%field_data);
}
return {fields => \@fields_out};
}
sub _legal_field_values {
my ($self, $params) = @_;
my $field = $params->{field};
my $field_name = $field->name;
my $user = Bugzilla->user;
my @result;
if (grep($_ eq $field_name, PRODUCT_SPECIFIC_FIELDS)) {
my @list;
if ($field_name eq 'version') {
@list = Bugzilla::Version->get_all;
}
elsif ($field_name eq 'component') {
@list = Bugzilla::Component->get_all;
}
else {
@list = Bugzilla::Milestone->get_all;
}
foreach my $value (@list) {
my $sortkey = $field_name eq 'target_milestone' ? $value->sortkey : 0;
# XXX This is very slow for large numbers of values.
my $product_name = $value->product->name;
if ($user->can_see_product($product_name)) {
push(
@result,
{
name => $self->type('string', $value->name),
sort_key => $self->type('int', $sortkey),
sortkey => $self->type('int', $sortkey), # deprecated
visibility_values => [$self->type('string', $product_name)],
}
);
}
}
}
elsif ($field_name eq 'bug_status') {
my @status_all = Bugzilla::Status->get_all;
foreach my $status (@status_all) {
my @can_change_to;
foreach my $change_to (@{$status->can_change_to}) {
# There's no need to note that a status can transition
# to itself.
next if $change_to->id == $status->id;
my %change_to_hash = (
name => $self->type('string', $change_to->name),
comment_required =>
$self->type('boolean', $change_to->comment_required_on_change_from($status)),
);
push(@can_change_to, \%change_to_hash);
}
push(
@result,
{
name => $self->type('string', $status->name),
is_open => $self->type('boolean', $status->is_open),
sort_key => $self->type('int', $status->sortkey),
sortkey => $self->type('int', $status->sortkey), # deprecated
can_change_to => \@can_change_to,
visibility_values => [],
}
);
}
}
elsif ($field_name eq 'keywords') {
my @legal_keywords = Bugzilla::Keyword->get_all;
foreach my $value (@legal_keywords) {
next unless $value->is_active;
push(
@result,
{
name => $self->type('string', $value->name),
description => $self->type('string', $value->description),
}
);
}
}
else {
my @values = Bugzilla::Field::Choice->type($field)->get_all();
foreach my $value (@values) {
my $vis_val = $value->visibility_value;
push(
@result,
{
name => $self->type('string', $value->name),
sort_key => $self->type('int', $value->sortkey),
sortkey => $self->type('int', $value->sortkey), # deprecated
visibility_values =>
[defined $vis_val ? $self->type('string', $vis_val->name) : ()],
}
);
}
}
return \@result;
}
sub comments {
my ($self, $params) = validate(@_, 'ids', 'comment_ids');
if (!(defined $params->{ids} || defined $params->{comment_ids})) {
ThrowCodeError('params_required',
{function => 'Bug.comments', params => ['ids', 'comment_ids']});
}
my $bug_ids = $params->{ids} || [];
my $comment_ids = $params->{comment_ids} || [];
my $skip_private = $params->{skip_private} ? 1 : 0;
my $dbh = Bugzilla->switch_to_shadow_db();
my $user = Bugzilla->user;
unless (Bugzilla->user->id) {
Bugzilla->check_rate_limit("get_comments", remote_ip());
}
if ($skip_private) {
# Cache permissions for bugs. This highly reduces the number of calls to the DB.
# visible_bugs() is only able to handle bug IDs, so we have to skip aliases.
my @int = grep { $_ =~ /^\d+$/ } @$bug_ids;
$user->visible_bugs(\@int);
}
my %bugs;
foreach my $bug_id (@$bug_ids) {
my $bug;
if ($skip_private) {
$bug = Bugzilla::Bug->new({id => $bug_id, cache => 1});
next if $bug->error || !$user->can_see_bug($bug->id);
}
else {
$bug = Bugzilla::Bug->check($bug_id);
}
# We want the API to always return comments in the same order.
my $comments
= $bug->comments({order => 'oldest_to_newest', after => $params->{new_since}
});
my @result;
foreach my $comment (@$comments) {
next if $comment->is_private && !$user->is_insider;
push(@result, $self->_translate_comment($comment, $params));
}
$bugs{$bug->id}{'comments'} = \@result;
}
my %comments;
if (scalar @$comment_ids) {
my @ids = map { trim($_) } @$comment_ids;
my $comment_data = Bugzilla::Comment->new_from_list(\@ids);
# See if we were passed any invalid comment ids.
my %got_ids = map { $_->id => 1 } @$comment_data;
foreach my $comment_id (@ids) {
if (!$got_ids{$comment_id}) {
ThrowUserError('comment_id_invalid', {id => $comment_id});
}
}
# Now make sure that we can see all the associated bugs.
my %got_bug_ids = map { $_->bug_id => 1 } @$comment_data;
Bugzilla::Bug->check($_) foreach (keys %got_bug_ids);
foreach my $comment (@$comment_data) {
if ($comment->is_private && !$user->is_insider) {
ThrowUserError('comment_is_private', {id => $comment->id});
}
$comments{$comment->id} = $self->_translate_comment($comment, $params);
}
}
return {bugs => \%bugs, comments => \%comments};
}
sub render_comment {
my ($self, $params) = @_;
unless (defined $params->{text}) {
ThrowCodeError('params_required',
{function => 'Bug.render_comment', params => ['text']});
}
Bugzilla->switch_to_shadow_db();
my $bug = $params->{id} ? Bugzilla::Bug->check($params->{id}) : undef;
my $html
= Bugzilla->params->{use_markdown}
? Bugzilla->markdown->render_html($params->{text}, $bug)
: Bugzilla::Template::quoteUrls($params->{text}, $bug);
return {html => $html};
}
# Helper for Bug.comments
sub _translate_comment {
my ($self, $comment, $filters, $types, $prefix) = @_;
my $attach_id = $comment->is_about_attachment ? $comment->extra_data : undef;
my $comment_hash = {
id => $self->type('int', $comment->id),
bug_id => $self->type('int', $comment->bug_id),
creator => $self->type('email', $comment->author->login),
author => $self->type('email', $comment->author->login),
time => $self->type('dateTime', $comment->creation_ts),
creation_time => $self->type('dateTime', $comment->creation_ts),
is_private => $self->type('boolean', $comment->is_private),
text => $self->type('string', $comment->body_full),
raw_text => $self->type('string', $comment->body),
attachment_id => $self->type('int', $attach_id),
count => $self->type('int', $comment->count),
};
# Don't load comment tags unless enabled
if (Bugzilla->params->{'comment_taggers_group'}) {
$comment_hash->{tags} = [map { $self->type('string', $_) } @{$comment->tags}];
}
return filter($filters, $comment_hash, $types, $prefix);
}
sub get {
my ($self, $params) = validate(@_, 'ids');
unless (Bugzilla->user->id) {
Bugzilla->check_rate_limit("get_bug", remote_ip());
}
Bugzilla->switch_to_shadow_db() unless Bugzilla->user->id;
my $ids = $params->{ids};
(defined $ids && scalar @$ids)
|| ThrowCodeError('param_required', {param => 'ids'});
my (@bugs, @faults, @hashes);
# Cache permissions for bugs. This highly reduces the number of calls to the DB.
# visible_bugs() is only able to handle bug IDs, so we have to skip aliases.
my @int = grep { $_ =~ /^\d+$/ } @$ids;
Bugzilla->user->visible_bugs(\@int);
foreach my $bug_id (@$ids) {
my $bug;
if ($params->{permissive}) {
eval { $bug = Bugzilla::Bug->check($bug_id); };
if ($@) {
push(@faults,
{id => $bug_id, faultString => $@->faultstring, faultCode => $@->faultcode,});
undef $@;
next;
}
}
else {
$bug = Bugzilla::Bug->check($bug_id);
}
push(@bugs, $bug);
push(@hashes, $self->_bug_to_hash($bug, $params));
}
# Set the ETag before inserting the update tokens
# since the tokens will always be unique even if
# the data has not changed.
$self->bz_etag(\@hashes);
$self->_add_update_tokens($params, \@bugs, \@hashes);
if (Bugzilla->user->id) {
foreach my $bug (@bugs) {
Bugzilla->log_user_request($bug->id, undef, 'bug-get');
}
}
return {bugs => \@hashes, faults => \@faults};
}
# this is a function that gets bug activity for list of bug ids
# it can be called as the following:
# $call = $rpc->call( 'Bug.history', { ids => [1,2] });
sub history {
my ($self, $params) = validate(@_, 'ids');
Bugzilla->switch_to_shadow_db();
my $ids = $params->{ids};
defined $ids || ThrowCodeError('param_required', {param => 'ids'});
my $user = Bugzilla->user;
my $skip_private = $params->{skip_private} ? 1 : 0;
if ($skip_private) {
# Cache permissions for bugs. This highly reduces the number of calls to the DB.
# visible_bugs() is only able to handle bug IDs, so we have to skip aliases.
my @int = grep { $_ =~ /^\d+$/ } @$ids;
$user->visible_bugs(\@int);
}
my @return;
foreach my $bug_id (@$ids) {
my %item;
my $bug;
if ($skip_private) {
$bug = Bugzilla::Bug->new({id => $bug_id, cache => 1});
next if $bug->error || !$user->can_see_bug($bug->id);
}
else {
$bug = Bugzilla::Bug->check($bug_id);
}
$bug_id = $bug->id;
$item{id} = $self->type('int', $bug_id);
my ($activity)
= Bugzilla::Bug::GetBugActivity($bug_id, undef, $params->{new_since}, 1);
my @history;
foreach my $changeset (@$activity) {
push(@history, $self->_changeset_to_hash($changeset, $params));
}
$item{history} = \@history;
# alias is returned in case users passes a mixture of ids and aliases
# then they get to know which bug activity relates to which value
# they passed
if (Bugzilla->params->{'usebugaliases'}) {
$item{alias} = $self->type('string', $bug->alias);
}
else {
# For API reasons, we always want the value to appear, we just
# don't want it to have a value if aliases are turned off.
$item{alias} = undef;
}
push(@return, \%item);
}
return {bugs => \@return};
}
sub search {
my ($self, $params) = @_;
my $user = Bugzilla->user;
my $dbh = Bugzilla->dbh;
Bugzilla->switch_to_shadow_db();
my $match_params = dclone($params);
delete $match_params->{include_fields};
delete $match_params->{exclude_fields};
# Determine whether this is a quicksearch query
if (exists $match_params->{quicksearch}) {
my $quicksearch = quicksearch($match_params->{'quicksearch'});
my $cgi = Bugzilla::CGI->new($quicksearch);
$match_params = $cgi->Vars;
}
if (defined($match_params->{offset}) and !defined($match_params->{limit})) {
ThrowCodeError('param_required',
{param => 'limit', function => 'Bug.search()'});
}
my $max_results = Bugzilla->params->{max_search_results};
unless (defined $match_params->{limit} && $match_params->{limit} == 0) {
if (!defined $match_params->{limit} || $match_params->{limit} > $max_results) {
$match_params->{limit} = $max_results;
}
}
else {
delete $match_params->{limit};
delete $match_params->{offset};
}
# Allow to search only in bug description (initial comment)
if (defined $match_params->{description}) {
$match_params->{longdesc} = delete $match_params->{description};
$match_params->{longdesc_initial} = 1;
}
$match_params = Bugzilla::Bug::map_fields($match_params);
my %options = (fields => ['bug_id']);
# Find the highest custom field id
my @field_ids = grep(/^f(\d+)$/, keys %$match_params);
my $last_field_id = @field_ids ? max @field_ids + 1 : 1;
# Do special search types for certain fields.
if (my $change_when = delete $match_params->{'delta_ts'}) {
$match_params->{"f${last_field_id}"} = 'delta_ts';
$match_params->{"o${last_field_id}"} = 'greaterthaneq';
$match_params->{"v${last_field_id}"} = $change_when;
$last_field_id++;
}
if (my $creation_when = delete $match_params->{'creation_ts'}) {
$match_params->{"f${last_field_id}"} = 'creation_ts';
$match_params->{"o${last_field_id}"} = 'greaterthaneq';
$match_params->{"v${last_field_id}"} = $creation_when;
$last_field_id++;
}
# Some fields require a search type such as short desc, keywords, etc.
foreach my $param (qw(short_desc longdesc status_whiteboard bug_file_loc)) {
if (defined $match_params->{$param}
&& !defined $match_params->{$param . '_type'})
{
$match_params->{$param . '_type'} = 'allwordssubstr';
}
}
if (defined $match_params->{'keywords'}
&& !defined $match_params->{'keywords_type'})
{
$match_params->{'keywords_type'} = 'allwords';
}
# Backwards compatibility with old method regarding role search
$match_params->{'reporter'} = delete $match_params->{'creator'}
if $match_params->{'creator'};
foreach my $role (qw(assigned_to reporter qa_contact triage_owner commenter cc))
{
next if !exists $match_params->{$role};
my $value = delete $match_params->{$role};
$match_params->{"f${last_field_id}"} = $role;
$match_params->{"o${last_field_id}"} = "anywordssubstr";
$match_params->{"v${last_field_id}"}
= ref $value ? join(" ", @{$value}) : $value;
$last_field_id++;
}
# If no other parameters have been passed other than limit and offset
# then we throw error if system is configured to do so.
if ( !grep(!/^(limit|offset)$/, keys %$match_params)
&& !Bugzilla->params->{search_allow_no_criteria})
{
ThrowUserError('buglist_parameters_required');
}
# Allow the use of order shortcuts similar to web UI
if ($match_params->{order}) {
# Convert the value of the "order" form field into a list of columns
# by which to sort the results.
my %order_types = (
"Bug Number" => ["bug_id"],
"Importance" => ["priority", "bug_severity"],
"Assignee" => ["assigned_to", "bug_status", "priority", "bug_id"],
"Last Updated" =>
["changeddate", "bug_status", "priority", "assigned_to", "bug_id"],
);
if ($order_types{$match_params->{order}}) {
$options{order} = $order_types{$match_params->{order}};
}
else {
$options{order} = [split(/\s*,\s*/, $match_params->{order})];
}
}
$options{params} = $match_params;
my $search = new Bugzilla::Search(%options);
my ($data) = $search->data;
# BMO if the caller only wants the count, that's all we need to return
if ($params->{count_only}) {
if (Bugzilla->usage_mode == USAGE_MODE_XMLRPC) {
return $data;
}
else {
return {bug_count => $self->type('int', $data)};
}
}
if (!scalar @$data) {
return {bugs => []};
}
# Search.pm won't return bugs that the user shouldn't see so no filtering is needed.
my @bug_ids = map { $_->[0] } @$data;
my %bug_objects
= map { $_->id => $_ } @{Bugzilla::Bug->new_from_list(\@bug_ids)};
my @bugs = map { $bug_objects{$_} } @bug_ids;
@bugs = map { $self->_bug_to_hash($_, $params) } @bugs;
# BzAPI
Bugzilla->request_cache->{bzapi_search_bugs}
= [map { $bug_objects{$_} } @bug_ids];
return {bugs => \@bugs};
}
sub possible_duplicates {
my ($self, $params) = validate(@_, 'product');
my $user = Bugzilla->user;
Bugzilla->switch_to_shadow_db();
state $params_type = Dict [
id => Optional [Int],
product => Optional [ArrayRef [Str]],
limit => Optional [Int],
summary => Optional [Str],
include_fields => Optional [ArrayRef [Str]],
Bugzilla_api_token => Optional [Str]
];
ThrowCodeError('param_invalid',
{function => 'Bug.possible_duplicates', param => 'A param'})
if !$params_type->check($params);
my $summary;
if ($params->{id}) {
my $bug = Bugzilla::Bug->check({id => $params->{id}, cache => 1});
$summary = $bug->short_desc;
}
elsif ($params->{summary}) {
$summary = $params->{summary};
}
else {
ThrowCodeError('param_required',
{function => 'Bug.possible_duplicates', param => 'id or summary'});
}
my @products;
foreach my $name (@{$params->{'product'} || []}) {
my $object = $user->can_enter_product($name, THROW_ERROR);
push(@products, $object);
}
my $possible_dupes
= Bugzilla::Bug->possible_duplicates({
summary => $summary, products => \@products, limit => $params->{limit}
});
# If a bug id was used, remove the bug with the same id from the list.
if ($params->{id}) {
@$possible_dupes = grep { $_->id != $params->{id} } @$possible_dupes;
}
my @hashes = map { $self->_bug_to_hash($_, $params) } @$possible_dupes;
$self->_add_update_tokens($params, $possible_dupes, \@hashes);
return {bugs => \@hashes};
}
sub update {
my ($self, $params) = validate(@_, 'ids');
# BMO: Don't allow updating of bugs if disabled
if (Bugzilla->params->{disable_bug_updates}) {
ThrowErrorPage(
'bug/process/updates-disabled.html.tmpl',
'Bug updates are currently disabled.'
);
}
my $user = Bugzilla->login(LOGIN_REQUIRED);
my $dbh = Bugzilla->dbh;
# We skip certain fields because their set_ methods actually use
# the external names instead of the internal names.
$params = Bugzilla::Bug::map_fields($params,
{summary => 1, platform => 1, severity => 1, type => 1, url => 1});
my $ids = delete $params->{ids};
defined $ids || ThrowCodeError('param_required', {param => 'ids'});
my @bugs = map { Bugzilla::Bug->check($_) } @$ids;
my %values = %$params;
$values{other_bugs} = \@bugs;
if (exists $values{comment} and exists $values{comment}{comment}) {
$values{comment}{body} = delete $values{comment}{comment};
}
# Prevent bugs that could be triggered by specifying fields that
# have valid "set_" functions in Bugzilla::Bug, but shouldn't be
# called using those field names.
delete $values{dependencies};
my $flags = delete $values{flags};
foreach my $bug (@bugs) {
if (!$user->can_edit_product($bug->product_obj->id)) {
ThrowUserError("product_edit_denied", {product => $bug->product});
}
$bug->set_all(\%values);
if ($flags) {
my ($old_flags, $new_flags) = extract_flags($flags, $bug);
$bug->set_flags($old_flags, $new_flags);
}
}
my %all_changes;
$dbh->bz_start_transaction();
foreach my $bug (@bugs) {
$all_changes{$bug->id} = $bug->update();
}
$dbh->bz_commit_transaction();
foreach my $bug (@bugs) {
$bug->send_changes($all_changes{$bug->id});
}
my @result;
foreach my $bug (@bugs) {
my %hash = (
id => $self->type('int', $bug->id),
last_change_time => $self->type('dateTime', $bug->delta_ts),
changes => {},
);
# alias is returned in case users pass a mixture of ids and aliases,
# so that they can know which set of changes relates to which value
# they passed.
if (Bugzilla->params->{'usebugaliases'}) {
$hash{alias} = $self->type('string', $bug->alias);
}
else {
# For API reasons, we always want the alias field to appear, we
# just don't want it to have a value if aliases are turned off.
$hash{alias} = $self->type('string', '');
}
my %changes = %{$all_changes{$bug->id}};
my $names = $api_field_names->();
foreach my $field (keys %changes) {
my $change = $changes{$field};
my $api_field = $names->{$field} || $field;
# We normalize undef to an empty string, so that the API
# stays consistent for things like Deadline that can become
# empty.
$change->[0] = '' if !defined $change->[0];
$change->[1] = '' if !defined $change->[1];
$hash{changes}->{$api_field} = {
removed => $self->type('string', $change->[0]),
added => $self->type('string', $change->[1])
};
}
push(@result, \%hash);
}
return {bugs => \@result};
}
sub create {
my ($self, $params) = @_;
my $dbh = Bugzilla->dbh;
# BMO: Don't allow updating of bugs if disabled
if (Bugzilla->params->{disable_bug_updates}) {
ThrowErrorPage(
'bug/process/updates-disabled.html.tmpl',
'Bug updates are currently disabled.'
);
}
Bugzilla->login(LOGIN_REQUIRED);
# Some fields cannot be sent to Bugzilla::Bug->create
foreach my $key (qw(login password token)) {
delete $params->{$key};
}
$params = Bugzilla::Bug::map_fields($params);
# Define the bug file method if missing
$params->{filed_via} //= 'api';
my $flags = delete $params->{flags};
# We start a nested transaction in case flag setting fails
# we want the bug creation to roll back as well.
$dbh->bz_start_transaction();
my $bug = Bugzilla::Bug->create($params);
# Set bug flags
if ($flags) {
my ($flags, $new_flags) = extract_flags($flags, $bug);
$bug->set_flags($flags, $new_flags);
$bug->update($bug->creation_ts);
}
$dbh->bz_commit_transaction();
$bug->send_changes();
return {id => $self->type('int', $bug->bug_id)};
}
sub legal_values {
my ($self, $params) = @_;
Bugzilla->switch_to_shadow_db();
defined $params->{field}
or ThrowCodeError('param_required', {param => 'field'});
my $field = Bugzilla::Bug::FIELD_MAP->{$params->{field}} || $params->{field};
my @global_selects = @{Bugzilla->fields({is_select => 1, is_abnormal => 0})};
my $values;
if (grep($_->name eq $field, @global_selects)) {
# The field is a valid one.
$values = get_legal_field_values($field);
}
elsif (grep($_ eq $field, PRODUCT_SPECIFIC_FIELDS)) {
my $id = $params->{product_id};
defined $id
|| ThrowCodeError('param_required',
{function => 'Bug.legal_values', param => 'product_id'});
grep($_->id eq $id, @{Bugzilla->user->get_accessible_products})
|| ThrowUserError('product_access_denied', {id => $id});
my $product = new Bugzilla::Product($id);
my @objects;
if ($field eq 'version') {
@objects = @{$product->versions};
}
elsif ($field eq 'target_milestone') {
@objects = @{$product->milestones};
}
elsif ($field eq 'component') {
@objects = @{$product->components};
}
$values = [map { $_->name } @objects];
}
else {
ThrowCodeError('invalid_field_name', {field => $params->{field}});
}
my @result;
foreach my $val (@$values) {
push(@result, $self->type('string', $val));
}
return {values => \@result};
}
sub add_attachment {
my ($self, $params) = validate(@_, 'ids');
my $dbh = Bugzilla->dbh;
# BMO: Don't allow updating of bugs if disabled
if (Bugzilla->params->{disable_bug_updates}) {
ThrowErrorPage(
'bug/process/updates-disabled.html.tmpl',
'Bug updates are currently disabled.'
);
}
Bugzilla->login(LOGIN_REQUIRED);
defined $params->{ids} || ThrowCodeError('param_required', {param => 'ids'});
defined $params->{data} || ThrowCodeError('param_required', {param => 'data'});
my @bugs = map { Bugzilla::Bug->check($_) } @{$params->{ids}};
foreach my $bug (@bugs) {
Bugzilla->user->can_edit_product($bug->product_id)
|| ThrowUserError("product_edit_denied", {product => $bug->product});
}
my @created;
$dbh->bz_start_transaction();
my $timestamp = $dbh->selectrow_array('SELECT LOCALTIMESTAMP(0)');
my $flags = delete $params->{flags};
my $comment = delete $params->{comment};
my $bug_flags = delete $params->{bug_flags};