1 # This module is part of debbugs, and
2 # is released under the terms of the GPL version 2, or any later
3 # version (at your option). See the file README and COPYING for more
5 # Copyright 2018 by Don Armstrong <don@donarmstrong.com>.
11 Debbugs::Bug -- OO interface to bugs
16 Debbugs::Bug->new(schema => $s,binaries => [qw(foo)],sources => [qw(bar)]);
27 use v5.10; # for state
30 use List::AllUtils qw(max first min any);
32 use Params::Validate qw(validate_with :types);
33 use Debbugs::Config qw(:config);
34 use Debbugs::Status qw(read_bug);
35 use Debbugs::Bug::Tag;
36 use Debbugs::Bug::Status;
37 use Debbugs::Collection::Package;
38 use Debbugs::Collection::Bug;
39 use Debbugs::Collection::Correspondent;
45 extends 'Debbugs::OOBase';
47 my $meta = __PACKAGE__->meta;
49 state $strong_severities =
50 {map {($_,1)} @{$config{strong_severities}}};
52 has bug => (is => 'ro', isa => 'Int',
60 has saved => (is => 'ro', isa => 'Bool',
62 writer => '_set_saved',
65 has status => (is => 'ro', isa => 'Debbugs::Bug::Status',
67 builder => '_build_status',
68 handles => {date => 'date',
70 message_id => 'message_id',
71 severity => 'severity',
72 archived => 'archived',
75 forwarded => 'forwarded',
81 return Debbugs::Bug::Status->new(bug=>$self->bug,
82 $self->schema_argument,
86 has log => (is => 'bare', isa => 'Debbugs::Log',
88 builder => '_build_log',
89 handles => {_read_record => 'read_record',
90 log_records => 'read_all_records',
96 return Debbugs::Log->new(bug_num => $self->id,
101 has spam => (is => 'bare', isa => 'Debbugs::Log::Spam',
103 builder => '_build_spam',
104 handles => ['is_spam'],
108 return Debbugs::Log::Spam->new(bug_num => $self->id);
111 has 'package_collection' => (is => 'ro',
112 isa => 'Debbugs::Collection::Package',
113 builder => '_build_package_collection',
117 sub _build_package_collection {
119 if ($self->has_schema) {
120 return Debbugs::Collection::Package->new(schema => $self->schema);
122 carp "No schema when building package collection";
123 return Debbugs::Collection::Package->new();
126 has bug_collection => (is => 'ro',
127 isa => 'Debbugs::Collection::Bug',
128 builder => '_build_bug_collection',
130 sub _build_bug_collection {
132 if ($self->has_schema) {
133 return Debbugs::Collection::Bug->new(schema => $self->schema);
135 return Debbugs::Collection::Bug->new();
138 has correspondent_collection =>
140 isa => 'Debbugs::Collection::Correspondent',
141 builder => '_build_correspondent_collection',
144 sub _build_correspondent_collection {
146 return Debbugs::Collection::Correspondent->new($self->schema_argument);
150 for my $attr (qw(packages affects sources)) {
153 isa => 'Debbugs::Collection::Package',
154 clearer => '_clear_'.$attr,
155 builder => '_build_'.$attr,
161 for my $attr (qw(blocks blocked_by mergedwith)) {
164 isa => 'Debbugs::Collection::Bug',
165 clearer => '_clear_'.$attr,
166 builder => '_build_'.$attr,
173 for my $attr (qw(owner submitter done)) {
176 isa => 'Maybe[Debbugs::Correspondent]',
178 builder => '_build_'.$attr.'_corr',
179 clearer => '_clear_'.$attr.'_corr',
180 handles => {$attr.'_url' => $attr.'_url',
181 $attr.'_email' => 'email',
182 $attr.'_phrase' => 'phrase',
185 $meta->add_method('has_'.$attr,
186 sub {my $self = shift;
187 my $m = $meta->find_method_by_name($attr);
188 return defined $m->($self);
190 $meta->add_method('_build_'.$attr.'_corr',
191 sub {my $self = shift;
192 my $m = $self->status->meta->find_method_by_name($attr);
193 my $v = $m->($self->status);
194 if (defined $v and length($v)) {
195 return $self->correspondent_collection->
196 get_or_add_by_key($v);
206 return $self->has_done;
209 sub strong_severity {
211 return exists $strong_severities->{$self->severity};
215 $_[0]->severity =~ m/^(.)/;
219 sub _build_packages {
221 return $self->package_collection->
222 limit($self->status->package);
227 return $self->affects->count > 0;
232 return $self->package_collection->
233 limit($self->status->affects);
238 my @sources = map {$_->sources} $self->packages->members;
244 return defined $self->owner;
249 return $self->blocks->count > 0;
254 return $self->bug_collection->
255 limit($self->status->blocks);
260 return $self->blocked_by->count > 0;
263 sub _build_blocked_by {
265 return $self->bug_collection->
266 limit($self->status->blocked_by);
270 length($_[0]->forwarded) > 0;
273 for my $attr (qw(fixed found)) {
276 isa => 'Debbugs::Collection::Version',
277 clearer => '_clear_'.$attr,
278 builder => '_build_'.$attr,
286 return any {1} $self->status->found;
291 return $self->packages->
292 get_source_versions($self->status->found);
297 return any {1} $self->status->fixed;
302 return $self->packages->
303 get_source_versions($self->status->fixed);
308 return any {1} $self->status->mergedwith;
311 sub _build_mergedwith {
313 return $self->bug_collection->
314 limit($self->status->mergedwith);
317 for my $attr (qw(created modified)) {
318 has $attr => (is => 'rw', isa => 'Object',
319 clearer => '_clear_'.$attr,
320 builder => '_build_'.$attr,
325 from_epoch(epoch => $_[0]->status->date);
327 sub _build_modified {
329 from_epoch(epoch => max($_[0]->status->log_modified,
330 $_[0]->status->last_modified
334 has tags => (is => 'ro',
335 isa => 'Debbugs::Bug::Tag',
336 clearer => '_clear_tags',
337 builder => '_build_tags',
342 return Debbugs::Bug::Tag->new(keywords => join(' ',$self->status->tags),
344 users => $self->bug_collection->users,
348 has pending => (is => 'ro',
350 clearer => '_clear_pending',
351 builder => '_build_pending',
358 my $pending = 'pending';
359 if (length($self->status->forwarded)) {
360 $pending = 'forwarded';
362 if ($self->tags->tag_is_set('pending')) {
363 $pending = 'pending-fixed';
365 if ($self->tags->tag_is_set('pending')) {
368 # XXX This isn't quite right
374 $bug->buggy('debbugs/2.6.0-1','debbugs/2.6.0-2');
375 $bug->buggy(Debbugs::Version->new('debbugs/2.6.0-1'),
376 Debbugs::Version->new('debbugs/2.6.0-2'),
379 Returns the output of Debbugs::Versions::buggy for a particular
380 package, version and found/fixed set. Automatically turns found, fixed
381 and version into source/version strings.
388 $self->package_collection->
389 universe->versiontree;
390 my $max_buggy = 'absent';
393 $ver = Debbugs::Version->
396 package_collection => $self->package_collection,
399 $vertree->load($ver->source);
401 $vertree->buggy($ver,
404 if ($buggy eq 'found') {
407 if ($buggy eq 'fixed') {
408 $max_buggy = 'fixed';
415 (is => 'ro', isa => 'Bool',
416 writer => '_set_archiveable',
417 builder => '_build_archiveable',
418 clearer => '_clear_archiveable',
421 has when_archiveable =>
422 (is => 'ro', isa => 'Num',
423 writer => '_set_when_archiveable',
424 builder => '_build_when_archiveable',
425 clearer => '_clear_when_archiveable',
429 sub _build_archiveable {
431 $self->_populate_archiveable(0);
432 return $self->archiveable;
434 sub _build_when_archiveable {
436 $self->_populate_archiveable(1);
437 return $self->when_archiveable;
440 sub _populate_archiveable {
442 my ($need_time) = @_;
444 # Bugs can be archived if they are
446 if (not $self->done) {
447 $self->_set_archiveable(0);
448 $self->_set_when_archiveable(-1);
451 # 2. Have no unremovable tags set
452 if (@{$config{removal_unremovable_tags}}) {
454 {map {($_=>1)} @{$config{removal_unremovable_tags}}};
455 for my $tag ($self->tags) {
456 if ($unrem_tags->{$tag}) {
457 $self->_set_archiveable(0);
458 $self->_set_when_archiveable(-1);
464 state $remove_time = 24 * 60 * 60 * ($config{removal_age} // 30);
465 # 4. Have been modified more than removal_age ago
467 $time - $self->modified->epoch;
468 # if we don't need to know when we can archive, we can stop here if it's
469 # been modified too recently
470 if ($moded_ago < $remove_time) {
471 $self->_set_archiveable(0);
472 return unless $need_time;
475 @{$config{removal_default_distribution_tags}};
476 if ($self->strong_severity) {
478 @{$config{removal_strong_severity_default_distribution_tags}};
480 # 3. Have a maximum buggy of fixed
481 my $buggy = $self->buggy($self->packages->
482 get_source_versions_distributions(@distributions));
483 if ('found' eq $buggy) {
484 $self->_set_archiveable(0);
485 $self->_set_when_archiveable(-1);
488 my $fixed_ago = $moded_ago;
489 # $fixed_ago = $time - $self->when_fixed(@distributions);
490 # if ($fixed_ago < $remove_time) {
491 # $self->_set_archiveable(0);
493 $self->_set_when_archiveable(($remove_time - min($fixed_ago,$moded_ago)) / (24 * 60 * 60));
494 if ($fixed_ago > $remove_time and
495 $moded_ago > $remove_time) {
496 $self->_set_archiveable(1);
497 $self->_set_when_archiveable(0);
504 my %param = validate_with(params => \@_,
505 spec => {seen_merged => {type => HASHREF,
506 default => sub {return {}},
508 repeat_merged => {type => BOOLEAN,
511 include => {type => HASHREF,
514 exclude => {type => HASHREF,
517 min_days => {type => SCALAR,
520 max_days => {type => SCALAR,
525 if (exists $param{include}) {
526 return 1 if not $self->matches($param{include});
528 if (exists $param{exclude}) {
529 return 1 if $self->matches($param{exclude});
531 if (exists $param{repeat_merged} and not $param{repeat_merged}) {
532 my @merged = sort {$a<=>$b} $self->bug, $self->status->mergedwith;
533 return 1 if first {sub {defined $_}}
534 @{$param{seen_merged}}{@merged};
535 @{$param{seen_merged}}{@merged} = (1) x @merged;
537 if (exists $param{min_days}) {
538 return 1 unless $param{min_days} <=
539 (DateTime->now() - $self->created)->days();
541 if (exists $param{max_days}) {
542 return 1 unless $param{max_days} >=
543 (DateTime->now() - $self->created)->days();
550 my ($field, $values) = @_;
551 my @ret = first {sub {$_ eq $field}} @{$values};
555 sub __contains_match {
556 my ($field, $values) = @_;
557 foreach my $value (@{$values}) {
558 return 1 if (index($field, $value) > -1);
564 {subject => sub {__contains_match($_[0]->subject,@_)},
566 for my $value (@{$_[1]}) {
567 if ($_[0]->tags->is_set($value)) {
573 severity => sub {__exact_match($_[0]->severity,@_)},
574 pending => sub {__exact_match($_[0]->pending,@_)},
575 originator => sub {__exact_match($_[0]->submitter,@_)},
576 submitter => sub {__exact_match($_[0]->submitter,@_)},
577 forwarded => sub {__exact_match($_[0]->forwarded,@_)},
578 owner => sub {__exact_match($_[0]->owner,@_)},
582 my ($self,$hash) = @_;
583 for my $key (keys %{$hash}) {
584 my $sub = $field_match->{$key};
585 if (not defined $sub) {
586 carp "No subroutine for key: $key";
589 return 1 if $sub->($self,$hash->{$key});
596 return $self->id.'@'.$config{email_domain};
599 sub subscribe_email {
601 return $self->id.'-subscribe@'.$config{email_domain};
606 return $config{web_domain}.'/'.$self->id;
611 return $config{web_domain}.'/mbox:'.$self->id;
614 sub mbox_status_url {
616 return $self->mbox_url.'?mboxstatus=yes';
621 $self->mbox_url.'?mboxmaint=yes';
626 my $url = Debbugs::URI->new('version.cgi?');
627 $url->query_form(package => $self->status->package(),
628 found => [$self->status->found],
629 fixed => [$self->status->fixed],
632 return $url->as_string;
635 sub related_packages_and_versions {
638 if (length($self->status->{package}//'')) {
639 @packages = split /,/,$self->status->{package}//'';
641 if (length($self->status->{affects}//'')) {
643 split /,/,$self->status->{affects}//'';
646 (@{$self->status->{found_versions}//[]},
647 @{$self->status->{fixed_versions}//[]});
648 my @unqualified_versions;
650 for my $ver (@versions) {
651 if ($ver =~ m{(<src>.+)/(<ver>.+)}) { # It's a src_pkg_ver
652 push @return, ['src:'.$+{src}, $+{ver}];
654 push @unqualified_versions,$ver;
657 for my $pkg (@packages) {
658 if (@unqualified_versions) {
660 [$pkg,@unqualified_versions];
670 return 'Debbugs::Bug={bug='.$self->bug.'}';
673 __PACKAGE__->meta->make_immutable;
681 # indent-tabs-mode: nil
682 # cperl-indent-level: 4