File Coverage

blib/lib/ShipIt/Step/JenkinsCheck.pm
Criterion Covered Total %
statement 21 62 33.8
branch 0 20 0.0
condition n/a
subroutine 7 12 58.3
pod 2 3 66.6
total 30 97 30.9


line stmt bran cond sub pod time code
1             package ShipIt::Step::JenkinsCheck;
2              
3 1     1   34230 use strict;
  1         3  
  1         61  
4 1     1   6 use warnings;
  1         2  
  1         39  
5              
6 1     1   6 use base qw/ ShipIt::Step /;
  1         7  
  1         4083  
7 1     1   1892 use JSON qw/ decode_json /;
  1         18904  
  1         6  
8 1     1   1294 use LWP::UserAgent;
  1         51624  
  1         45  
9 1     1   25644 use Try::Tiny;
  1         1832  
  1         67  
10 1     1   998 use ShipIt::Util qw/ $term /;
  1         83479  
  1         1803  
11              
12             our $VERSION = '0.01';
13              
14             sub init {
15 0     0 1   my ($self, $conf) = @_;
16 0           $self->{url} = $conf->value('JenkinsCheck.url');
17 0           $self->{jobs} = [ split /,/, $conf->value('JenkinsCheck.jobs') ];
18              
19 0 0         die "No Jenkins URL specified" unless $self->{url};
20 0 0         die "No Jenkins jobs specified" unless @{ $self->{jobs} };
  0            
21 0           return;
22             }
23              
24             # Is really a class method.
25             sub check_tests {
26 0     0 0   my ($self, $jenkins, @jobs) = @_;
27              
28 0           my $ua = LWP::UserAgent->new();
29 0           my $response = $ua->get("$jenkins/api/json");
30 0 0         die $response->status_line unless $response->is_success;
31              
32 0           my $obj = decode_json($response->decoded_content);
33              
34 0           my %jobs = map { $_->{name} => $_->{color} } @{ $obj->{jobs} };
  0            
  0            
35              
36 0           my @errors;
37              
38 0           foreach my $j (@jobs) {
39 0 0         if (!defined($jobs{$j})) {
    0          
40 0           push @errors, "$j is not being tested by your Jenkins at $jenkins";
41             }
42             elsif ($jobs{$j} !~ /^blue|blue_anime$/) {
43 0           push @errors, "$j has status ".$jobs{$j};
44             }
45             }
46              
47 0           return @errors;
48             }
49              
50             sub run {
51 0     0 1   my ($self, $state) = @_;
52 0           my @results;
53              
54             try {
55 0     0     @results = $self->check_tests($self->{url}, @{ $self->{jobs} });
  0            
56             }
57             catch {
58 0     0     my $err = $_;
59 0           while (1) {
60 0           my $line = $term->readline("Jenkins check failed with $err, continue build? (y/n)");
61 0 0         die "build aborted" if $line =~ /^n/i;
62 0 0         last if $line =~ /^y/i;
63             }
64 0           };
65              
66 0 0         unless (@results) {
67 0           print "Jenkins reports all your tests to be passing.\n";
68 0           return;
69             }
70              
71 0           foreach my $r (@results) {
72 0           print "$r\n";
73             }
74              
75 0           while (1) {
76 0           my $line = $term->readline("Jenkins reports trouble, continue build? (y/n)");
77 0 0         die "build aborted" if $line =~ /^n/i;
78 0 0         last if $line =~ /^y/i;
79             }
80 0           return;
81             }
82              
83              
84             1;
85             __END__