summaryrefslogtreecommitdiff
blob: 8cc386e0b77491de08e55d181955db8877f5d6c3 (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
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
# 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/.

package Bugzilla::Extension::RuleEngine::Rule;

use strict;
use warnings;
use 5.10.1;

use Scalar::Util qw(blessed looks_like_number);
use List::MoreUtils qw(any);

use Bugzilla::Constants;
use Bugzilla::Field;
use Bugzilla::Util;
use Bugzilla::Error;

use Bugzilla::Extension::RuleEngine::RuleDetail;
use Bugzilla::Extension::RuleEngine::RuleGroup;

use base qw(Bugzilla::Object);

###############################
####    Initialization     ####
###############################

use constant DB_TABLE       => 'rh_rule';
use constant LIST_ORDER     => 'sortkey, name';
use constant EMAIL_SPLIT_RX => qr/(?:(?:,\s+)|,|\s+)/;

use constant DB_COLUMNS => qw(
  id
  name
  description
  creation_ts
  rule_group_id
  current_detail_id
  sortkey
  is_periodic
  minor_update
);

use constant UPDATE_COLUMNS => qw(
  name
  description
  rule_group_id
  current_detail_id
  sortkey
  is_periodic
  minor_update
);

use constant VALIDATORS => {
  name              => \&_check_name,
  description       => \&_check_description,
  creation_ts       => \&_check_creation_ts,
  rule_group_id     => \&_check_rule_group_id,
  current_detail_id => \&_check_current_detail_id,
  sortkey           => \&_check_sortkey,
  is_periodic       => \&_check_is_periodic,
  minor_update      => \&Bugzilla::Object::check_boolean,
};

###############################
####      Validators       ####
###############################

sub _check_creation_ts {
  return Bugzilla->dbh->selectrow_array('SELECT LOCALTIMESTAMP(0)');
}

sub _check_current_detail_id {
  my ($invocant, $id) = @_;

  $id = trim($id);

  # Return blank if specified, or we are creating this rule
  return '' if !$invocant || $id eq '';

  # Check that the ruledetail belongs to this rule
  my $object = Bugzilla::Extension::RuleEngine::RuleDetail->check({id => $id});
  if ($object->rule_id != $invocant->id) {
    ThrowCodeError(
      'rule_id_mismatch',
      {
        other_id      => $object->id,
        other_rule_id => $object->rule_id,
        this_rule_id  => $invocant->id
      }
    );
  }
  return $object->id;
}

sub _check_description {
  my ($invocant, $description) = @_;

  $description = trim($description || '');
  return $description;
}

sub _check_rule_group_id {
  my ($invocant, $value) = @_;
  my $old_id = blessed($invocant) ? $invocant->rule_group_id : 0;
  $value = trim($value);

  my $object;

  if (looks_like_number($value)) {
    $object = Bugzilla::Extension::RuleEngine::RuleGroup->check({id => $value});
  }
  else {
    $object = Bugzilla::Extension::RuleEngine::RuleGroup->check({name => $value});
  }

  if ((!$old_id || $object->id != $old_id) && !$object->is_active) {
    ThrowUserError('value_inactive',
      {class => ref($object), value => $object->name});
  }
  return $object->id;
}

sub _check_name {
  my ($invocant, $name) = @_;

  $name = trim($name);
  $name || ThrowUserError('rule_not_specified');

  if (length($name) > 64) {
    ThrowUserError('rule_name_too_long', {'name' => $name});
  }

  my $rule = Bugzilla::Extension::RuleEngine::Rule->new({name => $name});
  if ($rule && (!ref $invocant || $rule->id != $invocant->id)) {
    ThrowUserError("rule_already_exists", {name => $rule->name});
  }
  return $name;
}

sub _check_sortkey {
  my ($invocant, $sortkey) = @_;

  $sortkey ||= 0;
  my $stored_sortkey = $sortkey;
  if (!detaint_natural($sortkey) || $sortkey > MAX_SMALLINT) {
    ThrowUserError('rule_invalid_sortkey', {'sortkey' => $stored_sortkey});
  }
  return $sortkey;
}

sub _check_is_periodic {
  my (undef, $number) = @_;

  if ($number >= 0 && $number <= 2) {
    return $number;
  }

  # Make the rule on-change if they pick something invalid
  return 0;
}

###############################
####       Methods         ####
###############################

sub set_name              { $_[0]->set('name',              $_[1]); return; }
sub set_description       { $_[0]->set('description',       $_[1]); return; }
sub set_rule_group_id     { $_[0]->set('rule_group_id',     $_[1]); return; }
sub set_current_detail_id { $_[0]->set('current_detail_id', $_[1]); return; }
sub set_sortkey           { $_[0]->set('sortkey',           $_[1]); return; }
sub set_is_periodic       { $_[0]->set('is_periodic',       $_[1]); return; }
sub set_minor_update      { $_[0]->set('minor_update',      $_[1]); return; }

sub rule_group {
  my $self = shift;
  my $dbh  = Bugzilla->dbh;

  if (!defined $self->{rule_group}) {
    $self->{rule_group}
      = Bugzilla::Extension::RuleEngine::RuleGroup->new($self->rule_group_id);
  }
  return $self->{rule_group};
}

sub current {
  my $self = shift;
  my $dbh  = Bugzilla->dbh;

  if (!defined $self->{current_detail}) {
    $self->{current_detail}
      = Bugzilla::Extension::RuleEngine::RuleDetail->new($self->current_detail_id);
  }
  return $self->{current_detail};
}

sub all_details {
  my $self = shift;
  my $dbh  = Bugzilla->dbh;

  if (!$self->{'all_details'}) {
    my $ids = $dbh->selectcol_arrayref(
      q{
            SELECT id FROM rh_rule_detail
            WHERE rule_id = ?
            ORDER BY creation_ts, id}, undef, $self->id
    );

    $self->{'all_details'}
      = Bugzilla::Extension::RuleEngine::RuleDetail->new_from_list($ids);
  }
  return $self->{'all_details'};
}

sub set_rule_owners {
  my $self = shift;
  my $owners = shift; # arrayref of Bugzilla::User objects or comma seperated string.

  unless (ref $owners) {
    my $strings = _split_rule_owners($owners);
    $owners = [map { Bugzilla::User->check({name => $_}) } @$strings];
  }

  $self->{rule_owners} = $owners;

  return $self->{rule_owners};
}

###############################
####      Accessors        ####
###############################

sub description       { return $_[0]->{'description'}; }
sub creation_ts       { return $_[0]->{'creation_ts'}; }
sub rule_group_id     { return $_[0]->{'rule_group_id'}; }
sub current_detail_id { return $_[0]->{'current_detail_id'}; }
sub sortkey           { return $_[0]->{'sortkey'}; }
sub periodic_type     { return $_[0]->{is_periodic}; }

sub is_periodic {
  my $self = shift;

  return $self->{'is_periodic'} == 1 || $self->{is_periodic} == 2;
}

sub minor_update {
  my $self = shift;

  return $self->{'minor_update'};
}

sub run_type {
  my $self = shift;

  my $type = 'On bug change';

  $type = 'Periodically' if ($self->{is_periodic} == 1);
  $type = 'On bug change & periodically' if ($self->{is_periodic} == 2);

  return ($type);
}

sub is_bug_change {
  my $self = shift;

  return $self->{is_periodic} == 0 || $self->{is_periodic} == 2;
}

sub rule_owners {
  my $self = shift;

  return $self->{'rule_owners'} if exists $self->{rule_owners};

  my $query       = 'SELECT profile_id FROM rh_rule_owner WHERE rule_id = ?';
  my $rule_owners = Bugzilla->dbh->selectcol_arrayref($query, undef, $self->id);

  $self->{rule_owners} = Bugzilla::User->new_from_list($rule_owners);

  return $self->{rule_owners};
}

sub last_used {
  my $self = shift;
  my $dbh  = Bugzilla->dbh;

  my $query = q{
        SELECT MAX(bug_when)
        FROM bugs_activity
        WHERE fieldid = (SELECT id FROM fielddefs WHERE name = 'rh_rule')
            AND added = ? };

  my $when = Bugzilla->dbh->selectrow_array($query, undef, $self->id);
  return $when || '';
}

sub use_count {
  my $self = shift;
  my $dbh  = Bugzilla->dbh;

  my $query = q{
        SELECT COUNT(*)
        FROM bugs_activity
        WHERE fieldid = (SELECT id FROM fielddefs WHERE name = 'rh_rule')
            AND added = ? };

  my $count = Bugzilla->dbh->selectrow_array($query, undef, $self->id);
  return $count || 0;
}

sub use_since_change {
  my $self = shift;
  my $dbh  = Bugzilla->dbh;

  my $query = q{
        SELECT COUNT(*)
        FROM bugs_activity
        WHERE fieldid = (SELECT id FROM fielddefs WHERE name = 'rh_rule')
            AND added = ?
            AND bug_when >= ?};

  my $count = Bugzilla->dbh->selectrow_array($query, undef, $self->id,
    $self->current->creation_ts);
  return $count || 0;
}

###############################
####       Helpers         ####
###############################

# inactive: If false, will only show rules that are active OR have been changed in the last 7 days
# If true, returns all rules.
# product: The name of the product to show rules for

sub match_rules {
  my ($self, $criteria) = @_;
  my $dbh = Bugzilla->dbh;

  my $query = q{
        SELECT r.id
        FROM rh_rule r JOIN rh_rule_detail d ON (r.current_detail_id = d.id)
        WHERE 1=1
    };

  # The inactive criteria can be done in SQL
  if (!$criteria->{inactive}) {

    # Only show active rules and those changed in the last 7 days
    my $sevendays = $dbh->sql_date_math('NOW()', '-', 7, 'DAY');
    $query .= " AND (d.isactive = 1 OR d.creation_ts > $sevendays)";
  }

  if ($criteria->{rule_group_id}) {
    $query .= ' AND rule_group_id = ' . $dbh->quote($criteria->{rule_group_id});
  }

  if ($criteria->{is_bug_change}) {
    $query .= ' AND is_periodic IN (0,2) ';
  }

  if ($criteria->{isactive}) {

    # Only show active rules
    $query .= " AND (d.isactive = 1)";
  }

  if ($criteria->{is_periodic}) {
    $query .= " AND is_periodic IN (1,2)";
  }

  # Get the rule objects.
  my $rule_ids       = $dbh->selectcol_arrayref($query);
  my $unsorted_rules = $self->new_from_list($rule_ids);

  # Do the sort. We can't do in SQL, since new_from_list ignores this
  # After everything else, we always sort by sortkey and id
  my $s     = $criteria->{sort_order} || 'run';
  my @rules = sort {
         ($s eq 'alpha' && $a->name cmp $b->name)
      || ($s eq 'create' && $a->creation_ts cmp $b->creation_ts)
      || ($s eq 'update' && $a->current->creation_ts cmp $b->current->creation_ts)
      || ($s eq 'last'   && $a->last_used cmp $b->last_used)
      || ($s eq 'hits'   && $a->use_count <=> $b->use_count)
      || $a->sortkey <=> $b->sortkey
      || lc($a->name) cmp lc($b->name)
  } @$unsorted_rules;

  # Do you need to reverse the list?
  @rules = reverse @rules if $criteria->{reverse};

  # If there are no product restrictions, we can return now.
  if (!$criteria->{product}) {
    return \@rules;
  }

  # Get the product name and its classification
  my $product_name        = $criteria->{product};
  my $product             = Bugzilla::Product->check($product_name);
  my $classification_name = $product->classification->name;

  my @matching_rules = ();
  foreach my $rule (@rules) {

    # Get the product and/or classification restrictions on this rule
    my $products        = $rule->current->match->{product}{values}        || [];
    my $classifications = $rule->current->match->{classification}{values} || [];

    # If a product restriction is specified, this product must be in the list
    if (scalar @$products) {
      unless (any { $_ eq $product_name } @$products) {
        next;
      }
    }

    # Ditto for the classification
    if (scalar @$classifications) {
      unless (any { $_ eq $classification_name } @$classifications) {
        next;
      }
    }

    # We can add this rule to the list
    push @matching_rules, $rule;
  }

  return \@matching_rules;

}

sub run_after {
  my $self = shift;

  if (not exists $self->{run_after}) {
    my $query = q{
            SELECT name
            FROM rh_rule
            WHERE (sortkey < ? OR (sortkey = ? AND name < ?))
            AND rule_group_id = ?
            ORDER BY sortkey DESC, name DESC
            LIMIT 1
        };

    my ($rule_name)
      = Bugzilla->dbh->selectrow_array($query, undef, $self->sortkey,
      $self->sortkey, $self->name, $self->rule_group_id);

    $self->{run_after} = defined $rule_name ? $rule_name : '';
  }

  return $self->{run_after};
}

sub last7days {
  my $self = shift;
  my $dbh  = Bugzilla->dbh;

  my $query = q{
        SELECT COUNT(*)
        FROM bugs_activity
        WHERE fieldid = (SELECT id FROM fielddefs WHERE name = 'rh_rule')
            AND added = ?
            AND bug_when > } . $dbh->sql_date_math('NOW()', '-', 7, 'DAY');

  my $count = Bugzilla->dbh->selectrow_array($query, undef, $self->id);
  return $count || 0;
}

sub last24hours {
  my $self = shift;
  my $dbh  = Bugzilla->dbh;

  my $query = q{
        SELECT COUNT(*)
        FROM bugs_activity
        WHERE fieldid = (SELECT id FROM fielddefs WHERE name = 'rh_rule')
            AND added = ?
            AND bug_when > } . $dbh->sql_date_math('NOW()', '-', 24, 'HOUR');

  my $count = Bugzilla->dbh->selectrow_array($query, undef, $self->id);
  return $count || 0;
}

sub set_active {
  my $self      = shift;
  my $is_active = shift;

  # 1. Create the rule detail. We always create a new row, never update
  # an existing row
  my $rule_detail = Bugzilla::Extension::RuleEngine::RuleDetail->create({
    match      => $self->current->match,
    action     => $self->current->action,
    isactive   => $is_active,
    creator_id => Bugzilla->user->id,
    rule_id    => $self->id,
  });

  # 2. Update the rule's current_detail_id
  $self->set_current_detail_id($rule_detail->id);
  $self->update();

  # No particular reason to return this
  return $rule_detail->id;
}

# Override Bugzilla::Object::create so we can handle the rule owners.
sub create {
  my ($class, $params) = @_;

  # Rule owners isn't stored in the rh_rule table, it's managed seperately
  my $rule_owners = delete $params->{rule_owners};

  # Construct the Rule object itself
  my $self = $class->SUPER::create($params);

  return $self unless defined $rule_owners;

  # If we have any rule owners, set them now.
  my $rule_owner_objs = $self->set_rule_owners($rule_owners);

  if (defined($rule_owner_objs)) {
    my $dbh = Bugzilla->dbh;
    foreach my $rule_owner (@$rule_owner_objs) {
      $dbh->do('INSERT INTO rh_rule_owner (profile_id, rule_id) VALUES (?,?)',
        undef, $rule_owner->id, $self->id);
    }
  }

  return $self;
}

sub update {
  my $self = shift;

  my ($changes, $old_rule) = $self->SUPER::update(@_);

  # Perform the update for the rule_owners.

  my @old_owners = map { $_->id } @{$old_rule->rule_owners};
  my @new_owners = map { $_->id } @{$self->rule_owners};

  my ($removed_owner, $added_owner) = diff_arrays(\@old_owners, \@new_owners);

  my $dbh = Bugzilla->dbh;

  if (scalar @$removed_owner) {
    $dbh->do(
      'DELETE FROM rh_rule_owner WHERE rule_id = ? AND '
        . $dbh->sql_in('profile_id', $removed_owner),
      undef, $self->id
    );
  }

  foreach my $added_owner_id (@$added_owner) {
    $dbh->do('INSERT INTO rh_rule_owner (profile_id, rule_id) VALUES (?,?)',
      undef, $added_owner_id, $self->id);
  }

  if (scalar(@$removed_owner) || scalar(@$added_owner)) {
    $changes->{rule_owners} = [$removed_owner, $added_owner];
  }

  return ($changes, $old_rule);
}

# Note, this is a function not an object method.

# Given a single string 'foo@bar.com, bob@redhat.com', split it into
# an array of email addresses.
#
# Email addresses should be seperated by a space or comma.
sub _split_rule_owners {
  my $owners = shift;

  my @strings = split(EMAIL_SPLIT_RX, $owners);

  return \@strings;
}

1;

__END__

=head1 NAME

Bugzilla::Extension::RuleEngine::Rule - Bugzilla Rule class.

=head1 SYNOPSIS

    use Bugzilla::Extension::RuleEngine::Rule;

    my $rule = new Bugzilla::Extension::RuleEngine::Rule(1);
    my $rule = new Bugzilla::Extension::RuleEngine::Rule({name => 'Acme'});

    my $id                = $rule->id;
    my $name              = $rule->name;
    my $description       = $rule->description;
    my $creation_ts       = $rule->creation_ts;
    my $rule_group_id     = $rule->rule_group_id;
    my $current_detail_id = $rule->current_detail_id;
    my $last_used         = $rule->last_used;
    my $use_count         = $rule->use_count;
    my $sortkey           = $rule->sortkey;
    my $is_periodic       = $rule->is_periodic;

=head1 DESCRIPTION

Bugzilla::Extension::RuleEngine::Rule represents a rule object. It is an
implementation of L<Bugzilla::Object>, and thus provides all methods
that L<Bugzilla::Object> provides.

The methods that are specific to L<Bugzilla::Extension::RuleEngine::Rule> are listed
below.

A Rule is a rule that is part of the Bugzilla Rules Engine. A rule is versioned
with a specific version called a rule detail.

=head1 METHODS

=over

=item C<current>

=over

=item B<Description>

Returns the current version of the rule

=item B<Params>

none.

=item B<Returns>

A Bugzilla::Extension::RuleEngine::RuleDetail object.

=back

=item C<all_details>

=over

=item B<Description>

Returns all versions of the rule

=item B<Params>

none.

=item B<Returns>

An arrayref of Bugzilla::Extension::RuleEngine::RuleDetail objects.

=back

=item C<match_rules>

=over

=item B<Description>

Returns rules that meet a certain criteria

=item B<Params>

=over

=item B<inactive>

If set, will return all rules. Otherwise will only return active rules or those
changed in the last 7 days.

=item B<product>

The name of the product. All products if not specified.

=item B<sort_order>

The order to sort rows by. Possible values are:
run, alpha, create, update, last or hits

=item B<reverse>

Reverse the sort order above

=item B<rule_group_id>

The id of the rule group to limit results to.

=item B<is_bug_change>

only rules that fire on bug change

=item B<isactive>

limit to active rules

=item B<is_periodic>

limit to periodic rules

=item B<minor_update>

Should changes made by this rule be marked as minor updates?

=back

=item B<Returns>

An arrayref of Bugzilla::Extension::RuleEngine::Rule objects.

=back

=item C<run_after>

=over

=item B<Description>

Returns the name of the rule that is run before this one

=item B<Params>

none.

=item B<Returns>

The name of the rule that is run before this one. Returns
an empty string if this is the first rule.

=back

=item C<set_active>

=over

=item B<Description>

Changes the active state of the bug. This is done by creating
a new rule detail row, and updating the current_detail_id value.

=item B<Params>

is_active (boolean) - Whether this rule should be active or
inactive.

=item B<Returns>

The id of the newly create rule detail.

=back

=back

=cut