| 1 | package TheEye::Plugin::Notify::Oncall; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use Mouse::Role; | ||
| 5 | use JSON; | ||
| 6 | use LWP::UserAgent; | ||
| 7 | use HTTP::Request::Common; | ||
| 8 | use Data::Dumper; | ||
| 9 | |||
| 10 | # ABSTRACT: Plugin for TheEye to raise alerts in OnCall | ||
| 11 | # | ||
| 12 | our $VERSION = '0.5'; # VERSION | ||
| 13 | |||
| 14 | has 'oncall_token' => ( | ||
| 15 | is => 'rw', | ||
| 16 | isa => 'Str', | ||
| 17 | lazy => 1, | ||
| 18 | required => 1, | ||
| 19 | default => 123, | ||
| 20 | ); | ||
| 21 | |||
| 22 | has 'oncall_url' => ( | ||
| 23 | is => 'ro', | ||
| 24 | isa => 'Str', | ||
| 25 | default => 'http://localhost:3000/add/', | ||
| 26 | lazy => 1, | ||
| 27 | required => 1, | ||
| 28 | ); | ||
| 29 | |||
| 30 | has 'oncall_host' => ( | ||
| 31 | is => 'rw', | ||
| 32 | isa => 'Str', | ||
| 33 | default => sub { my $host = qx{hostname}; chomp($host); return $host }, | ||
| 34 | lazy => 1, | ||
| 35 | required => 1, | ||
| 36 | ); | ||
| 37 | |||
| 38 | around 'notify' => sub { | ||
| 39 | my $orig = shift; | ||
| 40 | my ( $self, $tests ) = @_; | ||
| 41 | |||
| 42 | my @errors; | ||
| 43 | foreach my $test (@{$tests}) { | ||
| 44 | foreach my $step ( @{ $test->{steps} } ) { | ||
| 45 | if ( $step->{status} eq 'not_ok' ) { | ||
| 46 | my $message = 'Test: ' . $test->{file} . "\n"; | ||
| 47 | $message .= $step->{message} . "\n"; | ||
| 48 | $message .= $step->{comment} if $step->{comment}; | ||
| 49 | push(@errors, $message); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | } | ||
| 53 | if($errors[0]){ | ||
| 54 | my $msg = join("\n\n--~==##\n\n", @errors); | ||
| 55 | $self->oncall_send({message => $msg}); | ||
| 56 | } | ||
| 57 | }; | ||
| 58 | |||
| 59 | sub oncall_send { | ||
| 60 | my ($self, $message) = @_; | ||
| 61 | |||
| 62 | $message->{host} = $self->oncall_host unless exists $message->{host}; | ||
| 63 | my $ua = LWP::UserAgent->new; | ||
| 64 | my $res = $ua->post( | ||
| 65 | $self->oncall_url . $self->oncall_token, | ||
| 66 | Content_Type => 'form-data', | ||
| 67 | Content => { payload => to_json($message) }); | ||
| 68 | print Dumper($message) | ||
| 69 | if $self->debug; | ||
| 70 | return $res->is_success; | ||
| 71 | } | ||
| 72 | |||
| 73 | 1; | ||
| 74 | |||
| 75 | =pod | ||
| 76 | |||
| 77 | =encoding UTF-8 | ||
| 78 | |||
| 79 | =head1 NAME | ||
| 80 | |||
| 81 | TheEye::Plugin::Notify::Oncall - Plugin for TheEye to raise alerts in OnCall | ||
| 82 | |||
| 83 | =head1 VERSION | ||
| 84 | |||
| 85 | version 0.5 | ||
| 86 | |||
| 87 | =head1 AUTHOR | ||
| 88 | |||
| 89 | Lenz Gschwendtner <lenz@springtimesoft.com> | ||
| 90 | |||
| 91 | =head1 COPYRIGHT AND LICENSE | ||
| 92 | |||
| 93 | This software is copyright (c) 2014 by springtimesoft LTD. | ||
| 94 | |||
| 95 | This is free software; you can redistribute it and/or modify it under | ||
| 96 | the same terms as the Perl 5 programming language system itself. | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | __DATA__ |