Skip to content

Commit

Permalink
Added 'auto_paginate' attribute to Pithub::new() which allows 100+ repos
Browse files Browse the repository at this point in the history
(fixes #3)

- Add list(), returns the entire list of Github repo objects
- Add --list, prints out the name of all of the fetched repositories
- Add new tests for the above
  • Loading branch information
stevieb9 committed Nov 14, 2021
1 parent 0593450 commit abb1dc7
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 40 deletions.
34 changes: 19 additions & 15 deletions Changes
@@ -1,26 +1,30 @@
Revision history for Github-Backup

1.03 UNREL
-
- Added 'auto_pagination => 1' to Pithub instantiation which was limiting
the number of repositories we operate on to 100 (fixes #3)
- Added list(), returns a full list of repository objects
- Added --list, prints to STDOUT the full list of available repository
objects for the given user

1.02 2018-02-12
- fix missing newline backslash in SYNOPSIS
- added new "FUTURE DIRECTION" POD heading
- -h flag is taken as priority in github_backup, and no longer prints
erroneous warnings for missing args
- we now properly respect GITHUB_TOKEN env var
- fix missing newline backslash in SYNOPSIS
- added new "FUTURE DIRECTION" POD heading
- -h flag is taken as priority in github_backup, and no longer prints
erroneous warnings for missing args
- we now properly respect GITHUB_TOKEN env var

1.01 2018-02-11
- added repos() and issues()
- added tests
- added github_backup binary
- added prereq JSON
- cleaned up POD tests; we skip accessor sub docs
- we create repo directories under issues dir only if the repo has any
issues
- added repos() and issues()
- added tests
- added github_backup binary
- added prereq JSON
- cleaned up POD tests; we skip accessor sub docs
- we create repo directories under issues dir only if the repo has any
issues

0.01 Date/time
- First version, released on an unsuspecting world.
- added Travis-CI and Coveralls.io integration
- First version, released on an unsuspecting world.
- added Travis-CI and Coveralls.io integration


1 change: 1 addition & 0 deletions MANIFEST
Expand Up @@ -8,6 +8,7 @@ MANIFEST.SKIP
README
t/00-load.t
t/05-new.t
t/07-list.t
t/10-repos.t
t/15-dir.t
t/20-issues.t
Expand Down
36 changes: 22 additions & 14 deletions bin/github_backup
Expand Up @@ -13,6 +13,7 @@ GetOptions(
"t|token=s" => \$opts{token},
"d|dir=s" => \$opts{dir},
"p|proxy=s" => \$opts{proxy},
"l|list" => \$opts{list},
"r|repos" => \$opts{repos},
"i|issues" => \$opts{issues},
"h|help" => \$help
Expand All @@ -27,19 +28,21 @@ if (! $opts{user} || ! $opts{dir}){
help();
}

if (! $opts{token}){
if ($ENV{GITHUB_TOKEN}){
$opts{token} = $ENV{GITHUB_TOKEN};
}
else {
warn "This application requires a Github token either as an argument, " .
"or in the GITHUB_TOKEN environment variable.\n";
help();
if (! $opts{list}) {
if (! $opts{token}){
if ($ENV{GITHUB_TOKEN}){
$opts{token} = $ENV{GITHUB_TOKEN};
}
else {
warn "This application requires a Github token either as an argument, " .
"or in the GITHUB_TOKEN environment variable.\n";
help();
}
}
}

if (! $opts{repos} && ! $opts{issues}){
warn "You must supply at least one of -r|--repos or -i|--issues\n";
if (! $opts{repos} && ! $opts{issues} && ! $opts{list}){
warn "You must supply at least one of -r|--repos, -i|--issues or -l|--list\n";
help();
}

Expand Down Expand Up @@ -68,10 +71,15 @@ my $gh = Github::Backup->new(
proxy => $opts{proxy}
);

if ($opts{repos}){
if ($opts{list}) {
my $repos = $gh->list;
for (@$repos) {
print "$_->{name}\n";
}
}
if ($opts{repos}) {
$gh->repos;
}

if ($opts{issues}){
if ($opts{issues}) {
$gh->issues;
}
}
44 changes: 33 additions & 11 deletions lib/Github/Backup.pm
Expand Up @@ -78,7 +78,8 @@ sub BUILD {
my $gh = Pithub->new(
ua => $ua,
user => $self->api_user,
token => $self->token
token => $self->token,
auto_pagination => 1,
);

$self->stg($self->dir . '.stg');
Expand All @@ -93,33 +94,41 @@ sub BUILD {
mkdir $self->stg or die "can't create the backup staging directory...$!\n";

}

sub list {
my ($self) = @_;

if (! $self->{repo_list}) {
my $repo_list = $self->gh->repos->list(user => $self->user);
while (my $repo = $repo_list->next) {
push @{ $self->{repo_list} }, $repo;
}
}

return $self->{repo_list};
}
sub repos {
my ($self) = @_;

my $repo_list = $self->gh->repos->list(user => $self->user);
my $repo_list = $self->list;

my @repos;

while (my $repo = $repo_list->next){
push @repos, $repo;
}

for my $repo (@repos){

my $stg = $self->stg . "/$repo->{name}";

if (! $self->forks){
if (! exists $repo->{parent}){
Git::Repository->run(
clone => $repo->{clone_url} => $stg,
{quiet => 1}
{ quiet => 0 }
);
}
}
else {
Git::Repository->run(
clone => $repo->{clone_url} => $stg,
{ quiet => 1 }
{ quiet => 0 }
);
}
}
Expand Down Expand Up @@ -215,10 +224,16 @@ Mandatory: Your Github API token. If you wish to not include this on the
command line, you can put the token into the C<GITHUB_TOKEN> environment
variable.
=head2 -l | --list
Optional: Simply prints a list of all available repositories for the specified
user.
=head2 -d | --dir
Mandatory: The backup directory where your repositories and/or issues will be
stored. The format of the directory structure will be as follows:
Mandatory (if using C<--repos> or C<--issues>): The backup directory where your
repositories and/or issues will be stored. The format of the directory
structure will be as follows:
backup_dir/
- issues/
Expand Down Expand Up @@ -284,6 +299,13 @@ information to.
Optional, String: Send in a proxy in the format
C<https://proxy.example.com:PORT> and we'll use this to do our fetching.
=head2 list
Takes no parameters. Returns a list of all repository objects as returned from
L<Pithub> / the Github API.
Common fields are C<$repo->{name}>, C<$repo->{clone_url}> etc.
=head2 repos
Takes no parameters. Backs up all of your Github repositories, and stores them
Expand Down
43 changes: 43 additions & 0 deletions t/07-list.t
@@ -0,0 +1,43 @@
use strict;
use warnings;

use Github::Backup;
use Test::More;

if (! $ENV{AUTHOR_TESTING}){
plan skip_all => "author test only (set env var AUTHOR_TESTING=1)";
}

if (! $ENV{GITHUB_TOKEN}){
plan skip_all => "This test requires your Github token to be placed into " .
"the GITHUB_TOKEN environment variable\n";
}

{ # base

my $mod = 'Github::Backup';

my $o = $mod->new(
api_user => 'stevieb9',
# token => $ENV{GITHUB_TOKEN},
dir => 't/backup',
);

my $repos = $o->list;

is scalar @$repos > 100, 1, "Number of repos ok";


my @names;
for my $repo (@$repos) {
my $name = $repo->{name};
push @names, $name;
}

use Data::Dumper;
print Dumper \@names;
my $result = grep /^github-backup/, @names;
is $result, 1, "github-backup in the list of repos ok";
}

done_testing();

0 comments on commit abb1dc7

Please sign in to comment.