push.pl 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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(basename fileparse);
  7. use File::Temp;
  8. use Getopt::Long;
  9. use Mojo::File;
  10. use Mojo::UserAgent;
  11. use Mojo::Util qw(b64_encode decode encode trim);
  12. use Term::UI;
  13. use Term::ReadLine;
  14. my $hubLengthLimit = 25_000;
  15. my $githubBase = 'https://github.com/docker-library/docs/tree/master'; # TODO point this at the correct "dist-xxx" branch based on "namespace"
  16. my $username;
  17. my $password;
  18. my $batchmode;
  19. my $namespace;
  20. my $logos;
  21. GetOptions(
  22. 'u|username=s' => \$username,
  23. 'p|password=s' => \$password,
  24. 'batchmode!' => \$batchmode,
  25. 'namespace=s' => \$namespace,
  26. 'logos!' => \$logos,
  27. ) or die 'bad args';
  28. die 'no repos specified' unless @ARGV;
  29. my $ua = Mojo::UserAgent->new->max_redirects(10);
  30. $ua->transactor->name('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36');
  31. my $term = Term::ReadLine->new('docker-library-docs-push');
  32. unless (defined $username) {
  33. $username = $term->get_reply(prompt => 'Hub Username');
  34. }
  35. unless (defined $password) {
  36. $password = $term->get_reply(prompt => 'Hub Password'); # TODO hide the input? O:)
  37. }
  38. my $login = $ua->post('https://hub.docker.com/v2/users/login/' => {} => json => { username => $username, password => $password });
  39. die 'login failed' unless $login->res->is_success;
  40. my $token = $login->res->json->{token};
  41. my $csrf;
  42. for my $cookie (@{ $login->res->cookies }) {
  43. if ($cookie->name eq 'csrftoken') {
  44. $csrf = $cookie->value;
  45. last;
  46. }
  47. }
  48. die 'missing CSRF token' unless defined $csrf;
  49. my $attemptLogin = $ua->post('https://hub.docker.com/attempt-login/' => {} => json => { jwt => $token });
  50. die 'attempt-login failed' unless $attemptLogin->res->is_success;
  51. my $authorizationHeader = {
  52. Authorization => "JWT $token",
  53. 'X-CSRFToken' => $csrf,
  54. };
  55. my $userData = $ua->get('https://hub.docker.com/v2/user/' => $authorizationHeader);
  56. die 'user failed' unless $userData->res->is_success;
  57. $userData = $userData->res->json;
  58. my $supportedTagsRegex = qr%^(# Supported tags and respective `Dockerfile` links\n\n)(.*?\n)(?=# |\[)%ms;
  59. sub prompt_for_edit {
  60. my $currentText = shift;
  61. my $proposedFile = shift;
  62. my $lengthLimit = shift // 0;
  63. my $proposedText = Mojo::File->new($proposedFile)->slurp // '** FILE MISSING! **';
  64. $proposedText = trim(decode('UTF-8', $proposedText));
  65. # remove our warning about generated files (Hub doesn't support HTML comments in Markdown)
  66. $proposedText =~ s% ^ <!-- .*? --> \s* %%sx;
  67. # extract/re-inject sponsored links
  68. my $sponsoredLinks = '';
  69. if ($currentText =~ m{ ( ^ [#] \Q Sponsored Resources\E \n .*? \n --- \n ) }smx) {
  70. $sponsoredLinks = $1 . "\n";
  71. $proposedText =~ s%$supportedTagsRegex%$sponsoredLinks$1$2%;
  72. }
  73. my $alwaysShortTags = ($proposedFile eq 'neo4j/README.md');
  74. if ($alwaysShortTags || ($lengthLimit > 0 && length($proposedText) > $lengthLimit)) {
  75. # TODO https://github.com/docker/hub-beta-feedback/issues/238
  76. my $fullUrl = "$githubBase/$proposedFile";
  77. my $shortTags = "-\tSee [\"Supported tags and respective \`Dockerfile\` links\" at $fullUrl]($fullUrl#supported-tags-and-respective-dockerfile-links)\n\n";
  78. my $tagsNote = "**Note:** the description for this image is longer than the Hub length limit of $lengthLimit, so the \"Supported tags\" list has been trimmed to compensate. See [docker/hub-beta-feedback#238](https://github.com/docker/hub-beta-feedback/issues/238) for more information.\n\n" . $shortTags;
  79. my $genericNote = "**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.";
  80. my $startingNote = $genericNote . "\n\n";
  81. my $endingNote = "\n\n...\n\n" . $genericNote;
  82. $tagsNote = $shortTags if $alwaysShortTags;
  83. my $trimmedText = $proposedText;
  84. # if our text is too long for the Hub length limit, let's first try removing the "Supported tags" list and add $tagsNote and see if that's enough to let us put the full image documentation
  85. $trimmedText =~ s%$supportedTagsRegex%$sponsoredLinks$1$tagsNote%ms;
  86. # (we scrape until the next "h1" or a line starting with a link which is likely a build status badge for an architecture-namespace)
  87. $proposedText = $trimmedText if $alwaysShortTags;
  88. if (length($trimmedText) > $lengthLimit) {
  89. # ... if that doesn't do the trick, then do our older naïve description trimming
  90. $trimmedText = $startingNote . substr $proposedText, 0, ($lengthLimit - length($startingNote . $endingNote));
  91. # adding the "ending note" (https://github.com/docker/hub-feedback/issues/2220) is a bit more complicated as we have to deal with cutting off markdown ~cleanly so it renders correctly
  92. # TODO deal with "```foo" appropriately (so we don't drop our note in the middle of a code block) - the Hub's current markdown rendering (2022-04-07) does not auto-close a dangling block like this, so this isn't urgent
  93. if ($trimmedText =~ m/\n$/) {
  94. # if we already end with a newline, we should be fine to just trim newlines and add our ending note
  95. $trimmedText =~ s/\n+$//;
  96. }
  97. else {
  98. # otherwise, we need to get a little bit more creative and trim back to the last fully blank line (which we can reasonably assume is safe thanks to our markdownfmt)
  99. $trimmedText =~ s/\n\n(.\n?)*$//;
  100. }
  101. $trimmedText .= $endingNote;
  102. }
  103. $proposedText = $trimmedText;
  104. }
  105. return $currentText if $currentText eq $proposedText;
  106. my @proposedFileBits = fileparse($proposedFile, qr!\.[^.]*!);
  107. my $file = File::Temp->new(SUFFIX => '-' . basename($proposedFileBits[1]) . '-current' . $proposedFileBits[2]);
  108. my $filename = $file->filename;
  109. Mojo::File->new($filename)->spurt(encode('UTF-8', $currentText . "\n"));
  110. my $tempProposedFile = File::Temp->new(SUFFIX => '-' . basename($proposedFileBits[1]) . '-proposed' . $proposedFileBits[2]);
  111. my $tempProposedFilename = $tempProposedFile->filename;
  112. Mojo::File->new($tempProposedFilename)->spurt(encode('UTF-8', $proposedText . "\n"));
  113. system(qw(git --no-pager diff --no-index), $filename, $tempProposedFilename);
  114. my $reply;
  115. if ($batchmode) {
  116. $reply = 'yes';
  117. }
  118. else {
  119. $reply = $term->get_reply(
  120. prompt => 'Apply changes?',
  121. choices => [ qw( yes vimdiff no quit ) ],
  122. default => 'yes',
  123. );
  124. }
  125. if ($reply eq 'quit') {
  126. say 'quitting, as requested';
  127. exit;
  128. }
  129. if ($reply eq 'yes') {
  130. return $proposedText;
  131. }
  132. if ($reply eq 'vimdiff') {
  133. system('vimdiff', $tempProposedFilename, $filename) == 0 or die "vimdiff on $filename and $proposedFile failed";
  134. return trim(decode('UTF-8', Mojo::File->new($filename)->slurp));
  135. }
  136. return $currentText;
  137. }
  138. while (my $repo = shift) { # 'library/hylang', 'tianon/perl', etc
  139. $repo =~ s!^/+|/+$!!; # trim extra slashes (from "*/" globbing, for example)
  140. $repo = $namespace . '/' . $repo if $namespace; # ./push.pl --namespace xxx ...
  141. $repo = 'library/' . $repo unless $repo =~ m!/!; # "hylang" -> "library/hylang"
  142. my $repoName = $repo;
  143. $repoName =~ s!^.*/!!; # 'hylang', 'perl', etc
  144. my $repoUrl = 'https://hub.docker.com/v2/repositories/' . $repo . '/';
  145. if ($logos && $repo =~ m{ ^ library/ }x) {
  146. # the "library" org images include a logo which is displayed in the Hub UI
  147. # if we have a logo file, let's update that metadata first
  148. my $repoLogo120 = $repoName . '/logo-120.png';
  149. if (!-f $repoLogo120) {
  150. my $repoLogoPng = $repoName . '/logo.png';
  151. my $repoLogoSvg = $repoName . '/logo.svg';
  152. my $logoToConvert = (
  153. -f $repoLogoPng
  154. ? $repoLogoPng
  155. : $repoLogoSvg
  156. );
  157. if (-f $logoToConvert) {
  158. say 'converting ' . $logoToConvert . ' to ' . $repoLogo120;
  159. system(
  160. qw( convert -background none -density 1200 -strip -resize 120x120> -gravity center -extent 120x120 ),
  161. $logoToConvert,
  162. $repoLogo120,
  163. ) == 0 or die "failed to convert $logoToConvert into $repoLogo120";
  164. }
  165. }
  166. if (-f $repoLogo120) {
  167. my $proposedLogo = Mojo::File->new($repoLogo120)->slurp;
  168. my $currentLogo = $ua->get('https://d1q6f0aelx0por.cloudfront.net/product-logos/' . join('-', split(m{/}, $repo)) . '-logo.png', { 'Cache-Control' => 'no-cache' });
  169. $currentLogo = ($currentLogo->res->is_success ? $currentLogo->res->body : undef);
  170. if ($currentLogo && $currentLogo eq $proposedLogo) {
  171. say 'no change to ' . $repoName . ' logo; skipping';
  172. }
  173. else {
  174. say 'putting logo ' . $repoLogo120;
  175. my $logoUrl = $repoUrl . 'logo';
  176. my $logoPut = $ua->put($logoUrl => $authorizationHeader => json => {
  177. 'image_data' => b64_encode($proposedLogo),
  178. 'content_type' => 'image/png',
  179. 'file_ext' => 'png',
  180. });
  181. warn 'warning: put to ' . $logoUrl . ' failed: ' . $logoPut->res->text unless $logoPut->res->is_success;
  182. }
  183. }
  184. }
  185. my $repoTx = $ua->get($repoUrl => $authorizationHeader);
  186. warn 'warning: failed to get: ' . $repoUrl . ' (skipping)' and next unless $repoTx->res->is_success;
  187. my $repoDetails = $repoTx->res->json;
  188. $repoDetails->{description} //= '';
  189. $repoDetails->{full_description} //= '';
  190. my $hubShort = prompt_for_edit($repoDetails->{description}, $repoName . '/README-short.txt');
  191. my $hubLong = prompt_for_edit($repoDetails->{full_description}, $repoName . '/README.md', $hubLengthLimit);
  192. say 'no change to ' . $repoName . '; skipping' and next if $repoDetails->{description} eq $hubShort and $repoDetails->{full_description} eq $hubLong;
  193. say 'updating ' . $repoName;
  194. my $repoPatch = $ua->patch($repoUrl => $authorizationHeader => json => {
  195. description => $hubShort,
  196. full_description => $hubLong,
  197. });
  198. warn 'patch to ' . $repoUrl . ' failed: ' . $repoPatch->res->text and next unless $repoPatch->res->is_success;
  199. }