| 1 | package Net::Jenkins; | ||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | our $VERSION = '0.07'; | |||||||||||
| 5 | 2 | ▶ | use Net::Jenkins::Job; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | ▶ | use Net::Jenkins::Job::Build; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use Net::HTTP; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use LWP::UserAgent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 2 | ▶ | use methods; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 2 | ▶ | use URI; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 2 | ▶ | use JSON; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 2 | has scheme => ( is => 'rw', isa => 'Str', default => 'http' ); | |||||||||||
| 15 | |||||||||||||
| 16 | has host => ( | ||||||||||||
| 17 | is => 'rw', | ||||||||||||
| 18 | isa => 'Str' , | ||||||||||||
| 19 | 0 | ▶ | default => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 20 | 0 | ▶ | return $ENV{JENKINS_HOST} ? $ENV{JENKINS_HOST} : 'localhost'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 21 | 2 | }); | |||||||||||
| 22 | |||||||||||||
| 23 | has port => ( is => 'rw', isa => 'Int' , | ||||||||||||
| 24 | 0 | ▶ | default => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 25 | 0 | ▶ | return $ENV{JENKINS_PORT} ? $ENV{JENKINS_PORT} : 8080; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 26 | 2 | }); | |||||||||||
| 27 | |||||||||||||
| 28 | 4 | ▶ | has user_agent => ( is => 'rw' , default => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 29 | 4 | return LWP::UserAgent->new; | |||||||||||
| 30 | 2 | }); | |||||||||||
| 31 | |||||||||||||
| 32 | 2 | ▶ | method get_base_url { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
get_base_url: called CC=1 SCAR=0.0
| |||||||||||||
| 33 | 1 | return $self->scheme | |||||||||||
| 34 | . '://' . $self->host | ||||||||||||
| 35 | . ':' . $self->port; | ||||||||||||
| 36 | 2 | } | |||||||||||
| 37 | |||||||||||||
| 38 | |||||||||||||
| 39 | 0 | ▶ | method post_url ($uri,%args) { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=4.9
Subroutine
post_url: not called CC=1 SCAR=5.3
Subroutine
__ANON__: not called CC=1 SCAR=5.3
| |||||||||||||
| 40 | 0 | return $self->user_agent->post( $uri , \%args ); | |||||||||||
| 41 | 2 | } | |||||||||||
| 42 | |||||||||||||
| 43 | 0 | ▶ | method get_url ($uri) { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=4.9
Subroutine
get_url: not called CC=1 SCAR=5.3
Subroutine
__ANON__: not called CC=1 SCAR=5.3
| |||||||||||||
| 44 | 0 | return $self->user_agent->get($uri); | |||||||||||
| 45 | 2 | } | |||||||||||
| 46 | |||||||||||||
| 47 | 2 | ▶ | method get_json ( $uri ) { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
get_json: called CC=2 SCAR=7.0
| |||||||||||||
| 48 | 1 | my $response = $self->user_agent->get($uri); | |||||||||||
| 49 | 1 | ▶ | return decode_json $response->decoded_content if $response->is_success; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 50 | 2 | } | |||||||||||
| 51 | |||||||||||||
| 52 | 0 | ▶ | method summary { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
Subroutine
summary: not called CC=1 SCAR=4.1
| |||||||||||||
| 53 | 0 | my $uri = $self->get_base_url . '/api/json'; | |||||||||||
| 54 | 0 | return $self->get_json( $uri ); | |||||||||||
| 55 | 2 | } | |||||||||||
| 56 | |||||||||||||
| 57 | 0 | ▶ | method mode { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
Subroutine
mode: not called CC=1 SCAR=3.5
| |||||||||||||
| 58 | 0 | return $self->summary->{mode}; | |||||||||||
| 59 | 2 | } | |||||||||||
| 60 | |||||||||||||
| 61 | 0 | ▶ | method jobs { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
Subroutine
jobs: not called CC=1 SCAR=4.6
| |||||||||||||
| 62 | 0 | return map { Net::Jenkins::Job->new( %$_ , api => $self ) } @{ $self->summary->{jobs} }; | |||||||||||
| 63 | 2 | } | |||||||||||
| 64 | |||||||||||||
| 65 | 0 | ▶ | method use_security { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
Subroutine
use_security: not called CC=1 SCAR=3.5
| |||||||||||||
| 66 | 0 | return $self->summary->{useSecurity}; | |||||||||||
| 67 | 2 | } | |||||||||||
| 68 | |||||||||||||
| 69 | 0 | ▶ | method use_crumbs { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
Subroutine
use_crumbs: not called CC=1 SCAR=3.5
| |||||||||||||
| 70 | 0 | return $self->summary->{useCrumbs}; | |||||||||||
| 71 | 2 | } | |||||||||||
| 72 | |||||||||||||
| 73 | 0 | ▶ | method views { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
Subroutine
views: not called CC=1 SCAR=4.1
| |||||||||||||
| 74 | 0 | return @{ $self->summary->{views} }; | |||||||||||
| 75 | 2 | } | |||||||||||
| 76 | |||||||||||||
| 77 | 0 | ▶ | method restart { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
Subroutine
restart: not called CC=1 SCAR=4.1
| |||||||||||||
| 78 | 0 | my $uri = $self->get_base_url . '/restart'; | |||||||||||
| 79 | 0 | return $self->get_url( $uri )->is_success; | |||||||||||
| 80 | 2 | } | |||||||||||
| 81 | |||||||||||||
| 82 | |||||||||||||
| 83 | 0 | ▶ | method create_job ($job_name,$xml) { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=3.5
Subroutine
create_job: not called CC=1 SCAR=5.1
| |||||||||||||
| 84 | 0 | my $uri = URI->new( $self->get_base_url . '/createItem' ); | |||||||||||
| 85 | 0 | $uri->query_form( name => $job_name ); | |||||||||||
| 86 | 0 | my $response = $self->user_agent->post( $uri , "Content-Type" => "application/xml" , Content => $xml ); | |||||||||||
| 87 | 0 | return $response->code == 200; | |||||||||||
| 88 | 2 | } | |||||||||||
| 89 | |||||||||||||
| 90 | 0 | ▶ | method update_job ($job_name,$xml) { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=3.5
Subroutine
update_job: not called CC=1 SCAR=4.9
| |||||||||||||
| 91 | 0 | my $uri = URI->new( $self->job_url($job_name) . '/config.xml' ); | |||||||||||
| 92 | 0 | my $response = $self->user_agent->post( $uri , "Content-Type" => "application/xml" , Content => $xml ); | |||||||||||
| 93 | 0 | return $response->code == 200; | |||||||||||
| 94 | 2 | } | |||||||||||
| 95 | |||||||||||||
| 96 | 0 | ▶ | method copy_job ($job_name, $from_job_name) { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=3.5
Subroutine
copy_job: not called CC=2 SCAR=15.9
| |||||||||||||
| 97 | 0 | my $uri = URI->new( $self->get_base_url . '/createItem' ); | |||||||||||
| 98 | |||||||||||||
| 99 | # name=NEWJOBNAME&mode=copy&from=FROMJOBNAME | ||||||||||||
| 100 | 0 | $uri->query_form( name => $job_name , from => $from_job_name , mode => 'copy' ); | |||||||||||
| 101 | 0 | my $response = $self->user_agent->post( $uri ); | |||||||||||
| 102 | 0 | ▶ | return $response->code == 302 ? 1 : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 103 | 2 | } | |||||||||||
| 104 | |||||||||||||
| 105 | 0 | ▶ | method delete_job ($job_name) { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=3.5
Subroutine
delete_job: not called CC=2 SCAR=15.4
| |||||||||||||
| 106 | 0 | my $uri = $self->job_url($job_name) . '/doDelete'; | |||||||||||
| 107 | 0 | ▶ | return $self->post_url( $uri )->code == 302 ? 1 : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 108 | 2 | } | |||||||||||
| 109 | |||||||||||||
| 110 | 0 | ▶ | method disable_job ($job_name) { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=3.5
Subroutine
__ANON__: not called CC=2 SCAR=15.4
| |||||||||||||
| 111 | 0 | my $uri = $self->job_url($job_name) . '/disable'; | |||||||||||
| 112 | 0 | ▶ | return $self->post_url( $uri )->code == 302 ? 1 : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | 2 | } | |||||||||||
| 114 | |||||||||||||
| 115 | 0 | ▶ | method enable_job ($job_name) { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=3.5
Subroutine
enable_job: not called CC=2 SCAR=15.4
| |||||||||||||
| 116 | 0 | my $uri = $self->job_url($job_name) . '/enable'; | |||||||||||
| 117 | 0 | ▶ | return $self->post_url( $uri )->code == 302 ? 1 : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | 2 | } | |||||||||||
| 119 | |||||||||||||
| 120 | 0 | ▶ | method build_job ($job_name) { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=3.5
Subroutine
build_job: not called CC=2 SCAR=15.4
| |||||||||||||
| 121 | 0 | my $uri = $self->job_url($job_name) . '/build'; | |||||||||||
| 122 | 0 | ▶ | return $self->post_url( $uri )->code == 302 ? 1 : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 123 | 2 | } | |||||||||||
| 124 | |||||||||||||
| 125 | |||||||||||||
| 126 | 0 | ▶ | method get_job_details ($job_name) { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=3.5
Subroutine
get_job_details: not called CC=1 SCAR=4.1
| |||||||||||||
| 127 | 0 | return $self->get_json( | |||||||||||
| 128 | $self->job_url($job_name) . '/api/json' | ||||||||||||
| 129 | ); | ||||||||||||
| 130 | 2 | } | |||||||||||
| 131 | |||||||||||||
| 132 | |||||||||||||
| 133 | |||||||||||||
| 134 | |||||||||||||
| 135 | 0 | ▶ | method get_builds ($job_name) { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=3.5
Subroutine
__ANON__: not called CC=1 SCAR=4.9
| |||||||||||||
| 136 | 0 | my $config = $self->get_job_details($job_name); | |||||||||||
| 137 | 0 | return @{ $config->{builds} }; | |||||||||||
| 138 | 2 | } | |||||||||||
| 139 | |||||||||||||
| 140 | 2 | ▶ | method get_build_details ($job_name, $number) { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
get_build_details: called CC=1 SCAR=0.0
| |||||||||||||
| 141 | 1 | my $uri = $self->job_build_url($job_name,$number) . '/api/json'; | |||||||||||
| 142 | 1 | return $self->get_json($uri); | |||||||||||
| 143 | 2 | } | |||||||||||
| 144 | |||||||||||||
| 145 | |||||||||||||
| 146 | # Which returns a FH::Handle | ||||||||||||
| 147 | 0 | ▶ | method get_build_console_handle ($job_name, $number) { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=3.5
Subroutine
get_build_console_handle: not called CC=3 SCAR=23.1
| |||||||||||||
| 148 | 0 | my $uri = URI->new( $self->job_build_url($job_name,$number) . '/consoleText' ); | |||||||||||
| 149 | |||||||||||||
| 150 | # http://localhost:8080/job/Phifty/1/consoleText | ||||||||||||
| 151 | |||||||||||||
| 152 | 0 | ▶ | my $s = Net::HTTP->new(Host => $self->host ) || die $@; | ||||||||||
|
Condition0%0/2
'Net::HTTP'->new('Host', $self->host) || die($@)
MC/DC0%0/1
'Net::HTTP'->new('Host', $self->host) || die($@)
'Net::HTTP'->new('Host', $self->host)
Truth table0%0/2
A
A= 'Net::HTTP'->new('Host', $self->host)
| |||||||||||||
| 153 | 0 | $s->write_request(GET => $uri->path , 'User-Agent' => "Perl/Net::Jenkins"); | |||||||||||
| 154 | 0 | my($code, $mess, %h) = $s->read_response_headers; | |||||||||||
| 155 | |||||||||||||
| 156 | 0 | ▶ | return $s if $code == 200; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 157 | 2 | } | |||||||||||
| 158 | |||||||||||||
| 159 | 0 | ▶ | method get_build_console ($job_name, $number) { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=3.5
Subroutine
__ANON__: not called CC=4 SCAR=28.7
| |||||||||||||
| 160 | 0 | my $s = $self->get_build_console_handle( $job_name, $number ); | |||||||||||
| 161 | 0 | ▶ | return unless $s; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 162 | |||||||||||||
| 163 | 0 | my $body = '';; | |||||||||||
| 164 | 0 | while (1) { | |||||||||||
| 165 | 0 | my $buf; | |||||||||||
| 166 | 0 | my $n = $s->read_entity_body($buf, 1024); | |||||||||||
| 167 | 0 | ▶ | die "build console of $job_name #$number read failed: $!" unless defined $n; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 168 | 0 | ▶ | last unless $n; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 169 | 0 | $body .= $buf; | |||||||||||
| 170 | } | ||||||||||||
| 171 | 0 | return $body; | |||||||||||
| 172 | 2 | } | |||||||||||
| 173 | |||||||||||||
| 174 | # ================================ | ||||||||||||
| 175 | # URL methods | ||||||||||||
| 176 | # ================================ | ||||||||||||
| 177 | 2 | ▶ | method job_url ($job_name) { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 178 | 1 | return $self->get_base_url . '/job/' . $job_name; | |||||||||||
| 179 | 2 | } | |||||||||||
| 180 | |||||||||||||
| 181 | 2 | ▶ | method job_build_url ($job_name,$number) { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
job_build_url: called CC=1 SCAR=0.0
| |||||||||||||
| 182 | # http://localhost:8080/job/Phifty/2/api/json | ||||||||||||
| 183 | 1 | return $self->job_url($job_name) . '/' . $number; | |||||||||||
| 184 | 2 | } | |||||||||||
| 185 | |||||||||||||
| 186 | 2 | 1; | |||||||||||
| 187 | __END__ | ||||||||||||