blib/lib/GitHub/RSS.pm

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