Plugin/GitHub.pm

filter: Statementstmt 62.0% 49 / 79 Branchbran 18.1% 4 / 22 Conditioncond 18.1% 2 / 11 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 92.8% 13 / 14 Podpod n/a totaltotal 51.9% 68 / 131 CC 16 SCAR 27.0
CC
16
Cov
49%
__ANON__
37.1
_build_login
30.0
_auth_headers
6.3
CRAP 14.9
« Plugin/GitHub/Update.pm ↑ Summary App/Command/gh.pm »
1 package Dist::Zilla::Plugin::GitHub; # git description: v0.48-3-g3b5901f
2 # ABSTRACT: Plugins to integrate Dist::Zilla with GitHub
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.49';
7  
8 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Git::Wrapper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Class::Load qw(try_load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 has remote => (
16 is => 'ro',
17 isa => 'Maybe[Str]',
18 default => 'origin'
19 );
20  
21 1 has repo => (
22 is => 'ro',
23 isa => 'Maybe[Str]'
24 );
25  
26 1 has api => (
27 is => 'ro',
28 isa => 'Str',
29 default => 'https://api.github.com'
30 );
31  
32 1 has prompt_2fa => (
33 is => 'rw',
34 isa => 'Bool',
35 default => 0
36 );
37  
38 1 has _login => (
39 is => 'ro',
40 isa => 'Maybe[Str]',
41 lazy => 1,
42 builder => '_build_login',
43 );
44  
45 1 has _credentials => (
46 is => 'ro',
47 isa => 'HashRef',
48 lazy => 1,
49 builder => '_build_credentials',
50 );
51  
52 #pod =head1 DESCRIPTION
53 #pod
54 #pod B<Dist-Zilla-Plugin-GitHub> is a set of plugins for L<Dist::Zilla> intended
55 #pod to more easily integrate L<GitHub|https://github.com> in the C<dzil> workflow.
56 #pod
57 #pod The following is the list of the plugins shipped in this distribution:
58 #pod
59 #pod =over 4
60 #pod
61 #pod =item * L<Dist::Zilla::Plugin::GitHub::Create> Create GitHub repo on C<dzil new>
62 #pod
63 #pod =item * L<Dist::Zilla::Plugin::GitHub::Update> Update GitHub repo info on release
64 #pod
65 #pod =item * L<Dist::Zilla::Plugin::GitHub::Meta> Add GitHub repo info to F<META.{yml,json}>
66 #pod
67 #pod =back
68 #pod
69 #pod This distribution also provides a plugin bundle, L<Dist::Zilla::PluginBundle::GitHub>,
70 #pod which provides L<GitHub::Meta|Dist::Zilla::Plugin::GitHub::Meta> and
71 #pod L<[GitHub::Update|Dist::Zilla::Plugin::GitHub::Update> together in one convenient bundle.
72 #pod
73 #pod This distribution also provides an additional C<dzil> command (L<dzil
74 #pod gh|Dist::Zilla::App::Command::gh>) and a L<plugin
75 #pod bundle|Dist::Zilla::PluginBundle::GitHub>.
76 #pod
77 #pod =cut
78  
79 0 ▶sub _build_login {
Subroutine _build_login: not called CC=5 SCAR=34.0
80 0 my $self = shift;
81  
82 0 my ($login);
83  
84 0 ▶ my %identity = Config::Identity::GitHub->load
Branch: 1 branch
BranchTrueFalseTotal
if try_load_class("Config::Identity::GitHub") 0 0 0
85 if try_load_class('Config::Identity::GitHub');
86  
87 0 ▶ if (%identity) {
Branch: 1 branch
BranchTrueFalseTotal
if (%identity) { } 0 0 0
88 0 $login = $identity{login};
89 } else {
90 0 $login = `git config github.user`; chomp $login;
91 }
92  
93 0 ▶ if (!$login) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($login) 0 0 0
94 0 ▶ my $error = %identity ?
Branch: 1 branch
BranchTrueFalseTotal
%identity ? : 0 0 0
95 "Err: missing value 'user' in ~/.github" :
96 "Err: Missing value 'github.user' in git config";
97  
98 0 $self->log($error);
99 0 return undef;
100 }
101  
102 0 return $login;
103 }
104  
105 sub _build_credentials {
106 my $self = shift;
107  
108 my ($login, $pass, $token);
109  
110 $login = $self->_login;
111  
112 if (!$login) {
113 return {};
114 }
115  
116 my %identity = Config::Identity::GitHub->load
117 if try_load_class('Config::Identity::GitHub');
118  
119 if (%identity) {
120 $token = $identity{token};
121 $pass = $identity{password};
122 } else {
123 $token = `git config github.token`; chomp $token;
124 $pass = `git config github.password`; chomp $pass;
125 }
126  
127 if (!$pass and !$token) {
128 $pass = $self->zilla->chrome->prompt_str(
129 "GitHub password for '$login'", { noecho => 1 },
130 );
131 }
132  
133 return { login => $login, pass => $pass, token => $token };
134 }
135  
136 1 ▶sub _has_credentials {
Subroutine _has_credentials: called CC=1 SCAR=0.0
137 1 my $self = shift;
138 1 return keys %{$self->_credentials};
139 }
140  
141 1 ▶sub _auth_headers {
Subroutine _auth_headers: called CC=4 SCAR=18.5
142 1 my $self = shift;
143  
144 1 my $credentials = $self->_credentials;
145  
146 1 my %headers = ( Accept => 'application/vnd.github.v3+json' );
147 1 ▶ if ($credentials->{pass}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($credentials->{'pass'}) { } 0 1 1
elsif ($credentials->{'token'}) { } 0 1 1
148 0 require MIME::Base64;
149 0 my $basic = MIME::Base64::encode_base64("$credentials->{login}:$credentials->{pass}", '');
150 0 $headers{Authorization} = "Basic $basic";
151 }
152 elsif ($credentials->{token}) {
153 0 $headers{Authorization} = "token $credentials->{token}";
154 }
155  
156 # This can't be done at object creation because we autodetect the
157 # need for 2FA when GitHub says we need it, so we won't know to
158 # prompt at object creation time.
159 1 ▶ if ($self->prompt_2fa) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->prompt_2fa) 0 1 1
160 0 my $otp = $self->zilla->chrome->prompt_str(
161 "GitHub two-factor authentication code for '$credentials->{login}'",
162 { noecho => 1 },
163 );
164  
165 0 $headers{'X-GitHub-OTP'} = $otp;
166 0 $self->log([ "Using two-factor authentication" ]);
167 }
168  
169 1 return \%headers;
170 }
171  
172 sub _get_repo_name {
173 my ($self, $login) = @_;
174  
175 my $repo;
176 my $git = Git::Wrapper->new('./');
177  
178 $repo = $self->repo if $self->repo;
179  
180 my $url;
181 {
182 local $ENV{LANG}='C';
183 ($url) = map /Fetch URL: (.*)/,
184 $git->remote('show', '-n', $self->remote);
185 }
186  
187 $url =~ /github\.com.*?[:\/](.*)\.git$/;
188 $repo = $1 unless $repo and not $1;
189  
190 $repo = $self->zilla->name unless $repo;
191  
192 if ($repo !~ /.*\/.*/) {
193 $login = $self->_login;
194 if (defined $login) {
195 $repo = "$login/$repo";
196 }
197 }
198  
199 return $repo;
200 }
201  
202 2 ▶sub _check_response {
Subroutine _check_response: called CC=1 SCAR=3.3
203 2 my ($self, $response) = @_;
204  
205 2 ▶ try {
Subroutine __ANON__: called CC=6 SCAR=36.1
206 2 my $json_text = decode_json($response->{content});
207  
208 0 ▶ if (!$response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
209 return 'redo' if (($response->{status} eq '401') and
210 0 ▶ (($response->{headers}{'x-github-otp'} // '') =~ /^required/));
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"status"} eq 401 and ($response->{"headers"}{"x-github-otp"} // "") =~ /^required/ 0 0 0
Condition0%0/5
$response->{"status"} eq 401 and ($response->{"headers"}{"x-github-otp"} // "") =~ /^required/
!ll&&!rl&&r
000
$response->{"headers"}{"x-github-otp"} // ""
l!l
00
MC/DC0%0/2
$response->{"status"} eq 401 and ($response->{"headers"}{"x-github-otp"} // "") =~ /^required/
$response->{"status"} eq 401 $response->{"headers"}{"x-github-otp"}
Truth table0%0/3
A and B
A= $response->{"status"} eq 401B= $response->{"headers"}{"x-github-otp"}
ABresult
0X0
100
111
211  
212 0 ▶ if ($response->{status} eq '404') {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"status"} eq 404) 0 0 0
213 0 $self->log($response->{reason}.' (insufficient permissions to edit this resource?)');
214 0 return;
215 }
216  
217 0 require Data::Dumper;
218 0 $self->log("Err: ", Data::Dumper->new([ $response ])->Indent(2)->Terse(1)->Sortkeys(1)->Dump);
219 0 return;
220 }
221  
222 0 return $json_text;
223 2 ▶ } catch {
Subroutine __ANON__: called CC=4 SCAR=17.9
224 2 $self->log("Error: $_");
225 2 ▶ if ($response and !$response->{success} and
Branch: 1 branch
BranchTrueFalseTotal
if ($response and not $response->{"success"} and $response->{"status"} eq 599) 0 2 2
Condition33%2/6
$response and not $response->{"success"} and $response->{"status"} eq 599
!ll&&!rl&&r
200
$response and not $response->{"success"}
!ll&&!rl&&r
020
MC/DC0%0/3
$response and not $response->{"success"} and $response->{"status"} eq 599
$response not $response->{"success"} $response->{"status"} eq 599
Truth table25%1/4
A and not(B) and C
A= $responseB= not $response->{"success"}C= $response->{"status"} eq 599
ABCresult
0XX0
10X0
1100
1111
226 $response->{status} eq '599') {
227 #possibly HTTP::Tiny error
228 0 $self->log("Err: ", $response->{content});
229 0 return;
230 }
231  
232 2 $self->log("Error communicating with GitHub: $_");
233  
234 2 return;
235 2 };
236 }
237  
238 1 __PACKAGE__->meta->make_immutable;
239 1 1; # End of Dist::Zilla::Plugin::GitHub
240  
241 __END__
« Plugin/GitHub/Update.pm ↑ Summary App/Command/gh.pm »