[6a2e5f1] | 1 | package utils; |
---|
| 2 | use strict; |
---|
| 3 | use warnings; |
---|
| 4 | use utf8; |
---|
| 5 | use Carp qw(croak); |
---|
| 6 | |
---|
| 7 | use LWP::UserAgent; |
---|
| 8 | use JSON; |
---|
| 9 | |
---|
| 10 | sub json_api { |
---|
| 11 | my $url = shift; |
---|
| 12 | |
---|
[49aca65] | 13 | my $ua = LWP::UserAgent->new; |
---|
| 14 | my $json = JSON->new->utf8->indent; |
---|
| 15 | |
---|
[6a2e5f1] | 16 | my $res = $ua->get( |
---|
| 17 | "https://api.github.com$url" |
---|
| 18 | ); |
---|
| 19 | |
---|
| 20 | $res->is_success or croak $res->status_line; |
---|
| 21 | |
---|
| 22 | return $json->decode($res->content); |
---|
| 23 | } |
---|
| 24 | |
---|
[fc647d5] | 25 | sub get { |
---|
| 26 | my $url = shift; |
---|
[55d0513] | 27 | my %parameters = @_; |
---|
| 28 | |
---|
| 29 | my $parameters = ''; |
---|
| 30 | while (my($key, $value) = each %parameters) { |
---|
| 31 | $parameters .= "&$key=$value"; |
---|
| 32 | } |
---|
[fc647d5] | 33 | |
---|
| 34 | my $page = 1; |
---|
[d51047d] | 35 | my $data = []; |
---|
[fc647d5] | 36 | while(1) { |
---|
[55d0513] | 37 | my $result = json_api("$url?per_page=100&page=$page$parameters"); |
---|
[fc647d5] | 38 | if (ref($result) eq 'ARRAY' && scalar @$result > 0) { |
---|
| 39 | push @$data, @$result; |
---|
| 40 | $page++; |
---|
| 41 | |
---|
| 42 | next; |
---|
| 43 | } |
---|
| 44 | last; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | return $data; |
---|
| 48 | } |
---|
[19233f5] | 49 | |
---|
[6a2e5f1] | 50 | package GitHubBackup; |
---|
| 51 | |
---|
| 52 | use strict; |
---|
| 53 | use warnings; |
---|
| 54 | use utf8; |
---|
| 55 | use Carp qw(croak); |
---|
[6f31bfb] | 56 | use File::Spec; |
---|
[6a2e5f1] | 57 | |
---|
| 58 | |
---|
| 59 | # both hash and hashref are acceptable |
---|
| 60 | sub new { |
---|
| 61 | my $class = shift; |
---|
| 62 | my $args = (ref $_[0] eq 'HASH') ? $_[0] : {@_}; |
---|
[19233f5] | 63 | |
---|
[6f31bfb] | 64 | return bless $args, $class; |
---|
[6a2e5f1] | 65 | } |
---|
| 66 | |
---|
[19df400] | 67 | sub account { |
---|
| 68 | my $self = shift; |
---|
| 69 | my $args = shift; |
---|
| 70 | |
---|
| 71 | if (defined $args) { |
---|
| 72 | $self->{repos} = undef; |
---|
| 73 | $self->{account} = $args; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | return $self->{account}; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | sub repository { |
---|
| 80 | my $self = shift; |
---|
| 81 | my $args = shift; |
---|
| 82 | |
---|
| 83 | if (defined $args) { |
---|
| 84 | $self->{repos} = undef; |
---|
| 85 | $self->{repository} = $args; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | return $self->{repository}; |
---|
| 89 | } |
---|
| 90 | |
---|
[6f31bfb] | 91 | sub directory { |
---|
| 92 | my $self = shift; |
---|
| 93 | my $args = shift; |
---|
| 94 | |
---|
| 95 | if (defined $args) { |
---|
| 96 | $self->{directory} = File::Spec->rel2abs($args); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | return $self->{directory}; |
---|
| 100 | } |
---|
| 101 | |
---|
[6a2e5f1] | 102 | sub repos { |
---|
| 103 | my $self = shift; |
---|
[19233f5] | 104 | return $self->{repos} if ($self->{repos}); |
---|
[6a2e5f1] | 105 | |
---|
[d51047d] | 106 | $self->{repos} = []; |
---|
| 107 | |
---|
[6a2e5f1] | 108 | my $account = $self->account or croak "account is not set"; |
---|
[c5b427c] | 109 | my $result; |
---|
[6a2e5f1] | 110 | if (my $repository = $self->repository) { |
---|
[c5b427c] | 111 | $result = [ utils::json_api("/repos/$account/$repository") ]; |
---|
| 112 | } |
---|
| 113 | else { |
---|
| 114 | $result = utils::get("/users/$account/repos"); |
---|
[6a2e5f1] | 115 | } |
---|
| 116 | |
---|
[fc647d5] | 117 | foreach my $repos (@$result) { |
---|
[6a2e5f1] | 118 | push @{$self->{repos}}, |
---|
| 119 | GitHubBackup::Repository->new({ |
---|
[b8d7413] | 120 | directory => sub {$self->directory}, |
---|
[c5b427c] | 121 | repos => $repos, |
---|
[6a2e5f1] | 122 | }) |
---|
| 123 | ; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | return $self->{repos}; |
---|
| 127 | } |
---|
| 128 | |
---|
[19df400] | 129 | sub backup { |
---|
| 130 | my $self = shift; |
---|
| 131 | |
---|
| 132 | foreach my $repos (@{$self->repos}) { |
---|
| 133 | $repos->backup; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | return $self; |
---|
| 137 | } |
---|
| 138 | |
---|
[6a2e5f1] | 139 | |
---|
| 140 | package GitHubBackup::Repository; |
---|
| 141 | |
---|
| 142 | use strict; |
---|
| 143 | use warnings; |
---|
| 144 | use utf8; |
---|
| 145 | use Carp qw(croak); |
---|
[19233f5] | 146 | use Git::Repository; |
---|
| 147 | use File::chdir; |
---|
[6f31bfb] | 148 | use File::Spec; |
---|
| 149 | use File::Path qw(mkpath); |
---|
[49aca65] | 150 | use LWP::UserAgent; |
---|
| 151 | use JSON; |
---|
| 152 | |
---|
[b8d7413] | 153 | |
---|
[19233f5] | 154 | sub new { |
---|
| 155 | my $class = shift; |
---|
| 156 | my $args = shift; |
---|
| 157 | |
---|
[6f31bfb] | 158 | return bless $args, $class; |
---|
[19233f5] | 159 | } |
---|
| 160 | |
---|
[c5b427c] | 161 | sub clone_url { |
---|
| 162 | return (shift)->{repos}{clone_url}; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | sub full_name { |
---|
| 166 | return (shift)->{repos}{full_name}; |
---|
| 167 | } |
---|
| 168 | |
---|
[f38b66d] | 169 | sub has_downloads { |
---|
| 170 | return (shift)->{repos}{has_downloads}; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | sub forks_count { |
---|
| 174 | return (shift)->{repos}{forks_count}; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | sub has_wiki { |
---|
| 178 | return (shift)->{repos}{has_wiki}; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | sub has_issues { |
---|
| 182 | return (shift)->{repos}{has_issues}; |
---|
| 183 | } |
---|
| 184 | |
---|
[b8d7413] | 185 | sub directory { |
---|
| 186 | my $self = shift; |
---|
| 187 | |
---|
[c5b427c] | 188 | my $path = $self->full_name; |
---|
[6f31bfb] | 189 | if (my $base = $self->{directory}->()) { |
---|
| 190 | $path = File::Spec->catfile($base, $path); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | return $path; |
---|
[b8d7413] | 194 | } |
---|
| 195 | |
---|
[6473c3b] | 196 | sub sync { |
---|
[19233f5] | 197 | my $self = shift; |
---|
[6473c3b] | 198 | my $url = shift; |
---|
| 199 | my $dir = shift; |
---|
[19233f5] | 200 | |
---|
| 201 | if (-d "$dir") { |
---|
| 202 | local $CWD = $dir; |
---|
[9353238] | 203 | print "fetch $dir\n"; |
---|
[19233f5] | 204 | Git::Repository->run(fetch => '--all'); |
---|
| 205 | return $self; |
---|
| 206 | } |
---|
| 207 | |
---|
[9353238] | 208 | print "clone $dir\n"; |
---|
[6f31bfb] | 209 | mkpath $dir; |
---|
[6473c3b] | 210 | Git::Repository->run(clone => '--mirror' => $url => $dir); |
---|
| 211 | |
---|
| 212 | return $self; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | sub clone_git { |
---|
| 216 | my $self = shift; |
---|
| 217 | |
---|
| 218 | my $dir = $self->directory . '.git'; |
---|
[c5b427c] | 219 | my $url = $self->clone_url; |
---|
[6473c3b] | 220 | |
---|
| 221 | $self->sync($url => $dir); |
---|
| 222 | |
---|
[19233f5] | 223 | return $self; |
---|
| 224 | } |
---|
| 225 | |
---|
[55d0513] | 226 | sub forks { |
---|
[682d2d9] | 227 | my $self = shift; |
---|
| 228 | return $self->{forks} if ($self->{forks}); |
---|
| 229 | |
---|
[c5b427c] | 230 | $self->{forks} = utils::get("/repos/" . $self->full_name . "/forks"); |
---|
[682d2d9] | 231 | |
---|
[55d0513] | 232 | return $self->{forks}; |
---|
[682d2d9] | 233 | } |
---|
| 234 | |
---|
[19233f5] | 235 | sub set_forks { |
---|
| 236 | my $self = shift; |
---|
[682d2d9] | 237 | |
---|
| 238 | my $dir = $self->directory . '.git'; |
---|
| 239 | local $CWD = $dir; |
---|
| 240 | |
---|
| 241 | my $remotes = Git::Repository->run(branch => '--remotes'); |
---|
[6473c3b] | 242 | my @fetch; |
---|
[55d0513] | 243 | foreach my $fork (@{$self->forks}) { |
---|
[682d2d9] | 244 | if ($remotes =~ /$fork->{full_name}/) { |
---|
| 245 | print "skip ", $fork->{full_name}, "\n"; |
---|
| 246 | next; |
---|
| 247 | } |
---|
| 248 | print "add ", $fork->{full_name}, "\n"; |
---|
| 249 | Git::Repository->run(remote => add => $fork->{full_name} => $fork->{clone_url}); |
---|
[6473c3b] | 250 | push @fetch, $fork->{full_name}; |
---|
[682d2d9] | 251 | } |
---|
| 252 | |
---|
[6473c3b] | 253 | foreach my $fork (@fetch) { |
---|
[9353238] | 254 | print "fetch $fork\n"; |
---|
[6473c3b] | 255 | Git::Repository->run(fetch => $fork); |
---|
| 256 | } |
---|
[682d2d9] | 257 | |
---|
| 258 | return $self; |
---|
[19233f5] | 259 | } |
---|
| 260 | |
---|
| 261 | sub clone_wiki { |
---|
| 262 | my $self = shift; |
---|
[6473c3b] | 263 | |
---|
| 264 | my $dir = $self->directory . '.wiki.git'; |
---|
[c5b427c] | 265 | my $url = 'https://github.com/' . $self->full_name . '.wiki.git'; |
---|
[6473c3b] | 266 | |
---|
[9353238] | 267 | my $ua = LWP::UserAgent->new(max_redirect => 0); |
---|
| 268 | my $res = $ua->head( |
---|
| 269 | 'https://github.com/' . $self->full_name . '/wiki' |
---|
| 270 | ); |
---|
| 271 | if ($res->code != 200) { |
---|
| 272 | print "$dir does not exist\n"; |
---|
| 273 | return $self; |
---|
| 274 | } |
---|
| 275 | |
---|
[6473c3b] | 276 | $self->sync($url => $dir); |
---|
| 277 | |
---|
| 278 | return $self; |
---|
[19233f5] | 279 | } |
---|
| 280 | |
---|
[55d0513] | 281 | sub issues { |
---|
| 282 | my $self = shift; |
---|
| 283 | return $self->{issues} if ($self->{issues}); |
---|
| 284 | |
---|
[c5b427c] | 285 | my $open = utils::get("/repos/" . $self->full_name . "/issues"); |
---|
| 286 | my $closed = utils::get("/repos/" . $self->full_name . "/issues", state => 'closed'); |
---|
[55d0513] | 287 | |
---|
| 288 | if ($open) { push @{$self->{issues}}, @$open } |
---|
| 289 | if ($closed) { push @{$self->{issues}}, @$closed } |
---|
| 290 | |
---|
| 291 | return $self->{issues}; |
---|
| 292 | } |
---|
| 293 | |
---|
[19233f5] | 294 | sub save_issues { |
---|
| 295 | my $self = shift; |
---|
[55d0513] | 296 | |
---|
[49aca65] | 297 | my $ua = LWP::UserAgent->new; |
---|
| 298 | my $json = JSON->new->utf8->indent; |
---|
| 299 | |
---|
| 300 | my $dir = $self->directory . '.issues'; |
---|
| 301 | mkpath $dir unless (-d $dir); |
---|
| 302 | local $CWD = $dir; |
---|
| 303 | foreach my $issue (@{$self->issues}) { |
---|
| 304 | my $number = $issue->{number}; |
---|
| 305 | print "save issue/$number\n"; |
---|
| 306 | |
---|
| 307 | open my $fh, ">$number.json"; |
---|
| 308 | print $fh $json->encode($issue); |
---|
| 309 | close $fh; |
---|
| 310 | |
---|
| 311 | if (exists $issue->{pull_request}{patch_url}) { |
---|
| 312 | $ua->mirror($issue->{pull_request}{patch_url} => "$number.patch"); |
---|
| 313 | } |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | return $self; |
---|
[19233f5] | 317 | } |
---|
[6a2e5f1] | 318 | |
---|
[19df400] | 319 | sub backup { |
---|
| 320 | my $self = shift; |
---|
| 321 | |
---|
[f38b66d] | 322 | $self->clone_git if ($self->has_downloads eq 'true'); |
---|
| 323 | $self->set_forks if ($self->forks_count > 0); |
---|
| 324 | $self->clone_wiki if ($self->has_wiki eq 'true'); |
---|
| 325 | $self->save_issues if ($self->has_issues eq 'true'); |
---|
[19df400] | 326 | |
---|
| 327 | return $self; |
---|
| 328 | } |
---|
[6a2e5f1] | 329 | |
---|
| 330 | |
---|
| 331 | 1; |
---|
| 332 | __END__ |
---|