blib/lib/Net/Gitlab.pm untested

filter: Statementstmt 0.0% 0 / 201 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 269 CC 19 SCAR 34.9
CC
19
Cov
0%
_method
90.0
_call_api
42.0
_set_get
20.0
CRAP 32.6
1 package Net::Gitlab;
2  
3 ## no critic( ValuesAndExpressions::ProhibitAccessOfPrivateData )
4  
5 # ABSTRACT: Talk to a Gitlab installation via its API.
6  
7  
8 use utf8;
9 use strict;
10 use warnings;
11 use namespace::autoclean;
12  
13 use Carp;
14 use HTTP::Request ();
15 use JSON;
16 use LWP::UserAgent ();
17 use Params::Validate::Checks ':all';
18 use Readonly;
19 use Regexp::Common 'Email::Address';
20  
21 Readonly my $PASSWD_LENGTH = 6;
22  
23 our $VERSION = '0.04'; # VERSION
24  
25 { # Hide
26  
27 Params::Validate::Checks::register
28 email => qr/$RE{Email}{Address}/,
29 uri => qr/$RE{URI}{HTTP}{-scheme => 'https?'}/,
30 short_password => sub { length $_[0] > $PASSWD_LENGTH };
31  
32 my %validate = (
33  
34 assignee_id => { as 'pos_int' },
35 hook_id => { as 'pos_int' },
36 issue_id => { as 'pos_int' },
37 key_id => { as 'pos_int' },
38 milestone_id => { as 'pos_int' },
39 project_id => { as 'pos_int' },
40 snippet_id => { as 'pos_int' },
41 user_id => { as 'pos_int' },
42  
43 issues_enabled => { type => BOOLEAN },
44 merge_requests_enabled => { type => BOOLEAN },
45 wall_enabled => { type => BOOLEAN },
46 wiki_enabled => { type => BOOLEAN },
47  
48 # Are these hard coded into gitlab? if so, we can further restrict this
49 access_level => { as 'string' },
50 branch => { as 'string' },
51 closed => { as 'string' },
52 code => { as 'string' },
53 default_branch => { as 'string' },
54 description => { as 'string' },
55 due_date => { as 'string' },
56 email => { as 'email' },
57 file_name => { as 'string' },
58 key => { as 'string' },
59 labels => { as 'string' },
60 lifetime => { as 'string' },
61 linkedin => { as 'string' },
62 name => { as 'string' },
63 password => { as 'string', as 'short_password' },
64 path => { as 'string' },
65 private_token => { as 'string' },
66 projects_limit => { as 'pos_int' },
67 sha => { as 'string' },
68 skype => { as 'string' },
69 title => { as 'string' },
70 twitter => { as 'string' },
71 url => { as 'uri' },
72 username => { as 'string' },
73  
74 base_url => { as 'uri' },
75 error => { as 'string' },
76 status_code => { as 'pos_int' },
77  
78 ); ## end %validate
79  
80 my %method = (
81  
82 ## no critic qw( Tics::ProhibitLongLines )
83  
84 login => {
85  
86 action => 'POST',
87 path => 'session',
88 required => [qw( email password )],
89  
90 },
91  
92 users => {
93  
94 action => 'GET',
95 path => 'users',
96  
97 },
98  
99 user => {
100  
101 action => 'GET',
102 path => 'users/<user_id>',
103 required => [qw( user_id )],
104  
105 },
106  
107 add_user => {
108  
109 action => 'POST',
110 path => 'users',
111 required => [qw( email password username name )],
112 optional => [qw( skype linkedin twitter projects_limit extern_uid provider bio )],
113  
114 },
115  
116 self => {
117  
118 action => 'GET',
119 path => 'user',
120  
121 },
122  
123 self_issues => {
124  
125 action => 'GET',
126 path => 'issues',
127  
128 },
129  
130 self_keys => {
131  
132 action => 'GET',
133 path => 'user/keys',
134  
135 },
136  
137 self_key => {
138  
139 action => 'GET',
140 path => 'user/keys/<key_id>',
141 required => [qw( user_id )],
142  
143 },
144  
145 add_key => {
146  
147 action => 'POST',
148 path => 'user/keys',
149 required => [qw( title key )],
150  
151 },
152  
153 remove_key => {
154  
155 action => 'DELETE',
156 path => 'user/keys/<key_id>',
157 required => [qw( key_id )],
158  
159 },
160  
161 projects => {
162  
163 action => 'GET',
164 path => 'projects',
165  
166 },
167  
168 add_project => {
169  
170 action => 'POST',
171 path => 'projects',
172 required => [qw( name )],
173 optional => [
174 qw( code path description default_branch issues_enabled wall_enabled merge_requests_enabled wiki_enabled namespace_id visibility_level ),
175 ],
176  
177 },
178  
179 project => {
180  
181 action => 'GET',
182 path => 'projects/<project_id>',
183 required => [qw( project_id )],
184  
185 },
186  
187 branches => {
188  
189 action => 'GET',
190 path => 'projects/<project_id>/repository/branches',
191 required => [qw( project_id )],
192  
193 },
194  
195 branch => {
196  
197 action => 'GET',
198 path => 'projects/<project_id>/repository/branches/<branch>',
199 required => [qw( project_id branch )],
200  
201 },
202  
203 commits => {
204  
205 action => 'GET',
206 path => 'projects/<project_id>/repository/commits',
207 required => [qw( project_id )],
208  
209 },
210  
211 commit => {
212  
213 action => 'GET',
214 path => 'projects/<project_id>/repository/commits/<sha>/blob',
215 required => [qw( project_id sha )],
216  
217 },
218  
219 tags => {
220  
221 action => 'GET',
222 path => 'projects/<project_id>/repository/tags',
223 required => [qw( project_id )],
224  
225 },
226  
227 hooks => {
228  
229 action => 'GET',
230 path => 'projects/<project_id>/hooks',
231 required => [qw( project_id )],
232  
233 },
234  
235 hook => {
236  
237 action => 'GET',
238 path => 'projects/<project_id>/hooks/<hook_id>',
239 required => [qw( project_id hook_id )],
240  
241 },
242  
243 issues => {
244  
245 action => 'GET',
246 path => 'projects/<project_id>/issues',
247 required => [qw( project_id )],
248  
249 },
250  
251 issue => {
252  
253 action => 'GET',
254 path => 'projects/<project_id>/issues/<issue_id>',
255 required => [qw( project_id issue_id )],
256  
257 },
258  
259 members => {
260  
261 action => 'GET',
262 path => 'projects/<project_id>/members',
263 required => [qw( project_id )],
264  
265 },
266  
267 member => {
268  
269 action => 'GET',
270 path => 'projects/<project_id>/members/<user_id>',
271 required => [qw( project_id user_id )],
272  
273 },
274  
275 milestones => {
276  
277 action => 'GET',
278 path => 'projects/<project_id>/milestones',
279 required => [qw( project_id )],
280  
281 },
282  
283 milestone => {
284  
285 action => 'GET',
286 path => 'projects/<project_id>/milestones/<milestone_id>',
287 required => [qw( project_id milestone_id )],
288  
289 },
290  
291 snippets => {
292  
293 action => 'GET',
294 path => 'projects/<project_id>/snippets',
295 required => [qw( project_id )],
296  
297 },
298  
299 snippet => {
300  
301 action => 'GET',
302 path => 'projects/<project_id>/snippets/<snippet_id>',
303 required => [qw( project_id snippet_id )],
304  
305 },
306  
307 raw_snippet => {
308  
309 action => 'GET',
310 path => 'projects/<project_id>/snippets/<snippet_id>/raw',
311 required => [qw( project_id snippet_id )],
312  
313 },
314  
315 add_hook => {
316  
317 action => 'POST',
318 path => 'projects/<project_id>/hooks',
319 required => [qw( project_id url )],
320  
321 },
322  
323 add_issue => {
324  
325 action => 'POST',
326 path => 'projects/<project_id>/issues',
327 required => [qw( project_id title )],
328 optional => [qw( description assignee_id milestone_id labels )],
329  
330 },
331  
332 add_member => {
333  
334 action => 'POST',
335 path => 'projects/<id>/members',
336 required => [qw( id user_id )],
337 optional => [qw( access_level )],
338  
339 },
340  
341 add_milestone => {
342  
343 action => 'POST',
344 path => 'projects/<project_id>/milestones',
345 required => [qw( project_id title )],
346 optional => [qw( description due_date )],
347  
348 },
349  
350 add_snippet => {
351  
352 action => 'POST',
353 path => 'projects/<project_id>/snippets',
354 required => [qw( project_id title file_name code )],
355 optional => [qw( lifetime )],
356  
357 },
358  
359 modify_hook => {
360  
361 action => 'POST',
362 path => 'projects/<project_id>/hooks/<hook_id>',
363 required => [qw( project_id hook_id url )],
364  
365 },
366  
367 modify_issue => {
368  
369 action => 'PUT',
370 path => 'projects/<project_id>/issues/<issue_id>',
371 required => [qw( project_id issue_id )],
372 optional => [qw( title description assignee_id milestone_id labels )],
373  
374 },
375  
376 modify_member => {
377  
378 action => 'PUT',
379 path => 'projects/<project_id>/members/<user_id>',
380 required => [qw( project_id user_id access_level )],
381  
382 },
383  
384 modify_milestone => {
385  
386 action => 'PUT',
387 path => 'projects/<project_id>/milestones/<milestone_id>',
388 required => [qw( project_id milestone_id )],
389 optional => [qw( title description due_date closed )],
390  
391 },
392  
393 modify_snippet => {
394  
395 action => 'PUT',
396 path => 'projects/<project_id>/snippets/<snippet_id>',
397 required => [qw( project_id snippet_id )],
398 optional => [qw( title file_name lifetime code )],
399  
400 },
401  
402 remove_hook => {
403  
404 action => 'DELETE',
405 path => 'projects/<project_id>/hooks',
406 required => [qw( project_id )],
407  
408 },
409  
410 remove_member => {
411  
412 action => 'DELETE',
413 path => 'projects/<project_id>/members/<user_id>',
414 required => [qw( project_id user_id )],
415  
416 },
417  
418 remove_snippet => {
419  
420 action => 'DELETE',
421 path => 'projects/<project_id>/snippets/<snippet_id>',
422 required => [qw( project_id snippet_id )],
423  
424 },
425  
426 groups => {
427  
428 action => 'GET',
429 path => 'groups',
430  
431 },
432  
433 add_group => {
434  
435 action => 'POST',
436 path => 'groups',
437 required => [qw( name path )],
438  
439 },
440  
441 ); ## end %method
442  
443 my $valid_methods = join '|', sort keys %method;
444  
445 ###############################################################################
446  
447 sub _set_get {
448  
449 my ( $self, $key ) = @_;
450  
451 croak "unknown attribute ($key)"
452 unless exists $validate{ $key };
453  
454 my $validate = $validate{ $key };
455 $validate->{ optional } = 1;
456  
457 my ( $value ) = validate_pos( @_, $validate );
458  
459 if ( defined $value ) {
460  
461 $self->{ $key } = $value;
462 return 1;
463  
464 } else {
465  
466 croak "$key has not been set"
467 unless exists $self->{ $key };
468  
469 return $self->{ $key };
470  
471 }
472 } ## end sub _set_get
473  
474 sub _method {
475  
476 my $self = shift;
477 my $m = shift;
478  
479 croak "unkown method ($m)"
480 unless exists $method{ $m };
481  
482 my $method = $method{ $m };
483  
484 my $spec;
485  
486 if ( exists $method->{ required } ) {
487  
488 croak 'required needs to be an arrayref'
489 unless ref $method->{ required } eq 'ARRAY';
490  
491 $spec->{ $_ } = $validate{ $_ } for @{ $method->{ required } };
492  
493 }
494  
495 if ( exists $method->{ optional } ) {
496  
497 croak 'optional needs to be an arrayref'
498 unless ref $method->{ optional } eq 'ARRAY';
499  
500 for my $parm ( @{ $method->{ optional } } ) {
501  
502 croak "oops ... duplicate key ($parm) in optional and required arrays for method $m"
503 if exists $spec->{ $parm };
504  
505 $spec->{ $parm } = $validate{ $parm };
506 $spec->{ $parm }{ optional } = 1;
507  
508 }
509 }
510  
511 my %data;
512 %data = validate_with( params => \@_, spec => $spec )
513 if keys %$spec; ## no critic qw( References::ProhibitDoubleSigils )
514  
515 if ( keys %data ) {
516  
517 return $self->_call_api( $m, \%data );
518  
519 } else {
520  
521 return $self->_call_api( $m );
522  
523 }
524 } ## end sub _method
525  
526 our $AUTOLOAD;
527  
528 sub AUTOLOAD { ## no critic qw( ClassHierarchies::ProhibitAutoloading )
529  
530 my $self = shift;
531  
532 ( my $call = $AUTOLOAD ) =~ s/^.*:://;
533  
534 my $sub;
535  
536 if ( exists $validate{ $call } ) {
537  
538 $sub = sub { shift->_set_get( $call, @_ ) };
539  
540 } elsif ( exists $method{ $call } ) {
541  
542 $sub = sub { shift->_method( $call, @_ ) };
543  
544 } else {
545  
546 croak "Don't know how to handle $call";
547  
548 }
549  
550 ## no critic qw( References::ProhibitDoubleSigils )
551 no strict 'refs'; ## no critic( TestingAndDebugging::ProhibitNoStrict )
552 *$AUTOLOAD = $sub;
553  
554 unshift @_, $self;
555  
556 goto &$AUTOLOAD;
557  
558 } ## end sub AUTOLOAD
559  
560 DESTROY { }
561  
562 sub new {
563  
564 my $class = shift;
565 my $self = bless {}, ref $class || $class;
566  
567 my $validate;
568  
569 for my $k ( keys %validate ) {
570  
571 $validate->{ $k } = $validate{ $k };
572 $validate->{ $k }{ optional } = 1;
573  
574 }
575  
576 my %arg = validate_with( params => \@_, spec => $validate );
577  
578 $self->$_( $arg{ $_ } ) for keys %arg;
579  
580 return $self;
581  
582 } ## end sub new
583  
584 sub _ua { shift->{ ua } ||= LWP::UserAgent->new() }
585  
586 sub _call_api {
587  
588 my $self = shift;
589  
590 my @specs = { type => SCALAR, regex => qr/^($valid_methods)$/ };
591  
592 push @specs, { type => HASHREF }
593 if scalar @_ > 1;
594  
595 my ( $m, $data ) = validate_pos( @_, @specs );
596  
597 croak "no action specified for $m"
598 unless exists $method{ $m }->{ action };
599  
600 my $method = $method{ $m };
601  
602 my $action = $method->{ action };
603 my $url = sprintf '%s/%s', $self->base_url(), $method->{ path };
604  
605 $url =~ s/<$_>/delete $data->{ $_ }/ge for $url =~ /<([^>]*)>/g;
606  
607 my $req = HTTP::Request->new( $action => $url );
608  
609 $req->content_type( 'application/json' );
610  
611 $req->header( 'private_token' => $self->private_token() )
612 unless $method->{ path } eq '/session';
613  
614 $req->content( encode_json $data )
615 if keys %$data; ## no critic ( References::ProhibitDoubleSigils )
616  
617 my $res = $self->_ua->request( $req );
618 $self->status_code( $res->code() );
619  
620 if ( $res->is_success ) {
621  
622 return decode_json $res->content;
623  
624 } else {
625  
626 $self->error( $res->status_line );
627 return;
628  
629 }
630 }; ## end sub _call_api
631 } # No more hiding
632  
633 1;
634  
635 __END__
636  
637 =pod
638  
639 =head1 NAME
640  
641 Net::Gitlab - Talk to a Gitlab installation via its API.
642  
643 =head1 VERSION
644  
645 This document describes v0.04 of Net::Gitlab - released September 03, 2014 as part of Net-Gitlab.
646  
647 =head1 METHODS
648  
649 =head2 new
650  
651 Create a new instance of a Gitlab object.
652  
653 =head1 INSTALLATION
654  
655 See perlmodinstall for information and options on installing Perl modules.
656  
657 =head1 SOURCE
658  
659 The development version is on github at L<http://https://github.com/harleypig/Net-Gitlab>
660 and may be cloned from L<git://https://github.com/harleypig/Net-Gitlab.git>
661  
662 =head1 AUTHOR
663  
664 Alan Young <harleypig@gmail.com>
665  
666 =head1 COPYRIGHT AND LICENSE
667  
668 This software is copyright (c) 2014 by Alan Young.
669  
670 This is free software; you can redistribute it and/or modify it under
671 the same terms as the Perl 5 programming language system itself.
672  
673 =head1 DISCLAIMER OF WARRANTY
674  
675 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
676 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT
677 WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER
678 PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND,
679 EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
680 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
681 PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
682 SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME
683 THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
684  
685 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
686 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
687 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE
688 TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR
689 CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
690 SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
691 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
692 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
693 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
694 DAMAGES.
695  
696 =cut