| 1 | package GitHub::EmptyRepository; | ||
| 2 | |||
| 3 | use Moo 1.007000; | ||
| 4 | |||
| 5 | our $VERSION = '0.00002'; | ||
| 6 | |||
| 7 | use File::HomeDir (); | ||
| 8 | use GitHub::EmptyRepository::Repository (); | ||
| 9 | use List::Util qw( uniq ); | ||
| 10 | use Module::Runtime qw( require_module use_module ); | ||
| 11 | use MooX::HandlesVia; | ||
| 12 | use MooX::Options; | ||
| 13 | use MooX::StrictConstructor; | ||
| 14 | use Path::Tiny qw( path ); | ||
| 15 | use Pithub (); | ||
| 16 | use Pithub::PullRequests (); | ||
| 17 | use Pithub::Repos (); | ||
| 18 | use Pithub::Repos::Commits (); | ||
| 19 | use Text::SimpleTable::AutoWidth (); | ||
| 20 | use Types::Standard qw( ArrayRef Bool HashRef InstanceOf Int Str ); | ||
| 21 | use WWW::Mechanize::GZip (); | ||
| 22 | |||
| 23 | option debug_useragent => ( | ||
| 24 | is => 'ro', | ||
| 25 | isa => Int, | ||
| 26 | format => 'i', | ||
| 27 | default => 0, | ||
| 28 | documentation => 'Print a _lot_ of debugging info about LWP requests', | ||
| 29 | ); | ||
| 30 | |||
| 31 | my $token_help = <<'EOF'; | ||
| 32 | https://help.github.com/articles/creating-an-access-token-for-command-line-use for instructions on how to get your own GitHub access token | ||
| 33 | EOF | ||
| 34 | |||
| 35 | option cache_requests => ( | ||
| 36 | is => 'ro', | ||
| 37 | isa => Bool, | ||
| 38 | documentation => 'Try to cache GET requests', | ||
| 39 | ); | ||
| 40 | |||
| 41 | option github_token => ( | ||
| 42 | is => 'ro', | ||
| 43 | isa => Str, | ||
| 44 | required => 0, | ||
| 45 | format => 's', | ||
| 46 | documentation => $token_help, | ||
| 47 | ); | ||
| 48 | |||
| 49 | option github_user => ( | ||
| 50 | is => 'ro', | ||
| 51 | isa => Str, | ||
| 52 | required => 0, | ||
| 53 | format => 's', | ||
| 54 | documentation => 'The username of your GitHub account', | ||
| 55 | ); | ||
| 56 | |||
| 57 | option url => ( | ||
| 58 | is => 'ro', | ||
| 59 | isa => ArrayRef, | ||
| 60 | format => 's@', | ||
| 61 | required => 0, | ||
| 62 | documentation => | ||
| 63 | 'Full Github repo url or shorthand of username/repository. You can pass multiple url args.', | ||
| 64 | ); | ||
| 65 | |||
| 66 | option org => ( | ||
| 67 | is => 'ro', | ||
| 68 | isa => ArrayRef, | ||
| 69 | format => 's@', | ||
| 70 | required => 0, | ||
| 71 | documentation => 'An organization. You can pass multiple url args.', | ||
| 72 | ); | ||
| 73 | |||
| 74 | option terse => ( | ||
| 75 | is => 'ro', | ||
| 76 | required => 0, | ||
| 77 | documentation => 'Make the report very brief', | ||
| 78 | ); | ||
| 79 | |||
| 80 | has _report => ( | ||
| 81 | is => 'ro', | ||
| 82 | isa => HashRef, | ||
| 83 | handles_via => 'Hash', | ||
| 84 | init_arg => undef, | ||
| 85 | handles => { _repository_for_url => 'get', _report_urls => 'keys', }, | ||
| 86 | lazy => 1, | ||
| 87 | builder => '_build_report', | ||
| 88 | ); | ||
| 89 | |||
| 90 | has _github_client => ( | ||
| 91 | is => 'ro', | ||
| 92 | isa => InstanceOf ['Pithub'], | ||
| 93 | lazy => 1, | ||
| 94 | builder => '_build_github_client' | ||
| 95 | ); | ||
| 96 | |||
| 97 | has _mech => ( | ||
| 98 | is => 'ro', | ||
| 99 | isa => InstanceOf ['LWP::UserAgent'], | ||
| 100 | lazy => 1, | ||
| 101 | builder => '_build_mech', | ||
| 102 | ); | ||
| 103 | |||
| 104 | sub _build_github_client { | ||
| 105 | my $self = shift; | ||
| 106 | |||
| 107 | return Pithub->new( | ||
| 108 | $self->cache_requests | ||
| 109 | || $self->debug_useragent ? ( ua => $self->_mech ) : (), | ||
| 110 | $self->github_user ? ( user => $self->github_user ) : (), | ||
| 111 | $self->github_token ? ( token => $self->github_token ) : (), | ||
| 112 | ); | ||
| 113 | } | ||
| 114 | |||
| 115 | sub _build_mech { | ||
| 116 | my $self = shift; | ||
| 117 | |||
| 118 | my $mech; | ||
| 119 | |||
| 120 | if ( $self->cache_requests ) { | ||
| 121 | my $dir = path( File::HomeDir->my_home ); | ||
| 122 | $dir->child('.github-emptyrepository-cache')->mkpath; | ||
| 123 | |||
| 124 | require_module('CHI'); | ||
| 125 | $mech = use_module( 'WWW::Mechanize::Cached', 1.45 )->new( | ||
| 126 | cache => CHI->new( | ||
| 127 | driver => 'File', | ||
| 128 | root_dir => $dir->stringify, | ||
| 129 | ) | ||
| 130 | ); | ||
| 131 | } | ||
| 132 | else { | ||
| 133 | $mech = WWW::Mechanize::GZip->new; | ||
| 134 | } | ||
| 135 | if ( $self->debug_useragent ) { | ||
| 136 | use_module( 'LWP::ConsoleLogger::Easy', 0.000013 ); | ||
| 137 | LWP::ConsoleLogger::Easy::debug_ua( $mech, $self->debug_useragent ); | ||
| 138 | } | ||
| 139 | return $mech; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub _build_report { | ||
| 143 | my $self = shift; | ||
| 144 | |||
| 145 | my %report; | ||
| 146 | |||
| 147 | # Where we put all urls (from --url AND/OR --org) | ||
| 148 | my @urls = (); | ||
| 149 | |||
| 150 | # Where will go urls found from --org | ||
| 151 | if ( $self->org ) { | ||
| 152 | foreach my $org ( @{ $self->org } ) { | ||
| 153 | my $repos = Pithub::Repos->new( | ||
| 154 | $self->github_user ? ( user => $self->github_user ) : (), | ||
| 155 | $self->github_token ? ( token => $self->github_token ) : (), | ||
| 156 | ); | ||
| 157 | my $result = $repos->list( org => $org ); | ||
| 158 | |||
| 159 | $result->auto_pagination(1); | ||
| 160 | |||
| 161 | while ( my $row = $result->next ) { | ||
| 162 | push @urls, $row->{full_name}; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | # Merge --org urls (already in @urls) with --url urls and clean dups | ||
| 168 | if ( $self->url ) { | ||
| 169 | push @urls, @{ $self->url }; | ||
| 170 | } | ||
| 171 | @urls = uniq @urls; | ||
| 172 | |||
| 173 | foreach my $url (@urls) { | ||
| 174 | my $repo = GitHub::EmptyRepository::Repository->new( | ||
| 175 | github_client => $self->_github_client, | ||
| 176 | url => $url, | ||
| 177 | ); | ||
| 178 | $report{$url} = $repo; | ||
| 179 | } | ||
| 180 | |||
| 181 | return \%report; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub print_report { | ||
| 185 | my $self = shift; | ||
| 186 | |||
| 187 | my @repos = map { $self->_repository_for_url($_) } $self->_report_urls; | ||
| 188 | |||
| 189 | return unless @repos; | ||
| 190 | |||
| 191 | my $table = Text::SimpleTable::AutoWidth->new; | ||
| 192 | my @cols | ||
| 193 | = ( 'user', 'repo', 'empty?', 'commits', 'branches', 'pullrequests' ); | ||
| 194 | $table->captions( \@cols ); | ||
| 195 | |||
| 196 | foreach my $repository (@repos) { | ||
| 197 | my $report = $repository->report; | ||
| 198 | |||
| 199 | # TODO: | ||
| 200 | # Check if repository is old? | ||
| 201 | my $is_empty = 1; | ||
| 202 | if ( $report->{nb_branches} > 1 ) { | ||
| 203 | |||
| 204 | # No doubt, not empty because multiple branches | ||
| 205 | $is_empty = 0; | ||
| 206 | } | ||
| 207 | if ( $report->{nb_pullrequests} > 0 ) { | ||
| 208 | |||
| 209 | # No doubt, not empty because at least one pull request | ||
| 210 | $is_empty = 0; | ||
| 211 | } | ||
| 212 | if ( $report->{nb_commits} > 1 ) { | ||
| 213 | |||
| 214 | # No doubt, not empty because more than just a dummy commit | ||
| 215 | # It can be a lot of dummy commits xD but that's another story | ||
| 216 | $is_empty = 0; | ||
| 217 | } | ||
| 218 | elsif ( $is_empty and $report->{nb_commits} == 1 ) { | ||
| 219 | |||
| 220 | # Possibly "almost" empty if there is one commit with only a boilerplate file (advised by GitHub UI) | ||
| 221 | foreach my $file ( @{ $report->{files} } ) { | ||
| 222 | |||
| 223 | # Check filename against list of "whitelisted" filenames | ||
| 224 | if ( | ||
| 225 | !grep /$file/, | ||
| 226 | ( | ||
| 227 | "README.md", ".gitignore", | ||
| 228 | "LICENSE", "CONTRIBUTING.md" | ||
| 229 | ) | ||
| 230 | ) | ||
| 231 | { | ||
| 232 | # Not empty | ||
| 233 | $is_empty = 0; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | } | ||
| 237 | if ( $is_empty and $self->terse ) { | ||
| 238 | print $repository->user . "/" . $repository->name . "\n"; | ||
| 239 | } | ||
| 240 | else { | ||
| 241 | $table->row( | ||
| 242 | $repository->user, | ||
| 243 | $repository->name, | ||
| 244 | $is_empty ? "YES" : "NO", | ||
| 245 | $report->{nb_commits} > 1 ? "2+" : $report->{nb_commits}, | ||
| 246 | $report->{nb_branches} > 1 ? "2+" : $report->{nb_branches}, | ||
| 247 | $report->{nb_pullrequests} > 1 | ||
| 248 | ? "2+" | ||
| 249 | : $report->{nb_pullrequests}, | ||
| 250 | ); | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | if ( !$self->terse ) { print $table->draw } | ||
| 255 | |||
| 256 | return; | ||
| 257 | } | ||
| 258 | |||
| 259 | 1; | ||
| 260 | |||
| 261 | =pod | ||
| 262 | |||
| 263 | =encoding UTF-8 | ||
| 264 | |||
| 265 | =head1 NAME | ||
| 266 | |||
| 267 | GitHub::EmptyRepository - Scan for empty repositories | ||
| 268 | |||
| 269 | =head1 VERSION | ||
| 270 | |||
| 271 | version 0.00002 | ||
| 272 | |||
| 273 | =head1 SYNOPSIS | ||
| 274 | |||
| 275 | =head1 AUTHORS | ||
| 276 | |||
| 277 | =over 4 | ||
| 278 | |||
| 279 | =item * | ||
| 280 | |||
| 281 | Thibault Duponchelle <thibault.duponchelle@gmail.com> | ||
| 282 | |||
| 283 | =item * | ||
| 284 | |||
| 285 | Olaf Alders <olaf@wundercounter.com> | ||
| 286 | |||
| 287 | =back | ||
| 288 | |||
| 289 | =head1 COPYRIGHT AND LICENSE | ||
| 290 | |||
| 291 | This software is copyright (c) 2022 by Thibault Duponchelle. | ||
| 292 | |||
| 293 | This is free software; you can redistribute it and/or modify it under | ||
| 294 | the same terms as the Perl 5 programming language system itself. | ||
| 295 | |||
| 296 | =cut | ||
| 297 | |||
| 298 | __END__ |