[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 | my $ua = LWP::UserAgent->new; |
---|
| 10 | my $json = JSON->new->utf8->indent; |
---|
| 11 | |
---|
| 12 | sub json_api { |
---|
| 13 | my $url = shift; |
---|
| 14 | |
---|
| 15 | my $res = $ua->get( |
---|
| 16 | "https://api.github.com$url" |
---|
| 17 | ); |
---|
| 18 | |
---|
| 19 | $res->is_success or croak $res->status_line; |
---|
| 20 | |
---|
| 21 | return $json->decode($res->content); |
---|
| 22 | } |
---|
| 23 | |
---|
[19233f5] | 24 | |
---|
[6a2e5f1] | 25 | package GitHubBackup; |
---|
| 26 | |
---|
| 27 | use strict; |
---|
| 28 | use warnings; |
---|
| 29 | use utf8; |
---|
| 30 | use Carp qw(croak); |
---|
[6f31bfb] | 31 | use File::Spec; |
---|
[6a2e5f1] | 32 | |
---|
| 33 | |
---|
| 34 | # both hash and hashref are acceptable |
---|
| 35 | sub new { |
---|
| 36 | my $class = shift; |
---|
| 37 | my $args = (ref $_[0] eq 'HASH') ? $_[0] : {@_}; |
---|
[19233f5] | 38 | |
---|
[6f31bfb] | 39 | return bless $args, $class; |
---|
[6a2e5f1] | 40 | } |
---|
| 41 | |
---|
[19df400] | 42 | sub account { |
---|
| 43 | my $self = shift; |
---|
| 44 | my $args = shift; |
---|
| 45 | |
---|
| 46 | if (defined $args) { |
---|
| 47 | $self->{repos} = undef; |
---|
| 48 | $self->{account} = $args; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | return $self->{account}; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | sub repository { |
---|
| 55 | my $self = shift; |
---|
| 56 | my $args = shift; |
---|
| 57 | |
---|
| 58 | if (defined $args) { |
---|
| 59 | $self->{repos} = undef; |
---|
| 60 | $self->{repository} = $args; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | return $self->{repository}; |
---|
| 64 | } |
---|
| 65 | |
---|
[6f31bfb] | 66 | sub directory { |
---|
| 67 | my $self = shift; |
---|
| 68 | my $args = shift; |
---|
| 69 | |
---|
| 70 | if (defined $args) { |
---|
| 71 | $self->{directory} = File::Spec->rel2abs($args); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | return $self->{directory}; |
---|
| 75 | } |
---|
| 76 | |
---|
[6a2e5f1] | 77 | sub repos { |
---|
| 78 | my $self = shift; |
---|
[19233f5] | 79 | return $self->{repos} if ($self->{repos}); |
---|
[6a2e5f1] | 80 | |
---|
| 81 | my $account = $self->account or croak "account is not set"; |
---|
| 82 | if (my $repository = $self->repository) { |
---|
| 83 | $self->{repos} = [ |
---|
[19233f5] | 84 | GitHubBackup::Repository->new({ |
---|
[b8d7413] | 85 | directory => sub {$self->directory}, |
---|
[19233f5] | 86 | full_name => "$account/$repository", |
---|
| 87 | }) |
---|
[6a2e5f1] | 88 | ]; |
---|
| 89 | |
---|
| 90 | return $self->{repos}; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | my $page = 1; |
---|
| 94 | my @repos; |
---|
| 95 | while (1) { |
---|
| 96 | my $result = utils::json_api("/users/$account/repos?per_page=100&page=$page"); |
---|
| 97 | if (ref($result) eq 'ARRAY' && scalar @$result > 0) { |
---|
| 98 | push @repos, @$result; |
---|
| 99 | $page++; |
---|
| 100 | |
---|
| 101 | next; |
---|
| 102 | } |
---|
| 103 | last; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | foreach my $repos (@repos) { |
---|
| 107 | push @{$self->{repos}}, |
---|
| 108 | GitHubBackup::Repository->new({ |
---|
[b8d7413] | 109 | directory => sub {$self->directory}, |
---|
[6a2e5f1] | 110 | full_name => $repos->{full_name}, |
---|
| 111 | clone_url => $repos->{clone_url}, |
---|
| 112 | }) |
---|
| 113 | ; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | return $self->{repos}; |
---|
| 117 | } |
---|
| 118 | |
---|
[19df400] | 119 | sub backup { |
---|
| 120 | my $self = shift; |
---|
| 121 | |
---|
| 122 | foreach my $repos (@{$self->repos}) { |
---|
| 123 | $repos->backup; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | return $self; |
---|
| 127 | } |
---|
| 128 | |
---|
[6a2e5f1] | 129 | |
---|
| 130 | package GitHubBackup::Repository; |
---|
| 131 | |
---|
| 132 | use strict; |
---|
| 133 | use warnings; |
---|
| 134 | use utf8; |
---|
| 135 | use Carp qw(croak); |
---|
[19233f5] | 136 | use Git::Repository; |
---|
| 137 | use File::chdir; |
---|
[6f31bfb] | 138 | use File::Spec; |
---|
| 139 | use File::Path qw(mkpath); |
---|
[b8d7413] | 140 | |
---|
[19233f5] | 141 | sub new { |
---|
| 142 | my $class = shift; |
---|
| 143 | my $args = shift; |
---|
| 144 | |
---|
| 145 | if (! exists $args->{clone_url}) { |
---|
| 146 | my $result = utils::json_api('/repos/' . $args->{full_name}); |
---|
| 147 | $args->{clone_url} = $result->{clone_url}; |
---|
| 148 | } |
---|
| 149 | |
---|
[6f31bfb] | 150 | return bless $args, $class; |
---|
[19233f5] | 151 | } |
---|
| 152 | |
---|
[b8d7413] | 153 | sub directory { |
---|
| 154 | my $self = shift; |
---|
| 155 | |
---|
[6f31bfb] | 156 | my $path = $self->{full_name}; |
---|
| 157 | if (my $base = $self->{directory}->()) { |
---|
| 158 | $path = File::Spec->catfile($base, $path); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | return $path; |
---|
[b8d7413] | 162 | } |
---|
| 163 | |
---|
[19233f5] | 164 | sub clone_git { |
---|
| 165 | my $self = shift; |
---|
| 166 | |
---|
[6f31bfb] | 167 | my $dir = $self->directory; |
---|
[19233f5] | 168 | if (-d "$dir") { |
---|
| 169 | local $CWD = $dir; |
---|
[19df400] | 170 | print "fetch ", $dir, "\n"; |
---|
[19233f5] | 171 | Git::Repository->run(fetch => '--all'); |
---|
| 172 | return $self; |
---|
| 173 | } |
---|
| 174 | |
---|
[19df400] | 175 | print "clone ", $dir, "\n"; |
---|
[6f31bfb] | 176 | mkpath $dir; |
---|
[19233f5] | 177 | Git::Repository->run(clone => '--mirror' => $self->{clone_url} => $dir); |
---|
| 178 | return $self; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | sub set_forks { |
---|
| 182 | my $self = shift; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | sub clone_wiki { |
---|
| 186 | my $self = shift; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | sub save_issues { |
---|
| 190 | my $self = shift; |
---|
| 191 | } |
---|
[6a2e5f1] | 192 | |
---|
[19df400] | 193 | sub backup { |
---|
| 194 | my $self = shift; |
---|
| 195 | |
---|
| 196 | $self->clone_git; |
---|
| 197 | $self->set_forks; |
---|
| 198 | $self->clone_wiki; |
---|
| 199 | $self->save_issues; |
---|
| 200 | |
---|
| 201 | return $self; |
---|
| 202 | } |
---|
[6a2e5f1] | 203 | |
---|
| 204 | |
---|
| 205 | 1; |
---|
| 206 | __END__ |
---|