blib/lib/CGI/Github/Webhook.pm

filter: Statementstmt 94.4% 136 / 144 Branchbran 60.0% 18 / 30 Conditioncond 85.7% 6 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 96.9% 32 / 33 Podpod 100% 4 / 4 totaltotal 89.1% 198 / 222 SCAR 29.7
CC
19
Cov
88%
run
7.5
deploy_badge
4.2
__ANON__
3.3
CRAP 19.6
↑ Summary
1 package CGI::Github::Webhook;
2  
3 # ABSTRACT: Easily create CGI based GitHub webhooks
4  
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 our $VERSION = '0.05'; # VERSION
10  
11 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use CGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Digest::SHA qw(hmac_sha1_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use File::ShareDir qw(module_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 #=head1 EXPORT
23 #
24 #A list of functions that can be exported. You can delete this section
25 #if you don't export anything, such as for a purely object-oriented module.
26  
27  
28 has badges_from => (
29 is => 'rw',
30 5 ▶ default => sub { module_dir(__PACKAGE__); },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 isa => sub {
32 5 ▶ die "$_[0] needs to be an existing directory"
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless -d $_[0] 0 5 5
33 unless -d $_[0];
34 },
35 5 lazy => 1,
36 );
37  
38  
39 has badge_to => (
40 is => 'rw',
41 2 ▶ default => sub { return },
Subroutine __ANON__: called CC=1 SCAR=0.0
42 isa => sub {
43 7 ▶ die "$_[0] needs have a file suffix"
Subroutine __ANON__: called CC=3 SCAR=12.0
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and not $_[0] =~ /\./ 0 7 7
Condition66%2/3
defined $_[0] and not $_[0] =~ /\./
!ll&&!rl&&r
250
MC/DC0%0/2
defined $_[0] and not $_[0] =~ /\./
defined $_[0] not $_[0] =~ /\./
Truth table66%2/3
A and not(B)
A= defined $_[0]B= not $_[0] =~ /\./
ABresult
0X0
100
111
44 if (defined($_[0]) and $_[0] !~ /\./);
45 },
46 5 );
47  
48  
49 has cgi => (
50 is => 'ro',
51 4 ▶ default => sub { CGI->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
52 5 );
53  
54  
55 has log => (
56 is => 'ro',
57 0 ▶ default => sub { '/dev/stderr' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 isa => sub {
59 7 ▶ my $dir = dirname($_[0]);
Subroutine __ANON__: called CC=2 SCAR=7.2
60 7 ▶ die "$dir doesn't exist!" unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 7 7
61 },
62 5 );
63  
64  
65 has mime_type => (
66 is => 'ro',
67 7 ▶ default => sub { 'text/plain; charset=utf-8' },
Subroutine __ANON__: called CC=1 SCAR=0.0
68 5 );
69  
70  
71 5 has secret => (
72 is => 'ro',
73 required => 1,
74 );
75  
76  
77 has text_on_success => (
78 is => 'rw',
79 7 ▶ default => sub { 'Successfully triggered' },
Subroutine __ANON__: called CC=1 SCAR=0.0
80 5 );
81  
82  
83 has text_on_auth_fail => (
84 is => 'rw',
85 7 ▶ default => sub { 'Authentication failed' },
Subroutine __ANON__: called CC=1 SCAR=0.0
86 5 );
87  
88  
89 has text_on_trigger_fail => (
90 is => 'rw',
91 7 ▶ default => sub { 'Trigger failed' },
Subroutine __ANON__: called CC=1 SCAR=0.0
92 5 );
93  
94  
95  
96 5 has trigger => (
97 is => 'rw',
98 required => 1,
99 );
100  
101  
102 5 has trigger_backgrounded => (
103 is => 'rw',
104 default => 1,
105 );
106  
107  
108 5 has authenticated => (
109 is => 'lazy',
110 );
111  
112 sub _build_authenticated {
113 7 ▶ my $self = shift;
Subroutine _build_authenticated: called CC=3 SCAR=11.0
114  
115 7 my $logfile = $self->log;
116 7 my $q = $self->cgi;
117 7 my $secret = $self->secret;
118  
119 7 open(my $logfh, '>>', $logfile);
120 7 say $logfh "Date: ".localtime;
121 7 say $logfh "Remote IP: ".$q->remote_host()." (".$q->remote_addr().")";
122  
123 7 ▶ my $x_hub_signature =
Condition100%2/2
$q->http('X-Hub-Signature') || '<no-x-hub-signature>'
l!l
52
MC/DC100%1/1
$q->http('X-Hub-Signature') || '<no-x-hub-signature>'
$q->http('X-Hub-Signature')
Truth table100%2/2
A
A= $q->http('X-Hub-Signature')
Aresult
11
00
124 $q->http('X-Hub-Signature') || '<no-x-hub-signature>';
125 7 ▶ my $calculated_signature = 'sha1='.
Condition100%2/2
$self->payload // ""
l!l
52
MC/DC100%1/1
$self->payload // ""
$self->payload
Truth table100%2/2
A
A= $self->payload
Aresult
11
00
126 hmac_sha1_hex($self->payload // '', $secret);
127  
128 7 print $logfh Dumper($self->payload_perl,
129 $x_hub_signature, $calculated_signature);
130 7 close $logfh;
131  
132 7 return $x_hub_signature eq $calculated_signature;
133 }
134  
135  
136 5 has payload => (
137 is => 'lazy',
138 );
139  
140 sub _build_payload {
141 7 ▶ my $self = shift;
Subroutine _build_payload: called CC=2 SCAR=6.9
142 7 my $q = $self->cgi;
143  
144 7 ▶ if ($q->param('POSTDATA')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q->param('POSTDATA')) { } 5 2 7
145 5 return ''.$q->param('POSTDATA');
146 } else {
147 2 return;
148 }
149 }
150  
151  
152 5 has payload_json => (
153 is => 'lazy',
154 );
155  
156 sub _build_payload_json {
157 7 ▶ my $self = shift;
Subroutine _build_payload_json: called CC=2 SCAR=6.9
158 7 my $q = $self->cgi;
159  
160 7 my $payload = qq({"payload":"none"});
161 7 ▶ if ($self->payload) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->payload) 5 2 7
162 5 $payload = $self->payload;
163 try {
164 5 ▶ decode_json($payload);
Subroutine __ANON__: called CC=1 SCAR=0.0
165 } catch {
166 1 ▶ s/\"/\'/g; s/\n/ /gs;
Subroutine __ANON__: called CC=1 SCAR=0.0
167 1 $payload = qq({"error":"$_"});
168 5 };
169 }
170  
171 7 return $payload;
172 }
173  
174  
175 5 has payload_perl => (
176 is => 'lazy',
177 );
178  
179 sub _build_payload_perl {
180 7 ▶ my $self = shift;
Subroutine _build_payload_perl: called CC=1 SCAR=0.0
181  
182 7 return decode_json($self->payload_json);
183 }
184  
185  
186 sub deploy_badge {
187 5 ▶ my $self = shift;
Subroutine deploy_badge: called CC=4 SCAR=14.4
Pod documented
188 5 ▶ return unless $self->badge_to;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->badge_to 0 5 5
189  
190 5 my $basename = shift;
191 5 ▶ die "No basename provided" unless defined($basename);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $basename 0 5 5
192  
193 5 my $suffix = $self->badge_to;
194 5 $suffix =~ s/^.*(\.[^.]*?)$/$1/;
195 5 my $badge = $self->badges_from.'/'.$basename.$suffix;
196  
197 5 my $logfile = $self->log;
198 5 open(my $logfh, '>>', $logfile);
199  
200 5 my $file_copied = copy($badge, $self->badge_to);
201 5 ▶ if ($file_copied) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_copied) { } 5 0 5
202 5 say $logfh "$badge successfully copied to ".$self->badge_to;
203 5 return 1;
204 } else {
205 0 say $logfh "Couldn't copy $badge to ".$self->badge_to.": $!";
206 0 return;
207 }
208 }
209  
210  
211 sub header {
212 7 ▶ my $self = shift;
Subroutine header: called CC=2 SCAR=7.6
Pod documented
213 7 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 7 7
214 0 return $self->cgi->header(@_);
215 } else {
216 7 return $self->cgi->header($self->mime_type);
217 }
218 }
219  
220  
221 sub send_header {
222 3 ▶ my $self = shift;
Subroutine send_header: called CC=1 SCAR=0.0
Pod documented
223  
224 3 print $self->header(@_);
225 }
226  
227  
228 sub run {
229 3 ▶ my $self = shift;
Subroutine run: called CC=7 SCAR=20.2
Pod documented
230  
231 3 $self->send_header();
232  
233 3 my $logfile = $self->log;
234 3 open(my $logfh, '>>', $logfile);
235  
236 3 ▶ if ($self->authenticated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->authenticated) { } 2 1 3
237 2 ▶ my $trigger = $self->trigger.' >> '.$logfile.' 2>&1 '.
Branch: 1 branch
BranchTrueFalseTotal
$self->trigger_backgrounded ? : 0 2 2
238 ($self->trigger_backgrounded ? '&' : '');
239 2 my $rc = system($trigger);
240 2 ▶ if ($rc != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc != 0) { } 1 1 2
241 1 say $logfh $trigger;
242 1 say $self->text_on_trigger_fail;
243 1 say $logfh $self->text_on_trigger_fail;
244 1 ▶ if ($? == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($? == -1) { } 0 1 1
elsif ($? & 127) { } 0 1 1
245 0 say $logfh "Trigger failed to execute: $!";
246 0 $self->deploy_badge('errored');
247 } elsif ($? & 127) {
248 0 ▶ printf $logfh "child died with signal %d, %s coredump\n",
Branch: 1 branch
BranchTrueFalseTotal
$? & 128 ? : 0 0 0
249 ($? & 127), ($? & 128) ? 'with' : 'without';
250 0 $self->deploy_badge('errored');
251 } else {
252 1 printf $logfh "child exited with value %d\n", $? >> 8;
253 1 $self->deploy_badge('failed');
254 }
255 1 close $logfh;
256 1 return 0;
257 } else {
258 1 $self->deploy_badge('success');
259 1 say $self->text_on_success;
260 1 say $logfh $self->text_on_success;
261  
262 1 close $logfh;
263 1 return 1;
264 }
265 } else {
266 1 say $self->text_on_auth_fail;
267 1 say $logfh $self->text_on_auth_fail;
268 1 close $logfh;
269 1 return; # undef or empty list, i.e. false
270 }
271 }
272  
273  
274 5 1; # End of CGI::Github::Webhook
275  
276 __END__
↑ Summary