blib/lib/Jenkins/API.pm

filter: Statementstmt 72.4% 84 / 116 Branchbran 50.0% 9 / 18 Conditioncond 50.0% 6 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 75.0% 18 / 24 Podpod 100% 15 / 15 totaltotal 69.2% 133 / 192 CC 15 SCAR 11.7
CC
15
Cov
68%
general_call
7.5
_trigger_build
6.0
_json_api
4.0
CRAP 3.2
↑ Summary
1 package Jenkins::API;
2  
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use REST::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: A wrapper around the Jenkins API
11  
12 1 our $VERSION = '0.16';
13  
14 1 has base_url => (is => 'ro', isa => Str, required => 1);
15 1 has api_key => (is => 'ro', isa => Maybe[Str], required => 0);
16 1 has api_pass => (is => 'ro', isa => Maybe[Str], required => 0);
17  
18 has '_client' => (
19 is => 'ro',
20 lazy => 1,
21 default => sub {
22 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=12.3
23 1 my $client = REST::Client->new();
24 1 $client->setHost($self->base_url);
25 1 ▶ if (defined($self->api_key) and defined($self->api_pass)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->api_key and defined $self->api_pass) 0 1 1
Condition33%1/3
defined $self->api_key and defined $self->api_pass
!ll&&!rl&&r
100
MC/DC0%0/2
defined $self->api_key and defined $self->api_pass
defined $self->api_key defined $self->api_pass
Truth table33%1/3
A and B
A= defined $self->api_keyB= defined $self->api_pass
ABresult
0X0
100
111
26 0 $client->addHeader("Authorization", "Basic " .
27 encode_base64($self->api_key . ':' . $self->api_pass));
28 }
29 1 return $client;
30 }
31 1 );
32  
33  
34 sub create_job
35 {
36 0 ▶ my ($self, $name, $job_config) = @_;
Subroutine create_job: not called CC=1 SCAR=6.9
Pod documented
37  
38 0 my $uri = URI->new($self->base_url);
39 0 $uri->path_segments('createItem');
40 0 $uri->query_form( name => $name );
41 # curl -XPOST http://moe:8080/createItem?name=test -d@config.xml -v -H Content-Type:text/xml
42 0 $self->_client->POST($uri->path_query, $job_config, { 'Content-Type' => 'text/xml' });
43 0 return $self->_client->responseCode() eq '200';
44 }
45  
46 sub delete_project
47 {
48 0 ▶ my ($self, $name) = @_;
Subroutine delete_project: not called CC=1 SCAR=6.9
Pod documented
49  
50 0 my $uri = URI->new($self->base_url);
51 0 $uri->path_segments('job', $name, 'doDelete');
52 0 $self->_client->POST($uri->path_query, undef, { 'Content-Type' => 'text/xml' });
53 0 return $self->_client->responseCode() eq '302';
54 }
55  
56 sub trigger_build
57 {
58 0 ▶ my $self = shift;
Subroutine trigger_build: not called CC=1 SCAR=6.9
Pod documented
59 0 return $self->_trigger_build('build', @_);
60 }
61  
62 sub trigger_build_with_parameters
63 {
64 0 ▶ my $self = shift;
Subroutine trigger_build_with_parameters: not called CC=1 SCAR=6.9
Pod documented
65 0 return $self->_trigger_build('buildWithParameters', @_);
66 }
67  
68 sub _trigger_build
69 {
70 0 ▶ my $self = shift;
Subroutine _trigger_build: not called CC=2 SCAR=17.9
71 0 my $build_url = shift;
72 0 my $job = shift;
73 0 my $extra_params = shift;
74  
75 0 my $uri = URI->new($self->base_url);
76 0 $uri->path_segments('job', $job, $build_url);
77 0 ▶ $uri->query_form($extra_params) if $extra_params;
Branch: 1 branch
BranchTrueFalseTotal
if $extra_params 0 0 0
78 0 $self->_client->POST($uri->path_query);
79 0 return $self->_client->responseCode eq '201';
80 }
81  
82 sub project_config
83 {
84 1 ▶ my $self = shift;
Subroutine project_config: called CC=2 SCAR=7.0
Pod documented
85 1 my $job = shift;
86 1 my $extra_params = shift;
87  
88 1 my $uri = URI->new($self->base_url);
89 1 $uri->path_segments('job', $job, 'config.xml');
90 1 ▶ $uri->query_form($extra_params) if $extra_params;
Branch: 1 branch
BranchTrueFalseTotal
if $extra_params 0 1 1
91 1 $self->_client->GET($uri->path_query);
92 1 return $self->_client->responseContent;
93 }
94  
95 sub set_project_config
96 {
97 0 ▶ my $self = shift;
Subroutine set_project_config: not called CC=1 SCAR=6.9
Pod documented
98 0 my $job = shift;
99 0 my $config = shift;
100  
101 0 my $uri = URI->new($self->base_url);
102 0 $uri->path_segments('job', $job, 'config.xml');
103 0 $self->_client->POST($uri->path_query, $config, { 'Content-Type' => 'text/xml' });
104 0 return $self->_client->responseCode() eq '200';
105 }
106  
107 sub check_jenkins_url
108 {
109 1 ▶ my $self = shift;
Subroutine check_jenkins_url: called CC=2 SCAR=7.6
Pod documented
110 1 $self->_client->GET('/');
111 1 ▶ return $self->_client->responseCode() eq '200'
Condition33%1/3
$self->_client->responseCode eq '200' && $self->_client->responseHeader('X-Jenkins')
!ll&&!rl&&r
001
MC/DC0%0/2
$self->_client->responseCode eq '200' && $self->_client->responseHeader('X-Jenkins')
$self->_client->responseCode eq '200' $self->_client->responseHeader('X-Jenkins')
Truth table33%1/3
A && B
A= $self->_client->responseCode eq '200'B= $self->_client->responseHeader('X-Jenkins')
ABresult
0X0
100
111
112 && $self->_client->responseHeader('X-Jenkins');
113 }
114  
115 sub build_queue
116 {
117 2 ▶ my $self = shift;
Subroutine build_queue: called CC=1 SCAR=0.0
Pod documented
118 2 return $self->_json_api(['queue', 'api','json'], @_);
119 }
120  
121 sub load_statistics
122 {
123 1 ▶ my $self = shift;
Subroutine load_statistics: called CC=1 SCAR=0.0
Pod documented
124 1 return $self->_json_api(['overallLoad', 'api','json'], @_);
125 }
126  
127 sub get_job_details
128 {
129 2 ▶ my $self = shift;
Subroutine get_job_details: called CC=1 SCAR=0.0
Pod documented
130 2 my $job_name = shift;
131 2 return $self->_json_api(['job', $job_name, 'api', 'json'], @_);
132 }
133  
134 sub current_status
135 {
136 4 ▶ my $self = shift;
Subroutine current_status: called CC=1 SCAR=0.0
Pod documented
137 4 return $self->_json_api(['api','json'], @_);
138 }
139  
140 sub view_status
141 {
142 3 ▶ my $self = shift;
Subroutine view_status: called CC=1 SCAR=0.0
Pod documented
143 3 my $view = shift;
144 3 return $self->_json_api(['view', $view, 'api', 'json'], @_);
145 }
146  
147 sub _json_api
148 {
149 12 ▶ my $self = shift;
Subroutine _json_api: called CC=4 SCAR=13.9
150 12 my $uri_parts = shift;
151 12 my $args = shift;
152 12 my $extra_params = $args->{extra_params};
153 12 ▶ my $bits = $args->{path_parts} || [];
Condition100%2/2
$args->{'path_parts'} || []
l!l
111
MC/DC100%1/1
$args->{'path_parts'} || []
$args->{'path_parts'}
Truth table100%2/2
A
A= $args->{'path_parts'}
Aresult
11
00
154  
155 12 my $uri = URI->new($self->base_url);
156 12 $uri->path_segments(@$bits, @$uri_parts);
157 12 ▶ $uri->query_form($extra_params) if $extra_params;
Branch: 1 branch
BranchTrueFalseTotal
if $extra_params 7 5 12
158  
159 12 $self->_client->GET($uri->path_query);
160 12 ▶ die 'Invalid response' unless $self->_client->responseCode eq '200';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_client->responseCode eq 200 0 12 12
161 # NOTE: my server returns UTF8, if this turns out to be a broken
162 # assumption read the Content-Type header.
163 12 my $data = JSON->new->utf8->decode($self->_client->responseContent());
164 12 return $data;
165 }
166  
167 sub general_call
168 {
169 1 ▶ my $self = shift;
Subroutine general_call: called CC=7 SCAR=20.1
Pod documented
170 1 my $uri_parts = shift;
171 1 my $args = shift;
172  
173 1 my $extra_params = $args->{extra_params};
174 1 ▶ my $method = $args->{method} || 'GET';
Condition50%1/2
$args->{'method'} || 'GET'
l!l
10
MC/DC0%0/1
$args->{'method'} || 'GET'
$args->{'method'}
Truth table50%1/2
A
A= $args->{'method'}
Aresult
11
00
175 1 ▶ my $decode_json = exists $args->{decode_json} ? $args->{decode_json} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'decode_json'} ? : 1 0 1
176 1 ▶ my $expected_response = $args->{expected_response_code} || 200;
Condition50%1/2
$args->{'expected_response_code'} || 200
l!l
10
MC/DC0%0/1
$args->{'expected_response_code'} || 200
$args->{'expected_response_code'}
Truth table50%1/2
A
A= $args->{'expected_response_code'}
Aresult
11
00
177  
178 1 my $uri = URI->new($self->base_url);
179 1 $uri->path_segments(@$uri_parts);
180 1 ▶ $uri->query_form($extra_params) if $extra_params;
Branch: 1 branch
BranchTrueFalseTotal
if $extra_params 1 0 1
181  
182 1 $self->_client->$method($uri->path_query);
183 1 ▶ die 'Invalid response' unless $self->_client->responseCode eq $expected_response;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_client->responseCode eq $expected_response 0 1 1
184 1 my $response = $self->_client->responseContent();
185 1 ▶ if($decode_json)
Branch: 1 branch
BranchTrueFalseTotal
if ($decode_json) 1 0 1
186 {
187 1 $response = JSON->new->utf8->decode($response);
188 }
189 1 return $response;
190 }
191  
192  
193 sub response_code
194 {
195 1 ▶ my $self = shift;
Subroutine response_code: called CC=1 SCAR=0.0
Pod documented
196 1 return $self->_client->responseCode;
197 }
198  
199  
200 sub response_content
201 {
202 1 ▶ my $self = shift;
Subroutine response_content: called CC=1 SCAR=0.0
Pod documented
203 1 return $self->_client->responseContent;
204 }
205  
206  
207  
208 1 1; # End of Jenkins::API
209  
210 __END__
↑ Summary