Skip to content

Commit

Permalink
Merge pull request #212 from hadfl/multiss
Browse files Browse the repository at this point in the history
  • Loading branch information
oetiker authored Apr 20, 2024
2 parents 78eaf1f + b6bc121 commit 462da79
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 61 deletions.
6 changes: 6 additions & 0 deletions lib/Fenix.pm
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ has schema => sub($self) {
default => '120',
validator => $sv->regexp(qr/^\d+/, 'expected a positive integer'),
},
maxissue => {
description => "how many issues fenix will handle at maximum in one request",
example => '6',
default => '6',
validator => $sv->regexp(qr/^\d+/, 'expected a positive integer'),
},
CHANS => {
array => 1,
optional => 1,
Expand Down
23 changes: 16 additions & 7 deletions lib/Fenix/Model/Handler/Issue.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,26 @@ has handlers => sub($self) {

sub process_p($self, $chan, $from, $msg, $mentioned = 0) {
for my $hd (@{$self->handlers}) {
my ($issue, $opts) = $self->handler->{$hd}->issue($msg);
my ($issues, $opts) = $self->handler->{$hd}->issues($msg);
$opts //= {};

next if !($issue && ($mentioned || $opts->{url}));
next if !(@$issues && ($mentioned || $opts->{url}));

$opts //= {};
my $hdn = $self->handler->{$hd}->name;

return Mojo::Promise->resolve([])
if $self->utils->muted(\$self->mutemap->{"issue_$hdn"}->{$chan}, $issue);
my @issues;
for my $issue (@$issues) {
next if $self->utils->muted(\$self->mutemap->{"issue_$hdn"}->{$chan}, $issue);

push @issues, $issue;
}

return Mojo::Promise->resolve([]) if !@issues;

# limit the maximum number of issues fenix handles for one request
@issues = splice @issues, 0, $self->config->{maxissue};

return $self->handler->{$hd}->process_p($issue, $opts);
return $self->handler->{$hd}->process_p(\@issues, $opts);
}

return undef;
Expand All @@ -53,7 +62,7 @@ __END__
=head1 COPYRIGHT
Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
Copyright 2024 OmniOS Community Edition (OmniOSce) Association.
=head1 LICENSE
Expand Down
10 changes: 5 additions & 5 deletions lib/Fenix/Model/Handler/Issue/Gerrit.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ has issuestr => sub { 'code review' };
# issue should be called first in 'process'.
# It parses the message and checks whether it is the correct handler
# return either a valid issue or undef.
sub issue($self, $msg) {
sub issues($self, $msg) {
my $baseurl = $self->baseurl->to_string;
my $urlre = qr!\b\Q$baseurl\E/c/illumos-gate/\+/(\d+)\b!;
for ($msg) {
/$urlre/ && return ($1, { url => 1 });
/\bcode\b/i && return ($msg =~ /\b(\d{2,})\b/)[0];
return ([ /$urlre/g ], { url => 1 }) if /$urlre/;
return [ /\b(\d{2,})\b/g ] if /\bcode\b/i;
}

return undef;
return [];
}

sub issueURL($self, $issue) {
Expand Down Expand Up @@ -56,7 +56,7 @@ __END__
=head1 COPYRIGHT
Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
Copyright 2024 OmniOS Community Edition (OmniOSce) Association.
=head1 LICENSE
Expand Down
10 changes: 5 additions & 5 deletions lib/Fenix/Model/Handler/Issue/IPD.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ has issuestr => sub { 'IPD' };
# issue should be called first in 'process'.
# It parses the message and checks whether it is the correct handler
# return either a valid issue or undef.
sub issue($self, $msg) {
sub issues($self, $msg) {
my $baseurl = $GITHUB->to_string;
my $urlre = qr!\b\Q$baseurl\E/illumos/ipd/\S+/ipd/0+(\d+)/README\.md\b!i;
for ($msg) {
/$urlre/ && return ($1, { url => 1 });
/\bIPD[-\s]*(\d+)\b/i && return $1;
return ([ /$urlre/g ], { url => 1 }) if /$urlre/;
return [ /\bIPD[-\s]*(\d+)\b/ig ];
}
return undef;
return [];
}

sub issueURL($self, $issue) {
Expand Down Expand Up @@ -54,7 +54,7 @@ __END__
=head1 COPYRIGHT
Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
Copyright 2024 OmniOS Community Edition (OmniOSce) Association.
=head1 LICENSE
Expand Down
12 changes: 6 additions & 6 deletions lib/Fenix/Model/Handler/Issue/Illumos.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ has baseurl => sub { Mojo::URL->new('https://www.illumos.org') };
# issue should be called first in 'process'.
# It parses the message and checks whether it is the correct handler
# return either a valid issue or undef.
sub issue($self, $msg) {
sub issues($self, $msg) {
my $baseurl = $self->baseurl->to_string;
my $urlre = qr!\b\Q$baseurl\E/issues/(\d+)\b!;
for ($msg) {
/$urlre/ && return ($1, { url => 1 });
/\b(?:illumos|issue)\b/i && return ($msg =~ /\b(\d{3,})\b/)[0];
/#(\d{3,})\b/ && return $1;
return ([ /$urlre/g ], { url => 1 }) if /$urlre/;
return [ /\b(\d{3,})\b/g ] if /\b(?:illumos|issue)\b/i;
return [ /#(\d{3,})\b/g ];
}

return undef;
return [];
}

sub issueURL($self, $issue) {
Expand Down Expand Up @@ -55,7 +55,7 @@ __END__
=head1 COPYRIGHT
Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
Copyright 2024 OmniOS Community Edition (OmniOSce) Association.
=head1 LICENSE
Expand Down
10 changes: 5 additions & 5 deletions lib/Fenix/Model/Handler/Issue/OpenSolaris.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ has issuestr => sub { 'OpenSolaris issue' };
# issue should be called first in 'process'.
# It parses the message and checks whether it is the correct handler
# return either a valid issue or undef.
sub issue($self, $msg) {
sub issues($self, $msg) {
my $baseurl = $self->baseurl->to_string;
my $urlre = qr§\b\Q$baseurl\E/opensolaris/bugdb/bug\.html#!(\d{7})\b§;
for ($msg) {
/$urlre/ && return ($1, { url => 1 });
/\b(\d{7})\b/ && return $1;
return ([ /$urlre/g ], { url => 1 }) if /$urlre/;
return [ /\b(\d{7})\b/g ];
}

return undef;
return [];
}

sub issueURL($self, $issue) {
Expand All @@ -44,7 +44,7 @@ __END__
=head1 COPYRIGHT
Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
Copyright 2024 OmniOS Community Edition (OmniOSce) Association.
=head1 LICENSE
Expand Down
10 changes: 5 additions & 5 deletions lib/Fenix/Model/Handler/Issue/SmartOS.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ has baseurl => sub { Mojo::URL->new('https://smartos.org') };
# issue should be called first in 'process'.
# It parses the message and checks whether it is the correct handler
# return either a valid issue or undef.
sub issue($self, $msg) {
sub issues($self, $msg) {
my $baseurl = $self->baseurl->to_string;
my $urlre = qr!\b\Q$baseurl\E/bugview/([A-Z]+-\d+)\b!;
for ($msg) {
/$urlre/ && return ($1, { url => 1 });
/\b([A-Z]+-\d+)\b/ && return $1;
return ([ /$urlre/g ], { url => 1 }) if /$urlre/;
return [ /\b([A-Z]+-\d+)\b/g ];
}

return undef;
return [];
}

sub issueURL($self, $issue) {
Expand Down Expand Up @@ -53,7 +53,7 @@ __END__
=head1 COPYRIGHT
Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
Copyright 2024 OmniOS Community Edition (OmniOSce) Association.
=head1 LICENSE
Expand Down
10 changes: 5 additions & 5 deletions lib/Fenix/Model/Handler/Issue/XXARC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ has issuestr => sub { 'OpenSolaris ARC Material Archive' };
# issue should be called first in 'process'.
# It parses the message and checks whether it is the correct handler
# return either a valid issue or undef.
sub issue($self, $msg) {
sub issues($self, $msg) {
my $baseurl = $self->baseurl->to_string;
my $urlre = qr§\b\Q$baseurl\E/opensolaris/ARChive/((?:FW|LS|PS|WS)ARC/\d{4}/\d{3})/§;
for ($msg) {
/$urlre/ && return ($1, { url => 1 });
/\b((?:FW|LS|PS|WS)ARC(?:\s+|\/)\d{4}(?:\s+|\/)\d{3})\b/i && return $1;
return ([ /$urlre/g ], { url => 1 }) if /$urlre/;
return [ /\b((?:FW|LS|PS|WS)ARC(?:\s+|\/)\d{4}(?:\s+|\/)\d{3})\b/ig ];
}

return undef;
return [];
}

sub issueURL($self, $issue) {
Expand Down Expand Up @@ -75,7 +75,7 @@ __END__
=head1 COPYRIGHT
Copyright 2023 OmniOS Community Edition (OmniOSce) Association.
Copyright 2024 OmniOS Community Edition (OmniOSce) Association.
=head1 LICENSE
Expand Down
75 changes: 52 additions & 23 deletions lib/Fenix/Model/Handler/Issue/base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ has ua => sub {
# issue should be called first in 'process'.
# It parses the message and checks whether it is the correct handler
# return either a valid issue or undef.
sub issue($self, $msg) {
return undef;
sub issues($self, $msg) {
return [];
}

sub issueURL($self, $issue) {
Expand All @@ -42,33 +42,62 @@ sub processIssue($self, $issue, $res) {
return {};
}

sub process_p($self, $issue, $opts = {}) {
my $url = $self->issueURL($issue);
sub process_p($self, $issues, $opts = {}) {
my @urls = map { $self->issueURL($_) } @$issues;

my $p = Mojo::Promise->new;

$self->ua->get_p($url)->then(sub($get) {
my $res = $get->res;
my @reply;
Mojo::Promise->all_settled(map { $self->ua->get_p($_) } @urls)
->then(sub(@promises) {
for (my $i = 0; $i <= $#promises; $i++) {
my $promise = $promises[$i];
my $issue = $issues->[$i];

return $p->resolve([ $self->issuestr . " '$issue' is not public." ]) if $res->code == 403;
return $p->resolve([ $self->issuestr . " '$issue' not found..." ]) if !$res->is_success;
if ($promise->{status} ne 'fulfilled') {
push @reply, $self->issuestr . " '$issue' not found...";

my $data = $self->processIssue($issue, $res);
return $p->resolve([ $data ]) if !ref $data; # error string returned by the handler
return $p->resolve([]) if !%$data;
next;
}

return $p->resolve([
"$data->{id}: $data->{subject} ($data->{status})",
@{$data->{url}} ? '' . join (' | ', @{$data->{url}}) : (),
]) if !$opts->{url};
my $res = $promise->{value}->[0]->res;

return $p->resolve([
"$data->{id}: $data->{subject} ($data->{status})"
. (@{$data->{url}} > 1 ? " | $data->{url}->[1]" : ''),
]);
})->catch(sub(@) {
return $p->resolve([ $self->issuestr . " '$issue' not found..." ]);
});
if ($res->code == 403) {
push @reply, $self->issuestr . " '$issue' is not public.";

next;
}

if (!$res->is_success) {
push @reply, $self->issuestr . " '$issue' not found...";

next;
}

my $data = $self->processIssue($issue, $res);

# error string returned by the handler
if (!ref $data) {
push @reply, $data;

next;
}

next if !%$data;

if (!$opts->{url}) {
push @reply, "$data->{id}: $data->{subject} ($data->{status})",
@{$data->{url}} ? '' . join (' | ', @{$data->{url}}) : ();

next;
}

push @reply, "$data->{id}: $data->{subject} ($data->{status})"
. (@{$data->{url}} > 1 ? " | $data->{url}->[1]" : '');
}

$p->resolve(\@reply);
});

return $p;
}
Expand All @@ -79,7 +108,7 @@ __END__
=head1 COPYRIGHT
Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
Copyright 2024 OmniOS Community Edition (OmniOSce) Association.
=head1 LICENSE
Expand Down

0 comments on commit 462da79

Please sign in to comment.