| 1 | package Github::Hooks::Receiver; | ||||||||||||||
| 2 | 4 | ▶ | use 5.008001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 4 | our $VERSION = "0.02"; | |||||||||||||
| 7 | |||||||||||||||
| 8 | 4 | ▶ | use Github::Hooks::Receiver::Event; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 4 | ▶ | use JSON; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 4 | ▶ | use Plack::Request; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | use Class::Accessor::Lite ( | ||||||||||||||
| 13 | 4 | new => 1, | |||||||||||||
| 14 | ro => [qw/secret/], | ||||||||||||||
| 15 | 4 | ▶ | ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 4 | ▶ | use Digest::SHA; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 4 | ▶ | use String::Compare::ConstantTime; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | |||||||||||||||
| 19 | 24 | ▶ | sub events { shift->{events} ||= {} } | ||||||||||||
|
Subroutine
events: called CC=2 SCAR=6.9
Pod
undocumented
Condition100%2/2
+(shift())->{"events"} ||= {}
MC/DC100%1/1
+(shift())->{"events"} ||= {}
+(shift())->{"events"}
Truth table100%2/2
A
A= +(shift())->{"events"}
| |||||||||||||||
| 20 | |||||||||||||||
| 21 | sub to_app { | ||||||||||||||
| 22 | 4 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
to_app: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 23 | sub { | ||||||||||||||
| 24 | 13 | ▶ | my $env = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.1
| |||||||||||||||
| 25 | 13 | my $req = Plack::Request->new($env); | |||||||||||||
| 26 | 13 | ▶ | if ($req->method ne 'POST') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 27 | 0 | return [400, [], ['BAD REQUEST']]; | |||||||||||||
| 28 | } | ||||||||||||||
| 29 | |||||||||||||||
| 30 | # verify signature | ||||||||||||||
| 31 | 13 | ▶ | if ($self->secret) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 32 | 6 | my $expected = 'sha1=' . Digest::SHA::hmac_sha1_hex($req->content, $self->secret); | |||||||||||||
| 33 | 6 | ▶ | my $actual = $req->header('X-Hub-Signature') || ''; | ||||||||||||
|
Condition100%2/2
$req->header('X-Hub-Signature') || ''
MC/DC100%1/1
$req->header('X-Hub-Signature') || ''
$req->header('X-Hub-Signature')
Truth table100%2/2
A
A= $req->header('X-Hub-Signature')
| |||||||||||||||
| 34 | 6 | ▶ | if (!String::Compare::ConstantTime::equals($expected, $actual)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | 4 | return [400, [], ['BAD REQUEST']]; | |||||||||||||
| 36 | } | ||||||||||||||
| 37 | } | ||||||||||||||
| 38 | |||||||||||||||
| 39 | # Parse JSON payload | ||||||||||||||
| 40 | 9 | my $payload_json; | |||||||||||||
| 41 | 9 | ▶ | if (lc $req->header('content-type') eq 'application/json') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 42 | 2 | $payload_json = $req->content; | |||||||||||||
| 43 | } elsif (lc $req->header('content-type') eq 'application/x-www-form-urlencoded') { | ||||||||||||||
| 44 | 6 | $payload_json = $req->param('payload'); | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | 9 | ▶ | my $payload = eval { decode_json $payload_json } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | or return [400, [], ['BAD REQUEST']]; | ||||||||||||||
| 48 | |||||||||||||||
| 49 | 8 | my $event_name = $req->header('X-GitHub-Event'); | |||||||||||||
| 50 | 8 | my $event = Github::Hooks::Receiver::Event->new( | |||||||||||||
| 51 | payload => $payload, | ||||||||||||||
| 52 | event => $event_name, | ||||||||||||||
| 53 | ); | ||||||||||||||
| 54 | |||||||||||||||
| 55 | 8 | ▶ | if (my $code = $self->events->{''}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 8 | $code->($event, $req); | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | 8 | ▶ | if (my $code = $self->events->{$event_name}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 5 | $code->($event, $req); | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | 8 | [200, [], ['OK']]; | |||||||||||||
| 63 | 4 | }; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | sub on { | ||||||||||||||
| 67 | 8 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
on: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 68 | 8 | my ($event, $code) = @_; | |||||||||||||
| 69 | 8 | ▶ | if (ref $event eq 'CODE') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | 4 | $code = $event; | |||||||||||||
| 71 | 4 | $event = ''; | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | 8 | $self->events->{$event} = $code; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | sub run { | ||||||||||||||
| 77 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
run: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 78 | 0 | ▶ | my %opts = @_ == 1 ? %{$_[0]} : @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | |||||||||||||||
| 80 | 0 | my %server; | |||||||||||||
| 81 | 0 | my $server = delete $opts{server}; | |||||||||||||
| 82 | 0 | ▶ | $server{server} = $server if $server; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | |||||||||||||||
| 84 | 0 | my @options = %opts; | |||||||||||||
| 85 | 0 | require Plack::Runner; | |||||||||||||
| 86 | |||||||||||||||
| 87 | 0 | my $runner = Plack::Runner->new( | |||||||||||||
| 88 | %server, | ||||||||||||||
| 89 | options => \@options, | ||||||||||||||
| 90 | ); | ||||||||||||||
| 91 | 0 | $runner->run($self->to_app); | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 4 | 1; | |||||||||||||
| 95 | __END__ | ||||||||||||||