Skip to content

Commit 0cbb25c

Browse files
committed
Added cover script
1 parent eb48fd3 commit 0cbb25c

File tree

3 files changed

+121
-15
lines changed

3 files changed

+121
-15
lines changed

bin/cover.pl

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
use strict;
2+
use warnings;
3+
use v5.36;
4+
5+
use Cpanel::JSON::XS qw< decode_json >;
6+
use Getopt::Long;
7+
use Path::Tiny qw< path >;
8+
9+
use MetaCPAN::Logger qw< :log :dlog >;
10+
11+
use MetaCPAN::ES;
12+
use MetaCPAN::Ingest qw<
13+
handle_error
14+
read_url
15+
>;
16+
17+
# args
18+
my ( $json_file, $test );
19+
GetOptions(
20+
"json=s" => \$json_file,
21+
"test" => \$test
22+
);
23+
my $cover_url //= 'http://cpancover.com/latest/cpancover.json';
24+
my $cover_dev_url //= 'http://cpancover.com/latest/cpancover_dev.json';
25+
26+
# setup
27+
my %valid_keys
28+
= map { $_ => 1 } qw< branch condition statement subroutine total >;
29+
30+
my $es = MetaCPAN::ES->new( index => "cover", type => "cover" );
31+
my $bulk = $es->bulk();
32+
33+
my $data = retrieve_cover_data();
34+
35+
log_info {'Updating the cover index'};
36+
37+
for my $dist ( sort keys %{$data} ) {
38+
for my $version ( keys %{ $data->{$dist} } ) {
39+
my $release = $dist . '-' . $version;
40+
my $rel_check = $es->search(
41+
index => 'cpan',
42+
type => 'release',
43+
size => 0,
44+
body => {
45+
query => { term => { name => $release } },
46+
},
47+
);
48+
if ( $rel_check->{hits}{total} ) {
49+
log_info { "Adding release info for '" . $release . "'" };
50+
}
51+
else {
52+
log_warn { "Release '" . $release . "' does not exist." };
53+
next;
54+
}
55+
56+
my %doc_data = %{ $data->{$dist}{$version}{coverage}{total} };
57+
58+
for my $k ( keys %doc_data ) {
59+
delete $doc_data{$k} unless exists $valid_keys{$k};
60+
}
61+
62+
$bulk->update( {
63+
id => $release,
64+
doc => {
65+
distribution => $dist,
66+
version => $version,
67+
release => $release,
68+
criteria => \%doc_data,
69+
},
70+
doc_as_upsert => 1,
71+
} );
72+
}
73+
}
74+
75+
$bulk->flush;
76+
77+
###
78+
79+
sub retrieve_cover_data {
80+
return decode_json( path($json_file)->slurp ) if $json_file;
81+
82+
my $url = $test ? $cover_dev_url : $cover_url;
83+
84+
return decode_json( read_url($url) );
85+
}
86+
87+
1;
88+
89+
__END__
90+
91+
=pod
92+
93+
=head1 SYNOPSIS
94+
95+
# bin/metacpan cover [--test] [json_file]
96+
97+
=head1 DESCRIPTION
98+
99+
Retrieves the CPAN Cover data from its source and
100+
updates our ES information.
101+
102+
=cut

bin/cve.pl

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use MetaCPAN::Ingest qw<
1414
handle_error
1515
numify_version
16-
ua
16+
read_url
1717
>;
1818

1919
my %range_ops = qw( < lt <= lte > gt >= gte );
@@ -202,20 +202,7 @@ sub retrieve_cve_data {
202202

203203
my $url = $test ? $cve_dev_url : $cve_url;
204204

205-
log_info { 'Fetching data from ', $url };
206-
my $ua = ua();
207-
my $resp = $ua->get($url);
208-
209-
handle_error( $resp->status_line, 1 ) unless $resp->is_success;
210-
211-
# clean up headers if .json.gz is served as gzip type
212-
# rather than json encoded with gzip
213-
if ( $resp->header('Content-Type') eq 'application/x-gzip' ) {
214-
$resp->header( 'Content-Type' => 'application/json' );
215-
$resp->header( 'Content-Encoding' => 'gzip' );
216-
}
217-
218-
return decode_json( $resp->decoded_content );
205+
return decode_json( read_url($url) );
219206
}
220207

221208
1;

lib/MetaCPAN/Ingest.pm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use Sub::Exporter -setup => {
3434
read_02packages_fh
3535
read_06perms_fh
3636
read_06perms_iter
37+
read_url
3738
strip_pod
3839
tmp_dir
3940
ua
@@ -169,6 +170,22 @@ sub ua ( $proxy = undef ) {
169170
return $ua;
170171
}
171172

173+
sub read_url ( $url ) {
174+
my $ua = ua();
175+
my $resp = $ua->get($url);
176+
177+
handle_error( $resp->status_line, 1 ) unless $resp->is_success;
178+
179+
# clean up headers if .json.gz is served as gzip type
180+
# rather than json encoded with gzip
181+
if ( $resp->header('Content-Type') eq 'application/x-gzip' ) {
182+
$resp->header( 'Content-Type' => 'application/json' );
183+
$resp->header( 'Content-Encoding' => 'gzip' );
184+
}
185+
186+
return $resp->decoded_content;
187+
}
188+
172189
sub cpan_file_map () {
173190
my $cpan = cpan_dir();
174191
my $ls = $cpan->child(qw< indices find-ls.gz >);

0 commit comments

Comments
 (0)