TheEye/Plugin/Notify/PagerDuty.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 83 CC 5 SCAR 26.9
CC
5
Cov
0%
pd_send
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 14.7
1 package TheEye::Plugin::Notify::PagerDuty;
2  
3 use 5.010;
4 use Mouse::Role;
5 use LWP::UserAgent;
6 use URI::Escape;
7 use JSON;
8 use Data::Dumper;
9  
10 # ABSTRACT: Plugin for TheEye to raise alerts in Pager Duty
11 #
12 our $VERSION = '0.5'; # VERSION
13  
14 has 'pd_token' => (
15 is => 'rw',
16 isa => 'Str',
17 lazy => 1,
18 required => 1,
19 default => '',
20 );
21  
22 has 'pd_host' => (
23 is => 'rw',
24 isa => 'Str',
25 lazy => 1,
26 required => 1,
27 default => ''
28 );
29  
30 has 'pd_url' => (
31 is => 'ro',
32 isa => 'Str',
33 lazy => 1,
34 required => 1,
35 default => 'https://events.pagerduty.com/generic/2010-04-15/create_event.json',
36 );
37  
38 has 'pd_err' => (
39 is => 'rw',
40 isa => 'HashRef',
41 required => 0,
42 default => 0,
43 predicate => 'has_pd_err',
44 );
45  
46 around 'notify' => sub {
47 my $orig = shift;
48 my ( $self, $tests ) = @_;
49  
50 my @errors;
51 foreach my $test (@{$tests}) {
52 foreach my $step ( @{ $test->{steps} } ) {
53 if ( $step->{status} eq 'not_ok' ) {
54  
55 if($step->{message} =~ m{^(not ok \d+ -) ([^\s]+) (.*)$}){
56 $step->{lead_in} = $1;
57 $step->{node} = $2;
58 $step->{test_error} = $3;
59 }
60 my $message = {
61 service_key => $self->pd_token,
62 incident_key => $step->{node} || $test->{node},
63 event_type => 'trigger',
64 description => $step->{message},
65 details => {
66 node => $step->{node},
67 test => $test->{file},
68 result => $step->{comment},
69 host => $self->pd_host,
70 delta => $step->{delta},
71 },
72 };
73 my $res = $self->pd_send($message);
74 print Dumper $res if $self->is_debug;
75 }
76 }
77 }
78 };
79  
80 sub pd_send {
81 my ( $self, $content ) = @_;
82  
83 my $req = HTTP::Request->new();
84 $req->method('POST');
85 $req->uri($self->pd_url);
86 $req->header('Content-Type' => 'application/json');
87  
88 $req->content( to_json($content) ) if ($content);
89  
90 my $ua = LWP::UserAgent->new();
91 my $res = $ua->request($req);
92 print STDERR "Result: " . $res->decoded_content . "\n" if $self->is_debug;
93 if ( $res->is_success ) {
94 return from_json( $res->decoded_content );
95 }
96 else {
97 if ( my $_err = from_json( $res->decoded_content ) ) {
98 $self->pd_err($_err);
99 }
100 else {
101 $self->pd_err( $res->status_line );
102 }
103 }
104 return;
105 }
106  
107 1;
108  
109 =pod
110  
111 =encoding UTF-8
112  
113 =head1 NAME
114  
115 TheEye::Plugin::Notify::PagerDuty - Plugin for TheEye to raise alerts in Pager Duty
116  
117 =head1 VERSION
118  
119 version 0.5
120  
121 =head1 AUTHOR
122  
123 Lenz Gschwendtner <lenz@springtimesoft.com>
124  
125 =head1 COPYRIGHT AND LICENSE
126  
127 This software is copyright (c) 2014 by springtimesoft LTD.
128  
129 This is free software; you can redistribute it and/or modify it under
130 the same terms as the Perl 5 programming language system itself.
131  
132 =cut
133  
134 __DATA__