| 1 | # -*- mode: cperl; eval: (follow-mode) -*- | ||
| 2 | # | ||
| 3 | |||
| 4 | package App::gqmt; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use diagnostics; | ||
| 9 | |||
| 10 | use Data::Printer caller_info => 1, print_escapes => 1, output => 'stdout', class => { expand => 2 }, | ||
| 11 | caller_message => "DEBUG __FILENAME__:__LINE__ "; | ||
| 12 | use Getopt::Long qw(:config no_ignore_case gnu_getopt auto_help auto_version); | ||
| 13 | use Pod::Man; | ||
| 14 | use Pod::Usage qw(pod2usage); | ||
| 15 | use File::Basename; | ||
| 16 | use HTTP::Request (); | ||
| 17 | use LWP::UserAgent; | ||
| 18 | use JSON; | ||
| 19 | use Time::Piece; | ||
| 20 | use Template; | ||
| 21 | |||
| 22 | my @PROGARG = ($0, @ARGV); | ||
| 23 | our $VERSION = '1.02'; | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | my $class = shift; | ||
| 27 | my $self = | ||
| 28 | bless { | ||
| 29 | _progname => fileparse($0), | ||
| 30 | _progargs => [$0, @ARGV], | ||
| 31 | _option => { d => 0, | ||
| 32 | colored => 0, | ||
| 33 | rows_number => 100, | ||
| 34 | age => 60*60*24*14, | ||
| 35 | versions_to_hold => 2, | ||
| 36 | url => 'https://api.github.com/graphql', | ||
| 37 | single_iteration => 0, | ||
| 38 | http_timeout => 180, | ||
| 39 | pkg => { | ||
| 40 | alpine => 1, | ||
| 41 | api => 1, | ||
| 42 | app => 1, | ||
| 43 | scheduler => 1, | ||
| 44 | }, | ||
| 45 | re => { | ||
| 46 | default => '^(?:docker-base-layer|develop|release|master|v[0-9]+\.[0-9]+\.[0-9]+)$', | ||
| 47 | alpine => '^(?:docker-base-layer|develop|release|master|v[0-9]+\.[0-9]+\.[0-9]+)$', | ||
| 48 | api => '^(?:docker-base-layer|develop|release|master|v[0-9]+\.[0-9]+\.[0-9]+)$', | ||
| 49 | app => '^(?:docker-base-layer|develop|qa|release|master|v[0-9]+\.[0-9]+\.[0-9]+)$', | ||
| 50 | scheduler => '^(?:docker-base-layer|develop|release|master|v[0-9]+\.[0-9]+\.[0-9]+)$', | ||
| 51 | }, | ||
| 52 | }, | ||
| 53 | }, $class; | ||
| 54 | |||
| 55 | GetOptions ( | ||
| 56 | 'a|age=i' => \$self->{_option}{age}, | ||
| 57 | 'U|url=s' => \$self->{_option}{url}, | ||
| 58 | 'u|user=s' => \$self->{_option}{user}, | ||
| 59 | 'T|token=s' => \$self->{_option}{token}, | ||
| 60 | 'R|repository=s' => \$self->{_option}{repo}, | ||
| 61 | 'P|package=s' => \$self->{_option}{package}, | ||
| 62 | 'package-regex=s' => \$self->{_option}{package_regex}, | ||
| 63 | 'n|dry-run' => \$self->{_option}{dry_run}, | ||
| 64 | 'N|rows-number=i' => \$self->{_option}{rows_number}, | ||
| 65 | 'versions-to-hold=i' => \$self->{_option}{versions_to_hold}, | ||
| 66 | 'http-timeout=i' => \$self->{_option}{http_timeout}, | ||
| 67 | 'C|colored' => \$self->{_option}{colored}, | ||
| 68 | 'D|delete' => \$self->{_option}{delete}, | ||
| 69 | 's|single-iteration' => \$self->{_option}{single_iteration}, | ||
| 70 | 't|query-template=s' => \$self->{_option}{query_template}, | ||
| 71 | 'v|package-version=s' => \$self->{_option}{v}, | ||
| 72 | |||
| 73 | 'h|help' => sub { pod2usage(-exitval => 0, -verbose => 2); exit 0 }, | ||
| 74 | 'd|debug+' => \$self->{_option}{d}, | ||
| 75 | 'V|version' => sub { print "$self->{_progname}, version $VERSION\n"; exit 0 }, | ||
| 76 | ); | ||
| 77 | |||
| 78 | pod2usage(-exitval => 0, -verbose => 2, -msg => "\nERROR: repository owner not provided, option -u\n\n") | ||
| 79 | if ! $self->{_option}{user}; | ||
| 80 | |||
| 81 | pod2usage(-exitval => 0, -verbose => 2, -msg => "\nERROR: query template file does not exist, option -t\n\n") | ||
| 82 | if defined $self->{_option}{query_template} && ! -e $self->{_option}{query_template}; | ||
| 83 | |||
| 84 | pod2usage(-exitval => 2, -verbose => 2, -msg => "\nERROR: access token is not provided, option -T\n\n" ) | ||
| 85 | if ! $self->{_option}{token}; | ||
| 86 | |||
| 87 | pod2usage(-exitval => 2, -verbose => 2, -msg => "\nERROR: repository name is not provided, option -R\n\n" ) | ||
| 88 | if ! $self->{_option}{repo}; | ||
| 89 | |||
| 90 | pod2usage(-exitval => 2, -verbose => 2, -msg => "\nERROR: package name not provided, option -P\n\n") | ||
| 91 | if ! $self->{_option}{package}; | ||
| 92 | |||
| 93 | # pod2usage(-exitval => 2, -verbose => 2, -msg => "\nERROR: not supported package\n\n") | ||
| 94 | # if $self->{_option}{package} && ! exists $self->{_option}{pkg}{$self->{_option}{package}}; | ||
| 95 | |||
| 96 | pod2usage(-exitval => 2, -verbose => 2, -msg => "\nERROR: requested rows number should be 1..100\n\n") | ||
| 97 | if $self->{_option}{rows_number} && ( $self->{_option}{rows_number} < 1 || $self->{_option}{rows_number} > 100 ); | ||
| 98 | |||
| 99 | # pod2usage(-exitval => 0, -verbose => 2, -msg => "\nERROR: -v is mandatory when -D and -s are used together\n\n") | ||
| 100 | # if $delete && $single_iteration && ! $v; | ||
| 101 | |||
| 102 | p $self->{_option} if $self->{_option}{d} > 2; | ||
| 103 | |||
| 104 | $self->{_option}{req} = HTTP::Request->new( 'POST', | ||
| 105 | $self->{_option}{url}, | ||
| 106 | [ 'Authorization' => 'bearer ' . $self->{_option}{token} ] ); | ||
| 107 | |||
| 108 | return $self; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub progname { shift->{_progname} } | ||
| 112 | sub progargs { return join(' ', @{shift->{_progargs}}); } | ||
| 113 | |||
| 114 | sub option { | ||
| 115 | my ($self,$opt) = @_; | ||
| 116 | return $self->{_option}{$opt}; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub lwp { | ||
| 120 | my $self = shift; | ||
| 121 | my $lwp = LWP::UserAgent->new( agent => "$self->{_progname}/$VERSION ", | ||
| 122 | timeout => $self->option('http_timeout'), ); | ||
| 123 | return $lwp; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub jso { | ||
| 127 | my $self = shift; | ||
| 128 | my $jso = JSON->new->allow_nonref; | ||
| 129 | return $jso; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub run { | ||
| 133 | my $self = shift; | ||
| 134 | my $versions = []; | ||
| 135 | |||
| 136 | p ( $self->progargs, colored => $self->option('colored') ) if $self->option('d') > 0; | ||
| 137 | |||
| 138 | my $to_delete; | ||
| 139 | if ( ! $self->option('v') ) { | ||
| 140 | |||
| 141 | my $res = $self->get_versions ({ res => $versions }); | ||
| 142 | |||
| 143 | my $t_now = localtime; | ||
| 144 | my $t_ver; | ||
| 145 | # my $i = 0; | ||
| 146 | my $re; | ||
| 147 | if ( $self->option('package_regex') ) { | ||
| 148 | $re = $self->option('package_regex'); | ||
| 149 | } elsif ( exists $self->option('re')->{$self->option('package')} ) { | ||
| 150 | $re = $self->option('re')->{$self->option('package')} | ||
| 151 | } else { | ||
| 152 | $re = $self->option('re')->{default}; | ||
| 153 | } | ||
| 154 | |||
| 155 | foreach ( @{$versions} ) { | ||
| 156 | next if $_->{version} =~ /$re/; | ||
| 157 | p ($_, caller_message => "VERSION DOES NOT MATCH REGEX ($re) AND IS BEEN PROCESSED: __FILENAME__:__LINE__ ") if $self->option('d') > 2; | ||
| 158 | |||
| 159 | if ( defined $_->{files}->{nodes}->[0]->{updatedAt} ) { | ||
| 160 | $t_ver = Time::Piece->strptime( $_->{files}->{nodes}->[0]->{updatedAt}, | ||
| 161 | "%Y-%m-%dT%H:%M:%SZ" ); | ||
| 162 | |||
| 163 | next if ($t_ver->epoch + $self->option('age') ) >= $t_now->epoch; | ||
| 164 | } | ||
| 165 | |||
| 166 | # $to_delete->{ defined $_->{files}->{nodes}->[0]->{updatedAt} ? | ||
| 167 | # $_->{files}->{nodes}->[0]->{updatedAt} : sprintf('NODATE_%04d', $i++) } = $_->{version}; | ||
| 168 | |||
| 169 | $to_delete->{ $_->{id} } = { version => $_->{version}, | ||
| 170 | ts => $_->{files}->{nodes}->[0]->{updatedAt} }; | ||
| 171 | } | ||
| 172 | } else { | ||
| 173 | $to_delete->{ $self->option('v') } = { version => 'STUB VERSION', | ||
| 174 | ts => 'STUB TS' }; | ||
| 175 | } | ||
| 176 | |||
| 177 | p ($to_delete, caller_message => "VERSIONS TO DELETE: __FILENAME__:__LINE__ ") if $self->option('d') > 2; | ||
| 178 | |||
| 179 | if ( $self->option('delete') && defined $to_delete && | ||
| 180 | scalar(keys(%{$to_delete})) gt $self->option('versions_to_hold') ) { | ||
| 181 | $self->del_versions ({ | ||
| 182 | del => $to_delete, | ||
| 183 | # dbg => $self->option('d'), | ||
| 184 | # dry => $self->option('dry_run') | ||
| 185 | }); | ||
| 186 | |||
| 187 | } elsif ( $self->option('delete') && !defined $to_delete ) { | ||
| 188 | print "nothing to delete\n"; | ||
| 189 | } else { | ||
| 190 | # p ( $versions, colored => $self->option('colored') ) if $self->option('d') > 2 || $self->option('dry_run'); | ||
| 191 | my @vers_arr = map { | ||
| 192 | sprintf("%30s\t%20s\t%s\n", | ||
| 193 | $_->{version}, | ||
| 194 | scalar @{$_->{files}->{nodes}} > 0 && exists $_->{files}->{nodes}->[0]->{updatedAt} | ||
| 195 | ? $_->{files}->{nodes}->[0]->{updatedAt} : '', | ||
| 196 | $_->{id} | ||
| 197 | ) | ||
| 198 | } @{$versions}; | ||
| 199 | print "Versions of package \"", $self->option('package'), "\":\n\n", join('', @vers_arr); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | |||
| 204 | sub del_versions { | ||
| 205 | my ($self, $args) = @_; | ||
| 206 | my $arg = { | ||
| 207 | del => $args->{del} // [], # array of IDs to delete | ||
| 208 | }; | ||
| 209 | |||
| 210 | p ($arg->{del}, caller_message => "VERSIONS TO DELETE: __FILENAME__:__LINE__ ") if $self->option('d') > 2; | ||
| 211 | |||
| 212 | $self->option('req')->header(Accept => 'application/vnd.github.package-deletes-preview+json'); | ||
| 213 | |||
| 214 | my $query; | ||
| 215 | |||
| 216 | foreach ( keys( %{$arg->{del}} ) ) { | ||
| 217 | $query = sprintf('mutation { deletePackageVersion(input:{packageVersionId:"%s"}) { success }}', $_); | ||
| 218 | |||
| 219 | p ( $query, colored => $self->option('colored') ) if $self->option('d') > 1 || $self->option('dry_run'); | ||
| 220 | next if $self->option('dry_run'); | ||
| 221 | |||
| 222 | $self->option('req')->content( $self->jso->encode({ query => $query }) ); | ||
| 223 | |||
| 224 | my $res = $self->lwp->request($self->option('req')); | ||
| 225 | |||
| 226 | if ( ! $res->is_success ) { | ||
| 227 | my $res_cont = $self->jso->decode( $res->content ); | ||
| 228 | my $res_error = sprintf("--- ERROR ---\n\n%s\n\nMessage: %s\n doc: %s\n\n", | ||
| 229 | $res->status_line, | ||
| 230 | $res_cont->{message}, | ||
| 231 | $res_cont->{documentation_url} ); | ||
| 232 | print $res_error; | ||
| 233 | exit 1; | ||
| 234 | } | ||
| 235 | |||
| 236 | my $reply = $self->jso->decode( $res->decoded_content ); | ||
| 237 | |||
| 238 | if ( exists $reply->{errors} ) { | ||
| 239 | unshift @{$reply->{errors}}, "--- ERROR ---"; | ||
| 240 | p ( $reply->{errors}, colored => $self->option('colored') ); | ||
| 241 | exit 1; | ||
| 242 | } | ||
| 243 | |||
| 244 | p ( $reply, colored => $self->option('colored') ); | ||
| 245 | print "package of version ID: $_, has been successfully deleted\n" if $self->option('d') > 0; | ||
| 246 | |||
| 247 | } | ||
| 248 | |||
| 249 | } | ||
| 250 | |||
| 251 | |||
| 252 | sub get_versions { | ||
| 253 | my ($self, $args) = @_; | ||
| 254 | my $arg = { | ||
| 255 | res => $args->{res}, # result | ||
| 256 | inf => $args->{inf} // { # pageInfo | ||
| 257 | startCursor => undef, | ||
| 258 | endCursor => undef, | ||
| 259 | hasNextPage => -1, | ||
| 260 | hasPreviousPage => -1 | ||
| 261 | } | ||
| 262 | }; | ||
| 263 | |||
| 264 | my $query; | ||
| 265 | if ( defined $self->option('query_template') ) { | ||
| 266 | my $tt_out; | ||
| 267 | my $tt = Template->new( ABSOLUTE => 1, | ||
| 268 | RELATIVE => 1 ) || die Template->error(), "\n"; | ||
| 269 | |||
| 270 | $tt->process( | ||
| 271 | $self->option('query_template'), | ||
| 272 | { | ||
| 273 | repo => $self->option('repo'), | ||
| 274 | user => $self->option('user'), | ||
| 275 | pkg_num => $self->option('rows_number'), | ||
| 276 | pkg_name => $self->option('package'), | ||
| 277 | vers_num => $self->option('rows_number'), | ||
| 278 | cursor => $arg->{inf}->{hasPreviousPage} == 1 ? sprintf(', before: "%s"', $arg->{inf}->{startCursor}) : '' | ||
| 279 | }, | ||
| 280 | \$tt_out | ||
| 281 | ); | ||
| 282 | $query = { query => $tt_out }; | ||
| 283 | |||
| 284 | } else { | ||
| 285 | $query = $self-> | ||
| 286 | query_default({ inf => $arg->{inf}->{hasPreviousPage} == 1 ? sprintf(', before: "%s"', $arg->{inf}->{startCursor}) : ''}); | ||
| 287 | } | ||
| 288 | |||
| 289 | p( $query->{query}, colored => $self->option('colored'), print_escapes => 0 ) | ||
| 290 | if $self->option('d') > 0 && ! defined $arg->{inf}->{startCursor}; | ||
| 291 | |||
| 292 | my $json = $self->jso->encode( $query ); | ||
| 293 | |||
| 294 | $self->option('req')->content( $json ); | ||
| 295 | |||
| 296 | my $res = $self->lwp->request($self->option('req')); | ||
| 297 | |||
| 298 | if ( ! $res->is_success ) { | ||
| 299 | my $res_cont = $self->jso->decode( $res->content ); | ||
| 300 | my $res_error = sprintf("--- ERROR ---\n\n%s\n\nMessage: %s\n doc: %s\n\n", | ||
| 301 | $res->status_line, | ||
| 302 | $res_cont->{message}, | ||
| 303 | $res_cont->{documentation_url} ); | ||
| 304 | print $res_error; | ||
| 305 | exit 1; | ||
| 306 | } | ||
| 307 | |||
| 308 | my $reply = $self->jso->decode( $res->decoded_content ); | ||
| 309 | |||
| 310 | p ( $reply, caller_message => "REPLY: __FILENAME__:__LINE__ ", colored => $self->option('colored') ) | ||
| 311 | if $self->option('d') > 2 && ! defined $arg->{inf}->{startCursor}; | ||
| 312 | |||
| 313 | if ( exists $reply->{errors} ) { | ||
| 314 | unshift @{$reply->{errors}}, "--- ERROR ---"; | ||
| 315 | p ( $reply->{errors}, colored => $self->option('colored') ); | ||
| 316 | exit 1; | ||
| 317 | } elsif ( $reply->{data}->{repository}->{packages}->{nodes} ) { | ||
| 318 | print "WARNING: not hardcoded package name \"", $self->option('package'), "\"\n" | ||
| 319 | if $self->option('d') > 1; | ||
| 320 | } | ||
| 321 | |||
| 322 | push @{$arg->{res}}, @{$reply->{data}->{repository}->{packages}->{nodes}->[0]->{versions}->{nodes}}; | ||
| 323 | |||
| 324 | return 1 if $arg->{inf}->{hasPreviousPage} == 0 || $self->option('single_iteration') == 1; | ||
| 325 | |||
| 326 | my $pageInfo = $reply->{data}->{repository}->{packages}->{nodes}->[0]->{versions}->{pageInfo}; | ||
| 327 | $self->get_versions ({ | ||
| 328 | res => $arg->{res}, | ||
| 329 | inf => { | ||
| 330 | startCursor => $pageInfo->{startCursor}, | ||
| 331 | endCursor => $pageInfo->{endCursor}, | ||
| 332 | hasNextPage => $self->jso->decode( $pageInfo->{hasNextPage} ), | ||
| 333 | hasPreviousPage => $self->jso->decode( $pageInfo->{hasPreviousPage} ), | ||
| 334 | } | ||
| 335 | }); | ||
| 336 | |||
| 337 | return 0; | ||
| 338 | } | ||
| 339 | |||
| 340 | |||
| 341 | sub query_default { | ||
| 342 | my ($self, $args) = @_; | ||
| 343 | |||
| 344 | return { | ||
| 345 | query => sprintf('query { repository(name: "%s", owner: "%s") { | ||
| 346 | packages(first: %d names: ["%s"]) { | ||
| 347 | nodes { | ||
| 348 | id | ||
| 349 | name | ||
| 350 | versions(last: %d%s) { | ||
| 351 | nodes { | ||
| 352 | id | ||
| 353 | version | ||
| 354 | files(first:1, orderBy: {direction: DESC, field: CREATED_AT}) { | ||
| 355 | totalCount | ||
| 356 | nodes { | ||
| 357 | updatedAt | ||
| 358 | packageVersion { | ||
| 359 | version | ||
| 360 | id | ||
| 361 | } | ||
| 362 | } | ||
| 363 | } | ||
| 364 | } | ||
| 365 | pageInfo { | ||
| 366 | endCursor | ||
| 367 | hasNextPage | ||
| 368 | hasPreviousPage | ||
| 369 | startCursor | ||
| 370 | } | ||
| 371 | } | ||
| 372 | } | ||
| 373 | } | ||
| 374 | } | ||
| 375 | }', | ||
| 376 | $self->option('repo'), | ||
| 377 | $self->option('user'), | ||
| 378 | $self->option('rows_number'), | ||
| 379 | $self->option('package'), | ||
| 380 | $self->option('rows_number'), | ||
| 381 | $args->{inf}) | ||
| 382 | }; | ||
| 383 | } | ||
| 384 | |||
| 385 | 1; | ||
| 386 | |||
| 387 | |||
| 388 |