push.pl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. GetOptions(
  19. 'u|username=s' => \$username,
  20. 'p|password=s' => \$password,
  21. 'batchmode!' => \$batchmode,
  22. ) or die 'bad args';
  23. die 'no repos specified' unless @ARGV;
  24. my $ua = Mojo::UserAgent->new->max_redirects(10);
  25. $ua->transactor->name('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36');
  26. my $term = Term::ReadLine->new('docker-library-docs-push');
  27. unless (defined $username) {
  28. $username = $term->get_reply(prompt => 'Hub Username');
  29. }
  30. unless (defined $password) {
  31. $password = $term->get_reply(prompt => 'Hub Password'); # TODO hide the input? O:)
  32. }
  33. my $login = $ua->post('https://hub.docker.com/v2/users/login/' => {} => json => { username => $username, password => $password });
  34. die 'login failed' unless $login->success;
  35. my $token = $login->res->json->{token};
  36. my $attemptLogin = $ua->post('https://hub.docker.com/attempt-login/' => {} => json => { jwt => $token });
  37. die 'attempt-login failed' unless $attemptLogin->success;
  38. my $authorizationHeader = { Authorization => "JWT $token" };
  39. my $userData = $ua->get('https://hub.docker.com/v2/user/' => $authorizationHeader);
  40. die 'user failed' unless $userData->success;
  41. $userData = $userData->res->json;
  42. sub prompt_for_edit {
  43. my $currentText = shift;
  44. my $proposedFile = shift;
  45. my $lengthLimit = shift // 0;
  46. my $proposedText = slurp $proposedFile or warn 'missing ' . $proposedFile;
  47. $proposedText = trim(decode('UTF-8', $proposedText));
  48. if ($lengthLimit > 0 && length($proposedText) > $lengthLimit) {
  49. # TODO https://github.com/docker/hub-beta-feedback/issues/238
  50. my $fullUrl = "$githubBase/$proposedFile";
  51. 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";
  52. $proposedText = $note . substr $proposedText, 0, ($lengthLimit - length($note));
  53. }
  54. return $currentText if $currentText eq $proposedText;
  55. my @proposedFileBits = fileparse($proposedFile, qr!\.[^.]*!);
  56. my $file = File::Temp->new(SUFFIX => $proposedFileBits[2]);
  57. my $filename = $file->filename;
  58. spurt encode('UTF-8', $currentText . "\n"), $filename;
  59. system(qw(git --no-pager diff --no-index), $filename, $proposedFile);
  60. my $reply;
  61. if ($batchmode) {
  62. $reply = 'yes';
  63. }
  64. else {
  65. $reply = $term->get_reply(
  66. prompt => 'Apply changes?',
  67. choices => [ qw( yes vimdiff no quit ) ],
  68. default => 'yes',
  69. );
  70. }
  71. if ($reply eq 'quit') {
  72. say 'quitting, as requested';
  73. exit;
  74. }
  75. if ($reply eq 'yes') {
  76. return $proposedText;
  77. }
  78. if ($reply eq 'vimdiff') {
  79. system('vimdiff', $filename, $proposedFile) == 0 or die "vimdiff on $filename and $proposedFile failed";
  80. return trim(decode('UTF-8', slurp($filename)));
  81. }
  82. return $currentText;
  83. }
  84. while (my $repo = shift) { # '/library/hylang', '/tianon/perl', etc
  85. $repo =~ s!/+$!!;
  86. $repo = '/library/' . $repo unless $repo =~ m!/!;
  87. $repo = '/' . $repo unless $repo =~ m!^/!;
  88. my $repoName = $repo;
  89. $repoName =~ s!^.*/!!; # 'hylang', 'perl', etc
  90. my $repoUrl = 'https://hub.docker.com/v2/repositories' . $repo . '/';
  91. my $repoTx = $ua->get($repoUrl => $authorizationHeader);
  92. warn 'failed to get: ' . $repoUrl and next unless $repoTx->success;
  93. my $repoDetails = $repoTx->res->json;
  94. $repoDetails->{description} //= '';
  95. $repoDetails->{full_description} //= '';
  96. my $hubShort = prompt_for_edit($repoDetails->{description}, $repoName . '/README-short.txt');
  97. my $hubLong = prompt_for_edit($repoDetails->{full_description}, $repoName . '/README.md', $hubLengthLimit);
  98. say 'no change to ' . $repoName . '; skipping' and next if $repoDetails->{description} eq $hubShort and $repoDetails->{full_description} eq $hubLong;
  99. say 'updating ' . $repoName;
  100. my $repoPatch = $ua->patch($repoUrl => $authorizationHeader => json => {
  101. description => $hubShort,
  102. full_description => $hubLong,
  103. });
  104. warn 'patch to ' . $repoUrl . ' failed: ' . $repoPatch->res->text and next unless $repoPatch->success;
  105. }