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',
186 $meta->add_method('has_'.$attr,
187 sub {my $self = shift;
188 my $m = $meta->find_method_by_name($attr);
189 return defined $m->($self);
191 $meta->add_method('_build_'.$attr.'_corr',
192 sub {my $self = shift;
193 my $m = $self->status->meta->find_method_by_name($attr);
194 my $v = $m->($self->status);
195 if (defined $v and length($v)) {
196 return $self->correspondent_collection->
197 get_or_add_by_key($v);
207 return $self->has_done;
210 sub strong_severity {
212 return exists $strong_severities->{$self->severity};
216 $_[0]->severity =~ m/^(.)/;
220 sub _build_packages {
222 return $self->package_collection->
223 limit($self->status->package);
228 return $self->affects->count > 0;
233 return $self->package_collection->
234 limit($self->status->affects);
239 my @sources = map {$_->sources} $self->packages->members;
245 return defined $self->owner;
250 return $self->blocks->count > 0;
255 return $self->bug_collection->
256 limit($self->status->blocks);
261 return $self->blocked_by->count > 0;
264 sub _build_blocked_by {
266 return $self->bug_collection->
267 limit($self->status->blocked_by);
271 length($_[0]->forwarded) > 0;
274 for my $attr (qw(fixed found)) {
277 isa => 'Debbugs::Collection::Version',
278 clearer => '_clear_'.$attr,
279 builder => '_build_'.$attr,
287 return any {1} $self->status->found;
292 return $self->packages->
293 get_source_versions($self->status->found);
298 return any {1} $self->status->fixed;
303 return $self->packages->
304 get_source_versions($self->status->fixed);
309 return any {1} $self->status->mergedwith;
312 sub _build_mergedwith {
314 return $self->bug_collection->
315 limit($self->status->mergedwith);
318 for my $attr (qw(created modified)) {
319 has $attr => (is => 'rw', isa => 'Object',
320 clearer => '_clear_'.$attr,
321 builder => '_build_'.$attr,
326 from_epoch(epoch => $_[0]->status->date);
328 sub _build_modified {
330 from_epoch(epoch => max($_[0]->status->log_modified,
331 $_[0]->status->last_modified
335 has tags => (is => 'ro',
336 isa => 'Debbugs::Bug::Tag',
337 clearer => '_clear_tags',
338 builder => '_build_tags',
343 return Debbugs::Bug::Tag->new(keywords => join(' ',$self->status->tags),
345 users => $self->bug_collection->users,
349 has pending => (is => 'ro',
351 clearer => '_clear_pending',
352 builder => '_build_pending',
359 my $pending = 'pending';
360 if (length($self->status->forwarded)) {
361 $pending = 'forwarded';
363 if ($self->tags->tag_is_set('pending')) {
364 $pending = 'pending-fixed';
366 if ($self->tags->tag_is_set('pending')) {
369 # XXX This isn't quite right
375 $bug->buggy('debbugs/2.6.0-1','debbugs/2.6.0-2');
376 $bug->buggy(Debbugs::Version->new('debbugs/2.6.0-1'),
377 Debbugs::Version->new('debbugs/2.6.0-2'),
380 Returns the output of Debbugs::Versions::buggy for a particular
381 package, version and found/fixed set. Automatically turns found, fixed
382 and version into source/version strings.
389 $self->package_collection->
390 universe->versiontree;
391 my $max_buggy = 'absent';
394 $ver = Debbugs::Version->
397 package_collection => $self->package_collection,
400 $vertree->load($ver->source);
402 $vertree->buggy($ver,
405 if ($buggy eq 'found') {
408 if ($buggy eq 'fixed') {
409 $max_buggy = 'fixed';
416 (is => 'ro', isa => 'Bool',
417 writer => '_set_archiveable',
418 builder => '_build_archiveable',
419 clearer => '_clear_archiveable',
422 has when_archiveable =>
423 (is => 'ro', isa => 'Num',
424 writer => '_set_when_archiveable',
425 builder => '_build_when_archiveable',
426 clearer => '_clear_when_archiveable',
430 sub _build_archiveable {
432 $self->_populate_archiveable(0);
433 return $self->archiveable;
435 sub _build_when_archiveable {
437 $self->_populate_archiveable(1);
438 return $self->when_archiveable;
441 sub _populate_archiveable {
443 my ($need_time) = @_;
445 # Bugs can be archived if they are
447 if (not $self->done) {
448 $self->_set_archiveable(0);
449 $self->_set_when_archiveable(-1);
452 # 2. Have no unremovable tags set
453 if (@{$config{removal_unremovable_tags}}) {
455 {map {($_=>1)} @{$config{removal_unremovable_tags}}};
456 for my $tag ($self->tags) {
457 if ($unrem_tags->{$tag}) {
458 $self->_set_archiveable(0);
459 $self->_set_when_archiveable(-1);
465 state $remove_time = 24 * 60 * 60 * ($config{removal_age} // 30);
466 # 4. Have been modified more than removal_age ago
468 $time - $self->modified->epoch;
469 # if we don't need to know when we can archive, we can stop here if it's
470 # been modified too recently
471 if ($moded_ago < $remove_time) {
472 $self->_set_archiveable(0);
473 return unless $need_time;
476 @{$config{removal_default_distribution_tags}};
477 if ($self->strong_severity) {
479 @{$config{removal_strong_severity_default_distribution_tags}};
481 # 3. Have a maximum buggy of fixed
482 my $buggy = $self->buggy($self->packages->
483 get_source_versions_distributions(@distributions));
484 if ('found' eq $buggy) {
485 $self->_set_archiveable(0);
486 $self->_set_when_archiveable(-1);
489 my $fixed_ago = $moded_ago;
490 # $fixed_ago = $time - $self->when_fixed(@distributions);
491 # if ($fixed_ago < $remove_time) {
492 # $self->_set_archiveable(0);
494 $self->_set_when_archiveable(($remove_time - min($fixed_ago,$moded_ago)) / (24 * 60 * 60));
495 if ($fixed_ago > $remove_time and
496 $moded_ago > $remove_time) {
497 $self->_set_archiveable(1);
498 $self->_set_when_archiveable(0);
505 my %param = validate_with(params => \@_,
506 spec => {seen_merged => {type => HASHREF,
507 default => sub {return {}},
509 repeat_merged => {type => BOOLEAN,
512 include => {type => HASHREF,
515 exclude => {type => HASHREF,
518 min_days => {type => SCALAR,
521 max_days => {type => SCALAR,
526 if (exists $param{include}) {
527 return 1 if not $self->matches($param{include});
529 if (exists $param{exclude}) {
530 return 1 if $self->matches($param{exclude});
532 if (exists $param{repeat_merged} and not $param{repeat_merged}) {
533 my @merged = sort {$a<=>$b} $self->bug, $self->status->mergedwith;
534 return 1 if first {sub {defined $_}}
535 @{$param{seen_merged}}{@merged};
536 @{$param{seen_merged}}{@merged} = (1) x @merged;
538 if (exists $param{min_days}) {
539 return 1 unless $param{min_days} <=
540 (DateTime->now() - $self->created)->days();
542 if (exists $param{max_days}) {
543 return 1 unless $param{max_days} >=
544 (DateTime->now() - $self->created)->days();
551 my ($field, $values) = @_;
552 my @ret = first {sub {$_ eq $field}} @{$values};
556 sub __contains_match {
557 my ($field, $values) = @_;
558 foreach my $value (@{$values}) {
559 return 1 if (index($field, $value) > -1);
565 {subject => sub {__contains_match($_[0]->subject,@_)},
567 for my $value (@{$_[1]}) {
568 if ($_[0]->tags->is_set($value)) {
574 severity => sub {__exact_match($_[0]->severity,@_)},
575 pending => sub {__exact_match($_[0]->pending,@_)},
576 originator => sub {__exact_match($_[0]->submitter,@_)},
577 submitter => sub {__exact_match($_[0]->submitter,@_)},
578 forwarded => sub {__exact_match($_[0]->forwarded,@_)},
579 owner => sub {__exact_match($_[0]->owner,@_)},
583 my ($self,$hash) = @_;
584 for my $key (keys %{$hash}) {
585 my $sub = $field_match->{$key};
586 if (not defined $sub) {
587 carp "No subroutine for key: $key";
590 return 1 if $sub->($self,$hash->{$key});
597 return $self->id.'@'.$config{email_domain};
600 sub subscribe_email {
602 return $self->id.'-subscribe@'.$config{email_domain};
607 return $config{web_domain}.'/'.$self->id;
612 return $config{web_domain}.'/mbox:'.$self->id;
615 sub mbox_status_url {
617 return $self->mbox_url.'?mboxstatus=yes';
622 $self->mbox_url.'?mboxmaint=yes';
627 my $url = Debbugs::URI->new('version.cgi?');
628 $url->query_form(package => $self->status->package(),
629 found => [$self->status->found],
630 fixed => [$self->status->fixed],
633 return $url->as_string;
636 sub related_packages_and_versions {
639 if (length($self->status->{package}//'')) {
640 @packages = split /,/,$self->status->{package}//'';
642 if (length($self->status->{affects}//'')) {
644 split /,/,$self->status->{affects}//'';
647 (@{$self->status->{found_versions}//[]},
648 @{$self->status->{fixed_versions}//[]});
649 my @unqualified_versions;
651 for my $ver (@versions) {
652 if ($ver =~ m{(<src>.+)/(<ver>.+)}) { # It's a src_pkg_ver
653 push @return, ['src:'.$+{src}, $+{ver}];
655 push @unqualified_versions,$ver;
658 for my $pkg (@packages) {
659 if (@unqualified_versions) {
661 [$pkg,@unqualified_versions];
671 return 'Debbugs::Bug={bug='.$self->bug.'}';
674 __PACKAGE__->meta->make_immutable;
682 # indent-tabs-mode: nil
683 # cperl-indent-level: 4