Plugin/GitLab/Create.pm

filter: Statementstmt 36.2% 33 / 91 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 54.5% 6 / 11 Podpod 0.0% 0 / 1 totaltotal 27.2% 39 / 143 CC 19 SCAR 55.2
CC
19
Cov
26%
after_mint
380.0
__ANON__
2.0
__ANON__
2.0
CRAP 249.4
« Plugin/GitLab/Meta.pm ↑ Summary Plugin/GitLab.pm »
1 package Dist::Zilla::Plugin::GitLab::Create 1.0002;
2  
3 1 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Git::Wrapper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 extends 'Dist::Zilla::Plugin::GitLab';
11 1 with 'Dist::Zilla::Role::AfterMint';
12 1 with 'Dist::Zilla::Role::TextTemplate';
13  
14 1 has issues => (
15 is => 'ro',
16 isa => 'Bool',
17 default => 1,
18 );
19  
20 1 has merge_requests => (
21 is => 'ro',
22 isa => 'Bool',
23 default => 1,
24 );
25  
26 1 has namespace => (
27 is => 'ro',
28 isa => 'Maybe[Str]',
29 );
30  
31 1 has packages => (
32 is => 'ro',
33 isa => 'Bool',
34 default => 1,
35 );
36  
37 1 has prompt => (
38 is => 'ro',
39 isa => 'Bool',
40 default => 0,
41 );
42  
43 1 has public => (
44 is => 'ro',
45 isa => 'Bool',
46 default => 1,
47 );
48  
49 1 has remote => (
50 is => 'ro',
51 isa => 'Str',
52 default => 'origin',
53 );
54  
55 1 has repo => (
56 is => 'ro',
57 isa => 'Maybe[Str]',
58 );
59  
60 1 has snippets => (
61 is => 'ro',
62 isa => 'Bool',
63 default => 1,
64 );
65  
66 1 has wiki => (
67 is => 'ro',
68 isa => 'Bool',
69 default => 1,
70 );
71  
72 0 ▶sub after_mint {
Subroutine after_mint: not called CC=19 SCAR=59.4
Pod undocumented
73 0 my $self = shift;
74 0 my ($opts) = @_;
75  
76 0 ▶ return if $self->prompt and not $self->_confirm;
Branch: 1 branch
BranchTrueFalseTotal
if $self->prompt and not $self->_confirm 0 0 0
Condition0%0/3
$self->prompt and not $self->_confirm
!ll&&!rl&&r
000
MC/DC0%0/2
$self->prompt and not $self->_confirm
$self->prompt not $self->_confirm
Truth table0%0/3
A and not(B)
A= $self->promptB= not $self->_confirm
ABresult
0X0
100
111
77  
78 0 my $root = $opts->{mint_root};
79 0 my $repo_name = $self->zilla->name;
80 0 ▶ if ( $opts->{repo} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'repo'}) { } 0 0 0
elsif ($self->repo) { } 0 0 0
81 0 $repo_name = $opts->{repo};
82 }
83 elsif ( $self->repo ) {
84 0 $repo_name
85 = $self->fill_in_string( $self->repo, { dist => \( $self->zilla ) },
86 );
87 }
88  
89 0 $self->log( [ 'Creating new GitLab repository \'%s\'', $repo_name ] );
90 0 my $http = HTTP::Tiny->new;
91 0 my ( $params, $headers, $content );
92  
93 0 $headers = $self->_auth_headers;
94 0 ▶ if ( $self->namespace ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->namespace) 0 0 0
95 0 my $namespaces_url = $self->api . '/namespaces';
96 0 my $namespaces
97 = $http->request( 'GET', $namespaces_url, { headers => $headers } );
98 0 my $spaces = $self->_check_response($namespaces);
99 0 foreach my $space (@$spaces) {
100 0 ▶ next if $self->namespace ne $space->path;
Branch: 1 branch
BranchTrueFalseTotal
if $self->namespace ne $space->path 0 0 0
101 0 $params->{namespace_id} = $space->id;
102 }
103 }
104  
105 0 $params->{name} = $repo_name;
106 0 ▶ $params->{visibility} = $self->public ? 'public' : 'private';
Branch: 1 branch
BranchTrueFalseTotal
$self->public ? : 0 0 0
107 $params->{description}
108 = $opts->{description}
109 ? $opts->{description}
110 0 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'description'} ? : 0 0 0
111 0 $params->{issues_enabled} = $self->issues;
112 $self->log(
113 0 ▶ [ 'Issues are %s', $params->{issues_enabled} ? 'enabled' : 'disabled' ]
Branch: 1 branch
BranchTrueFalseTotal
$params->{'issues_enabled'} ? : 0 0 0
114 );
115 0 $params->{wiki_enabled} = $self->wiki;
116 $self->log(
117 0 ▶ [ 'Wiki is %s', $params->{wiki_enabled} ? 'enabled' : 'disabled' ] );
Branch: 1 branch
BranchTrueFalseTotal
$params->{'wiki_enabled'} ? : 0 0 0
118 0 $params->{packages_enabled} = $self->packages;
119 $self->log(
120 [
121 'Packages are %s',
122 0 ▶ $params->{packages_enabled} ? 'enabled' : 'disabled'
Branch: 1 branch
BranchTrueFalseTotal
$params->{'packages_enabled'} ? : 0 0 0
123 ]
124 );
125 0 $params->{snippets_enabled} = $self->snippets;
126 $self->log(
127 [
128 'Snippets are %s',
129 0 ▶ $params->{snippets_enabled} ? 'enabled' : 'disabled'
Branch: 1 branch
BranchTrueFalseTotal
$params->{'snippets_enabled'} ? : 0 0 0
130 ]
131 );
132 0 $params->{merge_requests_enabled} = $self->merge_requests;
133 $self->log(
134 [
135 'Merge requests are %s',
136 0 ▶ $params->{merge_requests_enabled} ? 'enabled' : 'disabled'
Branch: 1 branch
BranchTrueFalseTotal
$params->{'merge_requests_enabled'} ? : 0 0 0
137 ]
138 );
139  
140 0 my $url = $self->api . '/projects';
141 0 $content = encode_json($params);
142 0 $headers->{'content-type'} = 'application/json';
143 0 $self->log_debug("Sending POST $url");
144  
145 0 my $response = $http->request(
146 'POST', $url,
147 {
148 content => $content,
149 headers => $headers,
150 }
151 );
152  
153 0 my $repo = $self->_check_response($response);
154  
155 0 ▶ return if not $repo;
Branch: 1 branch
BranchTrueFalseTotal
unless $repo 0 0 0
156  
157 0 my $git_dir = "$root/.git";
158 0 my $rem_ref = $git_dir . '/refs/remotes/' . $self->remote;
159  
160 0 ▶ if ( ( -d $git_dir ) && ( not -d $rem_ref ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $git_dir and not -d $rem_ref) 0 0 0
Condition0%0/3
-d $git_dir and not -d $rem_ref
!ll&&!rl&&r
000
MC/DC0%0/2
-d $git_dir and not -d $rem_ref
-d $git_dir not -d $rem_ref
Truth table0%0/3
A and not(B)
A= -d $git_dirB= not -d $rem_ref
ABresult
0X0
100
111
161 0 my $git = Git::Wrapper->new($root);
162  
163 0 $self->log( [ 'Setting GitLab remote \'%s\'', $self->remote ] );
164 0 $git->remote( 'add', $self->remote, $repo->{ssh_url_to_repo} );
165  
166 0 ▶ my ($branch) = try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
167 0 $git->rev_parse( { abbrev_ref => 1, symbolic_full_name => 1 },
168 'HEAD' )
169 0 };
170  
171 0 ▶ if ($branch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($branch) 0 0 0
172 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
173 0 $git->config("branch.$branch.merge");
174 0 $git->config("branch.$branch.remote");
175 }
176 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
177 0 $self->log(
178 [ 'Setting up remote tracking for branch \'%s\'', $branch ] );
179  
180 0 $git->config( "branch.$branch.merge", "refs/heads/$branch" );
181 0 $git->config( "branch.$branch.remote", $self->remote );
182 0 };
183 }
184 }
185 }
186  
187 0 ▶sub _confirm {
Subroutine _confirm: not called CC=1 SCAR=6.9
188 0 my ($self) = @_;
189  
190 0 my $dist = $self->zilla->name;
191 0 my $prompt = "Shall I create a GitLab repository for $dist?";
192  
193 0 return $self->zilla->chrome->prompt_yn( $prompt, { default => 1 } );
194 }
195  
196 1 __PACKAGE__->meta->make_immutable;
197  
198 1 1;
199  
200 =pod
201  
202 =encoding UTF-8
203  
204 =head1 NAME
205  
206 Dist::Zilla::Plugin::GitLab::Create - Create a new GitLab repo on dzil new
207  
208 =head1 VERSION
209  
210 version 1.0002
211  
212 =head1 SYNOPSIS
213  
214 Configure git with your GitLab credentials:
215  
216 $ git config --global gitlab.user LoginName
217 $ git config --global gitlab.token AccessToken
218  
219 Alternatively you can install L<Config::Identity> and write your credentials
220 in the (optionally GPG-encrypted) C<~/.gitlab> file as follows:
221  
222 login LoginName
223 token AccessToken
224  
225 Set up an access token on GitLab, in your profile under "Personal Access Tokens." You
226 must grant the token the C<api> scope!
227  
228 then, in your F<profile.ini>:
229  
230 # default config
231 [GitLab::Create]
232  
233 # to override publicness
234 [GitLab::Create]
235 public = 0
236  
237 # use a template for the repository name
238 [GitLab::Create]
239 repo = {{ lc $dist->name }}
240  
241 See L</ATTRIBUTES> for more options.
242  
243 =head1 DESCRIPTION
244  
245 This Dist::Zilla plugin creates a new git repository on GitLab.com when
246 a new distribution is created with C<dzil new>.
247  
248 It will also add a new git remote pointing to the newly created GitLab
249 repository's private URL. See L</"ADDING REMOTE"> for more info.
250  
251 =head1 ATTRIBUTES
252  
253 =over
254  
255 =item C<issues>
256  
257 Enable issues for the new repository if this option is set to true (default).
258  
259 =item C<merge_requests>
260  
261 Enable merge requests for the new repository if this option is set to true (default).
262  
263 =item C<namespace>
264  
265 Specifies the project namespace path in which to create the repository
266 (by default the repository is created in the user's account).
267  
268 =item C<packages>
269  
270 Enable packages for the new repository if this option is set to true (default).
271  
272 =item C<prompt>
273  
274 Prompt for confirmation before creating a GitLab repository if this option is
275 set to true (default is false).
276  
277 =item C<public>
278  
279 Create a public repository if this option is set to true (default), otherwise
280 create a private repository.
281  
282 =item C<remote>
283  
284 Specifies the git remote name to be added (default 'origin'). This will point to
285 the newly created GitLab repository's private URL. See L</"ADDING REMOTE"> for
286 more info.
287  
288 =item C<repo>
289  
290 Specifies the name of the GitLab repository to be created (by default the name
291 of the dist is used). This can be a template, so something like the following
292 will work:
293  
294 repo = {{ lc $dist->name }}
295  
296 =item C<snippets>
297  
298 Enable snippets for the new repository if this option is set to true (default).
299  
300 =item C<wiki>
301  
302 Enable the wiki for the new repository if this option is set to true (default).
303  
304 =back
305  
306 =head1 ADDING REMOTE
307  
308 By default C<GitLab::Create> adds a new git remote pointing to the newly created
309 GitLab repository's private URL B<if, and only if,> a git repository has already
310 been initialized, and if the remote doesn't already exist in that repository.
311  
312 To take full advantage of this feature you should use, along with C<GitLab::Create>,
313 the L<Dist::Zilla::Plugin::Git::Init> plugin, leaving blank its C<remote> option,
314 as follows:
315  
316 [Git::Init]
317 ; here goes your Git::Init config, remember
318 ; to not set the 'remote' option
319 [GitLab::Create]
320  
321 You may set your preferred remote name, by setting the C<remote> option of the
322 C<GitLab::Create> plugin, as follows:
323  
324 [Git::Init]
325 [GitLab::Create]
326 remote = myremote
327  
328 Remember to put C<[Git::Init]> B<before> C<[GitLab::Create]>.
329  
330 After the new remote is added, the current branch will track it, unless remote
331 tracking for the branch was already set. This may allow one to use the
332 L<Dist::Zilla::Plugin::Git::Push> plugin without the need to do a C<git push>
333 between the C<dzil new> and C<dzil release>. Note though that this will work
334 only when the C<push.default> Git configuration option is set to either
335 C<upstream> or C<simple> (which will be the default in Git 2.0). If you are
336 using an older Git or don't want to change your config, you may want to have a
337 look at L<Dist::Zilla::Plugin::Git::PushInitial>.
338  
339 =head1 AUTHOR
340  
341 D Ruth Holloway <ruth@hiruthie.me>
342  
343 =head1 COPYRIGHT AND LICENSE
344  
345 This software is copyright (c) 2022 by D Ruth Holloway.
346  
347 This is free software; you can redistribute it and/or modify it under
348 the same terms as the Perl 5 programming language system itself.
349  
350 =cut
351  
352 __END__
« Plugin/GitLab/Meta.pm ↑ Summary Plugin/GitLab.pm »