|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Packagist. |
| 5 | + * |
| 6 | + * (c) Jordi Boggiano <[email protected]> |
| 7 | + * Nils Adermann <[email protected]> |
| 8 | + * |
| 9 | + * For the full copyright and license information, please view the LICENSE |
| 10 | + * file that was distributed with this source code. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace App\Command; |
| 14 | + |
| 15 | +use App\Entity\AuditRecord; |
| 16 | +use App\Entity\Package; |
| 17 | +use App\Entity\User; |
| 18 | +use App\Util\DoctrineTrait; |
| 19 | +use Composer\Console\Input\InputOption; |
| 20 | +use Doctrine\Persistence\ManagerRegistry; |
| 21 | +use Symfony\Component\Console\Command\Command; |
| 22 | +use Symfony\Component\Console\Helper\Table; |
| 23 | +use Symfony\Component\Console\Input\InputArgument; |
| 24 | +use Symfony\Component\Console\Input\InputInterface; |
| 25 | +use Symfony\Component\Console\Output\OutputInterface; |
| 26 | + |
| 27 | +class TransferOwnershipCommand extends Command |
| 28 | +{ |
| 29 | + use DoctrineTrait; |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + private readonly ManagerRegistry $doctrine, |
| 33 | + ) |
| 34 | + { |
| 35 | + parent::__construct(); |
| 36 | + } |
| 37 | + |
| 38 | + protected function configure(): void |
| 39 | + { |
| 40 | + $this |
| 41 | + ->setName('packagist:transfer-ownership') |
| 42 | + ->setDescription('Transfer all packages of a vendor') |
| 43 | + ->setDefinition([ |
| 44 | + new InputArgument('vendorOrPackage', InputArgument::REQUIRED,'Vendor or package name'), |
| 45 | + new InputArgument('maintainers', InputArgument::IS_ARRAY|InputArgument::REQUIRED, 'The usernames of the new maintainers'), |
| 46 | + new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Dry run'), |
| 47 | + ]) |
| 48 | + ; |
| 49 | + } |
| 50 | + |
| 51 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 52 | + { |
| 53 | + $dryRun = $input->getOption('dry-run'); |
| 54 | + |
| 55 | + if ($dryRun) { |
| 56 | + $output->writeln('ℹ️ DRY RUN'); |
| 57 | + } |
| 58 | + |
| 59 | + $vendorOrPackage = $input->getArgument('vendorOrPackage'); |
| 60 | + $maintainers = $this->queryAndValidateMaintainers($input, $output); |
| 61 | + |
| 62 | + if (!count($maintainers)) { |
| 63 | + return Command::FAILURE; |
| 64 | + } |
| 65 | + |
| 66 | + $packages = $this->queryPackages($vendorOrPackage); |
| 67 | + |
| 68 | + if (!count($packages)) { |
| 69 | + $output->writeln(sprintf('<error>No packages found for %s</error>', $vendorOrPackage)); |
| 70 | + return Command::FAILURE; |
| 71 | + } |
| 72 | + |
| 73 | + $this->outputPackageTable($output, $packages, $maintainers); |
| 74 | + |
| 75 | + if (!$dryRun) { |
| 76 | + $this->transferOwnership($packages, $maintainers); |
| 77 | + } |
| 78 | + |
| 79 | + return Command::SUCCESS; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @return User[] |
| 84 | + */ |
| 85 | + private function queryAndValidateMaintainers(InputInterface $input, OutputInterface $output): array |
| 86 | + { |
| 87 | + $usernames = array_map('mb_strtolower', $input->getArgument('maintainers')); |
| 88 | + sort($usernames); |
| 89 | + |
| 90 | + $maintainers = $this->getEM()->getRepository(User::class)->findUsersByUsername($usernames, ['usernameCanonical' => 'ASC']); |
| 91 | + |
| 92 | + if (array_keys($maintainers) === $usernames) { |
| 93 | + return $maintainers; |
| 94 | + } |
| 95 | + |
| 96 | + $notFound = []; |
| 97 | + |
| 98 | + foreach ($usernames as $username) { |
| 99 | + if (!array_key_exists($username, $maintainers)) { |
| 100 | + $notFound[] = $username; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + sort($notFound); |
| 105 | + |
| 106 | + $output->writeln(sprintf('<error>%d maintainers could not be found: %s</error>', count($notFound), implode(', ', $notFound))); |
| 107 | + |
| 108 | + return []; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @return Package[] |
| 113 | + */ |
| 114 | + private function queryPackages(string $vendorOrPackage): array |
| 115 | + { |
| 116 | + $repository = $this->getEM()->getRepository(Package::class); |
| 117 | + $isPackageName = str_contains($vendorOrPackage, '/'); |
| 118 | + |
| 119 | + if ($isPackageName) { |
| 120 | + $package = $repository->findOneBy(['name' => $vendorOrPackage]); |
| 121 | + |
| 122 | + return $package ? [$package] : []; |
| 123 | + } |
| 124 | + |
| 125 | + return $repository->findBy([ |
| 126 | + 'vendor' => $vendorOrPackage |
| 127 | + ]); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param Package[] $packages |
| 132 | + * @param User[] $maintainers |
| 133 | + */ |
| 134 | + private function outputPackageTable(OutputInterface $output, array $packages, array $maintainers): void |
| 135 | + { |
| 136 | + $rows = []; |
| 137 | + |
| 138 | + usort($packages, fn (Package $a, Package $b) => strcasecmp($a->getName(), $b->getName())); |
| 139 | + |
| 140 | + $newMaintainers = array_map(fn (User $user) => $user->getUsername(), $maintainers); |
| 141 | + |
| 142 | + foreach ($packages as $package) { |
| 143 | + $currentMaintainers = $package->getMaintainers()->map(fn (User $user) => $user->getUsername())->toArray(); |
| 144 | + sort($currentMaintainers); |
| 145 | + |
| 146 | + $rows[] = [ |
| 147 | + $package->getVendor(), |
| 148 | + $package->getPackageName(), |
| 149 | + implode(', ', $currentMaintainers), |
| 150 | + implode(', ', $newMaintainers), |
| 151 | + ]; |
| 152 | + } |
| 153 | + |
| 154 | + $table = new Table($output); |
| 155 | + $table |
| 156 | + ->setHeaders(['Vendor', 'Package', 'Current Maintainers', 'New Maintainers']) |
| 157 | + ->setRows($rows) |
| 158 | + ; |
| 159 | + $table->render(); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @param Package[] $packages |
| 164 | + * @param User[] $maintainers |
| 165 | + */ |
| 166 | + private function transferOwnership(array $packages, array $maintainers): void |
| 167 | + { |
| 168 | + $normalizedMaintainers = array_values(array_map(fn (User $user) => $user->getId(), $maintainers)); |
| 169 | + sort($normalizedMaintainers, SORT_NUMERIC); |
| 170 | + |
| 171 | + foreach ($packages as $package) { |
| 172 | + $oldMaintainers = $package->getMaintainers()->toArray(); |
| 173 | + |
| 174 | + $normalizedOldMaintainers = array_values(array_map(fn (User $user) => $user->getId(), $oldMaintainers)); |
| 175 | + sort($normalizedOldMaintainers, SORT_NUMERIC); |
| 176 | + if ($normalizedMaintainers === $normalizedOldMaintainers) { |
| 177 | + continue; |
| 178 | + } |
| 179 | + |
| 180 | + $package->getMaintainers()->clear(); |
| 181 | + foreach ($maintainers as $maintainer) { |
| 182 | + $package->addMaintainer($maintainer); |
| 183 | + } |
| 184 | + |
| 185 | + $this->doctrine->getManager()->persist(AuditRecord::packageTransferred($package, null, $oldMaintainers, array_values($maintainers))); |
| 186 | + } |
| 187 | + |
| 188 | + $this->doctrine->getManager()->flush(); |
| 189 | + } |
| 190 | +} |
0 commit comments