From abb1dc72c1af24c590ce48a1f6100797198928b5 Mon Sep 17 00:00:00 2001 From: Steve Bertrand Date: Sun, 14 Nov 2021 11:01:00 -0800 Subject: [PATCH] Added 'auto_paginate' attribute to Pithub::new() which allows 100+ repos (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 --- Changes | 34 +++++++++++++++++++--------------- MANIFEST | 1 + bin/github_backup | 36 ++++++++++++++++++++++-------------- lib/Github/Backup.pm | 44 +++++++++++++++++++++++++++++++++----------- t/07-list.t | 43 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+), 40 deletions(-) create mode 100644 t/07-list.t diff --git a/Changes b/Changes index a51e661..0374c3f 100644 --- a/Changes +++ b/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 diff --git a/MANIFEST b/MANIFEST index 21e9b83..a4e5eb9 100644 --- a/MANIFEST +++ b/MANIFEST @@ -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 diff --git a/bin/github_backup b/bin/github_backup index a07a6b9..b25a3dc 100755 --- a/bin/github_backup +++ b/bin/github_backup @@ -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 @@ -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(); } @@ -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; -} \ No newline at end of file +} diff --git a/lib/Github/Backup.pm b/lib/Github/Backup.pm index 65b7365..d0aa253 100644 --- a/lib/Github/Backup.pm +++ b/lib/Github/Backup.pm @@ -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'); @@ -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 } ); } } @@ -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 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/ @@ -284,6 +299,13 @@ information to. Optional, String: Send in a proxy in the format C 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 / 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 diff --git a/t/07-list.t b/t/07-list.t new file mode 100644 index 0000000..195e227 --- /dev/null +++ b/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();