File Coverage

blib/lib/Module/Setup/Plugin/Site/GitHub.pm
Criterion Covered Total %
statement 54 62 87.1
branch 16 30 53.3
condition 4 7 57.1
subroutine 8 9 88.8
pod 2 3 66.6
total 84 111 75.6


line stmt bran cond sub pod time code
1             package Module::Setup::Plugin::Site::GitHub;
2 2     2   2649 use strict;
  2         6  
  2         80  
3 2     2   12 use warnings;
  2         4  
  2         71  
4 2     2   11 use base 'Module::Setup::Plugin';
  2         5  
  2         717  
5 2     2   14 use JSON;
  2         5  
  2         21  
6 2     2   1462 use LWP::UserAgent;
  2         51599  
  2         1670  
7              
8             sub register {
9 3     3 0 7 my($self, ) = @_;
10 3         24 $self->add_trigger( before_dump_config => \&before_dump_config );
11 3         171 $self->add_trigger( finalize_create_skeleton => \&finalize_create_skeleton );
12             }
13              
14             sub before_dump_config {
15 1     1 1 14 my($self, $config) = @_;
16              
17 1         6 my %modules = (
18             readme_from => 'Module::Install::ReadmeFromPod',
19             readme_markdown_from => 'Module::Install::ReadmeMarkdownFromPod',
20             readme_pod_from => 'Module::Install::ReadmePodFromPod',
21             githubmeta => 'Module::Install::GithubMeta',
22             );
23              
24 1         6 while (my($name, $module) = each %modules) {
25 4   50     456 my $version = eval "require $module; 'installed '.\$$module\::VERSION;" || 'not installed';
26 4         39 $config->{$name} = 0;
27 4 50       41 if ($self->dialog("use $name? (depend $module, $version [Yn] ", 'y') =~ /[Yy]/) {
28 0         0 $config->{$name} = 1;
29             }
30             }
31              
32 1   50     23 $config->{github_format} ||= 'p5-%s';
33 1         6 $config->{github_format} = $self->dialog("github repository name format: ", $config->{github_format});
34             }
35              
36             # run github developer api
37             sub finalize_create_skeleton {
38 2     2 1 116 my $self = shift;
39 2         14 my $user = $self->shell('git config --get github.user');
40 2         35 chomp $user;
41 2         43 my $token = $self->shell('git config --get github.token');
42 2         34 chomp $token;
43 2 100 66     65 unless ($user && $token) {
44 1         33 $self->log("set the github.token And github.user for git config if you wants the create github repository.");
45 1         22 return;
46             }
47              
48 1 50       8 if ($self->dialog("create GitHub repository? [Yn] ", 'y') =~ /[Yy]/) {
49             # create repository
50 1         26 my $name = sprintf $self->config->{github_format}, $self->distribute->dist_name;
51 1         4 $name = $self->dialog("github repository name: ", $name);
52              
53 1         13 my $description = 'Perl Module of ' . $self->distribute->module;
54 1         5 $description = $self->dialog("github repository description: ", $description);
55              
56 1         10 my $homepage = '';
57 1         4 $homepage = $self->dialog("github repository homepage: ", $homepage);
58              
59 1         7 my $public = 1;
60 1 50       4 if ($self->dialog("create private repository? [yN] ", 'n') =~ /[Yy]/) {
61 0         0 $public = 0;
62             }
63              
64 1 50       15 unless (_create_repository(
65             login => $user,
66             token => $token,
67             name => $name,
68             description => $description,
69             homepage => $homepage,
70             public => $public,
71             )) {
72 0         0 $self->log('can not created on GitHub');
73 0         0 return;
74             }
75              
76 1 50       12 !$self->system('perl', 'Makefile.PL') or die $?;
77 1 50       13 !$self->system('make', 'test') or die $?;
78 1 50       12 !$self->system('make', 'distclean') or die $?;
79 1 50       41 unless (-d '.git') {
80 1 50       5 !$self->system('git', 'init') or die $?;
81 1 50       11 !$self->system('git', 'add', '.') or die $?;
82 1 50       12 !$self->system('git', 'commit', '-m', 'initial commit') or die $?;
83             }
84 1 50       18 !$self->system('git', 'remote', 'add', 'origin', "git\@github.com:${user}/${name}.git") or die $?;
85              
86 1 50       85 if ($self->dialog("try git push to GitHub? [Yn] ", 'y') =~ /[Yy]/) {
87 1 50       23 !$self->system('git', 'push', 'origin', 'master') or die $?;
88             }
89             }
90             }
91              
92             sub _create_repository {
93 0     0     my %args = @_;
94 0           my $ua = LWP::UserAgent->new(
95             agent => join('/', __PACKAGE__, $Module::Setup::VERSION),
96             cookie_jar => +{},
97             );
98 0           my $res = $ua->post(
99             'https://github.com/api/v2/json/repos/create' => \%args
100             );
101 0           $res->is_success;
102             }
103              
104             1;