File Coverage

blib/lib/Dist/Zilla/Plugin/GitHub/Update.pm
Criterion Covered Total %
statement 39 54 72.2
branch 4 18 22.2
condition 3 18 16.6
subroutine 7 7 100.0
pod 0 1 0.0
total 53 98 54.0


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::GitHub::Update;
2             # ABSTRACT: Update a GitHub repo's info on release
3 1     1   2440147 use strict;
  1         2  
  1         31  
4 1     1   5 use warnings;
  1         1  
  1         58  
5              
6             our $VERSION = '0.48';
7              
8 1     1   5 use JSON::MaybeXS;
  1         1  
  1         76  
9 1     1   33 use Moose;
  1         4  
  1         8  
10 1     1   5776 use List::Util 'first';
  1         2  
  1         670  
11              
12             extends 'Dist::Zilla::Plugin::GitHub';
13              
14             with 'Dist::Zilla::Role::AfterRelease';
15              
16             # deprecated and no longer documented. Use 'metacpan' instead!
17             has cpan => (
18             is => 'ro',
19             isa => 'Bool',
20             default => 0
21             );
22              
23             has p3rl => (
24             is => 'ro',
25             isa => 'Bool',
26             default => 0
27             );
28              
29             has metacpan => (
30             is => 'ro',
31             isa => 'Bool',
32             default => 1
33             );
34              
35             has meta_home => (
36             is => 'ro',
37             isa => 'Bool',
38             default => 0
39             );
40              
41             #pod =head1 SYNOPSIS
42             #pod
43             #pod Configure git with your GitHub credentials:
44             #pod
45             #pod $ git config --global github.user LoginName
46             #pod $ git config --global github.password GitHubPassword
47             #pod
48             #pod Alternatively you can install L<Config::Identity> and write your credentials
49             #pod in the (optionally GPG-encrypted) C<~/.github> file as follows:
50             #pod
51             #pod login LoginName
52             #pod password GitHubpassword
53             #pod
54             #pod (if only the login name is set, the password will be asked interactively).
55             #pod
56             #pod You can also generate an access token for "full control over repositories" by following
57             #pod L<these instructions|https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/>,
58             #pod
59             #pod then, in your F<dist.ini>:
60             #pod
61             #pod # default config
62             #pod [GitHub::Meta]
63             #pod
64             #pod # to override the repo name
65             #pod [GitHub::Meta]
66             #pod repo = SomeRepo
67             #pod
68             #pod See L</ATTRIBUTES> for more options.
69             #pod
70             #pod =head1 DESCRIPTION
71             #pod
72             #pod This Dist::Zilla plugin updates the information of the GitHub repository
73             #pod when C<dzil release> is run.
74             #pod
75             #pod =cut
76              
77             around dump_config => sub
78             {
79             my ($orig, $self) = @_;
80             my $config = $self->$orig;
81              
82             my $option = first { $self->$_ } qw(meta_home metacpan p3rl cpan);
83             if ($option eq 'cpan') {
84             $self->log->warn('the \'cpan\' option has been removed: please use \'metacpan\' instead');
85             $option = 'metacpan';
86             }
87              
88             $config->{+__PACKAGE__} = {
89             $option => ($self->$option ? 1 : 0),
90             };
91              
92             return $config;
93             };
94              
95             sub after_release {
96 1     1 0 126517 my $self = shift;
97 1         8 my ($opts) = @_;
98 1         182 my $dist_name = $self->zilla->name;
99              
100 1 50       97 return if (!$self->_has_credentials);
101              
102 1         39 my $repo_name = $self->_get_repo_name($self->_credentials->{login});
103 1 50       27 if (not $repo_name) {
104 0         0 $self->log('cannot update GitHub repository info');
105 0         0 return;
106             }
107              
108 1         16 $self->log("Updating GitHub repository info");
109              
110 1         309 my $params = {
111             name => ($repo_name =~ /\/(.*)$/)[0],
112             description => $self->zilla->abstract,
113             };
114              
115 1 50 33     109 if ($self->meta_home && (my $meta_home = $self->zilla->distmeta->{resources}{homepage})) {
    0          
    0          
116 1         77 $self->log("Using distmeta URL");
117 1         242 $params->{homepage} = $meta_home;
118             } elsif ($self->metacpan) {
119 0         0 $self->log("Using MetaCPAN URL");
120 0         0 $params->{homepage} = "https://metacpan.org/release/$dist_name/";
121             } elsif ($self->p3rl) {
122 0         0 $self->log("Using P3rl URL");
123 0         0 my $guess_name = $dist_name;
124 0         0 $guess_name =~ s/\-/\:\:/g;
125 0         0 $params->{homepage} = "https://p3rl.org/$guess_name";
126             }
127              
128 1         51 my $url = $self->api."/repos/$repo_name";
129              
130 1         9 my $current = $self->_current_params($url);
131 1 0 0     14 if ($current &&
      33        
      0        
      33        
      0        
      0        
132             ($current->{name} || '') eq $params->{name} &&
133             ($current->{description} || '') eq $params->{description} &&
134             ($current->{homepage} || '') eq $params->{homepage}) {
135              
136 0         0 $self->log("GitHub repo info is up to date");
137 0         0 return;
138             }
139              
140 1         8 $self->log_debug("Sending PATCH $url");
141 1         238 my $response = HTTP::Tiny->new->request('PATCH', $url, {
142             content => encode_json($params),
143             headers => $self->_auth_headers,
144             });
145              
146 1         19 my $repo = $self->_check_response($response);
147              
148 1 50       28 return if not $repo;
149              
150 0 0       0 if ($repo eq 'redo') {
151 0         0 $self->log("Retrying with two-factor authentication");
152 0         0 $self->prompt_2fa(1);
153 0         0 $repo = $self->after_release($opts);
154 0 0       0 return if not $repo;
155             }
156             }
157              
158             sub _current_params {
159 1     1   2 my $self = shift;
160 1         10 my ($url) = @_;
161              
162 1         24 my $http = HTTP::Tiny->new;
163              
164 1         252 $self->log_debug("Sending GET $url");
165 1         331 my $response = $http->request('GET', $url);
166              
167 1         39 return $self->_check_response($response);
168             }
169              
170             __PACKAGE__->meta->make_immutable;
171             1; # End of Dist::Zilla::Plugin::GitHub::Update
172              
173             __END__
174              
175             =pod
176              
177             =encoding UTF-8
178              
179             =head1 NAME
180              
181             Dist::Zilla::Plugin::GitHub::Update - Update a GitHub repo's info on release
182              
183             =head1 VERSION
184              
185             version 0.48
186              
187             =head1 SYNOPSIS
188              
189             Configure git with your GitHub credentials:
190              
191             $ git config --global github.user LoginName
192             $ git config --global github.password GitHubPassword
193              
194             Alternatively you can install L<Config::Identity> and write your credentials
195             in the (optionally GPG-encrypted) C<~/.github> file as follows:
196              
197             login LoginName
198             password GitHubpassword
199              
200             (if only the login name is set, the password will be asked interactively).
201              
202             You can also generate an access token for "full control over repositories" by following
203             L<these instructions|https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/>,
204              
205             then, in your F<dist.ini>:
206              
207             # default config
208             [GitHub::Meta]
209              
210             # to override the repo name
211             [GitHub::Meta]
212             repo = SomeRepo
213              
214             See L</ATTRIBUTES> for more options.
215              
216             =head1 DESCRIPTION
217              
218             This Dist::Zilla plugin updates the information of the GitHub repository
219             when C<dzil release> is run.
220              
221             =head1 ATTRIBUTES
222              
223             =over
224              
225             =item C<repo>
226              
227             The name of the GitHub repository. By default the name will be extracted from
228             the URL of the remote specified in the C<remote> option, and if that fails the
229             dist name (from dist.ini) is used. It can also be in the form C<user/repo>
230             when it belongs to another GitHub user/organization.
231              
232             =item C<remote>
233              
234             The name of the Git remote pointing to the GitHub repository (C<"origin"> by
235             default). This is used when trying to guess the repository name.
236              
237             =item C<p3rl>
238              
239             The GitHub homepage field will be set to the p3rl.org shortened URL
240             (e.g. C<https://p3rl.org/Dist::Zilla::Plugin::GitHub>) if this option is set to true (default is
241             false).
242              
243             =item C<metacpan>
244              
245             The GitHub homepage field will be set to the metacpan.org distribution URL
246             (e.g. C<https://metacpan.org/release/Dist-Zilla-Plugin-GitHub>) if this option is set to true
247             (default).
248              
249             This takes precedence over the C<p3rl> options (if both are
250             true, metacpan will be used).
251              
252             =item C<meta_home>
253              
254             The GitHub homepage field will be set to the value present in the dist meta
255             (e.g. the one set by other plugins) if this option is set to true (default is
256             false). If no value is present in the dist meta, this option is ignored.
257              
258             This takes precedence over the C<metacpan> and C<p3rl> options (if all
259             three are true, meta_home will be used).
260              
261             =item C<prompt_2fa>
262              
263             Prompt for GitHub two-factor authentication code if this option is set to true
264             (default is false). If this option is set to false but GitHub requires 2fa for
265             the login, it'll be automatically enabled.
266              
267             =back
268              
269             =head1 SUPPORT
270              
271             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Dist-Zilla-Plugin-GitHub>
272             (or L<bug-Dist-Zilla-Plugin-GitHub@rt.cpan.org|mailto:bug-Dist-Zilla-Plugin-GitHub@rt.cpan.org>).
273              
274             There is also a mailing list available for users of this distribution, at
275             L<http://dzil.org/#mailing-list>.
276              
277             There is also an irc channel available for users of this distribution, at
278             L<C<#distzilla> on C<irc.perl.org>|irc://irc.perl.org/#distzilla>.
279              
280             =head1 AUTHOR
281              
282             Alessandro Ghedini <alexbio@cpan.org>
283              
284             =head1 COPYRIGHT AND LICENSE
285              
286             This software is copyright (c) 2011 by Alessandro Ghedini.
287              
288             This is free software; you can redistribute it and/or modify it under
289             the same terms as the Perl 5 programming language system itself.
290              
291             =cut