blib/lib/Github/Import.pm

filter: Statementstmt 84.5% 120 / 142 Branchbran 32.0% 16 / 50 Conditioncond 25.0% 2 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 82.8% 29 / 35 Podpod 44.4% 4 / 9 totaltotal 68.6% 171 / 249 CC 29 SCAR 15.0
CC
29
Cov
69%
_build_token
12.0
do_create
11.2
_git_conf
10.8
CRAP 4.5
↑ Summary
1 package Github::Import;
2 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Moose::Util::TypeConstraints qw(enum);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use MooseX::Types::Path::Class qw(Dir File);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 with qw(MooseX::Getopt::Dashes);
11  
12 2 our $VERSION = "0.07";
13  
14 2 has use_config_file => (
15 traits => [qw(NoGetopt)],
16 isa => "Bool",
17 is => "ro",
18 default => 0,
19 );
20  
21 2 has config_file => (
22 traits => [qw(Getopt)],
23 isa => File,
24 is => "ro",
25 coerce => 1,
26 cmd_aliases => "f",
27 documentation => "Use an alternate config file",
28 predicate => "has_config_file",
29 );
30  
31 10 ▶sub _git_conf {
Subroutine _git_conf: called CC=4 SCAR=23.7
32 10 my ( $self, $method, $var, $default ) = @_;
33  
34 10 ▶ return $default unless $self->use_config_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->use_config_file 10 0 10
35  
36 0 ▶ local $ENV{GIT_CONFIG} = $self->config_file->stringify if $self->has_config_file;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_config_file 0 0 0
37  
38 0 ▶ if ( defined( my $value = $self->git_handle->$method($var) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $value = $self->git_handle->$method($var))) { } 0 0 0
39 0 return $value;
40 }
41 else {
42 0 return $default;
43 }
44 }
45  
46 4 ▶sub _conf_var {
Subroutine _conf_var: called CC=1 SCAR=0.0
47 4 my ( $self, @args ) = @_;
48 4 $self->_git_conf( config => @args );
49 }
50  
51 6 ▶sub _conf_bool {
Subroutine _conf_bool: called CC=1 SCAR=0.0
52 6 my ( $self, @args ) = @_;
53 6 $self->_git_conf( config_bool => @args );
54 }
55  
56 2 has git_handle => (
57 traits => [qw(NoGetopt)],
58 isa => "Git",
59 is => "ro",
60 lazy_build => 1,
61 );
62  
63 0 ▶sub _build_git_handle {
Subroutine _build_git_handle: not called CC=1 SCAR=6.9
64 0 my $self = shift;
65 0 require Git;
66 0 Git->repository( Directory => $self->project );
67 }
68  
69 # command-line args
70 2 has username => (
71 traits => [qw(Getopt)],
72 is => 'ro',
73 isa => 'Str',
74 lazy_build => 1,
75 cmd_aliases => "u",
76 documentation => 'username for github.com (defaults to $ENV{USER})',
77 );
78  
79 0 ▶sub _build_username {
Subroutine _build_username: not called CC=2 SCAR=17.9
80 0 my $self = shift;
81 0 ▶ $self->_conf_var( 'github-import.user' ) || $self->_conf_var( 'github.user' => $ENV{USER} );
Branch: 1 branch
BranchTrueFalseTotal
$self->_conf_var("github-import.user") or $self->_conf_var("github.user", $ENV{"USER"}) 0 0 0
82 }
83  
84 2 has token => (
85 traits => [qw(Getopt)],
86 is => 'ro',
87 isa => 'Str',
88 lazy_build => 1,
89 cmd_aliases => "P",
90 documentation => "api token for github.com",
91 );
92  
93 0 ▶sub _build_token {
Subroutine _build_token: not called CC=3 SCAR=24.8
94 0 my $self = shift;
95 0 ▶ $self->_conf_var('github-import.token') || $self->_conf_var('github.token') || croak "'token' is required";
Branch: 1 branch
BranchTrueFalseTotal
$self->_conf_var("github-import.token") or $self->_conf_var("github.token") or croak("'token' is required") 0 0 0
Condition0%0/3
$self->_conf_var("github-import.token") or $self->_conf_var("github.token")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_conf_var("github-import.token") or $self->_conf_var("github.token")
$self->_conf_var("github-import.token") $self->_conf_var("github.token")
Truth table0%0/3
A or B
A= $self->_conf_var("github-import.token")B= $self->_conf_var("github.token")
ABresult
1X1
011
000
96 }
97  
98 2 has ssl => (
99 traits => [qw(Getopt)],
100 is => 'ro',
101 isa => 'Bool',
102 lazy_build => 1,
103 documentation => "use https instead of http",
104 cmd_aliases => "S",
105 );
106  
107 1 ▶sub _build_ssl { shift->_conf_bool('github-import.ssl', 0) }
Subroutine _build_ssl: called CC=1 SCAR=0.0
108  
109 2 has dry_run => (
110 traits => [qw(Getopt)],
111 isa => "Bool",
112 is => "ro",
113 cmd_aliases => "n",
114 documentation => "don't actually do anything",
115 );
116  
117 2 has 'project' => (
118 traits => [qw(Getopt)],
119 is => 'ro',
120 isa => Dir,
121 default => ".",
122 coerce => 1,
123 cmd_aliases => "d",
124 documentation => "the directory of the repository (default is pwd)",
125 );
126  
127 2 has project_name => (
128 traits => [qw(Getopt)],
129 is => 'ro',
130 isa => 'Str',
131 lazy_build => 1,
132 cmd_aliases => "N",
133 documentation => "the name of the project to create",
134 );
135  
136 1 ▶sub _build_project_name {
Subroutine _build_project_name: called CC=2 SCAR=7.0
137 1 my $self = shift;
138  
139 1 my $name = $self->project->absolute->dir_list(-1);
140 1 ▶ $name = lc $name if $self->lowercase;
Branch: 1 branch
BranchTrueFalseTotal
if $self->lowercase 0 1 1
141  
142 1 $name =~ s{[:-]+}{-}g;
143  
144 1 return $name;
145 }
146  
147 2 has lowercase => (
148 traits => [qw(Getopt)],
149 is => 'ro',
150 isa => 'Bool',
151 lazy_build => 1,
152 cmd_aliases => "l",
153 documentation => "lowercase the project name for compatibility",
154 );
155  
156 1 ▶sub _build_lowercase { shift->_conf_bool( 'github-import.lowercase' => 0 ) }
Subroutine _build_lowercase: called CC=1 SCAR=0.0
157  
158 2 has description => (
159 traits => [qw(Getopt)],
160 is => 'ro',
161 isa => 'Str',
162 predicate => "has_description",
163 cmd_aliases => "D",
164 documentation => "Project description",
165 );
166  
167 2 has homepage => (
168 traits => [qw(Getopt)],
169 is => 'ro',
170 isa => 'Str',
171 predicate => "has_homepage",
172 cmd_aliases => "H",
173 documentation => "Project homepage",
174 );
175  
176 2 has create => (
177 traits => [qw(Getopt)],
178 is => 'ro',
179 isa => 'Bool',
180 lazy_build => 1,
181 cmd_aliases => "c",
182 documentation => "create the repo on github.com (default is true)",
183 );
184  
185 1 ▶sub _build_create { shift->_conf_bool( 'github-import.create' => 1 ) }
Subroutine _build_create: called CC=1 SCAR=0.0
186  
187 2 has push => (
188 traits => [qw(Getopt)],
189 is => 'ro',
190 isa => 'Bool',
191 lazy_build => 1,
192 cmd_aliases => "p",
193 documentation => "run git push (default is true)",
194 );
195  
196 1 ▶sub _build_push { shift->_conf_bool( 'github-import.push' => 1 ) }
Subroutine _build_push: called CC=1 SCAR=0.0
197  
198 2 has add_remote => (
199 traits => [qw(Getopt)],
200 is => "ro",
201 isa => "Bool",
202 lazy_build => 1,
203 cmd_aliases => "a",
204 documentation => "add a remote for github to .git/config (defaults to true)",
205 );
206  
207 1 ▶sub _build_add_remote { shift->_conf_bool( 'github-import.add-remote' => 1 ) }
Subroutine _build_add_remote: called CC=1 SCAR=0.0
208  
209 2 has push_tags => (
210 traits => [qw(Getopt)],
211 is => "ro",
212 isa => "Bool",
213 cmd_flag => "tags",
214 lazy_build => 1,
215 cmd_aliases => "t",
216 documentation => "specify --tags to push (default is true)",
217 );
218  
219 1 ▶sub _build_push_tags { shift->_conf_bool( 'github-import.push-tags' => 1 ) }
Subroutine _build_push_tags: called CC=1 SCAR=0.0
220  
221 2 has push_mode => (
222 traits => [qw(Getopt)],
223 is => "ro",
224 isa => enum([qw(all mirror)]),
225 predicate => "has_push_mode",
226 cmd_aliases => "m",
227 documentation => "'all' or 'mirror', overrides other push options",
228 );
229  
230 2 has remote => (
231 traits => [qw(Getopt)],
232 is => "ro",
233 isa => "Str",
234 lazy_build => 1,
235 cmd_aliases => "r",
236 documentation => "the remote to add to .git/config (default is 'github')",
237 );
238  
239 1 ▶sub _build_remote { shift->_conf_var( 'github-import.remote' => "github" ) }
Subroutine _build_remote: called CC=1 SCAR=0.0
240  
241 2 has refspec => (
242 traits => [qw(Getopt)],
243 is => "ro",
244 isa => "Str",
245 lazy_build => 1,
246 cmd_aliases => "b",
247 documentation => "the refspec to specify to push (default is 'master')",
248 );
249  
250 1 ▶sub _build_refspec { shift->_conf_var( 'github-import.refspec' => "master" ) }
Subroutine _build_refspec: called CC=1 SCAR=0.0
251  
252 2 has github_path => (
253 traits => [qw(NoGetopt)],
254 isa => "Str",
255 is => "ro",
256 lazy_build => 1,
257 );
258  
259 1 ▶sub _build_github_path {
Subroutine _build_github_path: called CC=1 SCAR=0.0
260 1 my $self = shift;
261 1 return join '/', $self->username, $self->project_name;
262 }
263  
264 2 has push_uri => (
265 traits => [qw(Getopt)],
266 isa => "Str",
267 is => "ro",
268 lazy_build => 1,
269 cmd_aliases => "u",
270 documentation => "override the default github push uri",
271 );
272  
273 1 ▶sub _build_push_uri {
Subroutine _build_push_uri: called CC=1 SCAR=0.0
274 1 my $self = shift;
275 1 return 'git@github.com:' . $self->github_path . '.git';
276 }
277  
278 2 has setup_rebase => (
279 traits => [qw(Getopt)],
280 isa => "Bool",
281 is => "ro",
282 lazy_build => 1,
283 documentation => "setup the new ref to auto-rebase when pulled",
284 );
285  
286 1 ▶sub _build_setup_rebase {
Subroutine _build_setup_rebase: called CC=4 SCAR=15.6
287 1 my $self = shift;
288 1 ▶ my $config = $self->_conf_var('branch.autosetuprebase') || 'never';
Condition50%1/2
$self->_conf_var('branch.autosetuprebase') || 'never'
l!l
01
MC/DC0%0/1
$self->_conf_var('branch.autosetuprebase') || 'never'
$self->_conf_var('branch.autosetuprebase')
Truth table50%1/2
A
A= $self->_conf_var('branch.autosetuprebase')
Aresult
11
00
289 1 ▶ return 1 if $config eq 'remote' || $config eq 'always';
Branch: 1 branch
BranchTrueFalseTotal
if $config eq "remote" or $config eq "always" 0 1 1
Condition33%1/3
$config eq "remote" or $config eq "always"
l!l&&r!l&&!r
001
MC/DC0%0/2
$config eq "remote" or $config eq "always"
$config eq "remote" $config eq "always"
Truth table33%1/3
A or B
A= $config eq "remote"B= $config eq "always"
ABresult
1X1
011
000
290 1 return 0;
291 }
292  
293 # internals
294 has 'user_agent' => (
295 traits => ['NoGetopt'],
296 is => 'ro',
297 isa => 'LWP::UserAgent',
298 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
299 1 require LWP::UserAgent;
300 1 LWP::UserAgent->new(
301 requests_redirectable => [qw/GET POST/],
302 );
303 }
304 2 );
305  
306 has 'logger' => (
307 traits => ['NoGetopt'],
308 is => 'ro',
309 isa => 'CodeRef',
310 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
311 0 ▶ return sub { warn "@_\n" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
312 0 },
313 2 );
314  
315 11 ▶sub msg {
Subroutine msg: called CC=1 SCAR=0.0
Pod undocumented
316 11 my ( $self, @msg ) = @_;
317 11 $self->logger->(@msg);
318 }
319  
320 0 ▶sub err {
Subroutine err: not called CC=1 SCAR=6.9
Pod undocumented
321 0 my ( $self, $msg ) = @_;
322 0 croak $msg;
323 }
324  
325 1 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.1
Pod undocumented
326 1 my $self = shift;
327  
328 1 my $p = $self->project;
329 1 ▶ confess "project '$p' does not exist" unless -d $p;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $p 0 1 1
330 }
331  
332 1 ▶sub run {
Subroutine run: called CC=4 SCAR=14.0
Pod documented
333 1 my $self = shift;
334  
335 1 ▶ if($self->create){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->create) 1 0 1
336 1 $self->msg('Adding project to github');
337 1 my $url = $self->do_create;
338 1 $self->msg('Project added OK: '. $url);
339 }
340  
341 1 ▶ if($self->add_remote){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->add_remote) 1 0 1
342 1 $self->msg(sprintf 'Adding remote "%s" to existing working copy', $self->remote);
343 1 $self->do_add_remote;
344 1 $self->msg('Remote added');
345 };
346  
347 1 ▶ if($self->push){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->push) 1 0 1
348 1 $self->msg('Pushing existing master to github');
349  
350 # Even when github has created the repo we could still get
351 # errors like this because it lags:
352 ## Pushing existing master to github
353 ## fatal: '/data/repositories/c/c3/30/32/avar/digest-whirlpool.git' does not appear to be a git repository
354 ## fatal: The remote end hung up unexpectedly
355 ## push --tags origin master: command returned error: 128
356 ##
357 # So sleep for a bit before pushing to the repo
358 1 sleep 3;
359  
360 1 $self->do_push;
361 1 $self->update_config_for_pull;
362 1 $self->msg('Pushed OK');
363 }
364 };
365  
366 1 ▶sub do_create {
Subroutine do_create: called CC=6 SCAR=24.2
Pod documented
367 1 my $self = shift;
368  
369 1 require URI;
370 1 require HTTP::Request::Common;
371  
372 1 my $uri = URI->new('http://github.com/repositories');
373  
374 1 ▶ $uri->scheme("https") if $self->ssl;
Branch: 1 branch
BranchTrueFalseTotal
if $self->ssl 0 1 1
375  
376 1 ▶ unless ( $self->dry_run ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->dry_run) 0 1 1
377 0 ▶ my $res = $self->user_agent->request(
Branch: 2 branches
BranchTrueFalseTotal
$self->has_description ? : 0 0 0
$self->has_homepage ? : 0 0 0
378 HTTP::Request::Common::POST( $uri, [
379 'commit' => 'Create repository',
380 'login' => $self->username,
381 'token' => $self->token,
382 'repository[name]' => $self->project_name,
383 'repository[public]' => 'true',
384 $self->has_description ? ( 'repository[description]' => $self->description ) : (),
385 $self->has_homepage ? ( 'repository[homepage]' => $self->homepage ) : (),
386 ]),
387 );
388  
389 # XXX: not sure how to detect errors here, other than the obvious
390 0 ▶ $self->err('Error creating project: ' . $res->status_line) unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
391 }
392 1 return $uri->scheme . '://github.com/' . $self->github_path;
393 };
394  
395 5 ▶sub run_git {
Subroutine run_git: called CC=4 SCAR=16.6
Pod undocumented
396 5 my ( $self, @args ) = @_;
397  
398 5 ▶ unshift @args, "command" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 5 0 5
399  
400 5 my %args = @args;
401  
402 5 my ( $command, $print_output ) = @args{qw(command print_output)};
403  
404 5 ▶ if ( $self->dry_run ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dry_run) { } 5 0 5
405 5 $self->msg("git @$command");
406 } else {
407 0 ▶ my $method = $print_output ? "command_noisy" : "command";
Branch: 1 branch
BranchTrueFalseTotal
$print_output ? : 0 0 0
408 0 $self->git_handle->$method(@$command);
409 }
410 }
411  
412 1 ▶sub do_add_remote {
Subroutine do_add_remote: called CC=3 SCAR=13.6
Pod documented
413 1 my $self = shift;
414  
415 1 my $remote = $self->remote;
416 1 my $push = $self->push_uri;
417  
418 1 ▶ if ( defined( my $url = $self->_conf_var("remote.${remote}.url") ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $url = $self->_conf_var("remote.$remote.url"))) { } 0 1 1
419 0 ▶ if ( $url ne $push ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url ne $push) { } 0 0 0
420 0 $self->err("remote $remote is already configured as $url");
421 } else {
422 0 $self->msg("remote $remote already added");
423 }
424 } else {
425 1 $self->run_git(
426 [qw(remote add), $remote, $push],
427 );
428  
429 1 $self->run_git(
430 [qw(config github.repo), $self->project_name],
431 );
432 }
433 }
434  
435 1 ▶sub do_push {
Subroutine do_push: called CC=4 SCAR=14.6
Pod documented
436 1 my $self = shift;
437  
438 1 ▶ my $remote = $self->add_remote ? $self->remote : $self->push_uri;
Branch: 1 branch
BranchTrueFalseTotal
$self->add_remote ? : 1 0 1
439 1 my $refspec = $self->refspec;
440  
441 1 ▶ my @args = $self->has_push_mode
Branch: 2 branches
BranchTrueFalseTotal
$self->has_push_mode ? : 0 1 1
$self->push_tags ? : 1 0 1
442 ? ( "--" . $self->push_mode, $self->remote )
443 : ( $self->push_tags ? "--tags" : (), $remote, $self->refspec );
444  
445 1 $self->run_git(
446 [ push => @args ],
447 print_output => 1,
448 );
449 }
450  
451 1 ▶sub update_config_for_pull {
Subroutine update_config_for_pull: called CC=2 SCAR=7.0
Pod undocumented
452 1 my $self = shift;
453 1 my $remote = $self->remote;
454  
455 1 $self->run_git(
456 [ config => 'branch.master.remote', $remote ],
457 );
458  
459 1 $self->run_git(
460 [ config => 'branch.master.merge', 'refs/heads/master' ],
461 );
462  
463 # if the standard option branch.autosetuprebase is set, then we do
464 # the same thing that "git clone" and other utils do.
465 1 ▶ $self->run_git(
Branch: 1 branch
BranchTrueFalseTotal
if $self->setup_rebase 0 1 1
466 [ config => 'branch.master.rebase', 'true' ]
467 ) if $self->setup_rebase;
468 }
469  
470 2 1;
471  
472 __END__
↑ Summary