Skip to content

Commit 6cd0988

Browse files
committed
Add a new API: /changes/by_releases
1 parent 4912ce5 commit 6cd0988

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

lib/MetaCPAN/Document/Release/Set.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ has query_release => (
2020
author_status
2121
by_author
2222
by_author_and_name
23+
by_author_and_names
2324
get_contributors
2425
get_files
2526
latest_by_author

lib/MetaCPAN/Query/Release.pm

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,51 @@ sub by_author_and_name {
357357
};
358358
}
359359

360+
sub by_author_and_names {
361+
my ( $self, $releases ) = @_;
362+
363+
# $releases: ArrayRef[ Dict[ author => Str, name => Str ] ]
364+
365+
my $body = {
366+
query => {
367+
bool => {
368+
should => [
369+
map { +{
370+
query => {
371+
bool => {
372+
must => [
373+
{ term => { author => uc($_->{author}) } },
374+
{ term => { 'name' => $_->{name} } },
375+
]
376+
}
377+
}
378+
} } @$releases
379+
]
380+
}
381+
}
382+
};
383+
384+
my $ret = $self->es->search(
385+
index => $self->index_name,
386+
type => 'release',
387+
body => $body,
388+
);
389+
return unless $ret->{hits}{total};
390+
391+
my @releases;
392+
for my $hit (@{ $ret->{hits}{hits} }) {
393+
my $src = $hit->{_source};
394+
single_valued_arrayref_to_scalar($src);
395+
push @releases, $src;
396+
}
397+
398+
return {
399+
took => $ret->{took},
400+
total => $ret->{hits}{total},
401+
releases => \@releases,
402+
};
403+
}
404+
360405
sub by_author {
361406
my ( $self, $pauseid, $size ) = @_;
362407
$size //= 1000;

lib/MetaCPAN/Server/Controller/Changes.pm

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,38 @@ sub all : Chained('index') : PathPart('') : Args(0) {
6565
$c->detach('not_found');
6666
}
6767

68+
sub by_releases : Path('by_releases') : Args(0) {
69+
my ( $self, $c ) = @_;
70+
71+
# ArrayRef[ Dict[ author => Str, name => Str ] ]
72+
my $arg = $c->read_param("releases");
73+
my $ret = $c->model('CPAN::Release')->by_author_and_names( $arg );
74+
75+
my @changes;
76+
for my $release (@{$ret->{releases}}) {
77+
my ($author, $name, $path) = @{$release}{ qw(author name changes_file) };
78+
my $source = $c->model('Source')->path( $author, $name, $path ) // '';
79+
80+
my $content;
81+
try {
82+
local $/;
83+
$content = Encode::decode(
84+
'UTF-8',
85+
(scalar $source->openr->getline),
86+
Encode::FB_CROAK | Encode::LEAVE_SRC
87+
);
88+
} catch {
89+
$content = undef;
90+
};
91+
92+
push @changes, {
93+
author => $author,
94+
release => $name,
95+
changes_text => $content,
96+
}
97+
}
98+
99+
$c->stash({ changes => \@changes });
100+
}
101+
68102
1;

0 commit comments

Comments
 (0)