File Coverage

blib/lib/Jenkins/NotificationListener.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Jenkins::NotificationListener;
2 2     2   811 use strict;
  2         2  
  2         114  
3 2     2   11 use warnings;
  2         4  
  2         82  
4             our $VERSION = '0.06';
5              
6 2     2   2389 use Net::Jenkins;
  0            
  0            
7             use Net::Jenkins::Job;
8             use Net::Jenkins::Job::Build;
9             use Jenkins::Notification;
10             use AnyEvent::Socket;
11             use Moose;
12             use methods;
13             use JSON::XS;
14              
15             our @ISA = ( 'Moose::Object', 'Exporter' );
16             our @EXPORT = qw(parse_jenkins_notification);
17              
18             has host => (is => 'rw');
19              
20             has port => (is => 'rw', isa => 'Int');
21              
22             has on_notify => (is => 'rw');
23              
24             has on_error => (is => 'rw', default => sub {
25             return sub { warn @_; };
26             });
27              
28             sub parse_jenkins_notification {
29             my $json = shift;
30             my $args = decode_json $json;
31             return Jenkins::Notification->new( %$args , raw_json => $json );
32             }
33              
34             method start ($prepare_cb) {
35             return tcp_server $self->host, $self->port , sub {
36             my ($fh, $host, $port) = @_;
37             my $json = '';
38             my $buf = '';
39             while( my $bytes = sysread $fh, $buf, 1024 ) {
40             $json .= $buf;
41             }
42             eval {
43             if( $json ) {
44             $self->on_notify->(
45             parse_jenkins_notification($json)
46             );
47             } else {
48             die 'Request body is empty.';
49             }
50             };
51             if ( $@ ) {
52             $self->on_error->( $@ );
53             }
54             }, $prepare_cb;
55             }
56              
57             1;
58             __END__
59              
60             =head1 NAME
61              
62             Jenkins::NotificationListener - is a TCP server that listens to messages from Jenkins Notification plugin.
63              
64             =head1 SYNOPSIS
65              
66             use Jenkins::NotificationListener;
67             Jenkins::NotificationListener->new( host => $host , port => $port , on_notify => sub {
68             my $payload = shift; # Jenkins::Notification;
69             print $payload->name , " #" , $payload->build->number, " : " , $payload->status
70             , " : " , $payload->phase
71             , " : " , $payload->url
72             , "\n";
73              
74             })->start;
75              
76             =head1 DESCRIPTION
77              
78             Jenkins::NotificationListener is a simple TCP server listens to messages from Jenkins' Notification plugin,
79              
80             L<Jenkins::NotificationListener> uses L<AnyEvent::Socket> to create tcp server object, so it's a non-blocking implementation.
81              
82             This tcp server reads JSON format notification from Jenkins Notification plugin, and creates payload object L<Jenkins::Notification>.
83             the payload object is built with L<Net::Jenkins::Job>,
84             L<Net::Jenkins::Job::Build> objects from the information that is provided from
85             notification json.
86              
87             By using L<Jenkins::NotificationListener>, you can simple use the payload object to interact with Jenkins server.
88              
89             To test your Jenkins notification plugin, you can also use L<jenkins-notification-listener.pl> script.
90              
91             $ jenkins-notification-listener.pl
92              
93             =head1 EXPORTED FUNCTION
94              
95             use Jenkins::NotificationListener; # export parse_jenkins_notification function
96             my $notification = parse_jenkins_notification($json);
97             $notification; # Jenkins::Notification object
98             $notification->job;
99             $notification->build;
100              
101             =head1 INSTALLATION
102              
103             $ cpan Jenkins::Notification
104              
105             $ cpanm Jenkins::Notification
106              
107             =head1 AUTHOR
108              
109             Yo-An Lin E<lt>cornelius.howl {at} gmail.comE<gt>
110              
111             =head1 SEE ALSO
112              
113             L<Net::Jenkins>
114              
115             =head1 LICENSE
116              
117             This library is free software; you can redistribute it and/or modify
118             it under the same terms as Perl itself.
119              
120             =cut