blib/lib/CGI/Github/Webhook.pm

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