File Coverage

blib/lib/Dist/Zilla/Plugin/GitLab.pm
Criterion Covered Total %
statement 35 84 41.6
branch 2 30 6.6
condition 0 6 0.0
subroutine 11 15 73.3
pod n/a
total 48 135 35.5


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::GitLab 1.0002;
2              
3 1     1   387521 use Modern::Perl;
  1         10  
  1         11  
4 1     1   199 use JSON::MaybeXS;
  1         2  
  1         69  
5 1     1   597 use Moose;
  1         503965  
  1         9  
6 1     1   8194 use Try::Tiny;
  1         3  
  1         77  
7 1     1   2369 use HTTP::Tiny;
  1         41975  
  1         52  
8 1     1   706 use Git::Wrapper;
  1         27711  
  1         62  
9 1     1   14 use Class::Load qw(try_load_class);
  1         3  
  1         1197  
10              
11             has api => (
12             is => 'ro',
13             isa => 'Str',
14             default => 'https://gitlab.com/api/v4',
15             );
16              
17             has remote => (
18             is => 'ro',
19             isa => 'Maybe[Str]',
20             default => 'origin',
21             );
22              
23             has repo => (
24             is => 'ro',
25             isa => 'Maybe[Str]',
26             );
27              
28             has _credentials => (
29             is => 'ro',
30             isa => 'HashRef',
31             lazy => 1,
32             builder => '_build_credentials',
33             );
34              
35             has _login => (
36             is => 'ro',
37             isa => 'Maybe[Str]',
38             lazy => 1,
39             builder => '_build_login',
40             );
41              
42             sub _build_login {
43 0     0   0 my $self = shift;
44              
45 0         0 my ( $login, %identity );
46              
47 0 0       0 %identity = Config::Identity::GitLab->load
48             if try_load_class('Config::Identity::GitLab');
49              
50 0 0       0 if (%identity) {
51 0         0 $login = $identity{login};
52             }
53             else {
54 0         0 $login = qx{git config gitlab.user};
55 0         0 chomp $login;
56             }
57              
58 0 0       0 if ( !$login ) {
59 0 0       0 my $error
60             = %identity
61             ? 'Err: missing value \'user\' in ~/.gitlab'
62             : 'Err: Missing value \'gitlab.user\' in git config';
63              
64 0         0 $self->log($error);
65 0         0 return undef;
66             }
67              
68 0         0 return $login;
69             }
70              
71             sub _build_credentials {
72 0     0   0 my $self = shift;
73              
74 0         0 my ( $login, $token );
75              
76 0         0 $login = $self->_login;
77              
78 0 0       0 if ( !$login ) {
79 0         0 return {};
80             }
81 0         0 my %identity;
82 0 0       0 %identity = Config::Identity::GitLab->load
83             if try_load_class('Config::Identity::GitLab');
84              
85 0 0       0 if (%identity) {
86 0         0 $token = $identity{token};
87             }
88             else {
89 0         0 $token = qx{git config gitlab.token};
90 0         0 chomp $token;
91             }
92              
93 0         0 return { login => $login, token => $token };
94             }
95              
96             sub _has_credentials {
97 1     1   12 my $self = shift;
98 1         10 return keys %{ $self->_credentials };
  1         73  
99             }
100              
101             sub _auth_headers {
102 1     1   8 my $self = shift;
103              
104 1         49 my $credentials = $self->_credentials;
105              
106 1         23 my %headers = ();
107 1 50       12 if ( $credentials->{token} ) {
108 1         12 $headers{'PRIVATE-TOKEN'} = $credentials->{token};
109             }
110              
111 1         16 return \%headers;
112             }
113              
114             sub _get_repo_name {
115 0     0   0 my ( $self, $login ) = @_;
116              
117 0         0 my $repo;
118 0         0 my $git = Git::Wrapper->new('./');
119              
120 0 0       0 $repo = $self->repo if $self->repo;
121              
122 0         0 my $url;
123             {
124 0         0 local $ENV{LANG} = 'C';
  0         0  
125 0         0 ($url) = map /Fetch URL: (.*)/,
126             $git->remote( 'show', '-n', $self->remote );
127             }
128 0 0       0 if ( !$repo ) {
129 0         0 ($repo) = $url =~ /gitlab\.com.*?[:\/](.*)\.git$/;
130             }
131              
132 0 0       0 $repo = $self->zilla->name unless $repo;
133              
134 0 0       0 if ( $repo !~ /.*\/.*/ ) {
135 0         0 $login = $self->_login;
136 0 0       0 if ( defined $login ) {
137 0         0 $repo = "$login/$repo";
138             }
139             }
140              
141 0         0 return $repo;
142             }
143              
144             sub _check_response {
145 2     2   12 my ( $self, $response ) = @_;
146              
147             try {
148 2     2   155 my $json_text = decode_json( $response->{content} );
149              
150 2 50       14 if ( !$response->{success} ) {
151              
152 0         0 require Data::Dumper;
153 0         0 $self->log(
154             'Err: ',
155             Data::Dumper->new( [$response] )->Indent(2)->Terse(1)
156             ->Sortkeys(1)->Dump
157             );
158 0         0 return;
159             }
160              
161 2         7 return $json_text;
162             }
163             catch {
164 0     0     $self->log("Error: $_");
165 0 0 0       if ( $response
      0        
166             && !$response->{success}
167             && $response->{status} eq '599' ) {
168              
169             #possibly HTTP::Tiny error
170 0           $self->log( 'Err: ', $response->{content} );
171 0           return;
172             }
173              
174 0           $self->log("Error communicating with GitLab: $_");
175              
176 0           return;
177 2         49 };
178             }
179              
180             __PACKAGE__->meta->make_immutable;
181              
182             1;
183              
184             =pod
185              
186             =encoding UTF-8
187              
188             =head1 NAME
189              
190             Dist::Zilla::Plugin::GitLab - Plugins to integrate Dist::Zilla with GitLab
191              
192             =head1 VERSION
193              
194             version 1.0002
195              
196             =head1 DESCRIPTION
197              
198             B<Dist-Zilla-Plugin-GitLab> is a set of plugins for L<Dist::Zilla> intended
199             to more easily integrate L<GitLab|https://gitlab.com> in the C<dzil> workflow.
200              
201             The following is the list of the plugins shipped in this distribution:
202              
203             =over 4
204              
205             =item * L<Dist::Zilla::Plugin::GitLab::Create> Create GitLab repo on C<dzil new>
206              
207             =item * L<Dist::Zilla::Plugin::GitLab::Update> Update GitLab repo info on release
208              
209             =item * L<Dist::Zilla::Plugin::GitLab::Meta> Add GitLab repo info to F<META.{yml,json}>
210              
211             =back
212              
213             This distribution also provides a plugin bundle, L<Dist::Zilla::PluginBundle::GitLab>,
214             which provides L<GitLab::Meta|Dist::Zilla::Plugin::GitLab::Meta> and
215             L<GitLab::Update|Dist::Zilla::Plugin::GitLab::Update> together in one convenient bundle.
216              
217             This distribution also provides an additional C<dzil> command (L<dzil
218             gh|Dist::Zilla::App::Command::gh>).
219              
220             =head1 AUTHOR
221              
222             D Ruth Holloway <ruth@hiruthie.me>
223              
224             =head1 COPYRIGHT AND LICENSE
225              
226             This software is copyright (c) 2022 by D Ruth Holloway.
227              
228             This is free software; you can redistribute it and/or modify it under
229             the same terms as the Perl 5 programming language system itself.
230              
231             =cut
232              
233             __END__
234              
235             # ABSTRACT: Plugins to integrate Dist::Zilla with GitLab
236