| 1 | |||||||||||||
| 2 | package Plack::App::HipChat::WebHook; | ||||||||||||
| 3 | |||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 4 | ▶ | use parent qw( Plack::Component ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 4 | ▶ | use Plack::Util::Accessor qw(webhooks hipchat_user_agent ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 4 | ▶ | use Plack::Util; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 4 | ▶ | use Plack::Request; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 4 | ▶ | use JSON qw(decode_json); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 4 | ▶ | use Try::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 9 | ▶ | sub call { | ||||||||||
|
Subroutine
call: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 17 | 9 | my($self, $env) = @_; | |||||||||||
| 18 | |||||||||||||
| 19 | 9 | my $Req = Plack::Request->new($env); | |||||||||||
| 20 | |||||||||||||
| 21 | 9 | my $rh_webhooks = $self->webhooks; | |||||||||||
| 22 | |||||||||||||
| 23 | 9 | my $path = $Req->path_info(); | |||||||||||
| 24 | 9 | ▶ | if ($rh_webhooks->{$path}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 25 | |||||||||||||
| 26 | ## Check headers | ||||||||||||
| 27 | |||||||||||||
| 28 | 7 | ▶ | my $hipchat_uagent = $self->hipchat_user_agent() // 'HipChat.com'; | ||||||||||
|
Condition100%2/2
$self->hipchat_user_agent // "HipChat.com"
MC/DC100%1/1
$self->hipchat_user_agent // "HipChat.com"
$self->hipchat_user_agent
Truth table100%2/2
A
A= $self->hipchat_user_agent
| |||||||||||||
| 29 | |||||||||||||
| 30 | 7 | ▶ | if ($Req->headers()->header('User-Agent') ne $hipchat_uagent) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 31 | # warn "No HipChat.com User-Agent header\n"; | ||||||||||||
| 32 | 2 | return $self->return_400(); | |||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | 5 | ▶ | if ($Req->headers()->header('Content-Type') ne 'application/json') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | # warn "Not application/json Content-Type\n"; | ||||||||||||
| 37 | 1 | return $self->return_400(); | |||||||||||
| 38 | } | ||||||||||||
| 39 | |||||||||||||
| 40 | 4 | my $rh; | |||||||||||
| 41 | 4 | ▶ | try { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 42 | 4 | $rh = decode_json($Req->content()); | |||||||||||
| 43 | 1 | ▶ | } catch { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 44 | # warn "Failed to decode JSON content\n"; | ||||||||||||
| 45 | 4 | }; | |||||||||||
| 46 | |||||||||||||
| 47 | 4 | ▶ | if (!$rh) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | 1 | return $self->return_400(); | |||||||||||
| 49 | } | ||||||||||||
| 50 | |||||||||||||
| 51 | 3 | my $rc = $rh_webhooks->{$path}; | |||||||||||
| 52 | 3 | return Plack::Util::run_app($rc, $rh); | |||||||||||
| 53 | } | ||||||||||||
| 54 | |||||||||||||
| 55 | 2 | return $self->return_404(); | |||||||||||
| 56 | } | ||||||||||||
| 57 | |||||||||||||
| 58 | 2 | ▶ | sub return_404 { | ||||||||||
|
Subroutine
return_404: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 59 | 2 | my $self = shift; | |||||||||||
| 60 | |||||||||||||
| 61 | # warn "Not found\n"; | ||||||||||||
| 62 | 2 | return [ 404, [ 'Content-Type' => 'text/plain', 'Content-Length' => 9 ], | |||||||||||
| 63 | ['Not found'] ]; | ||||||||||||
| 64 | } | ||||||||||||
| 65 | |||||||||||||
| 66 | 4 | ▶ | sub return_400 { | ||||||||||
|
Subroutine
return_400: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 67 | 4 | my $self = shift; | |||||||||||
| 68 | 4 | return [ 400, ['Content-Type' => 'text/plain', 'Content-Length' => 11 ], | |||||||||||
| 69 | ['Bad Request'] ]; | ||||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | 4 | 1; | |||||||||||
| 73 | |||||||||||||
| 74 | # ABSTRACT: HipChat WebHook Plack Application | ||||||||||||
| 75 | |||||||||||||
| 76 | __END__ | ||||||||||||