blib/lib/App/KGB/Client/ServerRef.pm

filter: Statementstmt 27.4% 34 / 124 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 55.5% 10 / 18 Podpod 100% 8 / 8 totaltotal 23.6% 52 / 220 CC 30 SCAR 39.4
CC
30
Cov
18%
send_changes
110.0
send_changes_json
90.0
send_changes_soap
42.0
CRAP 51.2
« blib/lib/App/KGB/Client.pm ↑ Summary blib/lib/App/KGB/Painter.pm »
1 # vim: ts=4:sw=4:et:ai:sts=4
2 #
3 # KGB - an IRC bot helping collaboration
4 # Copyright © 2008 Martina Ferrari
5 # Copyright © 2009,2010,2012 Damyan Ivanov
6 #
7 # This program is free software; you can redistribute it and/or modify it under
8 # the terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # This program is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 # details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # this program; if not, write to the Free Software Foundation, Inc., 51
19 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package App::KGB::Client::ServerRef;
21  
22 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 our $VERSION = 1.28;
25 8 ▶use feature 'switch';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 8 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 8 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 =head1 NAME
30  
31 App::KGB::Client::ServerRef - server instance in KGB client
32  
33 =head1 SYNOPSIS
34  
35 use App::KGB::Client::ServerRef;
36 my $s = App::KGB::Client::ServerRef->new(
37 { uri => "http://some.server:port/",
38 password => 's3cr1t',
39 timeout => 5
40 }
41 );
42  
43 $s->send_changes( $client, $protocol_ver, $commit, $branch, $module, { extra => stuff } );
44  
45 $s->relay_message( $client, $message, [ { opts } ] );
46  
47 =head1 DESCRIPTION
48  
49 B<App::KGB::Client::ServerRef> is used in L<App::KGB::Client> to refer to
50 remote KGB server instances. It encapsulates sending requests to the remote
51 server, maintaining protocol encapsulation and authentication.
52  
53 =head1 CONSTRUCTOR
54  
55 =over
56  
57 =item new
58  
59 The usual constructor. Accepts a hashref of initialiers.
60  
61 =back
62  
63 =head1 FIELDS
64  
65 =over
66  
67 =item B<uri> (B<mandatory>)
68  
69 The URI of the remote KGB server. Something like C<http://some.host:port/>.
70  
71 =item B<proxy>
72  
73 This is the SOAP proxy used to communicate with the server. If omitted,
74 defaults to the value of B<uri> field, with C<?session=KGB> appended.
75  
76 =item B<password> (B<mandatory>)
77  
78 Password, to be used for authentication to the remote KGB server.
79  
80 =item B<timeout>
81  
82 Specifies the timeout for the SOAP transaction in seconds. Defaults to 15
83 seconds.
84  
85 =item B<verbose>
86  
87 Be verbose about communicating with KGB server.
88  
89 =item B<dry_run>
90  
91 Perform all processing, but do not communicate with the server.
92  
93 =back
94  
95 =head1 METHODS
96  
97 =over
98  
99 =item B<send_changes> (I<message parameters>)
100  
101 Transmits the change set and all data about it along with the necessary
102 authentication hash. If an error occurs, an exception is thrown.
103  
104 Message parameters are passed as arguments in the following order:
105  
106 =over
107  
108 =item Client instance (L<App::KGB::Client>)
109  
110 =item Protocol version (or 'auto')
111  
112 =item Commit (an instance of L<App::KGB::Commit>)
113  
114 =item Branch
115  
116 =item Module
117  
118 =item Extra
119  
120 This is a hash reference with additional parameters.
121  
122 =back
123  
124 =item B<relay_message>(I<client>, I<message> [, I<options hash> ])
125  
126 Sends a message to the server for relaying.
127  
128 =item send_changes_v2($info)
129 =item send_changes_v3($info)
130 =item send_changes_v4($info)
131  
132 Methods implementing different protocol versions
133  
134 =item send_changes_soap($message)
135  
136 Helper method sending commit information via SOAP. Dies on any error or SOAP
137 FAULT.
138  
139 =item send_changes_json($message)
140  
141 Helper method sending commit information via JSON-RPC. Dies on errors.
142  
143 =back
144  
145 =cut
146  
147 8 require v5.10.0;
148 8 ▶use base 'Class::Accessor::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
149  
150 8 __PACKAGE__->mk_accessors( qw( uri proxy password timeout verbose dry_run ) );
151  
152 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
153 8 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
154 8 ▶use Digest::SHA qw(sha1_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
155 8 ▶use YAML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
156  
157 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
158 0 my $self = shift->SUPER::new( @_ );
159  
160 0 ▶ defined( $self->uri )
Branch: 1 branch
BranchTrueFalseTotal
defined $self->uri or confess("'uri' is mandatory") 0 0 0
161 or confess "'uri' is mandatory";
162 0 ▶ defined( $self->proxy )
Branch: 1 branch
BranchTrueFalseTotal
defined $self->proxy or $self->proxy($self->uri . '?session=KGB') 0 0 0
163 or $self->proxy( $self->uri . '?session=KGB' );
164 0 ▶ defined( $self->password )
Branch: 1 branch
BranchTrueFalseTotal
defined $self->password or confess("'password' is mandatory") 0 0 0
165 or confess "'password' is mandatory";
166  
167 0 return $self;
168 }
169  
170 0 ▶sub send_changes {
Subroutine send_changes: not called CC=10 SCAR=47.0
Pod documented
171 0 my ( $self, $client, $protocol_ver, $commit, $branch, $module, $extra )
172 = @_;
173  
174 # Detect utf8 strings and set the utf8 flag, or try to convert from latin1
175 0 my $repo_id = $client->repo_id;
176 0 my $commit_id = $commit->id;
177 0 my $commit_author = $commit->author;
178 0 my $commit_log = $commit->log;
179 0 ▶ my @commit_changes = $commit->changes ? @{ $commit->changes } : ();
Branch: 1 branch
BranchTrueFalseTotal
$commit->changes ? : 0 0 0
180 0 my $password = $self->password;
181  
182 0 my $slc = $client->single_line_commits;
183 0 ▶ if ( $slc eq 'forced' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($slc eq 'forced') { } 0 0 0
elsif ($slc eq 'auto') { } 0 0 0
184 0 $commit_log =~ s/\n.*//s;
185 }
186 elsif ( $slc eq 'auto' ) {
187 0 $commit_log =~ s/^[^\n]+\K\n\n.*//s;
188 }
189  
190 0 foreach ( $repo_id, $commit_id, @commit_changes, $commit_log,
191 $commit_author, $branch, $module, $password ) {
192 0 ▶ next unless ( defined );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
193 0 ▶ next if ( utf8::is_utf8($_) );
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($_) 0 0 0
194 0 my $t = $_;
195 0 ▶ if ( utf8::decode($t) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::decode($t)) { } 0 0 0
196 # valid utf8 char seq
197 0 utf8::decode($_);
198 } else {
199 # try with legacy encoding
200 0 utf8::upgrade($_);
201 }
202 }
203  
204 0 my $info = {
205 repo_id => $repo_id,
206 rev_prefix => $client->rev_prefix,
207 commit_id => $commit_id,
208 changes => [ map ( "$_", @commit_changes ) ],
209 commit_log => $commit_log,
210 author => $commit_author,
211 branch => $branch,
212 module => $module,
213 extra => $extra,
214 };
215  
216 0 my $meth;
217 0 ▶ if ( $protocol_ver eq 'auto' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol_ver eq 'auto') { } 0 0 0
218 0 $meth = 'send_changes_v4';
219 }
220 else {
221 0 $meth = "send_changes_v$protocol_ver";
222 0 ▶ die "Unsupported protocol version requested ($protocol_ver)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($meth) 0 0 0
223 unless $self->can($meth);
224 }
225  
226 0 $self->$meth($info);
227 }
228  
229 0 ▶sub relay_message {
Subroutine relay_message: not called CC=1 SCAR=6.9
Pod documented
230 0 my ( $self, $client, $message, $opts ) = @_;
231  
232 0 $self->send_changes_json( $client->repo_id,
233 { method => 'relay_message', params => [$message, $opts] } );
234 }
235  
236 0 ▶sub send_changes_soap {
Subroutine send_changes_soap: not called CC=6 SCAR=37.4
Pod documented
237 0 my ( $self, $message ) = @_;
238  
239 0 require SOAP::Lite;
240  
241 0 ▶ if ( $self->verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 0 0
242 0 printf "SOAP call: %s/%s\n", $self->uri, $self->proxy;
243 0 print "Message:\n", YAML::Dump($message);
244 }
245 0 ▶ if ( $self->dry_run ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dry_run) 0 0 0
246 0 return;
247 }
248  
249 0 my $s = SOAP::Lite->new( uri => $self->uri, proxy => $self->proxy );
250 0 ▶ $s->transport->proxy->timeout( $self->timeout // 15 );
Condition0%0/2
$self->timeout // 15
l!l
00
MC/DC0%0/1
$self->timeout // 15
$self->timeout
Truth table0%0/2
A
A= $self->timeout
Aresult
11
00
251  
252 0 my $res = $s->commit($message);
253  
254 # SOAP error?
255 0 ▶ if ( $res->fault ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->fault) 0 0 0
256 die 'SOAP FAULT while talking to '
257 . $self->uri . "\n"
258 . 'FAULT MESSAGE: ', $res->fault->{faultstring}, "\n"
259 . (
260 $res->fault->{detail}
261 ? 'FAULT DETAILS: ' . $res->fault->{detail}
262 0 ▶ : ''
Branch: 1 branch
BranchTrueFalseTotal
$res->fault->{'detail'} ? : 0 0 0
263 );
264 }
265 }
266  
267 0 ▶sub send_changes_json {
Subroutine send_changes_json: not called CC=9 SCAR=45.0
Pod documented
268 0 my ( $self, $repo_id, $message ) = @_;
269  
270 0 require JSON::XS;
271 0 require JSON::RPC::Client::Any;
272 0 my $rpc = JSON::RPC::Client::Any->new();
273  
274 0 ▶ $rpc->ua->timeout($self->timeout // 15);
Condition0%0/2
$self->timeout // 15
l!l
00
MC/DC0%0/1
$self->timeout // 15
$self->timeout
Truth table0%0/2
A
A= $self->timeout
Aresult
11
00
275 0 $message->{id} = 1;
276 0 $message->{version} = '1.1';
277 0 my $json = eval { JSON::XS::encode_json($message); };
278 0 ▶ unless ($json) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json) 0 0 0
279 0 my $dump;
280 0 ▶ if ( require Devel::PartialDump ) {
Branch: 2 branches
BranchTrueFalseTotal
if (require Devel::PartialDump) { } 0 0 0
elsif (require Data::Dumper) { } 0 0 0
281 0 $dump = Devel::PartialDump->new->dump($message);
282 }
283 elsif ( require Data::Dumper ) {
284 0 $dump = Data::Dumper::Dump($message);
285 }
286 else {
287 0 $dump = '(Neither Devel::PartialDump nor Data::Dumper available)';
288 }
289  
290 0 confess "Unable to encode message structure as JSON\n" . $dump . "\n"
291 . $@;
292 }
293  
294 0 my $hash = sha1_hex( $self->password, $repo_id, $json );
295  
296 0 $rpc->ua->default_header( 'X-KGB-Auth', $hash );
297 0 $rpc->ua->default_header( 'X-KGB-Project', $repo_id );
298  
299 0 ▶ if ( $self->verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 0 0
300 0 printf "JSON/RPC call: %s/json-rpc\n", $self->uri;
301 0 print "Headers:\n", $rpc->ua->default_headers->as_string;
302 0 print "Message:\n", YAML::Dump($message);
303 }
304 0 ▶ if ( $self->dry_run ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dry_run) 0 0 0
305 0 return;
306 }
307  
308 0 my $res = $rpc->call( $self->uri . '/json-rpc', $message );
309  
310 0 ▶ die "Transport error: " . $rpc->status_line . "\n" unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
311 0 ▶ die "Server returned error: " . $res->error_message . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_error 0 0 0
312 if $res->is_error;
313 }
314  
315 0 ▶sub send_changes_v2 {
Subroutine send_changes_v2: not called CC=5 SCAR=34.0
Pod documented
316 0 my ( $self, $info ) = @_;
317  
318 my $message = join( "",
319 $info->{repo_id},
320 $info->{commit_id} // (),
321 0 ▶ map( "$_", @{ $info->{changes} } ),
Condition0%0/9
$info->{"author"} // ()
l!l&&r!l&&!r
000
$info->{"branch"} // ()
l!l&&r!l&&!r
000
$info->{"module"} // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$info->{"author"} // ()
$info->{"author"} ()
MC/DC0%0/2
$info->{"branch"} // ()
$info->{"branch"} ()
MC/DC0%0/2
$info->{"module"} // ()
$info->{"module"} ()
Truth table0%0/3
A // B
A= $info->{"author"}B= ()
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $info->{"branch"}B= ()
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $info->{"module"}B= ()
ABresult
1X1
011
000
322 $info->{commit_log},
323 $info->{author} // (),
324 $info->{branch} // (),
325 0 ▶ $info->{module} // (),
Condition0%0/3
$info->{"commit_id"} // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$info->{"commit_id"} // ()
$info->{"commit_id"} ()
Truth table0%0/3
A // B
A= $info->{"commit_id"}B= ()
ABresult
1X1
011
000
326 $self->password );
327 0 utf8::encode($message);
328 0 my $checksum = sha1_hex($message);
329 # SOAP::Transport::HTTP tries to convert all characters to byte sequences,
330 # but fails. See around line 204
331 my @message = (
332 2,
333 ( map {
334 0 SOAP::Data->type(
335 string => Encode::encode( 'UTF-8', $_ ) )
336 } ( $info->{repo_id}, $checksum, $info->{rev_prefix}, $info->{commit_id} )
337 ),
338 0 [ map { SOAP::Data->type( string => "$_" ) } @{ $info->{changes} } ],
339 ( map {
340 0 SOAP::Data->type(
341 string => Encode::encode( 'UTF-8', $_ ) )
342 } ( $info->{commit_log}, $info->{author}, $info->{branch}, $info->{module} )
343 0 ),
344 );
345  
346 0 $self->send_changes_soap( \@message );
347 }
348  
349 0 ▶sub send_changes_v3 {
Subroutine send_changes_v3: not called CC=1 SCAR=6.9
Pod documented
350 0 my ( $self, $info ) = @_;
351  
352 0 my $serialized = Storable::nfreeze($info);
353  
354 my @message = (
355 3, $info->{repo_id}, $serialized,
356 0 sha1_hex( $info->{repo_id}, $serialized, $self->password )
357 );
358  
359 0 $self->send_changes_soap(\@message );
360 }
361  
362 0 ▶sub send_changes_v4 {
Subroutine send_changes_v4: not called CC=1 SCAR=6.9
Pod documented
363 0 my ( $self, $info ) = @_;
364  
365 $self->send_changes_json( $info->{repo_id},
366 0 { method => 'commit_v4', params => [$info] } );
367 }
368  
369 8 1;
« blib/lib/App/KGB/Client.pm ↑ Summary blib/lib/App/KGB/Painter.pm »