line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Jenkins::API; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
220452
|
use Moo; |
|
1
|
|
|
|
|
9549
|
|
|
1
|
|
|
|
|
5
|
|
4
|
1
|
|
|
1
|
|
1306
|
use Types::Standard -types; |
|
1
|
|
|
|
|
64818
|
|
|
1
|
|
|
|
|
13
|
|
5
|
1
|
|
|
1
|
|
4566
|
use JSON; |
|
1
|
|
|
|
|
8746
|
|
|
1
|
|
|
|
|
7
|
|
6
|
1
|
|
|
1
|
|
407
|
use MIME::Base64; |
|
1
|
|
|
|
|
463
|
|
|
1
|
|
|
|
|
59
|
|
7
|
1
|
|
|
1
|
|
293
|
use URI; |
|
1
|
|
|
|
|
3392
|
|
|
1
|
|
|
|
|
32
|
|
8
|
1
|
|
|
1
|
|
303
|
use REST::Client; |
|
1
|
|
|
|
|
34599
|
|
|
1
|
|
|
|
|
1003
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# ABSTRACT: A wrapper around the Jenkins API |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.16'; |
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
|
191
|
my $self = shift; |
85
|
1
|
|
|
|
|
2
|
my $job = shift; |
86
|
1
|
|
|
|
|
2
|
my $extra_params = shift; |
87
|
|
|
|
|
|
|
|
88
|
1
|
|
|
|
|
6
|
my $uri = URI->new($self->base_url); |
89
|
1
|
|
|
|
|
67
|
$uri->path_segments('job', $job, 'config.xml'); |
90
|
1
|
50
|
|
|
|
59
|
$uri->query_form($extra_params) if $extra_params; |
91
|
1
|
|
|
|
|
19
|
$self->_client->GET($uri->path_query); |
92
|
1
|
|
|
|
|
316
|
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
|
3549
|
my $self = shift; |
110
|
1
|
|
|
|
|
33
|
$self->_client->GET('/'); |
111
|
1
|
|
33
|
|
|
1398
|
return $self->_client->responseCode() eq '200' |
112
|
|
|
|
|
|
|
&& $self->_client->responseHeader('X-Jenkins'); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub build_queue |
116
|
|
|
|
|
|
|
{ |
117
|
2
|
|
|
2
|
1
|
1659
|
my $self = shift; |
118
|
2
|
|
|
|
|
10
|
return $self->_json_api(['queue', 'api','json'], @_); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub load_statistics |
122
|
|
|
|
|
|
|
{ |
123
|
1
|
|
|
1
|
1
|
884
|
my $self = shift; |
124
|
1
|
|
|
|
|
5
|
return $self->_json_api(['overallLoad', 'api','json'], @_); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub get_job_details |
128
|
|
|
|
|
|
|
{ |
129
|
2
|
|
|
2
|
1
|
1351
|
my $self = shift; |
130
|
2
|
|
|
|
|
3
|
my $job_name = shift; |
131
|
2
|
|
|
|
|
9
|
return $self->_json_api(['job', $job_name, 'api', 'json'], @_); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub current_status |
135
|
|
|
|
|
|
|
{ |
136
|
4
|
|
|
4
|
1
|
14512
|
my $self = shift; |
137
|
4
|
|
|
|
|
21
|
return $self->_json_api(['api','json'], @_); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub view_status |
141
|
|
|
|
|
|
|
{ |
142
|
3
|
|
|
3
|
1
|
2154
|
my $self = shift; |
143
|
3
|
|
|
|
|
6
|
my $view = shift; |
144
|
3
|
|
|
|
|
11
|
return $self->_json_api(['view', $view, 'api', 'json'], @_); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub _json_api |
148
|
|
|
|
|
|
|
{ |
149
|
12
|
|
|
12
|
|
23
|
my $self = shift; |
150
|
12
|
|
|
|
|
19
|
my $uri_parts = shift; |
151
|
12
|
|
|
|
|
18
|
my $args = shift; |
152
|
12
|
|
|
|
|
25
|
my $extra_params = $args->{extra_params}; |
153
|
12
|
|
100
|
|
|
56
|
my $bits = $args->{path_parts} || []; |
154
|
|
|
|
|
|
|
|
155
|
12
|
|
|
|
|
68
|
my $uri = URI->new($self->base_url); |
156
|
12
|
|
|
|
|
6580
|
$uri->path_segments(@$bits, @$uri_parts); |
157
|
12
|
100
|
|
|
|
923
|
$uri->query_form($extra_params) if $extra_params; |
158
|
|
|
|
|
|
|
|
159
|
12
|
|
|
|
|
913
|
$self->_client->GET($uri->path_query); |
160
|
12
|
50
|
|
|
|
5305
|
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
|
|
|
|
|
495
|
my $data = JSON->new->utf8->decode($self->_client->responseContent()); |
164
|
12
|
|
|
|
|
444
|
return $data; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub general_call |
168
|
|
|
|
|
|
|
{ |
169
|
1
|
|
|
1
|
1
|
547
|
my $self = shift; |
170
|
1
|
|
|
|
|
4
|
my $uri_parts = shift; |
171
|
1
|
|
|
|
|
2
|
my $args = shift; |
172
|
|
|
|
|
|
|
|
173
|
1
|
|
|
|
|
3
|
my $extra_params = $args->{extra_params}; |
174
|
1
|
|
50
|
|
|
4
|
my $method = $args->{method} || 'GET'; |
175
|
1
|
50
|
|
|
|
5
|
my $decode_json = exists $args->{decode_json} ? $args->{decode_json} : 1; |
176
|
1
|
|
50
|
|
|
4
|
my $expected_response = $args->{expected_response_code} || 200; |
177
|
|
|
|
|
|
|
|
178
|
1
|
|
|
|
|
11
|
my $uri = URI->new($self->base_url); |
179
|
1
|
|
|
|
|
67
|
$uri->path_segments(@$uri_parts); |
180
|
1
|
50
|
|
|
|
60
|
$uri->query_form($extra_params) if $extra_params; |
181
|
|
|
|
|
|
|
|
182
|
1
|
|
|
|
|
93
|
$self->_client->$method($uri->path_query); |
183
|
1
|
50
|
|
|
|
637
|
die 'Invalid response' unless $self->_client->responseCode eq $expected_response; |
184
|
1
|
|
|
|
|
55
|
my $response = $self->_client->responseContent(); |
185
|
1
|
50
|
|
|
|
22
|
if($decode_json) |
186
|
|
|
|
|
|
|
{ |
187
|
1
|
|
|
|
|
14
|
$response = JSON->new->utf8->decode($response); |
188
|
|
|
|
|
|
|
} |
189
|
1
|
|
|
|
|
7
|
return $response; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
sub response_code |
194
|
|
|
|
|
|
|
{ |
195
|
1
|
|
|
1
|
1
|
6
|
my $self = shift; |
196
|
1
|
|
|
|
|
15
|
return $self->_client->responseCode; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
sub response_content |
201
|
|
|
|
|
|
|
{ |
202
|
1
|
|
|
1
|
1
|
493
|
my $self = shift; |
203
|
1
|
|
|
|
|
21
|
return $self->_client->responseContent; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
1; # End of Jenkins::API |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
__END__ |