File Coverage

blib/lib/App/GitHub/update.pm
Criterion Covered Total %
statement 17 77 22.0
branch 0 24 0.0
condition 0 14 0.0
subroutine 7 11 63.6
pod 0 4 0.0
total 24 130 18.4


line stmt bran cond sub pod time code
1             package App::GitHub::update;
2             BEGIN {
3 1     1   93949 $App::GitHub::update::VERSION = '0.0011';
4             }
5             # ABSTRACT: Update a github repository (description, homepage, etc.) from the commandline
6              
7              
8 1     1   10 use strict;
  1         3  
  1         33  
9 1     1   6 use warnings;
  1         1  
  1         30  
10              
11 1     1   898 use Config::Identity::GitHub;
  1         73383  
  1         11  
12 1     1   1047 use LWP::UserAgent;
  1         53140  
  1         12  
13 1     1   1100 use Getopt::Long qw/ GetOptions /;
  1         10185  
  1         5  
14             my $agent = LWP::UserAgent->new;
15              
16             sub update {
17 0     0 0   my $self = shift;
18 0           my %given = @_;
19 0           my ( $login, $token, $repository, $description, $homepage );
20              
21 0           ( $repository, $description, $homepage ) = @given{qw/ repository description homepage /};
22 0   0       defined $_ && length $_ or die "Missing repository\n" for $repository;
      0        
23              
24 0           ( $login, $token ) = @given{qw/ login token /};
25 0 0 0       unless( defined $token && length $token ) {
26 0           my %identity = Config::Identity::GitHub->load;
27 0           ( $login, $token ) = @identity{qw/ login token /};
28             }
29              
30 0           my @arguments;
31 0 0         push @arguments, 'values[description]' => $description if defined $description;
32 0 0         push @arguments, 'values[homepage]' => $homepage if defined $homepage;
33              
34 0           my $uri = "https://github.com/api/v2/json/repos/show/$login/$repository";
35 0           my $response = $agent->post( $uri,
36             [ login => $login, token => $token, @arguments ] );
37              
38 0 0         unless ( $response->is_success ) {
39 0           die $response->status_line, "\n", $response->decoded_content, "\n";
40             }
41              
42 0           return $response;
43             }
44              
45             sub usage (;$) {
46 0     0 0   my $error = shift;
47 0           my $exit = 0;
48 0 0         if ( defined $error ) {
49 0 0         if ( $error ) {
50 0 0         if ( $error =~ m/^\-?\d+$/ ) { $exit = $error }
  0            
51             else {
52 0           chomp $error;
53 0           warn $error, "\n";
54 0           $exit = -1;
55             }
56             }
57             }
58 0           warn <<_END_;
59              
60             Usage: github-update [opt]
61              
62             --login ... Your github login
63             --token ... The github token associated with the given login
64              
65             Although required, if a login/token are not given,
66             github-create will attempt to load it from
67             \$HOME/.github or \$HOME/.github-identity (see
68             Config::Identity for more information)
69              
70             --repository ... The repository to update
71              
72             --description ... The new description of the repository
73             --homepage ... A homepage for the repository
74              
75             --help, -h, -? This help
76              
77              
78             _END_
79              
80             # --dzpl Guess repository and description from Dist::Dzpl
81             # configuration (name and abstract, respectively)
82              
83 0           exit $exit;
84             }
85              
86             sub guess_dzpl {
87 0     0 0   my $self = shift;
88 0           my %guess;
89              
90 0           eval {
91             # Oh god this is hacky
92             package App::GitHub::update::Sandbox;
93             BEGIN {
94 1     1   901 $App::GitHub::update::Sandbox::VERSION = '0.0011';
95             }
96 0           local @ARGV;
97 0           do './dzpl';
98 0           my $dzpl = $Dzpl::dzpl;
99 0           $dzpl = $Dzpl::dzpl;
100 0           $dzpl->zilla->_setup_default_plugins;
101 0           $_->gather_files for ( @{ $dzpl->zilla->plugins_with(-FileGatherer) } );
  0            
102 0           $guess{repository} = $dzpl->zilla->name;
103 0           $guess{description} = $dzpl->zilla->abstract;
104             };
105 0 0         die $@ if $@;
106              
107 0           return %guess;
108             }
109              
110             sub run {
111 0     0 0   my $self = shift;
112 0           my @arguments = @_;
113              
114 0 0         usage 0 unless @arguments;
115              
116 0           my ( $login, $token, $repository, $dzpl, $help );
117 0           my ( $homepage, $description );
118             {
119 0           local @ARGV = @arguments;
  0            
120 0           GetOptions(
121             'help|h|?' => \$help,
122              
123             'login=s' => \$login,
124             'token=s' => \$token,
125             'repository=s' => \$repository,
126              
127             'dzpl' => \$dzpl,
128              
129             'description=s' => \$description,
130             'homepage=s' => \$homepage,
131             );
132             }
133              
134 0 0         usage 0 if $help;
135              
136 0 0         if ( $dzpl ) {
137 0           my %guess = $self->guess_dzpl;
138 0   0       $repository ||= $guess{repository};
139 0   0       $description ||= $guess{description};
140             }
141            
142 0           eval {
143 0           my $response = $self->update(
144             login => $login, token => $token, repository => $repository,
145             description => $description, homepage => $homepage,
146             );
147              
148 0           print $response->as_string, "\n";
149             };
150 0 0         if ($@) {
151 0           usage <<_END_;
152             github-update: $@
153             _END_
154             }
155             }
156              
157             1;
158              
159             __END__