File Coverage

lib/App/gh/API.pm
Criterion Covered Total %
statement 27 129 20.9
branch 0 32 0.0
condition 0 26 0.0
subroutine 9 28 32.1
pod 9 19 47.3
total 45 234 19.2


line stmt bran cond sub pod time code
1             package App::gh::API;
2 2     2   11 use warnings;
  2         3  
  2         73  
3 2     2   9 use strict;
  2         4  
  2         50  
4 2     2   10 use Carp ();
  2         4  
  2         31  
5 2     2   2084 use LWP::UserAgent;
  2         124897  
  2         75  
6 2     2   34 use URI;
  2         4  
  2         46  
7 2     2   1320 use JSON;
  2         18774  
  2         18  
8 2     2   1768 use App::gh::Utils;
  2         9  
  2         172  
9 2     2   2143 use Try::Tiny;
  2         6275  
  2         123  
10 2     2   1658 use Net::GitHub;
  2         507749  
  2         3446  
11              
12             sub new_ua {
13 0     0 0   my $class = shift;
14 0           my $ua = LWP::UserAgent->new; # TODO: make this switchable. better client ? Furl ?
15 0           $ua->timeout(10);
16 0           $ua->env_proxy;
17 0           return $ua;
18             }
19              
20             sub request {
21 0     0 0   my ( $class, $verb, $query , %args ) = @_;
22 0           die('v2 API is deprecated from github');
23             }
24              
25             sub search {
26 0     0 1   my ( $class, $query, %args ) = @_;
27 0           return $class->request(GET => qq{repos/search/$query}, %args);
28             }
29              
30             sub fork {
31 0     0 1   my ( $class, $user, $repo) = @_;
32 0 0         Carp::croak("Missing mandatory parameter: user") unless defined $user;
33 0           my $gh_id = App::gh->config->github_id;
34 0           my $gh_password = App::gh->config->github_password;
35              
36 0           my $gh = Net::GitHub->new(
37             version => 3,
38             login => App::gh->config->github_id,
39             pass => App::gh->config->github_password,
40             );
41              
42 0           my $repos = $gh->repos;
43 0           $repos->set_default_user_repo( $user, $repo );
44              
45 0           return $repos->create_fork;
46             }
47              
48             sub repo_network {
49 0     0 1   my ( $class, $user, $repo ) = @_;
50 0           my $ret = $class->request(GET => qq(repos/show/$user/$repo/network));
51 0           return $ret->{network};
52             }
53              
54             sub repo_info {
55 0     0 1   my ( $class, $user, $repo ) = @_;
56 0           my $ret = $class->request(GET => qq{repos/show/$user/$repo});
57 0 0         return $ret->{repository} if $ret;
58             }
59              
60             sub repo_create {
61 0     0 1   my ($class,%args) = @_;
62 0           my $ret = $class->request(POST => qq{repos/create} , %args);
63 0 0         return $ret->{repository} if $ret;
64             }
65              
66             sub user_info {
67 0     0 1   my ($class,$user,$page) = @_;
68 0   0       $page ||= 1;
69 0           my $ret = $class->request(GET => qq{repos/show/$user?page=$page});
70 0 0         return $ret if $ret;
71             }
72              
73             sub user_repos {
74 0     0 0   my ($class,$user) = @_;
75 0           my @repos;
76 0           my $page = 1;
77 0           while (1) {
78 0           my $ret = $class->user_info( $user, $page++ );
79 0 0         last unless @{$ret->{repositories}};
  0            
80 0           push @repos, @{$ret->{repositories}};
  0            
81             }
82 0           return \@repos;
83             }
84              
85             # Added by RCT
86             sub repo_set_public {
87 0     0 1   my ( $class, $user, $repo, $public ) = @_;
88 0 0         my $visibility = $public ? "public" : "private";
89 0           my $ret = $class->request(POST => qq{repos/set/$visibility/$user/$repo});
90 0           return $ret;
91             }
92              
93             sub repo_set_info {
94 0     0 1   my ( $class, $user, $repo, %args ) = @_;
95 0 0         if (exists $args{public}) {
96 0           $class->repo_set_public( $user, $repo, $args{public} );
97 0           delete $args{public};
98             }
99             # Keys must be in the form 'values[key]'
100 0           %args = map { ("values[$_]" => $args{$_}) } (keys %args);
  0            
101 0           my $ret = $class->request(POST => qq{repos/show/$user/$repo} , %args);
102 0           return $ret->{repository};
103             }
104              
105             sub pullreq_send {
106 0     0 0   my ( $class, $user, $repo, $local_branch, $remote_branch, $title, $body) = @_;
107 0           my $gh_id = App::gh->config->github_id;
108 0           my $gh_token = App::gh->config->github_token;
109 0 0 0       unless( $gh_id && $gh_token ) {
110 0           die "Github authtoken not found. Can not send pull request.\n";
111             }
112 0           return $class->request(POST => sprintf("pulls/%s/%s?login=%s&token=%s", $user, $repo, $gh_id, $gh_token),
113             'pull[base]' => $remote_branch,
114             'pull[head]' => "$gh_id:$local_branch",
115             'pull[title]' => $title,
116             'pull[body]' => $body,
117             );
118             }
119              
120             sub pullreq_list {
121 0     0 1   my ( $class, $user, $repo) = @_;
122 0           my $gh_id = App::gh->config->github_id;
123 0           my $gh_token = App::gh->config->github_token;
124 0 0 0       unless( $gh_id && $gh_token ) {
125 0           die "Github authtoken not found. Can not get pull requests.\n";
126             }
127 0           return $class->request(GET => sprintf("pulls/%s/%s?login=%s&token=%s", $user, $repo, $gh_id, $gh_token));
128             }
129              
130             sub pullreq_get {
131 0     0 0   my ( $class, $user, $repo, $number) = @_;
132 0           my $gh_id = App::gh->config->github_id;
133 0           my $gh_token = App::gh->config->github_token;
134 0 0 0       unless( $gh_id && $gh_token ) {
135 0           die "Github authtoken not found. Can not get pull request.\n";
136             }
137 0           return $class->request(GET => sprintf("pulls/%s/%s/%s?login=%s&token=%s", $user, $repo, scalar $number, $gh_id, $gh_token));
138             }
139              
140             sub issue_edit {
141 0     0 0   my ( $class, $user, $repo, $number, $title, $body) = @_;
142 0           my $gh_id = App::gh->config->github_id;
143 0           my $gh_token = App::gh->config->github_token;
144 0 0 0       unless( $gh_id && $gh_token ) {
145 0           die "Github authtoken not found. Can not edit issue.\n";
146             }
147 0 0         if ($number) {
148 0           return $class->request(POST => sprintf("issues/edit/%s/%s/%s?login=%s&token=%s", $user, $repo, $gh_id, $gh_token, $number), "$title\n$body");
149             } else {
150 0           return $class->request(POST => sprintf("issues/open/%s/%s?login=%s&token=%s", $user, $repo, $gh_id, $gh_token), "$title\n$body");
151             }
152             }
153              
154             sub issue_list {
155 0     0 0   my ( $class, $user, $repo) = @_;
156 0           my $gh_id = App::gh->config->github_id;
157 0           my $gh_token = App::gh->config->github_token;
158 0 0 0       unless( $gh_id && $gh_token ) {
159 0           die "Github authtoken not found. Can not get issues.\n";
160             }
161 0           return $class->request(GET => sprintf("issues/list/%s/%s/open?login=%s&token=%s", $user, $repo, $gh_id, $gh_token));
162             }
163              
164             sub issue_get {
165 0     0 0   my ( $class, $user, $repo, $number) = @_;
166 0           my $gh_id = App::gh->config->github_id;
167 0           my $gh_token = App::gh->config->github_token;
168 0 0 0       unless( $gh_id && $gh_token ) {
169 0           die "Github authtoken not found. Can not get issues.\n";
170             }
171 0           return $class->request(GET => sprintf("issues/show/%s/%s/%s?login=%s&token=%s", $user, $repo, scalar $number, $gh_id, $gh_token));
172             }
173              
174             sub issue_get_comments {
175 0     0 0   my ( $class, $user, $repo, $number) = @_;
176 0           my $gh_id = App::gh->config->github_id;
177 0           my $gh_token = App::gh->config->github_token;
178 0 0 0       unless( $gh_id && $gh_token ) {
179 0           die "Github authtoken not found. Can not get issue comments.\n";
180             }
181 0           return $class->request(GET => sprintf("issues/comments/%s/%s/%s?login=%s&token=%s", $user, $repo, scalar $number, $gh_id, $gh_token));
182             }
183              
184             sub issue_comment {
185 0     0 0   my ( $class, $user, $repo, $number, $body) = @_;
186 0           my $gh_id = App::gh->config->github_id;
187 0           my $gh_token = App::gh->config->github_token;
188 0 0 0       unless( $gh_id && $gh_token ) {
189 0           die "Github authtoken not found. Can not comment to issue.\n";
190             }
191 0           return $class->request(POST => sprintf("issues/comment/%s/%s/%s?login=%s&token=%s", $user, $repo, $number, $gh_id, $gh_token),
192             comment => $body,
193             );
194             }
195              
196             1;
197             __END__