line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Jenkins::API; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
151703
|
use Moo; |
|
1
|
|
|
|
|
7575
|
|
|
1
|
|
|
|
|
5
|
|
4
|
1
|
|
|
1
|
|
1317
|
use Types::Standard -types; |
|
1
|
|
|
|
|
55672
|
|
|
1
|
|
|
|
|
10
|
|
5
|
1
|
|
|
1
|
|
4362
|
use JSON; |
|
1
|
|
|
|
|
6972
|
|
|
1
|
|
|
|
|
6
|
|
6
|
1
|
|
|
1
|
|
369
|
use MIME::Base64; |
|
1
|
|
|
|
|
435
|
|
|
1
|
|
|
|
|
51
|
|
7
|
1
|
|
|
1
|
|
291
|
use URI; |
|
1
|
|
|
|
|
3353
|
|
|
1
|
|
|
|
|
27
|
|
8
|
1
|
|
|
1
|
|
274
|
use REST::Client; |
|
1
|
|
|
|
|
30182
|
|
|
1
|
|
|
|
|
920
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# ABSTRACT: A wrapper around the Jenkins API |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.17'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has base_url => (is => 'ro', isa => Str, required => 1); |
15
|
|
|
|
|
|
|
has api_key => (is => 'ro', isa => Maybe[Str], required => 0); |
16
|
|
|
|
|
|
|
has api_pass => (is => 'ro', isa => Maybe[Str], required => 0); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has '_client' => ( |
19
|
|
|
|
|
|
|
is => 'ro', |
20
|
|
|
|
|
|
|
lazy => 1, |
21
|
|
|
|
|
|
|
default => sub { |
22
|
|
|
|
|
|
|
my $self = shift; |
23
|
|
|
|
|
|
|
my $client = REST::Client->new(); |
24
|
|
|
|
|
|
|
$client->setHost($self->base_url); |
25
|
|
|
|
|
|
|
if (defined($self->api_key) and defined($self->api_pass)) { |
26
|
|
|
|
|
|
|
$client->addHeader("Authorization", "Basic " . |
27
|
|
|
|
|
|
|
encode_base64($self->api_key . ':' . $self->api_pass)); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
return $client; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub create_job |
35
|
|
|
|
|
|
|
{ |
36
|
0
|
|
|
0
|
1
|
0
|
my ($self, $name, $job_config) = @_; |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
0
|
my $uri = URI->new($self->base_url); |
39
|
0
|
|
|
|
|
0
|
$uri->path_segments('createItem'); |
40
|
0
|
|
|
|
|
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
|
|
|
|
|
0
|
$self->_client->POST($uri->path_query, $job_config, { 'Content-Type' => 'text/xml' }); |
43
|
0
|
|
|
|
|
0
|
return $self->_client->responseCode() eq '200'; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub delete_project |
47
|
|
|
|
|
|
|
{ |
48
|
0
|
|
|
0
|
1
|
0
|
my ($self, $name) = @_; |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
0
|
my $uri = URI->new($self->base_url); |
51
|
0
|
|
|
|
|
0
|
$uri->path_segments('job', $name, 'doDelete'); |
52
|
0
|
|
|
|
|
0
|
$self->_client->POST($uri->path_query, undef, { 'Content-Type' => 'text/xml' }); |
53
|
0
|
|
|
|
|
0
|
return $self->_client->responseCode() eq '302'; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub trigger_build |
57
|
|
|
|
|
|
|
{ |
58
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
59
|
0
|
|
|
|
|
0
|
return $self->_trigger_build('build', @_); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub trigger_build_with_parameters |
63
|
|
|
|
|
|
|
{ |
64
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
65
|
0
|
|
|
|
|
0
|
return $self->_trigger_build('buildWithParameters', @_); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub _trigger_build |
69
|
|
|
|
|
|
|
{ |
70
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
71
|
0
|
|
|
|
|
0
|
my $build_url = shift; |
72
|
0
|
|
|
|
|
0
|
my $job = shift; |
73
|
0
|
|
|
|
|
0
|
my $extra_params = shift; |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
0
|
my $uri = URI->new($self->base_url); |
76
|
0
|
|
|
|
|
0
|
$uri->path_segments('job', $job, $build_url); |
77
|
0
|
0
|
|
|
|
0
|
$uri->query_form($extra_params) if $extra_params; |
78
|
0
|
|
|
|
|
0
|
$self->_client->POST($uri->path_query); |
79
|
0
|
|
|
|
|
0
|
return $self->_client->responseCode eq '201'; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub project_config |
83
|
|
|
|
|
|
|
{ |
84
|
1
|
|
|
1
|
1
|
237
|
my $self = shift; |
85
|
1
|
|
|
|
|
3
|
my $job = shift; |
86
|
1
|
|
|
|
|
1
|
my $extra_params = shift; |
87
|
|
|
|
|
|
|
|
88
|
1
|
|
|
|
|
5
|
my $uri = URI->new($self->base_url); |
89
|
1
|
|
|
|
|
64
|
$uri->path_segments('job', $job, 'config.xml'); |
90
|
1
|
50
|
|
|
|
58
|
$uri->query_form($extra_params) if $extra_params; |
91
|
1
|
|
|
|
|
18
|
$self->_client->GET($uri->path_query); |
92
|
1
|
|
|
|
|
312
|
return $self->_client->responseContent; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub set_project_config |
96
|
|
|
|
|
|
|
{ |
97
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
98
|
0
|
|
|
|
|
0
|
my $job = shift; |
99
|
0
|
|
|
|
|
0
|
my $config = shift; |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
0
|
my $uri = URI->new($self->base_url); |
102
|
0
|
|
|
|
|
0
|
$uri->path_segments('job', $job, 'config.xml'); |
103
|
0
|
|
|
|
|
0
|
$self->_client->POST($uri->path_query, $config, { 'Content-Type' => 'text/xml' }); |
104
|
0
|
|
|
|
|
0
|
return $self->_client->responseCode() eq '200'; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub check_jenkins_url |
108
|
|
|
|
|
|
|
{ |
109
|
1
|
|
|
1
|
1
|
3013
|
my $self = shift; |
110
|
1
|
|
|
|
|
24
|
$self->_client->GET('/'); |
111
|
1
|
|
33
|
|
|
1291
|
return $self->_client->responseCode() eq '200' |
112
|
|
|
|
|
|
|
&& $self->_client->responseHeader('X-Jenkins'); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub build_queue |
116
|
|
|
|
|
|
|
{ |
117
|
2
|
|
|
2
|
1
|
1494
|
my $self = shift; |
118
|
2
|
|
|
|
|
6
|
return $self->_json_api(['queue', 'api','json'], @_); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub load_statistics |
122
|
|
|
|
|
|
|
{ |
123
|
1
|
|
|
1
|
1
|
722
|
my $self = shift; |
124
|
1
|
|
|
|
|
6
|
return $self->_json_api(['overallLoad', 'api','json'], @_); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub get_job_details |
128
|
|
|
|
|
|
|
{ |
129
|
2
|
|
|
2
|
1
|
1189
|
my $self = shift; |
130
|
2
|
|
|
|
|
3
|
my $job_name = shift; |
131
|
2
|
|
|
|
|
8
|
return $self->_json_api(['job', $job_name, 'api', 'json'], @_); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub current_status |
135
|
|
|
|
|
|
|
{ |
136
|
4
|
|
|
4
|
1
|
12890
|
my $self = shift; |
137
|
4
|
|
|
|
|
15
|
return $self->_json_api(['api','json'], @_); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub view_status |
141
|
|
|
|
|
|
|
{ |
142
|
3
|
|
|
3
|
1
|
1953
|
my $self = shift; |
143
|
3
|
|
|
|
|
6
|
my $view = shift; |
144
|
3
|
|
|
|
|
10
|
return $self->_json_api(['view', $view, 'api', 'json'], @_); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub _json_api |
148
|
|
|
|
|
|
|
{ |
149
|
12
|
|
|
12
|
|
17
|
my $self = shift; |
150
|
12
|
|
|
|
|
18
|
my $uri_parts = shift; |
151
|
12
|
|
|
|
|
13
|
my $args = shift; |
152
|
12
|
|
|
|
|
24
|
my $extra_params = $args->{extra_params}; |
153
|
12
|
|
100
|
|
|
45
|
my $bits = $args->{path_parts} || []; |
154
|
|
|
|
|
|
|
|
155
|
12
|
|
|
|
|
58
|
my $uri = URI->new($self->base_url); |
156
|
12
|
|
|
|
|
6141
|
$uri->path_segments(@$bits, @$uri_parts); |
157
|
12
|
100
|
|
|
|
807
|
$uri->query_form($extra_params) if $extra_params; |
158
|
|
|
|
|
|
|
|
159
|
12
|
|
|
|
|
814
|
$self->_client->GET($uri->path_query); |
160
|
12
|
50
|
|
|
|
4720
|
die 'Invalid response' unless $self->_client->responseCode eq '200'; |
161
|
|
|
|
|
|
|
# NOTE: my server returns UTF8, if this turns out to be a broken |
162
|
|
|
|
|
|
|
# assumption read the Content-Type header. |
163
|
12
|
|
|
|
|
503
|
my $data = JSON->new->utf8->decode($self->_client->responseContent()); |
164
|
12
|
|
|
|
|
436
|
return $data; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub general_call |
168
|
|
|
|
|
|
|
{ |
169
|
1
|
|
|
1
|
1
|
523
|
my $self = shift; |
170
|
1
|
|
|
|
|
3
|
my $uri_parts = shift; |
171
|
1
|
|
|
|
|
3
|
my $args = shift; |
172
|
|
|
|
|
|
|
|
173
|
1
|
|
|
|
|
4
|
my $extra_params = $args->{extra_params}; |
174
|
1
|
|
50
|
|
|
7
|
my $method = $args->{method} || 'GET'; |
175
|
1
|
50
|
|
|
|
6
|
my $decode_json = exists $args->{decode_json} ? $args->{decode_json} : 1; |
176
|
1
|
|
50
|
|
|
6
|
my $expected_response = $args->{expected_response_code} || 200; |
177
|
|
|
|
|
|
|
|
178
|
1
|
|
|
|
|
22
|
my $uri = URI->new($self->base_url); |
179
|
1
|
|
|
|
|
111
|
$uri->path_segments(@$uri_parts); |
180
|
1
|
50
|
|
|
|
100
|
$uri->query_form($extra_params) if $extra_params; |
181
|
|
|
|
|
|
|
|
182
|
1
|
|
|
|
|
96
|
$self->_client->$method($uri->path_query); |
183
|
1
|
50
|
|
|
|
414
|
die 'Invalid response' unless $self->_client->responseCode eq $expected_response; |
184
|
1
|
|
|
|
|
33
|
my $response = $self->_client->responseContent(); |
185
|
1
|
50
|
|
|
|
20
|
if($decode_json) |
186
|
|
|
|
|
|
|
{ |
187
|
1
|
|
|
|
|
13
|
$response = JSON->new->utf8->decode($response); |
188
|
|
|
|
|
|
|
} |
189
|
1
|
|
|
|
|
6
|
return $response; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
sub response_code |
194
|
|
|
|
|
|
|
{ |
195
|
1
|
|
|
1
|
1
|
7
|
my $self = shift; |
196
|
1
|
|
|
|
|
15
|
return $self->_client->responseCode; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
sub response_content |
201
|
|
|
|
|
|
|
{ |
202
|
1
|
|
|
1
|
1
|
473
|
my $self = shift; |
203
|
1
|
|
|
|
|
20
|
return $self->_client->responseContent; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
1; # End of Jenkins::API |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
__END__ |