blib/lib/WWW/PagerDuty.pm

filter: Statementstmt 38.7% 43 / 111 Branchbran 6.7% 5 / 74 Conditioncond 3.7% 2 / 54 MC/DCmcdc 2.9% 1 / 34 Subroutinesub 84.6% 11 / 13 Podpod 0.0% 0 / 4 totaltotal 21.3% 62 / 290 CC 56 SCAR 63.5
CC
56
Cov
21%
resolve
930.0
trigger
506.0
new
6.3
CRAP 574.7
↑ Summary
1 package WWW::PagerDuty;
2  
3 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 2 local $@;
8
9 2 ▶ use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶ use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶ use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our @ISA = qw(Exporter);
14 2 our @EXPORT_OK = qw(new trigger resolve);
15  
16 2 ▶ use constant SERVICE_URL => "https://events.pagerduty.com/generic/2010-04-15/create_event.json";
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶ use constant TRIGGER_KEY => 'trigger';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶ use constant RESOLVE_KEY => 'resolve';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 our $VERSION = 0.2;
21 }
22  
23 1 ▶sub error($$) {
Subroutine error: called CC=1 SCAR=0.0
Pod undocumented
24 1 my ($self, $errstr) = @_;
25  
26 1 my $result = { errors => [] };
27  
28 1 push (@{$result->{errors}}, $errstr);
29  
30 1 return $result;
31 }
32  
33 2 ▶sub new($$) {
Subroutine new: called CC=6 SCAR=18.4
Pod undocumented
34  
35 2 my ($self, $params) = @_;
36  
37 2 ▶ return $self->error('Error: not enough parameters provided') if (!(defined $params) or ref $params ne 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $params or ref $params ne "HASH" 1 1 2
Condition66%2/3
not defined $params or ref $params ne "HASH"
l!l&&r!l&&!r
101
MC/DC50%1/2
not defined $params or ref $params ne "HASH"
not defined $params ref $params ne "HASH"
Truth table66%2/3
not(A) or B
A= not defined $paramsB= ref $params ne "HASH"
ABresult
1X1
011
000
38  
39 1 ▶ return $self->error("Error: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
40  
41 1 ▶ return $self->error('Error: service_key *must* be defined') if (!defined $params->{service_key});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{"service_key"} 0 1 1
42  
43 1 my $lwp = undef;
44  
45 1 eval '$lwp = LWP::UserAgent->new()';
46  
47 1 ▶ return $self->error('Error: could not instantiate LWP Object') if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
48  
49 $self = bless {
50 url => SERVICE_URL,
51 user_agent => $lwp,
52 trigger_key => TRIGGER_KEY,
53 resolve_key => RESOLVE_KEY,
54 service_key => $params->{service_key},
55 incident_key => $params->{incident_key}
56 1 }, "WWW::PagerDuty";
57  
58 1 return $self;
59 }
60  
61  
62 0 ▶sub trigger($$) {
Subroutine trigger: not called CC=22 SCAR=62.3
Pod undocumented
63 0 my ($self, $params) = @_;
64  
65 0 ▶ return $self->error('Error: not called correctly') if (!(defined $self) or ref $self ne 'WWW::PagerDuty');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self or ref $self ne "WWW::PagerDuty" 0 0 0
Condition0%0/3
not defined $self or ref $self ne "WWW::PagerDuty"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $self or ref $self ne "WWW::PagerDuty"
not defined $self ref $self ne "WWW::PagerDuty"
Truth table0%0/3
not(A) or B
A= not defined $selfB= ref $self ne "WWW::PagerDuty"
ABresult
1X1
011
000
66  
67 0 ▶ return $self->error('Error: parameters not passed') if (!(defined $params) or ref $params ne 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $params or ref $params ne "HASH" 0 0 0
Condition0%0/3
not defined $params or ref $params ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $params or ref $params ne "HASH"
not defined $params ref $params ne "HASH"
Truth table0%0/3
not(A) or B
A= not defined $paramsB= ref $params ne "HASH"
ABresult
1X1
011
000
68  
69 0 ▶ return $self->error('Error: incident_key *must* be passed') if (!(defined $params->{incident_key}) and !(defined $self->{incident_key}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{"incident_key"} or defined $self->{"incident_key"} 0 0 0
Condition0%0/3
defined $params->{"incident_key"} or defined $self->{"incident_key"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $params->{"incident_key"} or defined $self->{"incident_key"}
defined $params->{"incident_key"} defined $self->{"incident_key"}
Truth table0%0/3
A or B
A= defined $params->{"incident_key"}B= defined $self->{"incident_key"}
ABresult
1X1
011
000
70  
71 0 ▶ return $self->error('Error: description *must* be passed') if !(defined $params->{description});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{"description"} 0 0 0
72
73 0 my $details = $params->{details};
74  
75 0 ▶ if (defined $params->{details} && ref $params->{details} eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $params->{'details'} and ref $params->{'details'} eq 'SCALAR') { } 0 0 0
elsif (defined $params->{'details'} and ref $params->{'details'} eq 'HASH') { } 0 0 0
Condition0%0/3
defined $params->{"details"} and ref $params->{"details"} eq "SCALAR"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $params->{"details"} and ref $params->{"details"} eq "SCALAR"
defined $params->{"details"} ref $params->{"details"} eq "SCALAR"
Truth table0%0/3
A and B
A= defined $params->{"details"}B= ref $params->{"details"} eq "SCALAR"
ABresult
0X0
100
111
76 $details = {
77 'additional_info' => $params->{details}
78 0 };
79 ▶ } elsif (defined $params->{details} && ref $params->{details} eq 'HASH') {
Condition0%0/3
defined $params->{"details"} and ref $params->{"details"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $params->{"details"} and ref $params->{"details"} eq "HASH"
defined $params->{"details"} ref $params->{"details"} eq "HASH"
Truth table0%0/3
A and B
A= defined $params->{"details"}B= ref $params->{"details"} eq "HASH"
ABresult
0X0
100
111
80
81 } else {
82 0 $details = undef;
83 }
84  
85 my $request_body = {
86 'service_key' => $self->{service_key},
87 'incident_key' => (defined $params->{incident_key}) ? $params->{incident_key} : $self->{incident_key},
88 'event_type' => $self->{trigger_key},
89 'description' => $params->{description},
90 0 ▶ 'details' => $details
Branch: 1 branch
BranchTrueFalseTotal
defined $params->{'incident_key'} ? : 0 0 0
91 };
92  
93 0 my $return_body = undef;
94
95 0 local $@;
96  
97 0 my $data = undef;
98  
99 0 $data = eval { JSON::encode_json($request_body); };
100  
101 0 ▶ if ($data) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data) { } 0 0 0
elsif ($@) { } 0 0 0
102  
103 0 $return_body = eval { $self->{user_agent}->post($self->{url}, Content_Type => 'application/json', Content => $data); };
104  
105 0 ▶ if (defined $return_body) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $return_body) { } 0 0 0
elsif ($@) { } 0 0 0
106 0 my $result = JSON::decode_json($return_body->decoded_content);
107 0 ▶ if (defined $result && ref $result eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result and ref $result eq 'HASH') { } 0 0 0
Condition0%0/3
defined $result and ref $result eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $result and ref $result eq "HASH"
defined $result ref $result eq "HASH"
Truth table0%0/3
A and B
A= defined $resultB= ref $result eq "HASH"
ABresult
0X0
100
111
108 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
109 0 ▶ if (defined $result->{errors} && ref $result->{errors} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result->{"errors"} and ref $result->{"errors"} eq "ARRAY") 0 0 0
Condition0%0/3
defined $result->{"errors"} and ref $result->{"errors"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $result->{"errors"} and ref $result->{"errors"} eq "ARRAY"
defined $result->{"errors"} ref $result->{"errors"} eq "ARRAY"
Truth table0%0/3
A and B
A= defined $result->{"errors"}B= ref $result->{"errors"} eq "ARRAY"
ABresult
0X0
100
111
110 0 push(@{$result->{errors}}, $@);
111 }
112 }
113 0 return $result;
114 } else {
115 0 return $return_body;
116 }
117 } elsif($@) {
118 0 return $self->error("Error: $@");
119 } else {
120 0 return $self->error('Error: response body was not returned');
121 }
122  
123 } elsif ($@) {
124 0 return $self->error("Error: $@");
125 }
126  
127 0 return undef;
128 }
129  
130  
131 0 ▶sub resolve($$) {
Subroutine resolve: not called CC=30 SCAR=68.4
Pod undocumented
132 0 my ($self, $params) = @_;
133  
134 0 ▶ return WWW::PagerDuty::error(undef, "Error: calling convention not followed") if !(defined $self) and ref $self ne 'WWW::PagerDuty';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self and ref $self ne "WWW::PagerDuty" 0 0 0
Condition0%0/3
not defined $self and ref $self ne "WWW::PagerDuty"
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $self and ref $self ne "WWW::PagerDuty"
not defined $self ref $self ne "WWW::PagerDuty"
Truth table0%0/3
not(A) and B
A= not defined $selfB= ref $self ne "WWW::PagerDuty"
ABresult
0X0
100
111
135 0 ▶ return $self->error("Error: no parameters passed") if (!(defined $params) or ref $params ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $params or ref $params ne "HASH" 0 0 0
Condition0%0/3
not defined $params or ref $params ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $params or ref $params ne "HASH"
not defined $params ref $params ne "HASH"
Truth table0%0/3
not(A) or B
A= not defined $paramsB= ref $params ne "HASH"
ABresult
1X1
011
000
136 0 ▶ return $self->error("Error: not initialized correctly, service_key *must* be defined") if !(defined $self->{service_key});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"service_key"} 0 0 0
137 0 ▶ return $self->error("Error: incident_key *must* be passed") if !($self->{incident_key}) and !($params->{incident_key});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"incident_key"} or $params->{"incident_key"} 0 0 0
Condition0%0/3
$self->{"incident_key"} or $params->{"incident_key"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"incident_key"} or $params->{"incident_key"}
$self->{"incident_key"} $params->{"incident_key"}
Truth table0%0/3
A or B
A= $self->{"incident_key"}B= $params->{"incident_key"}
ABresult
1X1
011
000
138 0 ▶ return $self->error("Error: description *must* be defined") if !($params->{description});
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"description"} 0 0 0
139  
140 0 my $details = $params->{details};
141  
142 0 ▶ if (defined $details) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $details) 0 0 0
143 0 ▶ if (ref $details eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $details eq 'SCALAR') { } 0 0 0
elsif (ref $details eq 'HASH') { } 0 0 0
144 0 $details = {
145 additional_information => $details
146 };
147 } elsif (ref $details eq 'HASH') {
148
149 } else {
150 0 $details = undef;
151 }
152 }
153  
154 my $request_body = {
155 service_key => (defined $params->{service_key}) ? $params->{service_key} : $self->{service_key},
156 incident_key => (defined $params->{service_key}) ? $params->{incident_key} : $self->{incident_key},
157 event_type => $self->{resolve_key},
158 description => $params->{details},
159 0 ▶ details => $details
Branch: 2 branches
BranchTrueFalseTotal
defined $params->{'service_key'} ? : 0 0 0
defined $params->{'service_key'} ? : 0 0 0
160 };
161  
162 0 my $request_body_json = undef;
163
164 0 local $@;
165  
166 0 $request_body_json = eval { JSON::encode_json($request_body); };
167  
168 0 ▶ if (defined $request_body_json && $request_body_json && !$@) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $request_body_json and $request_body_json and not $@) { } 0 0 0
elsif ($@) { } 0 0 0
Condition0%0/6
defined $request_body_json and $request_body_json and not $@
!ll&&!rl&&r
000
defined $request_body_json and $request_body_json
!ll&&!rl&&r
000
MC/DC0%0/3
defined $request_body_json and $request_body_json and not $@
defined $request_body_json $request_body_json not $@
Truth table0%0/4
A and B and not(C)
A= defined $request_body_jsonB= $request_body_jsonC= not $@
ABCresult
0XX0
10X0
1100
1111
169 0 my $http_response_body = undef;
170  
171 0 $http_response_body = eval { $self->{user_agent}->post($self->{url}, Content_Type => 'application/json', Content => $request_body_json); };
172  
173 0 ▶ if (defined $http_response_body && !$@) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $http_response_body and not $@) { } 0 0 0
elsif (defined $http_response_body and $@) { } 0 0 0
elsif ($@) { } 0 0 0
Condition0%0/3
defined $http_response_body and not $@
!ll&&!rl&&r
000
MC/DC0%0/2
defined $http_response_body and not $@
defined $http_response_body not $@
Truth table0%0/3
A and not(B)
A= defined $http_response_bodyB= not $@
ABresult
0X0
100
111
174 0 my $http_response = undef;
175  
176 0 $http_response = eval { JSON::decode_json($http_response_body->decoded_content); };
177  
178 0 ▶ if (defined $http_response) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $http_response) 0 0 0
179 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
180 0 ▶ if (ref $http_response eq 'HASH' && defined $http_response->{errors} && ref $http_response->{errors} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $http_response eq 'HASH' and defined $http_response->{'errors'} and ref $http_response->{'errors'} eq 'ARRAY') { } 0 0 0
elsif (ref $http_response eq 'HASH' and not defined $http_response->{'errors'}) { } 0 0 0
Condition0%0/6
ref $http_response eq "HASH" and defined $http_response->{"errors"} and ref $http_response->{"errors"} eq "ARRAY"
!ll&&!rl&&r
000
ref $http_response eq "HASH" and defined $http_response->{"errors"}
!ll&&!rl&&r
000
MC/DC0%0/3
ref $http_response eq "HASH" and defined $http_response->{"errors"} and ref $http_response->{"errors"} eq "ARRAY"
ref $http_response eq "HASH" defined $http_response->{"errors"} ref $http_response->{"errors"} eq "ARRAY"
Truth table0%0/4
A and B and C
A= ref $http_response eq "HASH"B= defined $http_response->{"errors"}C= ref $http_response->{"errors"} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
181 0 push(@{$http_response->{errors}}, $@);
182 ▶ } elsif (ref $http_response eq 'HASH' && !defined $http_response->{errors}) {
Condition0%0/3
ref $http_response eq "HASH" and not defined $http_response->{"errors"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $http_response eq "HASH" and not defined $http_response->{"errors"}
ref $http_response eq "HASH" not defined $http_response->{"errors"}
Truth table0%0/3
A and not(B)
A= ref $http_response eq "HASH"B= not defined $http_response->{"errors"}
ABresult
0X0
100
111
183 0 $http_response->{errors} = [];
184 0 push(@{$http_response->{errors}}, $@);
185 }
186 }
187 0 return $http_response;
188 }
189  
190 ▶ } elsif (defined $http_response_body && $@) {
Condition0%0/3
defined $http_response_body and $@
!ll&&!rl&&r
000
MC/DC0%0/2
defined $http_response_body and $@
defined $http_response_body $@
Truth table0%0/3
A and B
A= defined $http_response_bodyB= $@
ABresult
0X0
100
111
191 0 return { errors => ["Error: $@"], http_response_body => $http_response_body };
192 } elsif ($@) {
193 0 return $self->error("Error: $@");
194 }
195 } elsif ($@) {
196 0 return $self->error("Error: $@");
197 }
198  
199 0 return undef;
200 }
201  
202  
203  
204 2 1
↑ Summary