line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::GitHubWebhooks2Ikachan; |
2
|
9
|
|
|
9
|
|
829461
|
use 5.008005; |
|
9
|
|
|
|
|
35
|
|
|
9
|
|
|
|
|
439
|
|
3
|
9
|
|
|
9
|
|
50
|
use strict; |
|
9
|
|
|
|
|
19
|
|
|
9
|
|
|
|
|
260
|
|
4
|
9
|
|
|
9
|
|
48
|
use warnings; |
|
9
|
|
|
|
|
31
|
|
|
9
|
|
|
|
|
290
|
|
5
|
9
|
|
|
9
|
|
240565
|
use Encode qw/encode_utf8/; |
|
9
|
|
|
|
|
299119
|
|
|
9
|
|
|
|
|
1031
|
|
6
|
9
|
|
|
9
|
|
11426
|
use Getopt::Long; |
|
9
|
|
|
|
|
116137
|
|
|
9
|
|
|
|
|
63
|
|
7
|
9
|
|
|
9
|
|
10905
|
use JSON; |
|
9
|
|
|
|
|
110042
|
|
|
9
|
|
|
|
|
60
|
|
8
|
9
|
|
|
9
|
|
9308
|
use Log::Minimal; |
|
9
|
|
|
|
|
828148
|
|
|
9
|
|
|
|
|
79
|
|
9
|
9
|
|
|
9
|
|
11436
|
use LWP::UserAgent; |
|
9
|
|
|
|
|
2000371
|
|
|
9
|
|
|
|
|
351
|
|
10
|
9
|
|
|
9
|
|
8737
|
use Plack::Builder; |
|
9
|
|
|
|
|
111128
|
|
|
9
|
|
|
|
|
945
|
|
11
|
9
|
|
|
9
|
|
7822
|
use Plack::Runner; |
|
9
|
|
|
|
|
439050
|
|
|
9
|
|
|
|
|
332
|
|
12
|
9
|
|
|
9
|
|
1149
|
use Plack::Request; |
|
9
|
|
|
|
|
219472
|
|
|
9
|
|
|
|
|
187
|
|
13
|
9
|
|
|
9
|
|
9444
|
use Pod::Usage; |
|
9
|
|
|
|
|
395616
|
|
|
9
|
|
|
|
|
1584
|
|
14
|
9
|
|
|
9
|
|
6092
|
use App::GitHubWebhooks2Ikachan::Events; |
|
9
|
|
|
|
|
34
|
|
|
9
|
|
|
|
|
376
|
|
15
|
|
|
|
|
|
|
use Class::Accessor::Lite( |
16
|
9
|
|
|
|
|
44
|
new => '0', |
17
|
|
|
|
|
|
|
rw => [qw/ua ikachan_url/], |
18
|
9
|
|
|
9
|
|
59
|
); |
|
9
|
|
|
|
|
22
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $VERSION = "0.10"; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub new { |
23
|
8
|
|
|
8
|
0
|
109
|
my ($class, $args) = @_; |
24
|
|
|
|
|
|
|
|
25
|
8
|
|
|
|
|
97
|
my $ua = LWP::UserAgent->new( |
26
|
|
|
|
|
|
|
agent => "App::GitHubWebhooks2Ikachan (Perl)", |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
8
|
|
|
|
|
25910
|
bless { |
30
|
|
|
|
|
|
|
ua => $ua, |
31
|
|
|
|
|
|
|
ikachan_url => $args->{ikachan_url}, |
32
|
|
|
|
|
|
|
debug => $args->{debug}, |
33
|
|
|
|
|
|
|
}, $class; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub to_app { |
37
|
0
|
|
|
0
|
0
|
0
|
my ($self) = @_; |
38
|
|
|
|
|
|
|
|
39
|
0
|
0
|
|
|
|
0
|
if ($self->{debug}) { |
40
|
0
|
|
|
|
|
0
|
infof("*** RUNNING IN DEBUG MODE ***"); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
0
|
infof("App::GitHubWebhooks2Ikachan Version: %.2f", $VERSION); |
44
|
0
|
|
|
|
|
0
|
infof("ikachan url: %s", $self->ikachan_url); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
builder { |
47
|
0
|
|
|
0
|
|
0
|
enable 'AccessLog'; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub { |
50
|
0
|
|
|
|
|
0
|
my $env = shift; |
51
|
0
|
|
|
|
|
0
|
my $req = Plack::Request->new($env); |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
0
|
return $self->respond_to_ikachan($req); |
54
|
0
|
|
|
|
|
0
|
}; |
55
|
0
|
|
|
|
|
0
|
}; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub respond_to_ikachan { |
59
|
20
|
|
|
20
|
0
|
83892
|
my ($self, $req) = @_; |
60
|
|
|
|
|
|
|
|
61
|
20
|
|
|
|
|
147
|
(my $channel = $req->path_info) =~ s!\A/+!!; |
62
|
20
|
100
|
|
|
|
374
|
if (!$channel) { |
63
|
1
|
|
|
|
|
6
|
return [400, ['Content-Type' => 'text/plain', 'Content-Length' => 20], ['Missing channel name']]; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
19
|
|
|
|
|
108
|
my $payload = $req->param('payload'); |
67
|
19
|
100
|
|
|
|
3601
|
unless ($payload) { |
68
|
1
|
|
|
|
|
5
|
return [400, ['Content-Type' => 'text/plain', 'Content-Length' => 18], ['Payload is nothing']]; |
69
|
|
|
|
|
|
|
} |
70
|
18
|
|
|
|
|
2812
|
my $dat = decode_json($payload); |
71
|
|
|
|
|
|
|
|
72
|
18
|
50
|
|
|
|
136
|
if ($self->{debug}) { |
73
|
0
|
|
|
|
|
0
|
infof("Payload: %s", $payload); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
18
|
|
|
|
|
95
|
my $event_name = $req->header('X-GitHub-Event'); |
77
|
|
|
|
|
|
|
|
78
|
18
|
|
|
|
|
3880
|
my $event_dispatcher = App::GitHubWebhooks2Ikachan::Events->new( |
79
|
|
|
|
|
|
|
dat => $dat, |
80
|
|
|
|
|
|
|
req => $req, |
81
|
|
|
|
|
|
|
); |
82
|
|
|
|
|
|
|
|
83
|
18
|
|
|
|
|
263
|
my $send_texts = $event_dispatcher->dispatch($event_name); |
84
|
18
|
100
|
|
|
|
773
|
if ($send_texts) { |
85
|
15
|
100
|
|
|
|
54
|
if (ref $send_texts ne 'ARRAY') { |
86
|
13
|
|
|
|
|
37
|
$send_texts = [$send_texts]; |
87
|
|
|
|
|
|
|
} |
88
|
15
|
|
|
|
|
39
|
for my $send_text (@$send_texts) { |
89
|
16
|
|
|
|
|
385
|
$self->send_to_ikachan($channel, $send_text); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
18
|
|
|
|
|
186722
|
return [200, ['Content-Type' => 'text/plain', 'Content-Length' => 2], ['OK']]; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub send_to_ikachan { |
97
|
16
|
|
|
16
|
0
|
42
|
my ($self, $channel, $text) = @_; |
98
|
|
|
|
|
|
|
|
99
|
16
|
|
|
|
|
80
|
my $res = $self->ua->post($self->ikachan_url, [ |
100
|
|
|
|
|
|
|
message => $text, |
101
|
|
|
|
|
|
|
channel => $channel, |
102
|
|
|
|
|
|
|
]); |
103
|
|
|
|
|
|
|
|
104
|
16
|
|
|
|
|
7282566
|
$text = encode_utf8($text); |
105
|
|
|
|
|
|
|
|
106
|
16
|
|
|
|
|
186
|
$channel =~ s/\A\%23/#/; |
107
|
16
|
|
|
|
|
115
|
infof("POST %s, %s", $channel, $text); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub parse_options { |
111
|
0
|
|
|
0
|
0
|
|
my ($class, @argv) = @_; |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
my $p = Getopt::Long::Parser->new( |
114
|
|
|
|
|
|
|
config => [qw(posix_default no_ignore_case auto_help pass_through)], |
115
|
|
|
|
|
|
|
); |
116
|
|
|
|
|
|
|
|
117
|
0
|
0
|
|
|
|
|
$p->getoptionsfromarray(\@argv, \my %opt, qw/ |
118
|
|
|
|
|
|
|
ikachan_url=s |
119
|
|
|
|
|
|
|
debug |
120
|
|
|
|
|
|
|
/) or pod2usage(); |
121
|
0
|
0
|
|
|
|
|
$opt{ikachan_url} || pod2usage(); |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
return (\%opt, \@argv); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub run { |
127
|
0
|
|
|
0
|
0
|
|
my ($self, @argv) = @_; |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
my $runner = Plack::Runner->new; |
130
|
0
|
|
|
|
|
|
$runner->parse_options('--port=5555', @argv); |
131
|
0
|
|
|
|
|
|
$runner->run($self->to_app); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
1; |
135
|
|
|
|
|
|
|
__END__ |