File Coverage

blib/lib/Jenkins/API.pm
Criterion Covered Total %
statement 83 103 80.5
branch 9 16 56.2
condition 5 9 55.5
subroutine 18 22 81.8
pod 13 13 100.0
total 128 163 78.5


line stmt bran cond sub pod time code
1             package Jenkins::API;
2              
3 2     2   476968 use Moo;
  2         23837  
  2         12  
4 2     2   4301 use Types::Standard -types;
  2         153383  
  2         23  
5 2     2   11008 use JSON;
  2         21377  
  2         13  
6 2     2   1330 use MIME::Base64;
  2         1239  
  2         130  
7 2     2   1112 use URI;
  2         9093  
  2         98  
8 2     2   1093 use REST::Client;
  2         83380  
  2         91  
9 2     2   19 use HTTP::Status qw(HTTP_CREATED HTTP_FOUND HTTP_OK);
  2         4  
  2         2931  
10              
11             # ABSTRACT: A wrapper around the Jenkins API
12              
13             our $VERSION = '0.18';
14              
15             has base_url => (is => 'ro', isa => Str, required => 1);
16             has api_key => (is => 'ro', isa => Maybe[Str], required => 0);
17             has api_pass => (is => 'ro', isa => Maybe[Str], required => 0);
18              
19             has '_client' => (
20             is => 'ro',
21             lazy => 1,
22             default => sub {
23             my $self = shift;
24             my $client = REST::Client->new();
25             $client->setHost($self->base_url);
26             if (defined($self->api_key) and defined($self->api_pass)) {
27             $client->addHeader('Authorization', 'Basic ' .
28             encode_base64($self->api_key . ':' . $self->api_pass));
29             }
30             return $client;
31             },
32             handles => {
33             response_code => 'responseCode',
34             response_content => 'responseContent',
35             response_header => 'responseHeader'
36             }
37             );
38              
39              
40             sub create_job
41             {
42 0     0 1 0 my ($self, $name, $job_config) = @_;
43              
44 0         0 my $uri = URI->new($self->base_url);
45 0         0 $uri->path_segments('createItem');
46 0         0 $uri->query_form( name => $name );
47             # curl -XPOST http://moe:8080/createItem?name=test -d@config.xml -v -H Content-Type:text/xml
48 0         0 $self->_client->POST($uri->path_query, $job_config, { 'Content-Type' => 'text/xml' });
49 0         0 return $self->response_code == HTTP_OK;
50             }
51              
52             sub delete_project
53             {
54 0     0 1 0 my ($self, $name) = @_;
55              
56 0         0 my $uri = URI->new($self->base_url);
57 0         0 $uri->path_segments('job', $name, 'doDelete');
58 0         0 $self->_client->POST($uri->path_query, undef, { 'Content-Type' => 'text/xml' });
59 0         0 return $self->response_code == HTTP_FOUND;
60             }
61              
62             sub trigger_build
63             {
64 0     0 1 0 my $self = shift;
65 0         0 return $self->_trigger_build('build', @_);
66             }
67              
68             sub trigger_build_with_parameters
69             {
70 2     2 1 3710 my $self = shift;
71 2         10 return $self->_trigger_build('buildWithParameters', @_);
72             }
73              
74             sub _trigger_build
75             {
76 2     2   4 my $self = shift;
77 2         5 my $build_url = shift;
78 2         4 my $job = shift;
79 2         15 my $extra_params = shift;
80              
81 2         20 my $uri = URI->new($self->base_url);
82 2         7834 $uri->path_segments('job', $job, $build_url);
83 2 50       245 $uri->query_form($extra_params) if $extra_params;
84 2         239 $self->_client->POST($uri->path_query);
85 2         7730 return $self->response_code == HTTP_CREATED;
86             }
87              
88             sub project_config
89             {
90 1     1 1 1003 my $self = shift;
91 1         4 my $job = shift;
92 1         2 my $extra_params = shift;
93              
94 1         7 my $uri = URI->new($self->base_url);
95 1         81 $uri->path_segments('job', $job, 'config.xml');
96 1 50       71 $uri->query_form($extra_params) if $extra_params;
97 1         22 $self->_client->GET($uri->path_query);
98 1         388 return $self->response_content;
99             }
100              
101             sub set_project_config
102             {
103 0     0 1 0 my $self = shift;
104 0         0 my $job = shift;
105 0         0 my $config = shift;
106              
107 0         0 my $uri = URI->new($self->base_url);
108 0         0 $uri->path_segments('job', $job, 'config.xml');
109 0         0 $self->_client->POST($uri->path_query, $config, { 'Content-Type' => 'text/xml' });
110 0         0 return $self->response_code == HTTP_OK;
111             }
112              
113             sub check_jenkins_url
114             {
115 1     1 1 3592 my $self = shift;
116 1         23 $self->_client->GET('/');
117 1   33     1498 return $self->response_code == HTTP_OK
118             && $self->response_header('X-Jenkins');
119             }
120              
121             sub build_queue
122             {
123 2     2 1 2160 my $self = shift;
124 2         10 return $self->_json_api(['queue', 'api','json'], @_);
125             }
126              
127             sub load_statistics
128             {
129 1     1 1 1034 my $self = shift;
130 1         9 return $self->_json_api(['overallLoad', 'api','json'], @_);
131             }
132              
133             sub get_job_details
134             {
135 2     2 1 1757 my $self = shift;
136 2         7 my $job_name = shift;
137 2         8 return $self->_json_api(['job', $job_name, 'api', 'json'], @_);
138             }
139              
140             sub current_status
141             {
142 4     4 1 17808 my $self = shift;
143 4         19 return $self->_json_api(['api','json'], @_);
144             }
145              
146             sub view_status
147             {
148 3     3 1 2684 my $self = shift;
149 3         7 my $view = shift;
150 3         12 return $self->_json_api(['view', $view, 'api', 'json'], @_);
151             }
152              
153             sub _json_api
154             {
155 12     12   39 my $self = shift;
156 12         19 my $uri_parts = shift;
157 12         15 my $args = shift;
158 12         21 my $extra_params = $args->{extra_params};
159 12   100     63 my $bits = $args->{path_parts} || [];
160              
161 12         65 my $uri = URI->new($self->base_url);
162 12         8418 $uri->path_segments(@$bits, @$uri_parts);
163 12 100       903 $uri->query_form($extra_params) if $extra_params;
164              
165 12         927 $self->_client->GET($uri->path_query);
166 12 50       5688 die 'Invalid response' unless $self->response_code == HTTP_OK;
167             # NOTE: my server returns UTF8, if this turns out to be a broken
168             # assumption read the Content-Type header.
169 12         779 my $data = JSON->new->utf8->decode($self->response_content());
170 12         679 return $data;
171             }
172              
173             sub general_call
174             {
175 1     1 1 629 my $self = shift;
176 1         6 my $uri_parts = shift;
177 1         2 my $args = shift;
178              
179 1         3 my $extra_params = $args->{extra_params};
180 1   50     5 my $method = $args->{method} || 'GET';
181 1 50       5 my $decode_json = exists $args->{decode_json} ? $args->{decode_json} : 1;
182 1   50     4 my $expected_response = $args->{expected_response_code} || HTTP_OK;
183              
184 1         11 my $uri = URI->new($self->base_url);
185 1         79 $uri->path_segments(@$uri_parts);
186 1 50       74 $uri->query_form($extra_params) if $extra_params;
187              
188 1         107 $self->_client->$method($uri->path_query);
189 1 50       461 die 'Invalid response' unless $self->response_code == $expected_response;
190 1         56 my $response = $self->response_content();
191 1 50       41 if($decode_json)
192             {
193 1         13 $response = JSON->new->utf8->decode($response);
194             }
195 1         8 return $response;
196             }
197              
198              
199              
200              
201              
202             1; # End of Jenkins::API
203              
204             __END__