File Coverage

blib/lib/Dist/Zilla/Plugin/GitLab/Update.pm
Criterion Covered Total %
statement 39 43 90.7
branch 4 8 50.0
condition 3 10 30.0
subroutine 8 8 100.0
pod 0 1 0.0
total 54 70 77.1


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::GitLab::Update 1.0002;
2              
3 1     1   2909403 use Modern::Perl;
  1         3  
  1         10  
4 1     1   158 use Carp;
  1         2  
  1         99  
5 1     1   9 use JSON::MaybeXS;
  1         3  
  1         71  
6 1     1   7 use Moose;
  1         3  
  1         6  
7 1     1   6686 use List::Util qw(first);
  1         3  
  1         65  
8 1     1   470 use URL::Encode qw(url_encode_utf8);
  1         5302  
  1         455  
9             extends 'Dist::Zilla::Plugin::GitLab';
10             with 'Dist::Zilla::Role::AfterRelease';
11              
12             sub after_release {
13 1     1 0 285206 my $self = shift;
14              
15 1 50       61 return if ( !$self->_has_credentials );
16              
17 1         44 my $repo_name = $self->_get_repo_name( $self->_credentials->{login} );
18 1 50       20 if ( not $repo_name ) {
19 0         0 $self->log('cannot update GitLab repository info');
20 0         0 return;
21             }
22              
23 1         69 my $params = {
24             name => ( $repo_name =~ /\/(.*)$/ )[0],
25             description => $self->zilla->abstract,
26             };
27              
28 1         107 $self->log('Updating GitLab repository info');
29              
30 1         415 my $url = $self->api . '/projects/' . url_encode_utf8($repo_name);
31              
32 1         56 my $current = $self->_current_params($url);
33 1 50 50     80 if ( $current
      33        
      0        
      33        
34             && ( $current->{name} || q{} ) eq $params->{name}
35             && ( $current->{description} || q{} ) eq $params->{description} ) {
36              
37 0         0 $self->log('GitLab repo info is up to date');
38 0         0 return;
39             }
40 1         16 my $headers = $self->_auth_headers;
41 1         91 $headers->{'content-type'} = 'application/json';
42              
43 1         20 $self->log_debug("Sending PUT $url");
44 1         307 my $response = HTTP::Tiny->new->request(
45             'PUT', $url,
46             {
47             content => encode_json($params),
48             headers => $headers,
49             }
50             );
51              
52 1         110 my $repo = $self->_check_response($response);
53              
54 1 50       29 return if not $repo;
55             }
56              
57             sub _current_params {
58 1     1   7 my $self = shift;
59 1         12 my ($url) = @_;
60              
61 1         32 my $http = HTTP::Tiny->new;
62              
63 1         264 $self->log_debug("Sending GET $url");
64 1         440 my $response = $http->request( 'GET', $url );
65              
66 1         52 return $self->_check_response($response);
67             }
68              
69             __PACKAGE__->meta->make_immutable;
70              
71             1;
72              
73             =pod
74              
75             =encoding UTF-8
76              
77             =head1 NAME
78              
79             Dist::Zilla::Plugin::GitLab::Update - Update a GitLab repo's info on release
80              
81             =head1 VERSION
82              
83             version 1.0002
84              
85             =head1 SYNOPSIS
86              
87             Configure git with your GitLab login name:
88              
89             $ git config --global gitlab.user LoginName
90             $ git config --global gitlab.token AccessToken
91              
92             Set up an access token on GitLab, in your profile under "Personal Access Tokens." You
93             must grant the token the C<api> scope!
94              
95             then, in your F<dist.ini>:
96              
97             # default config
98             [GitLab::Update]
99              
100             # to override the repo name
101             [GitLab::Update]
102             repo = SomeRepo
103              
104             See L</ATTRIBUTES> for more options.
105              
106             =head1 DESCRIPTION
107              
108             This Dist::Zilla plugin updates the information of the GitLab repository
109             when C<dzil release> is run.
110              
111             =head1 ATTRIBUTES
112              
113             =over
114              
115             =item C<remote>
116              
117             Specifies the git remote name to be used when guessing the repo name (default C<origin>).
118              
119             =item C<repo>
120              
121             The name of the GitLab repository. By default the name will be extracted from
122             the URL of the remote specified in the C<remote> option, and if that fails the
123             dist name (from dist.ini) is used. It can also be in the form C<user/repo>
124             when it belongs to another GitLab user/organization.
125              
126             =back
127              
128             =head1 AUTHOR
129              
130             D Ruth Holloway <ruth@hiruthie.me>
131              
132             =head1 COPYRIGHT AND LICENSE
133              
134             This software is copyright (c) 2022 by D Ruth Holloway.
135              
136             This is free software; you can redistribute it and/or modify it under
137             the same terms as the Perl 5 programming language system itself.
138              
139             =cut
140              
141             __END__
142              
143             # ABSTRACT: Update a GitLab repo's info on release
144