#!perl
no
warnings
'experimental::signatures'
;
our
$VERSION
=
'0.03'
;
GetOptions(
'filter=s'
=> \
my
$issue_regex
,
'issue=s'
=> \
my
$github_issue
,
'user=s'
=> \
my
$github_user
,
'repo=s'
=> \
my
$github_repo
,
'dbfile=s'
=> \
my
$store
,
'output-file=s'
=> \
my
$output_file
,
);
$store
//=
'db/issues.sqlite'
;
my
$gh
= GitHub::RSS->new(
dbh
=> {
dsn
=>
"dbi:SQLite:dbname=$store"
,
},
);
my
$since
= strftime
'%Y-%m-%dT%H:%M:%SZ'
,
gmtime
(
time
()-10*3600*24);
my
$feed
= XML::Feed->new(
'RSS'
);
$feed
->title(
"Github comments for $github_user/$github_repo"
);
sub
verbatim_section(
$s
) {
my
$res
= encode_entities(
$s
);
$res
=~ s! !
 
;!g;
return
"<code>$res</code>"
;
}
my
@comments
=
map
{
my
$entry
= XML::Feed::Entry->new(
'RSS'
);
$entry
->id(
$_
->{id} );
$entry
->title(
"Comment by $_->{user}->{login}"
);
$entry
->
link
(
$_
->{html_url} );
my
$issue
=
$gh
->issue(
$_
->{issue_number} );
my
$header
=
<<HTML;
<header>
<a href="$issue->{html_url}">$issue->{title}</a> in <a href="https://github.com/$github_user/$github_repo">$github_user/$github_repo</a>
</header>
HTML
my
$footer
=
<<HTML;
<footer>
<hr />
</footer>
HTML
my
$content
=
$_
->{body} ||
''
;
$content
=~ s!\\(.)!$1!g;
$content
=~ s![\x00-\x08\x0B\x0C\x0E-\x1F]!.!g;
$content
=~ s!^\`\`\`(.*?)\`\`\`!verbatim_section($1)!msge;
my
$body
= Text::Markdown->new->markdown(
$content
);
$entry
->content(
join
""
,
$header
,
$body
,
$footer
);
$entry
->author(
$_
->{user}->{login} );
if
(
$_
->{updated_at} ) {
my
$modified
= DateTime::Format::ISO8601->parse_datetime(
$_
->{updated_at}
);
$entry
->modified(
$modified
);
};
my
$created
= DateTime::Format::ISO8601->parse_datetime(
$_
->{created_at}
);
$entry
->issued(
$created
);
$feed
->add_entry(
$entry
);
}
$gh
->issues_and_comments(
$since
,
);
sub
update_file(
$fn
,
$content
) {
my
$needs_update
= ! -e
$fn
;
if
( !
$needs_update
) {
open
my
$old
,
'<'
,
$fn
or
die
"Couldn't read old content from '$fn': $!"
;
binmode
$old
,
':utf8'
;
local
$/;
my
$old_content
= <
$old
>;
$needs_update
=
$old_content
ne
$content
;
};
if
(
$needs_update
) {
open
my
$fh
,
'>'
,
$fn
or
die
"Couldn't create '$fn': $!"
;
binmode
$fh
,
':utf8'
;
print
{
$fh
}
$content
;
};
}
update_file(
$output_file
,
$feed
->as_xml );