-
-
Notifications
You must be signed in to change notification settings - Fork 479
Add command to transfer ownership #1609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+434
−1
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7bab04e
Add command to transfer ownership in bulk
stevenrombauts 7200a78
Track package transferred event for each individual package
stevenrombauts 466eed6
Use `admin` as fallback string for actor when transferring packages i…
stevenrombauts 33169ca
Allow transfer command to accept a single package name too
stevenrombauts 102f955
Use `mb_strtolower` to conform with the canonicalization in `User::se…
stevenrombauts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of Packagist. | ||
| * | ||
| * (c) Jordi Boggiano <j.boggiano@seld.be> | ||
| * Nils Adermann <naderman@naderman.de> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace App\Command; | ||
|
|
||
| use App\Entity\AuditRecord; | ||
| use App\Entity\Package; | ||
| use App\Entity\User; | ||
| use App\Util\DoctrineTrait; | ||
| use Composer\Console\Input\InputOption; | ||
| use Doctrine\Persistence\ManagerRegistry; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Helper\Table; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| class TransferOwnershipCommand extends Command | ||
| { | ||
| use DoctrineTrait; | ||
|
|
||
| public function __construct( | ||
| private readonly ManagerRegistry $doctrine, | ||
| ) | ||
| { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| protected function configure(): void | ||
| { | ||
| $this | ||
| ->setName('packagist:transfer-ownership') | ||
| ->setDescription('Transfer all packages of a vendor') | ||
| ->setDefinition([ | ||
| new InputArgument('vendorOrPackage', InputArgument::REQUIRED,'Vendor or package name'), | ||
| new InputArgument('maintainers', InputArgument::IS_ARRAY|InputArgument::REQUIRED, 'The usernames of the new maintainers'), | ||
| new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Dry run'), | ||
| ]) | ||
| ; | ||
| } | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output): int | ||
| { | ||
| $dryRun = $input->getOption('dry-run'); | ||
|
|
||
| if ($dryRun) { | ||
| $output->writeln('ℹ️ DRY RUN'); | ||
| } | ||
|
|
||
| $vendorOrPackage = $input->getArgument('vendorOrPackage'); | ||
| $maintainers = $this->queryAndValidateMaintainers($input, $output); | ||
|
|
||
| if (!count($maintainers)) { | ||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| $packages = $this->queryPackages($vendorOrPackage); | ||
|
|
||
| if (!count($packages)) { | ||
| $output->writeln(sprintf('<error>No packages found for %s</error>', $vendorOrPackage)); | ||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| $this->outputPackageTable($output, $packages, $maintainers); | ||
|
|
||
| if (!$dryRun) { | ||
| $this->transferOwnership($packages, $maintainers); | ||
| } | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| /** | ||
| * @return User[] | ||
| */ | ||
| private function queryAndValidateMaintainers(InputInterface $input, OutputInterface $output): array | ||
| { | ||
| $usernames = array_map('mb_strtolower', $input->getArgument('maintainers')); | ||
| sort($usernames); | ||
|
|
||
| $maintainers = $this->getEM()->getRepository(User::class)->findUsersByUsername($usernames, ['usernameCanonical' => 'ASC']); | ||
|
|
||
| if (array_keys($maintainers) === $usernames) { | ||
| return $maintainers; | ||
| } | ||
|
|
||
| $notFound = []; | ||
|
|
||
| foreach ($usernames as $username) { | ||
| if (!array_key_exists($username, $maintainers)) { | ||
| $notFound[] = $username; | ||
| } | ||
| } | ||
|
|
||
| sort($notFound); | ||
|
|
||
| $output->writeln(sprintf('<error>%d maintainers could not be found: %s</error>', count($notFound), implode(', ', $notFound))); | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| /** | ||
| * @return Package[] | ||
| */ | ||
| private function queryPackages(string $vendorOrPackage): array | ||
| { | ||
| $repository = $this->getEM()->getRepository(Package::class); | ||
| $isPackageName = str_contains($vendorOrPackage, '/'); | ||
|
|
||
| if ($isPackageName) { | ||
| $package = $repository->findOneBy(['name' => $vendorOrPackage]); | ||
|
|
||
| return $package ? [$package] : []; | ||
| } | ||
|
|
||
| return $repository->findBy([ | ||
| 'vendor' => $vendorOrPackage | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * @param Package[] $packages | ||
| * @param User[] $maintainers | ||
| */ | ||
| private function outputPackageTable(OutputInterface $output, array $packages, array $maintainers): void | ||
| { | ||
| $rows = []; | ||
|
|
||
| usort($packages, fn (Package $a, Package $b) => strcasecmp($a->getName(), $b->getName())); | ||
|
|
||
| $newMaintainers = array_map(fn (User $user) => $user->getUsername(), $maintainers); | ||
|
|
||
| foreach ($packages as $package) { | ||
| $currentMaintainers = $package->getMaintainers()->map(fn (User $user) => $user->getUsername())->toArray(); | ||
| sort($currentMaintainers); | ||
|
|
||
| $rows[] = [ | ||
| $package->getVendor(), | ||
| $package->getPackageName(), | ||
| implode(', ', $currentMaintainers), | ||
| implode(', ', $newMaintainers), | ||
| ]; | ||
| } | ||
|
|
||
| $table = new Table($output); | ||
| $table | ||
| ->setHeaders(['Vendor', 'Package', 'Current Maintainers', 'New Maintainers']) | ||
| ->setRows($rows) | ||
| ; | ||
| $table->render(); | ||
| } | ||
|
|
||
| /** | ||
| * @param Package[] $packages | ||
| * @param User[] $maintainers | ||
| */ | ||
| private function transferOwnership(array $packages, array $maintainers): void | ||
| { | ||
| foreach ($packages as $package) { | ||
| $oldMaintainers = $package->getMaintainers()->toArray(); | ||
|
|
||
| $package->getMaintainers()->clear(); | ||
| foreach ($maintainers as $maintainer) { | ||
| $package->addMaintainer($maintainer); | ||
| } | ||
|
|
||
| $this->doctrine->getManager()->persist(AuditRecord::packageTransferred($package, null, $oldMaintainers, array_values($maintainers))); | ||
| } | ||
|
|
||
| $this->doctrine->getManager()->flush(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about packages that were already own by the new maintainers ? Should they actually be marked as transferred to the same maintainers ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on my understanding, this transfer command is only necessary when people no longer have access, and it's highly unlikely that current maintainers remain in place. Besides that, storing all old and new maintainers also tells us exactly what arguments the command was used with?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When applying that to a whole vendor namespace, the existing packages might have different maintainers. Maybe one of them already has only the new maintainers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think what we could do here is at least check that the old and new arent' an exact match and if they are skip transferring that package as that's a noop. I'll add that post merge.