blib/lib/GitHub/RSS.pm

filter: Statementstmt 29.2% 43 / 147 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 40.0% 12 / 30 Podpod 23.0% 3 / 13 totaltotal 24.2% 58 / 239 CC 45 SCAR 33.6
CC
45
Cov
23%
store_issues_comments
90.0
fetch_and_store
56.0
_find_gh_token_file
42.0
CRAP 28.8
↑ Summary
1 package GitHub::RSS 0.06;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moo 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Filter::signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use PerlX::Maybe;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Net::GitHub;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 NAME
18  
19 GitHub::RSS - collect data from Github.com for feeding into RSS
20  
21 =head1 SYNOPSIS
22  
23 my $gh = GitHub::RSS->new(
24 dbh => {
25 dsn => "dbi:SQLite:dbname=$store",
26 },
27 );
28  
29 my $last_updated = $gh->last_check;
30 $gh->fetch_and_store( $github_user => $github_repo, $last_updated );
31 if( $verbose ) {
32 print "Updated from $last_updated to " . $gh->last_check, "\n";
33 };
34  
35 =head1 DESCRIPTION
36  
37 This module provides a cache database for GitHub issues and scripts to
38 periodically update the database from GitHub.
39  
40 This is mainly used for creating an RSS feed from the database, hence the
41 name.
42  
43 =head1 METHODS
44  
45 =head2 C<< ->new >>
46  
47 my $gh = GitHub::RSS->new(
48 dbh => {
49 dsn => 'dbi:SQLite:dbname=db/issues.sqlite',
50 },
51 );
52  
53 Constructs a new GitHub::RSS instance
54  
55 =over 4
56  
57 =item *
58  
59 B<gh> - instance of L<Net::GitHub>
60  
61 =cut
62  
63 has 'gh' => (
64 is => 'ro',
65 0 ▶ default => sub( $self ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 Net::GitHub->new(
67 maybe access_token => $self->token
68 ),
69 },
70 1 );
71  
72 =item *
73  
74 B<token_file> - name and path of the JSON-format token file containing the
75 GitHub API token By default, that file is searched for under the name
76 C<github.credentials> in C<.>, C<$ENV{XDG_DATA_HOME}>, C<$ENV{USERPROFILE}>
77 and C<$ENV{HOME}>.
78  
79 =cut
80  
81 1 has 'token_file' => (
82 is => 'lazy',
83 default => \&_find_gh_token_file,
84 );
85  
86 =item *
87  
88 B<token> - GitHub API token. If this is missing, it will be attempted to read
89 it from the C<token_file>.
90  
91 =cut
92  
93 1 has 'token' => (
94 is => 'lazy',
95 default => \&_read_gh_token,
96 );
97  
98 =item *
99  
100 B<default_user> - name of the GitHub user whose repos will be read
101  
102 =cut
103  
104 1 has default_user => (
105 is => 'ro',
106 );
107  
108 =item *
109  
110 B<default_repo> - name of the GitHub repo whose issues will be read
111  
112 =cut
113  
114 1 has default_repo => (
115 is => 'ro',
116 );
117  
118 =item *
119  
120 B<dbh> - premade database handle or alternatively a hashref containing
121 the L<DBI> arguments
122  
123 dbh => $dbh,
124  
125 or alternatively
126  
127 dbh => {
128 user => 'scott',
129 password => 'tiger',
130 dsn => 'dbi:SQLite:dbname=db/issues.sqlite',
131 }
132  
133 =cut
134  
135 1 has dbh => (
136 is => 'ro',
137 required => 1,
138 coerce => \&_build_dbh,
139 );
140  
141 0 ▶sub _build_dbh( $args ) {
Subroutine _build_dbh: not called CC=3 SCAR=24.8
142 0 ▶ return $args if ref($args) eq 'DBI::db';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args eq "DBI::db" 0 0 0
143 0 ▶ ref($args) eq 'HASH' or die 'Not a DB handle nor a hashref';
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq "HASH" or die "Not a DB handle nor a hashref" 0 0 0
144 0 return DBI->connect( @{$args}{qw/dsn user password options/} );
145 }
146  
147 =item *
148  
149 B<fetch_additional_pages> - number of additional pages to fetch from GitHub.
150 This is relevant when catching up a database for a repository with many issues.
151  
152 =back
153  
154 =cut
155  
156 1 has fetch_additional_pages => (
157 is => 'ro',
158 default => '1',
159 );
160  
161 0 ▶sub _find_gh_token_file( $self, $env=undef ) {
Subroutine _find_gh_token_file: not called CC=6 SCAR=37.4
162 0 ▶ $env //= \%ENV;
Condition0%0/2
$env //= \%ENV
l!l
00
MC/DC0%0/1
$env //= \%ENV
$env
Truth table0%0/2
A
A= $env
Aresult
11
00
163  
164 0 my $token_file;
165  
166 # This should use File::User
167 0 for my $candidate_dir ('.',
168 $ENV{XDG_DATA_HOME},
169 $ENV{USERPROFILE},
170 $ENV{HOME}
171 ) {
172 0 ▶ next unless defined $candidate_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $candidate_dir 0 0 0
173 0 ▶ if( -f "$candidate_dir/github.credentials" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "$candidate_dir/github.credentials") 0 0 0
174 0 $token_file = "$candidate_dir/github.credentials";
175 last
176 0 };
177 };
178  
179 0 return $token_file
180 }
181  
182 0 ▶sub _read_gh_token( $self, $token_file=undef ) {
Subroutine _read_gh_token: not called CC=5 SCAR=34.0
183 0 ▶ my $file = $token_file // $self->token_file;
Condition0%0/3
$token_file // $self->token_file
l!l&&r!l&&!r
000
MC/DC0%0/2
$token_file // $self->token_file
$token_file $self->token_file
Truth table0%0/3
A // B
A= $token_fileB= $self->token_file
ABresult
1X1
011
000
184  
185 0 ▶ if( $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 0 0 0
186 0 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Couldn't open file '${file}': $!" 0 0 0
187 or die "Couldn't open file '$file': $!";
188 0 binmode $fh;
189 0 local $/;
190 0 my $json = <$fh>;
191 0 my $token_json = decode_json( $json );
192 0 return $token_json->{token};
193 } else {
194 # We'll run without a known account
195 return
196 0 }
197 }
198  
199 0 ▶sub fetch_all_issues( $self,
Subroutine fetch_all_issues: not called CC=5 SCAR=34.0
Pod undocumented
200 0 $user = $self->default_user,
201 0 $repo = $self->default_repo,
202 0 $since=undef ) {
203 0 my @issues = $self->fetch_issues( $user, $repo, $since );
204 0 my $gh = $self->gh;
205 0 ▶ while ($gh->issue->has_next_page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gh->issue->has_next_page) 0 0 0
206 0 push @issues, $gh->issue->next_page;
207 }
208 @issues
209 0 }
210  
211 0 ▶sub fetch_issues( $self,
Subroutine fetch_issues: not called CC=4 SCAR=30.0
Pod undocumented
212 0 $user = $self->default_user,
213 0 $repo = $self->default_repo,
214 0 $since=undef ) {
215 0 my $gh = $self->gh;
216 0 my @issues = $gh->issue->repos_issues($user => $repo,
217 { sort => 'updated',
218 direction => 'asc', # so we can interrupt any time
219 state => 'all', # so we find issues that got closed
220 maybe since => $since,
221 }
222 );
223 };
224  
225 =head2 C<< ->fetch_issue_comments >>
226  
227 =cut
228  
229 0 ▶sub fetch_issue_comments( $self, $issue_number,
Subroutine fetch_issue_comments: not called CC=3 SCAR=24.8
Pod documented
230 0 $user=$self->default_user,
231 0 $repo=$self->default_repo
232 ) {
233 # Shouldn't this loop as well, just like with the issues?!
234 0 return $self->gh->issue->comments($user, $repo, $issue_number );
235 }
236  
237 0 ▶sub write_data( $self, $table, @rows) {
Subroutine write_data: not called CC=3 SCAR=24.8
Pod undocumented
238 0 my @columns = sort keys %{ $rows[0] };
239 0 ▶ if( ! @columns ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@columns) 0 0 0
240 # we have an empty row?!
241 0 return;
242 };
243 0 my $statement = sprintf q{replace into "%s" (%s) values (%s)},
244 $table,
245 join( ",", map qq{"$_"}, @columns ),
246 join( ",", ('?') x (0+@columns))
247 ;
248 0 my $sth = $self->dbh->prepare( $statement );
249 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $sth->execute_for_fetch(sub { @rows ? [@{shift @rows;}{@columns}] : (); } , \my @errors) } } or die Dumper(\@rows) 0 0 0
250 0 ▶ $sth->execute_for_fetch(sub { @rows ? [ @{ shift @rows }{@columns} ] : () }, \my @errors);
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
@rows ? : 0 0 0
251 } or die Dumper \@rows;
252 #if( @errors ) {
253 # warn Dumper \@errors if (0+@errors);
254 #};
255 }
256  
257 0 ▶sub store_issues_comments( $self, $user, $repo, $issues ) {
Subroutine store_issues_comments: not called CC=9 SCAR=45.0
Pod undocumented
258 # Munge some columns:
259 0 for (@$issues) {
260 0 my $u = $_->{user};
261 0 @{ $_ }{qw( user_id user_login user_gravatar_id )}
262 0 = @{ $u }{qw( id login gravatar_id )};
263  
264 # Squish all structure into JSON, for later
265 0 for (values %$_) {
266 0 ▶ if( ref($_) ) { $_ = encode_json($_) };
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) 0 0 0
267 };
268 };
269  
270 0 for my $issue (@$issues) {
271 #$|=1;
272 #print sprintf "% 6d %s\r", $issue->{number}, $issue->{updated_at};
273 0 my @comments = $self->fetch_issue_comments( $issue->{number}, $user => $repo );
274  
275 # Squish all structure into JSON, for later
276 0 for (@comments) {
277 0 for (values %$_) {
278 0 ▶ if( ref($_) ) { $_ = encode_json($_) };
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) 0 0 0
279 };
280 };
281 0 ▶ $self->write_data( 'comment' => @comments )
Branch: 1 branch
BranchTrueFalseTotal
if @comments 0 0 0
282 if @comments;
283 };
284  
285 # We wrote the comments first so we will refetch them if there is a problem
286 # when writing the issue
287 0 $self->write_data( 'issue' => @$issues );
288 };
289  
290 =head2 C<< ->fetch_and_store($user, $repo, $since) >>
291  
292 my $since = $gh->last_check;
293 $gh->fetch_and_store($user, $repo, $since)
294  
295 Fetches all issues and comments modified after the C<$since> timestamp.
296 If C<$since> is missing or C<undef>, all issues will be retrieved.
297  
298 =cut
299  
300 0 ▶sub fetch_and_store( $self,
Subroutine fetch_and_store: not called CC=7 SCAR=40.3
Pod documented
301 0 $user = $self->default_user,
302 0 $repo = $self->default_repo,
303 0 $since = undef) {
304 0 my $dbh = $self->dbh;
305 0 my $gh = $self->gh;
306  
307 0 my $can_fetch_more = $self->fetch_additional_pages;
308  
309 0 FETCH:
310 my @issues = $self->fetch_issues( $user => $repo, $since );
311 0 my $has_more = $gh->issue->has_next_page;
312 0 $self->store_issues_comments( $user => $repo, \@issues );
313  
314 0 ▶ if( $has_more and (!defined($can_fetch_more) or $can_fetch_more-- > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_more and !defined($can_fetch_more) || $can_fetch_more-- > 0) 0 0 0
Condition0%0/6
$has_more and !defined($can_fetch_more) || $can_fetch_more-- > 0
!ll&&!rl&&r
000
not defined $can_fetch_more or $can_fetch_more-- > 0
l!l&&r!l&&!r
000
MC/DC0%0/3
$has_more and !defined($can_fetch_more) || $can_fetch_more-- > 0
$has_more not defined $can_fetch_more $can_fetch_more-- > 0
Truth table0%0/4
A and not(B) or C
A= $has_moreB= not defined $can_fetch_moreC= $can_fetch_more-- > 0
ABCresult
0XX0
1000
11X1
1011
315 0 $since = $issues[-1]->{updated_at};
316 0 goto FETCH;
317 }
318 }
319  
320 0 ▶sub refetch_issues( $self,
Subroutine refetch_issues: not called CC=3 SCAR=24.8
Pod undocumented
321 0 $user = $self->default_user,
322 0 $repo = $self->default_repo,
323 @issue_numbers) {
324 0 my $dbh = $self->dbh;
325 0 my $gh = $self->gh;
326  
327 0 my @issues = map { scalar $gh->issue->issue($user => $repo, $_) } @issue_numbers;
328 0 $self->store_issues_comments( $user => $repo, \@issues );
329 }
330  
331 0 ▶sub inflate_fields( $self, $item, @fields ) {
Subroutine inflate_fields: not called CC=3 SCAR=24.8
Pod undocumented
332 0 for (@fields) {
333 0 ▶ $item->{$_} = $item->{$_} ? decode_json( $item->{$_} ) : $item->{$_};
Branch: 1 branch
BranchTrueFalseTotal
$item->{$_} ? : 0 0 0
334 }
335 }
336  
337 0 ▶sub issues_and_comments( $self, $since ) {
Subroutine issues_and_comments: not called CC=1 SCAR=6.9
Pod undocumented
338 map {
339 0 $self->inflate_fields( $_, qw(user closed_by));
340 0 $_
341 }
342 0 @{ $self->dbh->selectall_arrayref(<<'SQL', { Slice => {}}, $since, $since) }
343 select
344 i.id
345 , i.user
346 , i.html_url
347 , i.body
348 , i.created_at
349 , i.updated_at
350 , i.title as issue_title
351 , i.number as issue_number
352 from issue i
353 where i.updated_at >= ?
354 union all
355 select
356 c.id
357 , c.user
358 , c.html_url
359 , c.body
360 , c.created_at
361 , c.updated_at
362 , i.title as issue_title
363 , i.number as issue_number
364 from comment c
365 join issue i on c.issue_url=i.url
366 where c.updated_at >= ?
367 order by i.updated_at, html_url
368 SQL
369 }
370  
371 0 ▶sub issues_with_patches( $self ) {
Subroutine issues_with_patches: not called CC=1 SCAR=6.9
Pod undocumented
372 map {
373 0 $self->inflate_fields( $_, qw(user closed_by));
374 0 $_
375 }
376 0 @{ $self->dbh->selectall_arrayref(<<'SQL', { Slice => {}}) }
377 select distinct
378 i.* -- later, expand to explicit list
379 from issue i
380 join comment c on c.issue_url=i.url
381 where c.body like '%```diff%'
382 and i.state = 'open'
383 order by i.url
384 SQL
385 }
386  
387 0 ▶sub issue( $self, $issue ) {
Subroutine issue: not called CC=1 SCAR=6.9
Pod undocumented
388 0 $self->dbh->selectall_arrayref(<<'SQL', { Slice => {}}, $issue)->[0]
389 select
390 * -- later, expand to explicit list
391 from issue i
392 where i.number = ?
393 order by i.url
394 SQL
395 }
396  
397 0 ▶sub comments( $self, $issue ) {
Subroutine comments: not called CC=1 SCAR=6.9
Pod undocumented
398 0 @{ $self->dbh->selectall_arrayref(<<'SQL', { Slice => {}}, $issue) }
399 select
400 c.* -- later, expand to explicit list
401 from comment c
402 join issue i on c.issue_url=i.url
403 where i.number = ?
404 order by c.url
405 SQL
406 }
407  
408 =head2 C<< ->last_check >>
409  
410 my $since = $gh->last_check;
411  
412 Returns the timestamp of the last stored modification or C<undef>
413 if no issue or comment is stored.
414  
415 =cut
416  
417  
418 0 ▶sub last_check( $self,
Subroutine last_check: not called CC=4 SCAR=30.0
Pod documented
419 0 $user = $self->default_user,
420 0 $repo = $self->default_repo ) {
421 0 my $last = $self->dbh->selectall_arrayref(<<'SQL', { Slice => {} });
422 select
423 max(updated_at) as updated_at
424 from issue
425 SQL
426 0 ▶ if( @$last ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$last) { } 0 0 0
427 return $last->[0]->{updated_at}
428 0 } else {
429 return undef # empty DB
430 0 }
431 }
432  
433 1 1;
↑ Summary