blib/lib/Teamcity/Executor.pm

filter: Statementstmt 28.4% 41 / 144 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 40.7% 11 / 27 Podpod 0.0% 0 / 10 totaltotal 25.1% 52 / 207 CC 20 SCAR 23.1
CC
20
Cov
25%
poll_teamcity_results
30.0
http_request
20.0
__ANON__
20.0
CRAP 10.1
↑ Summary
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.1";
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 => (
46 is => 'ro',
47 isa => 'Str',
48 lazy => 1,
49 0 ▶ default => sub ($self) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
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 0 ▶ retry {
Subroutine __ANON__: not called CC=4 SCAR=30.0
66 0 $response = $self->http->request(
67 $method, $url,
68 {
69 headers => $headers,
70 content => $content,
71 }
72 );
73  
74 0 ▶ if ($response->{status} == 599 || ($response->{status} == 401 && !$response->{reason})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"status"} == 599 or $response->{"status"} == 401 and not $response->{"reason"}) 0 0 0
Condition0%0/6
$response->{"status"} == 599 or $response->{"status"} == 401 and not $response->{"reason"}
l!l&&r!l&&!r
000
$response->{"status"} == 401 and not $response->{"reason"}
!ll&&!rl&&r
000
MC/DC0%0/3
$response->{"status"} == 599 or $response->{"status"} == 401 and not $response->{"reason"}
$response->{"status"} == 599 $response->{"status"} == 401 not $response->{"reason"}
Truth table0%0/4
A or B and not(C)
A= $response->{"status"} == 599B= $response->{"status"} == 401C= not $response->{"reason"}
ABCresult
1XX1
0111
00X0
0100
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 0 ▶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 0 ▶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 0 ▶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 0 ▶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 0 ▶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 0 ▶ on_tick => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
261 0 $self->poll_teamcity_results();
262 },
263 0 );
264  
265 0 $self->loop->add($timer);
266 0 $timer->start();
267 }
268  
269 3 1;
270 __END__
↑ Summary