1
0

push.pl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use 5.010;
  5. use open ':encoding(utf8)';
  6. use File::Basename qw(fileparse);
  7. use File::Temp;
  8. use Getopt::Long;
  9. use Mojo::UserAgent;
  10. use Mojo::Util qw(decode encode slurp spurt trim);
  11. use Term::UI;
  12. use Term::ReadLine;
  13. my $hubLengthLimit = 25_000;
  14. my $githubBase = 'https://github.com/docker-library/docs/tree/master';
  15. my $username;
  16. my $password;
  17. my $batchmode;
  18. my $namespace;
  19. GetOptions(
  20. 'u|username=s' => \$username,
  21. 'p|password=s' => \$password,
  22. 'batchmode!' => \$batchmode,
  23. 'namespace=s' => \$namespace,
  24. ) or die 'bad args';
  25. die 'no repos specified' unless @ARGV;
  26. my $ua = Mojo::UserAgent->new->max_redirects(10);
  27. $ua->transactor->name('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36');
  28. my $term = Term::ReadLine->new('docker-library-docs-push');
  29. unless (defined $username) {
  30. $username = $term->get_reply(prompt => 'Hub Username');
  31. }
  32. unless (defined $password) {
  33. $password = $term->get_reply(prompt => 'Hub Password'); # TODO hide the input? O:)
  34. }
  35. my $login = $ua->post('https://hub.docker.com/v2/users/login/' => {} => json => { username => $username, password => $password });
  36. die 'login failed' unless $login->success;
  37. my $token = $login->res->json->{token};
  38. my $attemptLogin = $ua->post('https://hub.docker.com/attempt-login/' => {} => json => { jwt => $token });
  39. die 'attempt-login failed' unless $attemptLogin->success;
  40. my $authorizationHeader = { Authorization => "JWT $token" };
  41. my $userData = $ua->get('https://hub.docker.com/v2/user/' => $authorizationHeader);
  42. die 'user failed' unless $userData->success;
  43. $userData = $userData->res->json;
  44. sub prompt_for_edit {
  45. my $currentText = shift;
  46. my $proposedFile = shift;
  47. my $lengthLimit = shift // 0;
  48. my $proposedText = slurp $proposedFile or warn 'missing ' . $proposedFile;
  49. $proposedText = trim(decode('UTF-8', $proposedText));
  50. # remove our warning about generated files (Hub doesn't support HTML comments in Markdown)
  51. $proposedText =~ s% ^ <!-- .*? --> \s* %%sx;
  52. if ($lengthLimit > 0 && length($proposedText) > $lengthLimit) {
  53. # TODO https://github.com/docker/hub-beta-feedback/issues/238
  54. my $fullUrl = "$githubBase/$proposedFile";
  55. my $note = "**Note:** the description for this image is longer than the Hub length limit of $lengthLimit, so has been trimmed. The full description can be found at [$fullUrl]($fullUrl). See [docker/hub-beta-feedback#238](https://github.com/docker/hub-beta-feedback/issues/238) for more information.\n\n";
  56. $proposedText = $note . substr $proposedText, 0, ($lengthLimit - length($note));
  57. }
  58. return $currentText if $currentText eq $proposedText;
  59. my @proposedFileBits = fileparse($proposedFile, qr!\.[^.]*!);
  60. my $file = File::Temp->new(SUFFIX => $proposedFileBits[2]);
  61. my $filename = $file->filename;
  62. spurt encode('UTF-8', $currentText . "\n"), $filename;
  63. system(qw(git --no-pager diff --no-index), $filename, $proposedFile);
  64. my $reply;
  65. if ($batchmode) {
  66. $reply = 'yes';
  67. }
  68. else {
  69. $reply = $term->get_reply(
  70. prompt => 'Apply changes?',
  71. choices => [ qw( yes vimdiff no quit ) ],
  72. default => 'yes',
  73. );
  74. }
  75. if ($reply eq 'quit') {
  76. say 'quitting, as requested';
  77. exit;
  78. }
  79. if ($reply eq 'yes') {
  80. return $proposedText;
  81. }
  82. if ($reply eq 'vimdiff') {
  83. system('vimdiff', $filename, $proposedFile) == 0 or die "vimdiff on $filename and $proposedFile failed";
  84. return trim(decode('UTF-8', slurp($filename)));
  85. }
  86. return $currentText;
  87. }
  88. while (my $repo = shift) { # 'library/hylang', 'tianon/perl', etc
  89. $repo =~ s!^/+|/+$!!; # trim extra slashes (from "*/" globbing, for example)
  90. $repo = $namespace . '/' . $repo if $namespace; # ./push.pl --namespace xxx ...
  91. $repo = 'library/' . $repo unless $repo =~ m!/!; # "hylang" -> "library/hylang"
  92. my $repoName = $repo;
  93. $repoName =~ s!^.*/!!; # 'hylang', 'perl', etc
  94. my $repoUrl = 'https://hub.docker.com/v2/repositories/' . $repo . '/';
  95. my $repoTx = $ua->get($repoUrl => $authorizationHeader);
  96. warn 'warning: failed to get: ' . $repoUrl . ' (skipping)' and next unless $repoTx->success;
  97. my $repoDetails = $repoTx->res->json;
  98. $repoDetails->{description} //= '';
  99. $repoDetails->{full_description} //= '';
  100. my $hubShort = prompt_for_edit($repoDetails->{description}, $repoName . '/README-short.txt');
  101. my $hubLong = prompt_for_edit($repoDetails->{full_description}, $repoName . '/README.md', $hubLengthLimit);
  102. say 'no change to ' . $repoName . '; skipping' and next if $repoDetails->{description} eq $hubShort and $repoDetails->{full_description} eq $hubLong;
  103. say 'updating ' . $repoName;
  104. my $repoPatch = $ua->patch($repoUrl => $authorizationHeader => json => {
  105. description => $hubShort,
  106. full_description => $hubLong,
  107. });
  108. warn 'patch to ' . $repoUrl . ' failed: ' . $repoPatch->res->text and next unless $repoPatch->success;
  109. }