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
|
# 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::Constants;
use Bugzilla::Field;
use Bugzilla::Product;
use Bugzilla::Component;
use Bugzilla::Error;
use Bugzilla::Extension::BMO::Data;
our $VERSION = '1';
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') {
# 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 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) {
my $params = { product_id => $product->id };
$params->{'component_id'} = $component->id if $component;
@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) = @_;
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;
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} ||= '---';
};
my $name = "Bugzilla::Bug::$flag_name";
if (!Bugzilla::Bug->can($flag_name)) {
no strict 'refs';
*{$name} = $accessor;
}
}
}
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'} = "$bugs_alias.value";
}
sub bug_end_of_create {
my ($self, $args) = @_;
my $bug = $args->{'bug'};
my $timestamp = $args->{'timestamp'};
my $params = Bugzilla->input_params;
my $user = Bugzilla->user;
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 bug_end_of_update {
my ($self, $args) = @_;
my $bug = $args->{'bug'};
my $timestamp = $args->{'timestamp'};
my $changes = $args->{'changes'};
my $params = Bugzilla->input_params;
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 $new_value = $params->{$flag->name} || '---';
my $old_value = $flag->bug_flag->value;
next if $new_value eq $old_value;
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 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);
}
}
}
__PACKAGE__->NAME;
|