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
|
# 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::TrackingFlags;
use strict;
use base qw(Bugzilla::Extension);
use Bugzilla::Extension::TrackingFlags::Constants;
use Bugzilla::Extension::TrackingFlags::Flag;
use Bugzilla::Extension::TrackingFlags::Flag::Bug;
use Bugzilla::Extension::TrackingFlags::Admin;
use Bugzilla::Bug;
use Bugzilla::Component;
use Bugzilla::Constants;
use Bugzilla::Error;
use Bugzilla::Extension::BMO::Data;
use Bugzilla::Field;
use Bugzilla::Install::Filesystem;
use Bugzilla::Product;
our $VERSION = '1';
BEGIN {
*Bugzilla::tracking_flag_names = \&_tracking_flag_names;
}
sub _tracking_flag_names {
# return just a list of names, hitting the database directly to avoid the
# overhead of object creation
return Bugzilla->request_cache->{tracking_flag_names} ||=
Bugzilla->dbh->selectcol_arrayref("SELECT name FROM tracking_flags");
}
sub page_before_template {
my ($self, $args) = @_;
my $page = $args->{'page_id'};
my $vars = $args->{'vars'};
if ($page eq 'tracking_flags_admin_list.html') {
Bugzilla->user->in_group('admin')
|| ThrowUserError('auth_failure',
{ group => 'admin',
action => 'access',
object => 'administrative_pages' });
admin_list($vars);
} elsif ($page eq 'tracking_flags_admin_edit.html') {
Bugzilla->user->in_group('admin')
|| ThrowUserError('auth_failure',
{ group => 'admin',
action => 'access',
object => 'administrative_pages' });
admin_edit($vars);
}
}
sub template_before_process {
my ($self, $args) = @_;
my $file = $args->{'file'};
my $vars = $args->{'vars'};
if ($file eq 'bug/create/create.html.tmpl'
|| $file eq 'bug/create/create-winqual.html.tmpl')
{
$vars->{'tracking_flags'} = Bugzilla::Extension::TrackingFlags::Flag->match({
product => $vars->{'product'}->name,
enter_bug => 1,
is_active => 1,
});
$vars->{'tracking_flag_types'} = FLAG_TYPES;
}
elsif ($file eq 'bug/edit.html.tmpl'|| $file eq 'bug/show.xml.tmpl'
|| $file eq 'email/bugmail.html.tmpl' || $file eq 'email/bugmail.txt.tmpl')
{
# note: bug/edit.html.tmpl doesn't support multiple bugs
my $bug = exists $vars->{'bugs'} ? $vars->{'bugs'}[0] : $vars->{'bug'};
if ($bug && !$bug->{error}) {
$vars->{'tracking_flags'} = Bugzilla::Extension::TrackingFlags::Flag->match({
product => $bug->product,
component => $bug->component,
bug_id => $bug->id,
is_active => 1,
});
}
$vars->{'tracking_flag_types'} = FLAG_TYPES;
}
elsif ($file eq 'list/edit-multiple.html.tmpl' && $vars->{'one_product'}) {
$vars->{'tracking_flags'} = Bugzilla::Extension::TrackingFlags::Flag->match({
product => $vars->{'one_product'}->name,
is_active => 1
});
}
}
sub db_schema_abstract_schema {
my ($self, $args) = @_;
$args->{'schema'}->{'tracking_flags'} = {
FIELDS => [
id => {
TYPE => 'MEDIUMSERIAL',
NOTNULL => 1,
PRIMARYKEY => 1,
},
field_id => {
TYPE => 'INT3',
NOTNULL => 1,
REFERENCES => {
TABLE => 'fielddefs',
COLUMN => 'id',
DELETE => 'CASCADE'
}
},
name => {
TYPE => 'varchar(64)',
NOTNULL => 1,
},
description => {
TYPE => 'varchar(64)',
NOTNULL => 1,
},
type => {
TYPE => 'varchar(64)',
NOTNULL => 1,
},
sortkey => {
TYPE => 'INT2',
NOTNULL => 1,
DEFAULT => '0',
},
enter_bug => {
TYPE => 'BOOLEAN',
NOTNULL => 1,
DEFAULT => 'TRUE',
},
is_active => {
TYPE => 'BOOLEAN',
NOTNULL => 1,
DEFAULT => 'TRUE',
},
],
INDEXES => [
tracking_flags_idx => {
FIELDS => ['name'],
TYPE => 'UNIQUE',
},
],
};
$args->{'schema'}->{'tracking_flags_values'} = {
FIELDS => [
id => {
TYPE => 'MEDIUMSERIAL',
NOTNULL => 1,
PRIMARYKEY => 1,
},
tracking_flag_id => {
TYPE => 'INT3',
NOTNULL => 1,
REFERENCES => {
TABLE => 'tracking_flags',
COLUMN => 'id',
DELETE => 'CASCADE',
},
},
setter_group_id => {
TYPE => 'INT3',
NOTNULL => 0,
REFERENCES => {
TABLE => 'groups',
COLUMN => 'id',
DELETE => 'SET NULL',
},
},
value => {
TYPE => 'varchar(64)',
NOTNULL => 1,
},
sortkey => {
TYPE => 'INT2',
NOTNULL => 1,
DEFAULT => '0',
},
enter_bug => {
TYPE => 'BOOLEAN',
NOTNULL => 1,
DEFAULT => 'TRUE',
},
is_active => {
TYPE => 'BOOLEAN',
NOTNULL => 1,
DEFAULT => 'TRUE',
},
],
INDEXES => [
tracking_flags_values_idx => {
FIELDS => ['tracking_flag_id', 'value'],
TYPE => 'UNIQUE',
},
],
};
$args->{'schema'}->{'tracking_flags_bugs'} = {
FIELDS => [
id => {
TYPE => 'MEDIUMSERIAL',
NOTNULL => 1,
PRIMARYKEY => 1,
},
tracking_flag_id => {
TYPE => 'INT3',
NOTNULL => 1,
REFERENCES => {
TABLE => 'tracking_flags',
COLUMN => 'id',
DELETE => 'CASCADE',
},
},
bug_id => {
TYPE => 'INT3',
NOTNULL => 1,
REFERENCES => {
TABLE => 'bugs',
COLUMN => 'bug_id',
DELETE => 'CASCADE',
},
},
value => {
TYPE => 'varchar(64)',
NOTNULL => 1,
},
],
INDEXES => [
tracking_flags_bugs_idx => {
FIELDS => ['tracking_flag_id', 'bug_id'],
TYPE => 'UNIQUE',
},
],
};
$args->{'schema'}->{'tracking_flags_visibility'} = {
FIELDS => [
id => {
TYPE => 'MEDIUMSERIAL',
NOTNULL => 1,
PRIMARYKEY => 1,
},
tracking_flag_id => {
TYPE => 'INT3',
NOTNULL => 1,
REFERENCES => {
TABLE => 'tracking_flags',
COLUMN => 'id',
DELETE => 'CASCADE',
},
},
product_id => {
TYPE => 'INT2',
NOTNULL => 1,
REFERENCES => {
TABLE => 'products',
COLUMN => 'id',
DELETE => 'CASCADE',
},
},
component_id => {
TYPE => 'INT2',
NOTNULL => 0,
REFERENCES => {
TABLE => 'components',
COLUMN => 'id',
DELETE => 'CASCADE',
},
},
],
INDEXES => [
tracking_flags_visibility_idx => {
FIELDS => ['tracking_flag_id', 'product_id', 'component_id'],
TYPE => 'UNIQUE',
},
],
};
}
sub install_update_db {
my $dbh = Bugzilla->dbh;
my $fk = $dbh->bz_fk_info('tracking_flags', 'field_id');
if ($fk and !defined $fk->{DELETE}) {
$fk->{DELETE} = 'CASCADE';
$dbh->bz_alter_fk('tracking_flags', 'field_id', $fk);
}
$dbh->bz_add_column(
'tracking_flags',
'enter_bug',
{
TYPE => 'BOOLEAN',
NOTNULL => 1,
DEFAULT => 'TRUE',
}
);
}
sub install_filesystem {
my ($self, $args) = @_;
my $files = $args->{files};
my $extensions_dir = bz_locations()->{extensionsdir};
$files->{"$extensions_dir/TrackingFlags/bin/bulk_flag_clear.pl"} = {
perms => Bugzilla::Install::Filesystem::OWNER_EXECUTE
};
}
sub active_custom_fields {
my ($self, $args) = @_;
my $fields = $args->{'fields'};
my $params = $args->{'params'};
my $product = $params->{'product'};
my $component = $params->{'component'};
# Create a hash of current fields based on field names
my %field_hash = map { $_->name => $_ } @$$fields;
my @tracking_flags;
if ($product) {
$params->{'product_id'} = $product->id;
$params->{'component_id'} = $component->id if $component;
$params->{'is_active'} = 1;
@tracking_flags = @{ Bugzilla::Extension::TrackingFlags::Flag->match($params) };
}
else {
@tracking_flags = Bugzilla::Extension::TrackingFlags::Flag->get_all;
}
# Add tracking flags to fields hash replacing if already exists for our
# flag object instead of the usual Field.pm object
foreach my $flag (@tracking_flags) {
$field_hash{$flag->name} = $flag;
}
@$$fields = sort { $a->sortkey <=> $b->sortkey } values %field_hash;
}
sub buglist_columns {
my ($self, $args) = @_;
my $columns = $args->{columns};
my $dbh = Bugzilla->dbh;
my @tracking_flags = Bugzilla::Extension::TrackingFlags::Flag->get_all;
foreach my $flag (@tracking_flags) {
$columns->{$flag->name} = {
name => "COALESCE(map_" . $flag->name . ".value, '---')",
title => $flag->description
};
}
}
sub buglist_column_joins {
my ($self, $args) = @_;
# if there are elements in the tracking_flags array, then they have been
# removed from the query, so we mustn't generate joins
return if scalar @{ $args->{search}->{tracking_flags} };
my $column_joins = $args->{'column_joins'};
my @tracking_flags = Bugzilla::Extension::TrackingFlags::Flag->get_all;
foreach my $flag (@tracking_flags) {
$column_joins->{$flag->name} = {
as => 'map_' . $flag->name,
table => 'tracking_flags_bugs',
extra => [ 'map_' . $flag->name . '.tracking_flag_id = ' . $flag->flag_id ]
};
}
}
sub bug_create_cf_accessors {
my ($self, $args) = @_;
# Create the custom accessors for the flag values
my @tracking_flags = Bugzilla::Extension::TrackingFlags::Flag->get_all;
foreach my $flag (@tracking_flags) {
my $flag_name = $flag->name;
if (!Bugzilla::Bug->can($flag_name)) {
my $accessor = sub {
my $self = shift;
return $self->{$flag_name} if defined $self->{$flag_name};
if (!exists $self->{'_tf_bug_values_preloaded'}) {
# preload all values currently set for this bug
my $bug_values
= Bugzilla::Extension::TrackingFlags::Flag::Bug->match({ bug_id => $self->id });
foreach my $value (@$bug_values) {
$self->{$value->tracking_flag->name} = $value->value;
}
$self->{'_tf_bug_values_preloaded'} = 1;
}
return $self->{$flag_name} ||= '---';
};
no strict 'refs';
*{"Bugzilla::Bug::$flag_name"} = $accessor;
}
if (!Bugzilla::Bug->can("set_$flag_name")) {
my $setter = sub {
my ($self, $value) = @_;
$value = ref($value) eq 'ARRAY'
? $value->[0]
: $value;
$self->set($flag_name, $value);
};
no strict 'refs';
*{"Bugzilla::Bug::set_$flag_name"} = $setter;
}
}
}
sub bug_editable_bug_fields {
my ($self, $args) = @_;
my $fields = $args->{'fields'};
my @tracking_flags = Bugzilla::Extension::TrackingFlags::Flag->get_all;
foreach my $flag (@tracking_flags) {
push(@$fields, $flag->name);
}
}
sub search_operator_field_override {
my ($self, $args) = @_;
my $operators = $args->{'operators'};
my @tracking_flags = Bugzilla::Extension::TrackingFlags::Flag->get_all;
foreach my $flag (@tracking_flags) {
$operators->{$flag->name} = {
_non_changed => sub {
_tracking_flags_search_nonchanged($flag->flag_id, @_)
}
};
}
}
sub _tracking_flags_search_nonchanged {
my ($flag_id, $search, $args) = @_;
my ($bugs_table, $chart_id, $joins, $value, $operator) =
@$args{qw(bugs_table chart_id joins value operator)};
my $dbh = Bugzilla->dbh;
return if ($operator =~ m/^changed/);
my $bugs_alias = "tracking_flags_bugs_$chart_id";
my $flags_alias = "tracking_flags_$chart_id";
my $bugs_join = {
table => 'tracking_flags_bugs',
as => $bugs_alias,
from => $bugs_table . ".bug_id",
to => "bug_id",
extra => [$bugs_alias . ".tracking_flag_id = $flag_id"]
};
push(@$joins, $bugs_join);
$args->{'full_field'} = "COALESCE($bugs_alias.value, '---')";
}
sub bug_end_of_create {
my ($self, $args) = @_;
my $bug = $args->{'bug'};
my $timestamp = $args->{'timestamp'};
my $user = Bugzilla->user;
my $params = Bugzilla->request_cache->{tracking_flags_create_params};
return if !$params;
my $tracking_flags = Bugzilla::Extension::TrackingFlags::Flag->match({
product => $bug->product,
component => $bug->component,
is_active => 1,
});
foreach my $flag (@$tracking_flags) {
next if !$params->{$flag->name};
foreach my $value (@{$flag->values}) {
next if $value->value ne $params->{$flag->name};
next if $value->value eq '---'; # do not insert if value is '---', same as empty
if (!$flag->can_set_value($value->value)) {
ThrowUserError('tracking_flags_change_denied',
{ flag => $flag, value => $value });
}
Bugzilla::Extension::TrackingFlags::Flag::Bug->create({
tracking_flag_id => $flag->flag_id,
bug_id => $bug->id,
value => $value->value,
});
# Add the name/value pair to the bug object
$bug->{$flag->name} = $value->value;
}
}
}
sub object_end_of_set_all {
my ($self, $args) = @_;
my $object = $args->{object};
my $params = $args->{params};
return unless $object->isa('Bugzilla::Bug');
# Do not filter by product/component as we may be changing those
my $tracking_flags = Bugzilla::Extension::TrackingFlags::Flag->match({
bug_id => $object->id,
is_active => 1,
});
foreach my $flag (@$tracking_flags) {
my $flag_name = $flag->name;
if (exists $params->{$flag_name}) {
my $value = ref($params->{$flag_name}) eq 'ARRAY'
? $params->{$flag_name}->[0]
: $params->{$flag_name};
$object->set($flag_name, $value);
}
}
}
sub bug_check_can_change_field {
my ($self, $args) = @_;
my ($bug, $field, $old_value, $new_value, $priv_results)
= @$args{qw(bug field old_value new_value priv_results)};
return if $field !~ /^cf_/ or $old_value eq $new_value;
return unless my $flag = Bugzilla::Extension::TrackingFlags::Flag->new({ name => $field });
if ($flag->can_set_value($new_value)) {
push @$priv_results, PRIVILEGES_REQUIRED_NONE;
}
else {
push @$priv_results, PRIVILEGES_REQUIRED_EMPOWERED;
}
}
sub bug_end_of_update {
my ($self, $args) = @_;
my ($bug, $old_bug, $timestamp, $changes)
= @$args{qw(bug old_bug timestamp changes)};
my $user = Bugzilla->user;
# Do not filter by product/component as we may be changing those
my $tracking_flags = Bugzilla::Extension::TrackingFlags::Flag->match({
bug_id => $bug->id,
is_active => 1,
});
my (@flag_changes);
foreach my $flag (@$tracking_flags) {
my $flag_name = $flag->name;
my $new_value = $bug->$flag_name;
my $old_value = $old_bug->$flag_name;
if ($new_value ne $old_value) {
# Do not allow if the user cannot set the old value or the new value
if (!$flag->can_set_value($new_value)) {
ThrowUserError('tracking_flags_change_denied',
{ flag => $flag, value => $new_value });
}
push(@flag_changes, { flag => $flag,
added => $new_value,
removed => $old_value });
}
}
foreach my $change (@flag_changes) {
my $flag = $change->{'flag'};
my $added = $change->{'added'};
my $removed = $change->{'removed'};
if ($added eq '---') {
$flag->bug_flag->remove_from_db();
}
elsif ($removed eq '---') {
Bugzilla::Extension::TrackingFlags::Flag::Bug->create({
tracking_flag_id => $flag->flag_id,
bug_id => $bug->id,
value => $added,
});
}
else {
$flag->bug_flag->set_value($added);
$flag->bug_flag->update($timestamp);
}
$changes->{$flag->name} = [ $removed, $added ];
LogActivityEntry($bug->id, $flag->name, $removed, $added, $user->id, $timestamp);
# Update the name/value pair in the bug object
$bug->{$flag->name} = $added;
}
}
sub bug_end_of_create_validators {
my ($self, $args) = @_;
my $params = $args->{params};
# We need to stash away any params that are setting/updating tracking
# flags early on. Otherwise set_all or insert_create_data will complain.
my @tracking_flags = Bugzilla::Extension::TrackingFlags::Flag->get_all;
my $cache = Bugzilla->request_cache->{tracking_flags_create_params} ||= {};
foreach my $flag (@tracking_flags) {
my $flag_name = $flag->name;
if (defined $params->{$flag_name}) {
$cache->{$flag_name} = delete $params->{$flag_name};
}
}
}
sub mailer_before_send {
my ($self, $args) = @_;
my $email = $args->{email};
# Add X-Bugzilla-Tracking header or add to it
# if already exists
if ($email->header('X-Bugzilla-ID')) {
my $bug_id = $email->header('X-Bugzilla-ID');
my $tracking_flags
= Bugzilla::Extension::TrackingFlags::Flag->match({ bug_id => $bug_id });
my @set_values = ();
foreach my $flag (@$tracking_flags) {
next if $flag->bug_flag->value eq '---';
push(@set_values, $flag->description . ":" . $flag->bug_flag->value);
}
if (@set_values) {
my $set_values_string = join(' ', @set_values);
if ($email->header('X-Bugzilla-Tracking')) {
$set_values_string = $email->header('X-Bugzilla-Tracking') .
" " . $set_values_string;
}
$email->header_set('X-Bugzilla-Tracking' => $set_values_string);
}
}
}
# Purpose: generically handle generating pretty blocking/status "flags" from
# custom field names.
sub quicksearch_map {
my ($self, $args) = @_;
my $map = $args->{'map'};
foreach my $name (keys %$map) {
if ($name =~ /^cf_(blocking|tracking|status)_([a-z]+)?(\d+)?$/) {
my $type = $1;
my $product = $2;
my $version = $3;
if ($version) {
$version = join('.', split(//, $version));
}
my $pretty_name = $type;
if ($product) {
$pretty_name .= "-" . $product;
}
if ($version) {
$pretty_name .= $version;
}
$map->{$pretty_name} = $name;
}
}
}
sub reorg_move_component {
my ($self, $args) = @_;
my $new_product = $args->{new_product};
my $component = $args->{component};
Bugzilla->dbh->do(
"UPDATE tracking_flags_visibility SET product_id=? WHERE component_id=?",
undef,
$new_product->id, $component->id,
);
}
sub sanitycheck_check {
my ($self, $args) = @_;
my $status = $args->{status};
$status->('tracking_flags_check');
my ($count) = Bugzilla->dbh->selectrow_array("
SELECT COUNT(*)
FROM tracking_flags_visibility
INNER JOIN components ON components.id = tracking_flags_visibility.component_id
WHERE tracking_flags_visibility.product_id <> components.product_id
");
if ($count) {
$status->('tracking_flags_alert', undef, 'alert');
$status->('tracking_flags_repair');
}
}
sub sanitycheck_repair {
my ($self, $args) = @_;
return unless Bugzilla->cgi->param('tracking_flags_repair');
my $status = $args->{'status'};
my $dbh = Bugzilla->dbh;
$status->('tracking_flags_repairing');
my $rows = $dbh->selectall_arrayref("
SELECT DISTINCT tracking_flags_visibility.product_id AS bad_product_id,
components.product_id AS good_product_id,
tracking_flags_visibility.component_id
FROM tracking_flags_visibility
INNER JOIN components ON components.id = tracking_flags_visibility.component_id
WHERE tracking_flags_visibility.product_id <> components.product_id
",
{ Slice => {} }
);
foreach my $row (@$rows) {
$dbh->do("
UPDATE tracking_flags_visibility
SET product_id=?
WHERE product_id=? AND component_id=?
", undef,
$row->{good_product_id},
$row->{bad_product_id},
$row->{component_id},
);
}
}
__PACKAGE__->NAME;
|