File Coverage

blib/lib/Net/GitHub/V3/Repos.pm
Criterion Covered Total %
statement 18 115 15.6
branch 0 40 0.0
condition 0 10 0.0
subroutine 6 18 33.3
pod 10 12 83.3
total 34 195 17.4


line stmt bran cond sub pod time code
1             package Net::GitHub::V3::Repos;
2              
3 1     1   3 use Moo;
  1         14  
  1         4  
4              
5             our $VERSION = '0.64';
6             our $AUTHORITY = 'cpan:FAYLAND';
7              
8 1     1   221 use Carp;
  1         1  
  1         50  
9 1     1   4 use URI::Escape;
  1         1  
  1         45  
10 1     1   4 use URI;
  1         1  
  1         16  
11 1     1   453 use HTTP::Request::Common qw(POST);
  1         1589  
  1         1110  
12              
13             with 'Net::GitHub::V3::Query';
14              
15             sub list {
16 0     0 1   my ( $self, $args ) = @_;
17              
18             # for old
19 0 0         unless (ref($args) eq 'HASH') {
20 0           $args = { type => $args };
21             }
22              
23 0           my $uri = URI->new('/user/repos');
24 0           $uri->query_form($args);
25 0           return $self->query($uri->as_string);
26             }
27              
28             sub list_all {
29 0     0 1   my ( $self, $since ) = @_;
30 0   0       $since ||= 'first';
31 0           my $u = '/repositories';
32 0 0         $u .= '?since=' . $since if $since ne 'first';
33 0           return $self->query($u);
34             }
35              
36             sub list_user {
37 0     0 1   my ($self, $user, $args) = @_;
38 0   0       $user ||= $self->u;
39              
40             # for old
41 0 0         unless (ref($args) eq 'HASH') {
42 0           $args = { type => $args };
43             }
44              
45 0           my $uri = URI->new("/users/" . uri_escape($user) . "/repos");
46 0           $uri->query_form($args);
47 0           return $self->query($uri->as_string);
48             }
49              
50             sub list_org {
51 0     0 1   my ($self, $org, $type) = @_;
52 0   0       $type ||= 'all';
53 0           my $u = "/orgs/" . uri_escape($org) . "/repos";
54 0 0         $u .= '?type=' . $type if $type ne 'all';
55 0           return $self->query($u);
56             }
57              
58             sub create {
59 0     0 1   my ( $self, $data ) = @_;
60              
61 0           my $u = '/user/repos';
62 0 0         if (exists $data->{org}) {
63 0           my $o = delete $data->{org};
64 0           $u = "/orgs/" . uri_escape($o) . "/repos";
65             }
66              
67 0           return $self->query('POST', $u, $data);
68             }
69              
70             sub upload_asset {
71 0     0 1   my $self = shift;
72 0 0         unshift @_, $self->u, $self->repo if @_ < 5;
73 0           my ($user, $repos, $release_id, $name, $content_type, $file_content) = @_;
74              
75 0           my $ua = $self->ua;
76 0           my $url = $self->upload_url . "/repos/$user/$repos/releases/$release_id/assets?name=" . uri_escape($name);
77 0           my $req = HTTP::Request->new( 'POST', $url );
78 0           $req->accept_decodable;
79 0           $req->content($file_content);
80 0           $req->header( 'Content-Type', $content_type );
81              
82 0           my $res = $ua->request($req);
83              
84 0           my $data;
85 0 0 0       if ($res->header('Content-Type') and $res->header('Content-Type') =~ 'application/json') {
86 0           my $json = $res->decoded_content;
87 0           $data = eval { $self->json->decode($json) };
  0            
88 0 0         unless ($data) {
89             # We tolerate bad JSON for errors,
90             # otherwise we just rethrow the JSON parsing problem.
91 0 0         die unless $res->is_error;
92 0           $data = { message => $res->message };
93             }
94             } else {
95 0           $data = { message => $res->message };
96             }
97              
98 0 0         return wantarray ? %$data : $data;
99             }
100              
101             sub commits {
102 0     0 1   my $self = shift;
103 0 0         if (@_ < 2) {
104 0           unshift @_, $self->repo;
105 0           unshift @_, $self->u;
106             }
107 0           my ($user, $repos, $args) = @_;
108              
109 0           my $uri = URI->new("/repos/" . uri_escape($user) . "/" . uri_escape($repos) . '/commits');
110 0           $uri->query_form($args);
111 0           return $self->query($uri->as_string);
112             }
113              
114             sub list_deployments {
115 0     0 1   my $self = shift;
116 0 0         if (@_ < 2) {
117 0           unshift @_, $self->repo;
118 0           unshift @_, $self->u;
119             }
120 0           my ($user, $repos, $args) = @_;
121              
122 0           my $uri = URI->new("/repos/" . uri_escape($user) . "/" . uri_escape($repos) . '/deployments');
123 0           $uri->query_form($args);
124 0           return $self->query($uri->as_string);
125             }
126              
127             ## build methods on fly
128             my %__methods = (
129              
130             get => { url => "/repos/%s/%s" },
131             update => { url => "/repos/%s/%s", method => 'PATCH', args => 1 },
132             contributors => { url => "/repos/%s/%s/contributors" },
133             languages => { url => "/repos/%s/%s/languages" },
134             teams => { url => "/repos/%s/%s/teams" },
135             tags => { url => "/repos/%s/%s/tags" },
136             branches => { url => "/repos/%s/%s/branches" },
137             branch => { url => "/repos/%s/%s/branches/%s" },
138             delete => { url => "/repos/%s/%s", method => 'DELETE', check_status => 204 },
139              
140             # http://developer.github.com/v3/repos/collaborators/
141             collaborators => { url => "/repos/%s/%s/collaborators" },
142             is_collaborator => { url => "/repos/%s/%s/collaborators/%s", check_status => 204 },
143             add_collaborator => { url => "/repos/%s/%s/collaborators/%s", method => 'PUT', check_status => 204 },
144             delete_collaborator => { url => "/repos/%s/%s/collaborators/%s", method => 'DELETE', check_status => 204 },
145              
146             # http://developer.github.com/v3/repos/commits/
147             commit => { url => "/repos/%s/%s/commits/%s" },
148             comments => { url => "/repos/%s/%s/comments" },
149             comment => { url => "/repos/%s/%s/comments/%s" },
150             commit_comments => { url => "/repos/%s/%s/commits/%s/comments" },
151             create_comment => { url => "/repos/%s/%s/commits/%s/comments", method => 'POST', args => 1 },
152             update_comment => { url => "/repos/%s/%s/comments/%s", method => 'PATCH', args => 1 },
153             delete_comment => { url => "/repos/%s/%s/comments/%s", method => 'DELETE', check_status => 204 },
154             compare_commits => { url => "/repos/%s/%s/compare/%s...%s" },
155              
156             # http://developer.github.com/v3/repos/contents/
157             readme => { url => "/repos/%s/%s/readme" },
158             get_content => { url => "/repos/%s/%s/contents/%s" },
159              
160             # http://developer.github.com/v3/repos/downloads/
161             downloads => { url => "/repos/%s/%s/downloads" },
162             download => { url => "/repos/%s/%s/downloads/%s" },
163             delete_download => { url => "/repos/%s/%s/downloads/%s", method => 'DELETE', check_status => 204 },
164              
165             # http://developer.github.com/v3/repos/releases/
166             releases => { url => "/repos/%s/%s/releases" },
167             release => { url => "/repos/%s/%s/releases/%s" },
168             create_release => { url => "/repos/%s/%s/releases", method => 'POST', args => 1 },
169             update_release => { url => "/repos/%s/%s/releases/%s", method => 'PATCH', args => 1 },
170             delete_release => { url => "/repos/%s/%s/releases/%s", method => 'DELETE', check_status => 204 },
171              
172             release_assets => { url => "/repos/%s/%s/releases/%s/assets" },
173             release_asset => { url => "/repos/%s/%s/releases/%s/assets/%s" },
174             update_release_asset => { url => "/repos/%s/%s/releases/%s/assets/%s", method => 'PATCH', args => 1 },
175             delete_release_asset => { url => "/repos/%s/%s/releases/%s/assets/%s", method => 'DELETE', check_status => 204 },
176              
177             forks => { url => "/repos/%s/%s/forks" },
178              
179             # http://developer.github.com/v3/repos/keys/
180             keys => { url => "/repos/%s/%s/keys" },
181             key => { url => "/repos/%s/%s/keys/%s" },
182             create_key => { url => "/repos/%s/%s/keys", method => 'POST', args => 1 },
183             update_key => { url => "/repos/%s/%s/keys/%s", method => 'PATCH', check_status => 204, args => 1 },
184             delete_key => { url => "/repos/%s/%s/keys/%s", method => 'DELETE', check_status => 204 },
185              
186             # http://developer.github.com/v3/repos/watching/
187             watchers => { url => "/repos/%s/%s/watchers" },
188             is_watching => { url => "/user/watched/%s/%s", is_u_repo => 1, check_status => 204 },
189             watch => { url => "/user/watched/%s/%s", is_u_repo => 1, method => 'PUT', check_status => 204 },
190             unwatch => { url => "/user/watched/%s/%s", is_u_repo => 1, method => 'DELETE', check_status => 204 },
191              
192             subscribers => { url => "/repos/%s/%s/subscribers" },
193             subscription => { url => "/repos/%s/%s/subscription" },
194             is_subscribed => { url => "/repos/%s/%s/subscription", check_status => 200 },
195             subscribe => { url => "/repos/%s/%s/subscription", method => 'PUT',
196             check_status => 200, args => 1 },
197             unsubscribe => { url => "/repos/%s/%s/subscription", method => 'DELETE', check_status => 204 },
198              
199             # http://developer.github.com/v3/repos/hooks/
200             hooks => { url => "/repos/%s/%s/hooks" },
201             hook => { url => "/repos/%s/%s/hooks/%s" },
202             delete_hook => { url => "/repos/%s/%s/hooks/%s", method => 'DELETE', check_status => 204 },
203             test_hook => { url => "/repos/%s/%s/hooks/%s/test", method => 'POST', check_status => 204 },
204             create_hook => { url => "/repos/%s/%s/hooks", method => 'POST', args => 1 },
205             update_hook => { url => "/repos/%s/%s/hooks/%s", method => 'PATCH', args => 1 },
206              
207             # http://developer.github.com/v3/repos/merging/
208             merges => { url => "/repos/%s/%s/merges", method => 'POST', args => 1 },
209              
210             # http://developer.github.com/v3/repos/statuses/
211             list_statuses => { url => "/repos/%s/%s/statuses/%s" },
212             create_status => { url => "/repos/%s/%s/statuses/%s", method => 'POST', args => 1 },
213              
214             # https://developer.github.com/v3/repos/deployments
215             create_deployment => { url => "/repos/%s/%s/deployments", method => 'POST', args => 1 },
216             create_deployment_status => { url => "/repos/%s/%s/deployments/%s/statuses", method => 'POST', args => 1 },
217             list_deployment_statuses => { url => "/repos/%s/%s/deployments/%s/statuses", method => 'GET'},
218              
219             contributor_stats => { url => "/repos/%s/%s/stats/contributors", method => 'GET'},
220             commit_activity => { url => "/repos/%s/%s/stats/commit_activity", method => 'GET'},
221             code_frequency => { url => "/repos/%s/%s/stats/code_frequency", method => 'GET'},
222             participation => { url => "/repos/%s/%s/stats/participation", method => 'GET'},
223             punch_card => { url => "/repos/%s/%s/stats/punch_card", method => 'GET'},
224              
225             );
226             __build_methods(__PACKAGE__, %__methods);
227              
228             sub create_download {
229 0     0 0   my $self = shift;
230              
231 0 0         if (@_ == 1) {
232 0           unshift @_, $self->repo;
233 0           unshift @_, $self->u;
234             }
235 0           my ($user, $repos, $download) = @_;
236              
237 0           my $file = delete $download->{file};
238              
239 0           my $u = "/repos/" . uri_escape($user) . "/" . uri_escape($repos) . '/downloads';
240 0           my $d = $self->query('POST', $u, $download);
241 0 0         if (defined $file) {
242 0           return $self->upload_download($d, $file);
243             } else {
244 0           return $d;
245             }
246             }
247              
248             sub upload_download {
249 0     0 0   my $self = shift;
250              
251 0 0         if (@_ < 3) {
252 0           unshift @_, $self->repo;
253 0           unshift @_, $self->u;
254             }
255 0           my ($user, $repos, $download, $file) = @_;
256              
257             # must successful on create_download
258 0 0         return 0 unless exists $download->{s3_url};
259              
260             ## POST form-data
261             my %data = (
262             Content_Type => 'form-data',
263             Content => [
264             'key' => $download->{path},
265             'acl' => $download->{acl},
266             'success_action_status' => 201,
267             'Filename' => $download->{name},
268             'AWSAccessKeyId' => $download->{accesskeyid},
269             'Policy' => $download->{policy},
270             'Signature' => $download->{signature},
271             'Content-Type' => $download->{mime_type},
272 0           'file' => [ $file ],
273             ],
274             );
275 0           my $request = POST $download->{s3_url}, %data;
276 0           my $res = $self->ua->request($request);
277 0 0         return $res->code == 201 ? 1 : 0;
278             }
279              
280             ## http://developer.github.com/v3/repos/forks/
281             sub create_fork {
282 0     0 1   my $self = shift;
283              
284 0 0         if (@_ < 2) {
285 0           unshift @_, $self->repo;
286 0           unshift @_, $self->u;
287             }
288 0           my ($user, $repos, $org) = @_;
289              
290 0           my $u = "/repos/" . uri_escape($user) . "/" . uri_escape($repos) . '/forks';
291 0 0         $u .= '?org=' . $org if defined $org;
292 0           return $self->query('POST', $u);
293             }
294              
295             ## http://developer.github.com/v3/repos/watching/
296             sub watched {
297 0     0 1   my ($self, $user) = @_;
298              
299 0 0         my $u = $user ? '/users/' . uri_escape($user). '/watched' : '/user/watched';
300 0           return $self->query($u);
301             }
302              
303 1     1   5 no Moo;
  1         2  
  1         5  
304              
305             1;
306             __END__