/
gitHubReleaseWatcher.pl
executable file
·139 lines (117 loc) · 3.4 KB
/
gitHubReleaseWatcher.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/perl --
use v5.34.0;
use strict;
use warnings;
use utf8;
use feature qw(say);
use LWP::UserAgent;
use JSON::XS;
use DateTime::Format::ISO8601;
use Data::Dump qw(dump);
use URI::Escape;
use Module::Load qw( load );
use Getopt::Long;
use FindBin qw( $RealBin );
use lib "$RealBin";
binmode $_,":utf8" for \*STDOUT,\*STDERR;
my $verbose = 0;
my $postTo = "mastodon";
GetOptions(
"verbose:+"=>\$verbose,
"postTo=s"=>\$postTo,
) or die "bad options.";
my @repoNames = qw(
matrix-org/synapse
vector-im/element-web
coturn/coturn
LemmyNet/lemmy
mastodon/mastodon
redis/redis
);
# LemmyNet/lemmy-ui はタグだけ打っててリリースしないからチェックをやめる
chdir($RealBin) or die "chdir failed. $! $RealBin";
my $iso8601 = DateTime::Format::ISO8601->new;
my $ua = LWP::UserAgent->new( timeout => 30);
# return true if not $b or $a > $b
sub newer($$){
my($a,$b)=@_;
return 1 if not $b;
my $adt = $iso8601->parse_datetime($a->{created_at});
my $bdt = $iso8601->parse_datetime($b->{created_at});
my $i = DateTime->compare( $adt, $bdt );
return $i > 0;
}
sub safeName($){
my($a)=@_;
$a =~ s/[\\\/:*?"<>|_]+/_/g;
$a;
}
sub loadYaml($){
load 'YAML::Syck';
{
no warnings;
$YAML::Syck::ImplicitUnicode = 1;
}
return YAML::Syck::LoadFile(@_);
}
my $lemmyPoster = undef;
sub postToLemmy{
my($repoName,$name,$url)=@_;
if( not $lemmyPoster){
my $configFile = "lemmyPoster.yml";
my $config = loadYaml($configFile);
die "missing 'community' in $configFile" unless $config->{community};
load 'LemmyPoster';
$lemmyPoster = LemmyPoster->new( %$config, ua => $ua ,verbose=>$verbose);
}
$lemmyPoster->post(
$lemmyPoster->{community},
$url,
"$repoName $name"
);
}
my $mastodonPoster = undef;
sub postToMastodon{
my($repoName,$name,$url)=@_;
if( not $mastodonPoster){
my $configFile = "mastodonPoster.yml";
my $config = loadYaml($configFile);
load 'MastodonPoster';
$mastodonPoster = MastodonPoster->new( %$config, ua => $ua ,verbose=>$verbose);
}
$mastodonPoster->post(
status => "$repoName $name $url",
);
}
mkdir "check";
for my $repoName (@repoNames){
# https://developer.github.com/v3/repos/releases/
my $url = "https://api.github.com/repos/$repoName/releases";
my $res = $ua->get($url,'Accept' => 'application/vnd.github.v3+json');
$res->is_success or die "$url\n",$res->status_line;
my $list = decode_json $res->content;
my $latest;
for my $item (@$list){
next if $item->{prerelease};
$latest = $item if newer $item,$latest;
}
if( not $latest){
warn "missing latest: $repoName\n";
print dump($latest);
next;
}
my $name = $latest->{name};
$name =~ s/\A\s+//;
$name =~ s/\s+\z//;
$verbose and say "$repoName $name";
$url = $latest->{ html_url };
my $checkFile = "check/".safeName($url);
next if -e $checkFile;
if(lc($postTo) eq "lemmy" ){
postToLemmy($repoName,$name,$url);
}else{
postToMastodon($repoName,$name,$url);
}
open(my $fh,">:raw",$checkFile) or die "$! $checkFile";
close($fh) or die "$! $checkFile";
}