blib/lib/Teamcity/Executor.pm

filter: Statementstmt 28.4% 41 / 144 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 40.7% 11 / 27 Podpod 0.0% 0 / 10 totaltotal 26.2% 52 / 198 SCAR 50.2
CC
18
Cov
26%
poll_teamcity_results
30.0
http_request
20.0
start_teamcity_build
12.0
CRAP 151.3
↑ Summary Build »
1 package Teamcity::Executor;
2 3 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = "1.3.0";
7  
8 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Cpanel::JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use IO::Async::Timer::Periodic;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Try::Tiny::Retry ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 has credentials => (is => 'ro', isa => 'HashRef');
19  
20 3 has build_id_mapping => (is => 'ro', isa => 'HashRef');
21  
22 has http => (
23 is => 'ro',
24 isa => 'HTTP::Tiny',
25 0 ▶ default => sub { HTTP::Tiny->new(timeout => 10) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 3 );
27  
28 3 has loop => (
29 is => 'ro',
30 isa => 'IO::Async::Loop',
31 );
32  
33 has teamcity_builds => (
34 is => 'ro',
35 isa => 'HashRef',
36 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 3 );
38  
39 3 has poll_interval => (
40 is => 'ro',
41 isa => 'Int',
42 default => 10,
43 );
44  
45 ▶has teamcity_auth_url => (
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 is => 'ro',
47 isa => 'Str',
48 lazy => 1,
49 default => sub ($self) {
50 0 my $url = $self->credentials->{url};
51 0 my $user = $self->credentials->{user};
52 0 my $pass = $self->credentials->{pass};
53  
54 0 my ($protocol, $address) = $url =~ m{(http[s]://)(.*)};
55  
56 0 return $protocol . $user . ':' . $pass . '@' . $address;
57 }
58 3 );
59  
60 0 ▶sub http_request ($self, $method, $url, $headers = {}, $content = '') {
Subroutine http_request: not called CC=4 SCAR=30.0
Pod undocumented
61 0 my $response;
62  
63 # this code handles the teamcity authentification issues (sometimes authentification fails
64 # without a reason)
65 retry {
66 0 ▶ $response = $self->http->request(
Subroutine __ANON__: not called CC=2 SCAR=17.9
67 $method, $url,
68 {
69 headers => $headers,
70 content => $content,
71 }
72 );
73  
74 0 ▶ if ($response->{status} == 599) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"status"} == 599) 0 0 0
75 0 $log->info("Authentification to teamcity failed, retrying.");
76 0 die 'Authentification to teamcity failed';
77 }
78 }
79 0 ▶ delay_exp { 10, 1e6 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
80  
81 0 ▶ if (!$response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
82 0 die "HTTP $method request to $url failed: " . "$response->{status}: $response->{reason}";
83 }
84  
85 0 return $response;
86 }
87  
88 ▶sub start_teamcity_build ($self, $build_type_id, $properties, $build_name) {
Subroutine start_teamcity_build: not called CC=3 SCAR=24.8
Pod undocumented
89 0 ▶ $build_name //= 'unnamed-build';
Condition0%0/2
$build_name //= "unnamed-build"
l!l
00
MC/DC0%0/1
$build_name //= "unnamed-build"
$build_name
Truth table0%0/2
A
A= $build_name
Aresult
11
00
90  
91 0 my $build_queue_url = $self->teamcity_auth_url . '/httpAuth/app/rest/buildQueue';
92  
93 0 my $xml_properties = '';
94  
95 0 for my $key (keys %{$properties}) {
96 0 my $value = $properties->{$key};
97 0 $xml_properties .= qq{<property name="$key" value="$value" />\n};
98 }
99  
100 0 my $request_body = qq{<build>
101 <buildType id="$build_type_id"/>
102 <properties>
103 $xml_properties
104 </properties>
105 </build>};
106  
107 0 my $response = $self->http_request(
108 'POST',
109 $build_queue_url,
110 {
111 'Content-Type' => 'application/xml',
112 'Accept' => 'application/json',
113 },
114 $request_body,
115 );
116  
117 0 return decode_json $response->{content};
118 }
119  
120 0 ▶sub run_teamcity_build ($self, $build_type_id, $properties, $build_name, $wait = 1) {
Subroutine run_teamcity_build: not called CC=3 SCAR=24.8
Pod undocumented
121  
122 0 my $json = $self->start_teamcity_build($build_type_id, $properties, $build_name);
123  
124 0 my $build_id = $json->{id};
125 0 my $build_detail_href = $json->{webUrl};
126  
127 0 my $f = $self->loop->new_future();
128  
129 0 ▶ if ($wait) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wait) { } 0 0 0
130 $self->teamcity_builds->{$build_id} = {
131 id => $build_id,
132 status_href => $json->{href},
133 0 href => $build_detail_href,
134 name => $build_name,
135 params => $properties,
136 future => $f,
137 };
138 }
139 else {
140 0 $f->done({ id => $build_id, href => $build_detail_href, status => '', params => $properties, output => '' });
141 }
142  
143 0 return $f, $build_id, $json->{webUrl};
144 }
145  
146 ▶sub get_artifact_list ($self, $build_result) {
Subroutine get_artifact_list: not called CC=2 SCAR=17.9
Pod undocumented
147 # get artifacts summary
148 0 my $artifacts_href = $build_result->{output}{artifacts}{href};
149 0 my $artifacts_url = $self->teamcity_auth_url . $artifacts_href;
150 0 my $response = $self->http_request('GET', $artifacts_url, { 'Accept' => 'application/json' },);
151  
152 0 my $json = decode_json $response->{content};
153  
154 0 my %artifacts;
155  
156 # get individual artifacts URLs
157 0 for my $node (@{$json->{file}}) {
158 0 my $content_href = $node->{content}{href};
159 0 my $metadata_href = $node->{content}{href};
160 0 my $name = $node->{name};
161 0 $artifacts{$name} = {
162 name => $name,
163 content_href => $content_href,
164 metadata_href => $metadata_href,
165 };
166 }
167  
168 0 return \%artifacts;
169 }
170  
171 ▶sub get_artifact_content ($self, $build_result, $artifact_name) {
Subroutine get_artifact_content: not called CC=2 SCAR=17.9
Pod undocumented
172 0 my $artifact_list = $self->get_artifact_list($build_result);
173  
174 0 ▶ die "The artifact $artifact_name could not be found!" unless %$artifact_list{$artifact_name};
Branch: 1 branch
BranchTrueFalseTotal
unless %$artifact_list{$artifact_name} 0 0 0
175  
176 0 my $content_url = $self->teamcity_auth_url . $artifact_list->{$artifact_name}{content_href};
177  
178 0 my $response = $self->http_request('GET', $content_url);
179  
180 0 return $response->{content};
181 }
182  
183 0 ▶sub run ($self, $build_name, $properties = {}) {
Subroutine run: not called CC=2 SCAR=17.9
Pod undocumented
184  
185 0 my $teamcity_job_parameters = join(', ', map { "$_: '$properties->{$_}'" } keys %{$properties});
186 0 $log->info("RUN\t$build_name($teamcity_job_parameters)");
187  
188 0 my ($f, $id, $url) = $self->run_teamcity_build($self->build_id_mapping->{$build_name}, $properties, $build_name,);
189  
190 0 $log->info("\t[$id]\t$url");
191  
192 0 return $f;
193 }
194  
195 0 ▶sub touch ($self, $build_name, $properties = {}) {
Subroutine touch: not called CC=2 SCAR=17.9
Pod undocumented
196 0 my $teamcity_job_parameters = join(', ', map { "$_: '$properties->{$_}'" } keys %{$properties});
197 0 $log->info("TOUCH\t$build_name($teamcity_job_parameters)");
198  
199 0 my ($f, $id, $url) = $self->run_teamcity_build($self->build_id_mapping->{$build_name}, $properties, $build_name, 0);
200  
201 0 $log->info("\t[$id]\t$url");
202  
203 0 return $f;
204 }
205  
206 0 ▶sub touch_without_future ($self, $build_name, $properties = {}) {
Subroutine touch_without_future: not called CC=2 SCAR=17.9
Pod undocumented
207 0 my $teamcity_job_parameters = join(', ', map { "$_: '$properties->{$_}'" } keys %{$properties});
208 0 $log->info("TOUCH WITHOUT FUTURE\t$build_name($teamcity_job_parameters)");
209  
210 0 my $result_json = $self->start_teamcity_build($self->build_id_mapping->{$build_name}, $properties, $build_name);
211
212 0 $log->info("\t[$result_json->{id}]\t$result_json->{webUrl}");
213 0 return { id => $result_json->{id}, href => $result_json->{webUrl}, status => '', params => $properties, output => $result_json };
214 }
215  
216  
217 ▶sub poll_teamcity_results($self) {
Subroutine poll_teamcity_results: not called CC=5 SCAR=34.0
Pod undocumented
218 0 $log->info('.');
219  
220 0 for my $build (values %{$self->teamcity_builds}) {
221 0 my $url = $self->teamcity_auth_url . $build->{status_href};
222  
223 0 my $response = $self->http_request('GET', $url, { 'Accept' => 'application/json' },);
224  
225 0 my $json = decode_json $response->{content};
226  
227 0 my $state = $json->{state};
228 0 my $status = $json->{status};
229  
230 0 ▶ $log->info("$build->{name} [$build->{id}]: QUEUED") if $state eq 'queued';
Branch: 1 branch
BranchTrueFalseTotal
if $state eq "queued" 0 0 0
231  
232 0 ▶ next if $state ne 'finished';
Branch: 1 branch
BranchTrueFalseTotal
if $state ne "finished" 0 0 0
233  
234 my $job_result = {
235 id => $build->{id},
236 href => $build->{href},
237 status => $status,
238 params => $build->{params},
239 0 output => $json
240 };
241  
242 0 my $teamcity_job_parameters = join(', ', map { "$_: '$build->{params}->{$_}'" } keys %{$build->{params}});
243 0 $log->info("$status\t".$build->{name}."($teamcity_job_parameters)");
244 0 $log->info("\t[".$build->{id}."]\t".$build->{href});
245
246 0 ▶ if ($status eq 'SUCCESS') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq 'SUCCESS') { } 0 0 0
247 0 $build->{future}->done($job_result);
248 }
249 else {
250 0 $build->{future}->fail($job_result);
251 }
252  
253 0 delete $self->teamcity_builds->{ $build->{id} };
254 }
255 }
256  
257 ▶sub register_polling_timer($self) {
Subroutine register_polling_timer: not called CC=1 SCAR=6.9
Pod undocumented
258 my $timer = IO::Async::Timer::Periodic->new(
259 interval => $self->poll_interval,
260 on_tick => sub {
261 0 ▶ $self->poll_teamcity_results();
Subroutine __ANON__: not called CC=1 SCAR=6.9
262 },
263 0 );
264  
265 0 $self->loop->add($timer);
266 0 $timer->start();
267 }
268  
269 3 1;
270 __END__
271  
272 =encoding utf-8
273  
274 =head1 NAME
275  
276 Teamcity::Executor - Executor of TeamCity build configurations
277  
278 =head1 SYNOPSIS 1 - asynchronous usage
279  
280 use Teamcity::Executor;
281 use IO::Async::Loop;
282 use Log::Any::Adapter;
283  
284 Log::Any::Adapter->set(
285 'Dispatch',
286 outputs => [
287 [
288 'Screen',
289 min_level => 'debug',
290 stderr => 1,
291 newline => 1
292 ]
293 ]
294 );
295  
296 my $loop = IO::Async::Loop->new;
297 my $tc = Teamcity::Executor->new(
298 credentials => {
299 url => 'https://teamcity.example.com',
300 user => 'user',
301 pass => 'password',
302 },
303 build_id_mapping => {
304 hello_world => 'playground_HelloWorld',
305 hello_name => 'playground_HelloName',
306 }
307 poll_interval => 10,
308 loop => $loop,
309 );
310  
311 $tc->register_polling_timer();
312  
313 my $future = $tc->run('hello_name', { name => 'TeamCity' })->then(
314 sub {
315 my ($build) = @_;
316 print "Build succeeded\n";
317 my $greeting = $tc->get_artifact_content($build, 'greeting.txt');
318 print "Content of greeting.txt artifact: $greeting\n";
319 },
320 sub {
321 print "Build failed\n";
322 exit 1
323 }
324 );
325  
326 my $touch_future = $tc->touch('hello_name', { name => 'TeamCity' })->then(
327 sub {
328 my ($build) = @_;
329 print "Touch build started\n";
330 $loop->stop();
331 },
332 sub {
333 print "Touch build failed to start\n";
334 exit 1
335 }
336 );
337  
338 $loop->run();
339  
340 =head1 SYNOPSIS 2 - synchronous usage
341  
342 use Teamcity::Executor;
343 use Log::Any::Adapter;
344  
345 Log::Any::Adapter->set(
346 'Dispatch',
347 outputs => [
348 [
349 'Screen',
350 min_level => 'debug',
351 stderr => 1,
352 newline => 1
353 ]
354 ]
355 );
356  
357 my $tc = Teamcity::Executor->new(
358 credentials => {
359 url => 'https://teamcity.example.com',
360 user => 'user',
361 pass => 'password',
362 },
363 build_id_mapping => {
364 hello_world => 'playground_HelloWorld',
365 hello_name => 'playground_HelloName',
366 }
367 );
368  
369 my $resp = $tc->touch_without_future('hello_name', {});
370  
371 print "id: $resp->{id}\n";
372 print "webUrl: $resp->{webUrl}\n";
373  
374 =head1 DESCRIPTION
375  
376 Teamcity::Executor is a module for executing Teamcity build configurations.
377 When you execute one, you'll receive a future of the build. Teamcity::Executor
378 polls TeamCity and when it finds the build has ended, it resolves the future.
379  
380 =head1 LICENSE
381  
382 Copyright (C) Avast Software
383  
384 This library is free software; you can redistribute it and/or modify
385 it under the same terms as Perl itself.
386  
387 =head1 AUTHOR
388  
389 Miroslav Tynovsky E<lt>tynovsky@avast.comE<gt>
390  
391 =cut
↑ Summary Build »