blib/lib/App/GitHub/create.pm

filter: Statementstmt 26.2% 21 / 80 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 70.0% 7 / 10 Podpod 0.0% 0 / 3 totaltotal 17.1% 28 / 163 CC 29 SCAR 49.0
CC
29
Cov
15%
run
208.4
create
156.0
usage
20.0
CRAP 133.8
↑ Summary
1 package App::GitHub::create;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $App::GitHub::create::VERSION = '0.0012';
4 }
5 # ABSTRACT: Create a github repository from the commandline
6  
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Config::Identity::GitHub;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Getopt::Long qw/ GetOptions /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 my $agent = LWP::UserAgent->new;
15  
16 0 ▶sub create {
Subroutine create: not called CC=12 SCAR=50.5
Pod undocumented
17 0 my $self = shift;
18 0 my %given = @_;
19 0 my ( $login, $token, $name, $description, $homepage, $public );
20  
21 ( $name, $description, $homepage, $public ) =
22 0 @given{qw/ name description homepage public /};
23 0 ▶ defined $_ && length $_ or die "Missing name\n" for $name;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ or die "Missing name\n" 0 0 0
Condition0%0/3
defined $_ and length $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ and length $_
defined $_ length $_
Truth table0%0/3
A and B
A= defined $_B= length $_
ABresult
0X0
100
111
24  
25 0 ( $login, $token ) = @given{qw/ login token /};
26 0 ▶ unless( defined $token && length $token ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $token and length $token) 0 0 0
Condition0%0/3
defined $token and length $token
!ll&&!rl&&r
000
MC/DC0%0/2
defined $token and length $token
defined $token length $token
Truth table0%0/3
A and B
A= defined $tokenB= length $token
ABresult
0X0
100
111
27 0 my %identity = Config::Identity::GitHub->load;
28 0 ( $login, $token ) = @identity{qw/ login token /};
29 }
30  
31  
32 0 my @arguments;
33 0 ▶ push @arguments, 'name' => $name if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 0 0 0
34 0 ▶ push @arguments, 'description' => $description if defined $description;
Branch: 1 branch
BranchTrueFalseTotal
if defined $description 0 0 0
35 0 ▶ push @arguments, 'homepage' => $homepage if defined $homepage;
Branch: 1 branch
BranchTrueFalseTotal
if defined $homepage 0 0 0
36 0 ▶ push @arguments, 'public' => $public if defined $public and $public;
Branch: 1 branch
BranchTrueFalseTotal
if defined $public and $public 0 0 0
Condition0%0/3
defined $public and $public
!ll&&!rl&&r
000
MC/DC0%0/2
defined $public and $public
defined $public $public
Truth table0%0/3
A and B
A= defined $publicB= $public
ABresult
0X0
100
111
37  
38 0 my $uri = "https://github.com/api/v2/json/repos/create";
39 0 my $response = $agent->post( $uri,
40 [ login => $login, token => $token, @arguments ] );
41  
42 0 ▶ unless ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
43 0 die $response->status_line, ": ", $response->decoded_content, "\n";
44 }
45  
46 0 return $response;
47 }
48  
49 0 ▶sub usage (;$) {
Subroutine usage: not called CC=4 SCAR=30.0
Pod undocumented
50 0 my $error = shift;
51 0 my $exit = 0;
52 0 ▶ if ( defined $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 0 0 0
53 0 ▶ if ( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
54 0 ▶ if ( $error =~ m/^\-?\d+$/ ) { $exit = $error }
Branch: 1 branch
BranchTrueFalseTotal
if ($error =~ /^\-?\d+$/) { } 0 0 0
55 else {
56 0 chomp $error;
57 0 warn $error, "\n";
58 0 $exit = -1;
59 }
60 }
61 }
62 0 warn <<_END_;
63  
64 Usage: github-create [options] <name>
65  
66 --login ... Your github login
67 --token ... The github token associated with the given login
68  
69 Although required, if a login/token are not given,
70 github-create will attempt to load it from
71 \$HOME/.github or \$HOME/.github-identity (see
72 Config::Identity for more information)
73  
74 --name ... The name of the repository to create (required)
75 --description ... A description of the repository (optional)
76 --homepage ... A homepage for the repository (optional)
77 --private The repository is private (default)
78 --public The repository is public
79  
80 --help, -h, -? This help
81  
82 _END_
83  
84 0 exit $exit;
85 }
86  
87 0 ▶sub run {
Subroutine run: not called CC=15 SCAR=53.4
Pod undocumented
88 0 my $self = shift;
89 0 my @arguments = @_;
90  
91 0 my ( $login, $token, $help );
92 0 my ( $name, $homepage, $description, $private, $public, $dry_run );
93  
94 0 ▶ usage 0 unless @arguments;
Branch: 1 branch
BranchTrueFalseTotal
unless @arguments 0 0 0
95  
96 {
97 0 local @ARGV = @arguments;
98 0 GetOptions(
99 'help|h|?' => \$help,
100  
101 'login=s' => \$login,
102 'token=s' => \$token,
103  
104 'name=s' => \$name,
105 'description=s' => \$description,
106 'homepage=s' => \$homepage,
107
108 'private' => \$private,
109 'public' => \$public,
110  
111 'dry-run' => \$dry_run,
112 );
113 0 @arguments = @ARGV;
114 }
115  
116 0 ▶ usage 0 if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 0 0
117  
118 0 ▶ $name = $arguments[0] unless defined $name && length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 0 0
Condition0%0/3
defined $name and length $name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table0%0/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
119 0 ▶ unless ( defined $name && length $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name and length $name) 0 0 0
Condition0%0/3
defined $name and length $name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table0%0/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
120 0 usage <<_END_;
121 github-create: Missing name (--name)
122 _END_
123 }
124  
125 0 ▶ if ( $private and $public ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($private and $public) 0 0 0
Condition0%0/3
$private and $public
!ll&&!rl&&r
000
MC/DC0%0/2
$private and $public
$private $public
Truth table0%0/3
A and B
A= $privateB= $public
ABresult
0X0
100
111
126 0 usage <<_END_;
127 github-create: Repository cannot be both private AND public
128 _END_
129 }
130 0 ▶ $public = $public ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$public ? : 0 0 0
131  
132 0 eval {
133 0 my $owner = $login;
134 0 ▶ $owner = '<username>' unless defined $owner;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $owner 0 0 0
135 0 ▶ unless ( $dry_run ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dry_run) 0 0 0
136  
137 0 my $response = $self->create(
138 login => $login, token => $token,
139 name => $name, description => $description, homepage => $homepage, public => $public,
140 );
141  
142 0 print $response->as_string;
143  
144 0 eval {
145 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
146 0 require JSON;
147 0 my $data = JSON->new->decode( $response->decoded_content );
148 0 ▶ $_ and $owner = $_ for $data->{repository}->{owner};
Branch: 1 branch
BranchTrueFalseTotal
$_ and $owner = $_ 0 0 0
149 };
150 }
151  
152 0 print <<_END_;
153  
154 Repository $name created. To track it, run the following:
155  
156 # git remote add origin git\@github.com:$owner/$name.git
157 # git push origin master
158  
159 _END_
160 };
161 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
162 0 usage <<_END_;
163 github-create: $@
164 _END_
165 }
166 }
167  
168 # for my $option (keys %options) {
169 # next unless $options{$option};
170 # push @arguments, "values[has_$option]" => 'true';
171 # }
172  
173 # --enable ... Enable wiki, issues, and/or the downloads page
174 # Can be a series of options separated by a comma:
175 #
176 # all Enable everything
177 # none Disable everything
178 # wiki Enable the wiki
179 # issues Enable issues
180 # downloads Enable downloads
181  
182 # The default is 'none'
183  
184 # my %options;
185 # my @enable = split m/\s*,\s*/, $enable;
186 # for ( @enable ) {
187 # s/^\s*//, s/\s*$//;
188 # next unless $_;
189 # if ( m/^none$/i ) { undef %options }
190 # elsif ( m/^all$/i ) { %options = qw/ wiki 1 issues 1 downloads 1 / }
191 # elsif ( m/^wiki$/i ) { $options{lc $_} = 1 }
192 # elsif ( m/^issues$/i ) { $options{lc $_} = 1 }
193 # elsif ( m/^downloads$/i ) { $options{lc $_} = 1 }
194 # else { usage <<_END_ }
195 #github-create: Unknown enable option: $_
196 #_END_
197 # }
198
199 1 1;
200  
201 __END__
↑ Summary