blib/lib/Teamcity/Executor.pm

filter: Statementstmt 34.6% 44 / 127 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 52.1% 12 / 23 Podpod 0.0% 0 / 7 totaltotal 32.1% 56 / 174 SCAR 45.0
CC
15
Cov
31%
http_request
41.9
poll_teamcity_results
30.0
run_teamcity_build
12.0
CRAP 89.7
↑ 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 = "0.1.1";
7  
8 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use autobox::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Cpanel::JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use IO::Async::Timer::Periodic;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
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', isa => 'HTTP::Tiny',
24 0 ▶ default => sub { HTTP::Tiny->new(timeout => 10) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 3 );
26  
27 3 has loop => (
28 is => 'ro', isa => 'IO::Async::Loop',
29 );
30  
31 has teamcity_builds => (
32 0 ▶ is => 'ro', isa => 'HashRef', default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 3 );
34  
35 3 has poll_interval => (
36 is => 'ro', isa => 'Int', default => 10,
37 );
38  
39 ▶has teamcity_auth_url => (
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 is => 'ro', isa => 'Str', lazy => 1, default => sub ($self) {
41 0 my $url = $self->credentials->{url};
42 0 my $user = $self->credentials->{user};
43 0 my $pass = $self->credentials->{pass};
44  
45 0 my ($protocol, $address) = $url =~ m{(http[s]://)(.*)};
46  
47 0 return $protocol . $user . ':' . $pass . '@' . $address;
48 }
49 3 );
50  
51  
52 0 ▶sub http_request($self, $method, $url, $headers = {}, $content = '') {
Subroutine http_request: not called CC=7 SCAR=37.3
Pod undocumented
53  
54 0 my $desecretized_url = $url =~ s{(http[s]://)[^/]+:[^@]+@}{$1}r;
55 # say STDERR "# $method\t$desecretized_url";
56  
57 0 my $response;
58  
59 0 my $retry = 0;
60 0 while (1) {
61 0 $response = $self->http->request($method, $url, {
62 headers => $headers,
63 content => $content,
64 });
65  
66 0 ▶ last if $response->{status} != 599;
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"status"} != 599 0 0 0
67 0 ▶ print ' [TeamCity request retry: ' if !$retry;
Branch: 1 branch
BranchTrueFalseTotal
unless $retry 0 0 0
68 0 print '.';
69 0 sleep 1;
70 0 $retry = 1;
71 }
72 0 ▶ print "] " if $retry;
Branch: 1 branch
BranchTrueFalseTotal
if $retry 0 0 0
73  
74 # say STDERR 'done';
75  
76 0 ▶ if (! $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
77 3 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 0 print Dumper $response;
79 0 die "HTTP $method request to $url failed: " .
80 "$response->{status}: $response->{reason}"
81 }
82  
83 0 return $response
84 }
85  
86  
87 sub run_teamcity_build {
88 0 ▶ my ($self, $build_type_id, $properties, $build_name) = @_;
Subroutine run_teamcity_build: not called CC=3 SCAR=24.8
Pod undocumented
89  
90 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
91  
92 0 my $build_queue_url =
93 $self->teamcity_auth_url . '/httpAuth/app/rest/buildQueue';
94  
95 0 my $xml_properties = '';
96  
97 0 for my $key ($properties->keys) {
98 0 my $value = $properties->{$key};
99 0 $xml_properties .= qq{<property name="$key" value="$value" />\n};
100 }
101  
102 0 my $request_body =
103 qq{<build>
104 <buildType id="$build_type_id"/>
105 <properties>
106 $xml_properties
107 </properties>
108 </build>};
109  
110 0 my $response = $self->http_request(
111 'POST',
112 $build_queue_url,
113 {
114 'Content-Type' => 'application/xml',
115 'Accept' => 'application/json',
116 },
117 $request_body,
118 );
119  
120 0 my $json = decode_json $response->{content};
121  
122 0 my $build_id = $json->{id};
123 0 my $build_href = $json->{href};
124 0 my $f = $self->loop->new_future();
125  
126 0 $self->teamcity_builds->{$build_id} = {
127 id => $build_id,
128 href => $build_href,
129 name => $build_name,
130 future => $f,
131 };
132  
133 return $f, $build_id, $json->{webUrl}
134 0 }
135  
136 ▶sub get_artifact_list($self, $build_result) {
Subroutine get_artifact_list: not called CC=2 SCAR=17.9
Pod undocumented
137  
138 # get build result
139 0 my $result_url = $self->teamcity_auth_url . $build_result->{href};
140 0 my $response = $self->http_request(
141 'GET',
142 $result_url,
143 { 'Accept' => 'application/json' },
144 );
145 0 my $json = decode_json $response->{content};
146  
147 # get artifacts summary
148 0 my $artifacts_href = $json->{artifacts}{href};
149 0 my $artifacts_url = $self->teamcity_auth_url . $artifacts_href;
150 0 $response = $self->http_request(
151 'GET',
152 $artifacts_url,
153 { 'Accept' => 'application/json' },
154 );
155  
156 0 $json = decode_json $response->{content};
157  
158 0 my %artifacts;
159 # get individual artifacts URLs
160 0 for my $node ($json->{file}->elements) {
161 0 my $content_href = $node->{content}{href};
162 0 my $metadata_href = $node->{content}{href};
163 0 my $name = $node->{name};
164 0 $artifacts{$name} = {
165 name => $name,
166 content_href => $content_href,
167 metadata_href => $metadata_href,
168 };
169 }
170  
171 0 return \%artifacts
172 }
173  
174 ▶sub get_artifact_content($self, $build_result, $artifact_name) {
Subroutine get_artifact_content: not called CC=1 SCAR=6.9
Pod undocumented
175 0 my $artifact_list = $self->get_artifact_list($build_result);
176  
177 my $content_url = $self->teamcity_auth_url .
178 0 $artifact_list->{$artifact_name}{content_href};
179 0 my $response = $self->http_request('GET', $content_url);
180  
181 return $response->{content}
182 0 }
183  
184  
185 0 ▶sub run($self, $build_name, $properties = {}) {
Subroutine run: not called CC=2 SCAR=17.9
Pod undocumented
186 0 print "RUN\t$build_name(";
187 0 print join(', ', map { "$_: '$properties->{$_}'" } $properties->keys);
188 0 print ")";
189  
190 my ($f, $id, $url) = $self->run_teamcity_build(
191 0 $self->build_id_mapping->{$build_name},
192 $properties,
193 $build_name,
194 );
195  
196 0 say " [$id]\n\t$url";
197  
198 0 return $f;
199 }
200  
201 ▶sub poll_teamcity_results($self) {
Subroutine poll_teamcity_results: not called CC=5 SCAR=34.0
Pod undocumented
202 0 say 'TICK';
203 0 for my $build ($self->teamcity_builds->values) {
204 0 my $url = $self->teamcity_auth_url . $build->{href};
205 0 my $response = $self->http_request(
206 'GET',
207 $url,
208 { 'Accept' => 'application/json' },
209 );
210  
211 0 my $json = decode_json $response->{content};
212  
213 0 my $state = $json->{state};
214 0 my $status = $json->{status};
215  
216 0 ▶ next if $state ne 'finished';
Branch: 1 branch
BranchTrueFalseTotal
if $state ne "finished" 0 0 0
217  
218 0 say "RESULT\t$build->{name} [$build->{id}]: $status";
219  
220 0 ▶ if ($status eq 'SUCCESS') {
Branch: 2 branches
BranchTrueFalseTotal
if ($status eq 'SUCCESS') { } 0 0 0
elsif ($status eq 'FAILURE') { } 0 0 0
221 0 my $href = $json->{href};
222 0 $build->{future}->done({ id => $build->{id}, href => $href });
223 }
224 elsif ($status eq 'FAILURE') {
225 0 $build->{future}->fail($json->{statusText});
226 }
227  
228 0 delete $self->teamcity_builds->{$build->{id}};
229 }
230 }
231  
232 ▶sub register_polling_timer($self) {
Subroutine register_polling_timer: not called CC=1 SCAR=6.9
Pod undocumented
233 my $timer = IO::Async::Timer::Periodic->new(
234 interval => $self->poll_interval,
235 on_tick => sub {
236 0 ▶ $self->poll_teamcity_results();
Subroutine __ANON__: not called CC=1 SCAR=6.9
237 },
238 0 );
239  
240 0 $self->loop->add($timer);
241 0 $timer->start();
242 }
243  
244  
245 3 1;
246 __END__
247  
248 =encoding utf-8
249  
250 =head1 NAME
251  
252 Teamcity::Executor - Executor of TeamCity build configurations
253  
254 =head1 SYNOPSIS
255  
256 use Teamcity::Executor;
257 use IO::Async::Loop;
258  
259 my $loop = IO::Async::Loop->new;
260 my $tc = Teamcity::Executor->new(
261 credentials => {
262 url => 'https://teamcity.example.com',
263 user => 'user',
264 pass => 'password',
265 },
266 build_id_mapping => {
267 hello_world => 'playground_HelloWorld',
268 hello_name => 'playground_HelloName',
269 }
270 poll_interval => 10,
271 loop => $loop,
272 )
273  
274 $tc->register_polling_timer();
275  
276 $tc->run('hello_name', { name => 'TeamCity' })->then(
277 sub {
278 my ($build) = @_;
279 print "Build succeeded\n";
280 my $greeting = $tc->get_artifact_content($build, 'greeting.txt');
281 print "Content of greeting.txt artifact: $greeting\n";
282 },
283 sub {
284 print "Build failed\n";
285 exit 1
286 }
287 );
288  
289 $loop->run();
290  
291  
292 =head1 DESCRIPTION
293  
294 Teamcity::Executor is a module for executing Teamcity build configurations.
295 When you execute one, you'll receive a future of the build. Teamcity::Executor
296 polls TeamCity and when it finds the build has ended, it resolves the future.
297  
298 =head1 LICENSE
299  
300 Copyright (C) Avast Software
301  
302 This library is free software; you can redistribute it and/or modify
303 it under the same terms as Perl itself.
304  
305 =head1 AUTHOR
306  
307 Miroslav Tynovsky E<lt>tynovsky@avast.comE<gt>
308  
309 =cut
310  
↑ Summary Build »