From 352cb71e243a5069d64c7229de4491816ff1f70f Mon Sep 17 00:00:00 2001 From: Flavio Heleno Date: Tue, 31 Oct 2023 14:16:18 -0300 Subject: [PATCH] Replace broken packagist:get-data with packagist:get-package --- app/console.php | 5 +- bin/console.php | 8 +- .../Console/Packagist/GetDataCommand.php | 425 ------------------ .../Console/Packagist/GetPackageCommand.php | 115 +++++ .../Console/Packagist/MassImportCommand.php | 2 +- 5 files changed, 123 insertions(+), 432 deletions(-) delete mode 100644 src/Application/Console/Packagist/GetDataCommand.php create mode 100644 src/Application/Console/Packagist/GetPackageCommand.php diff --git a/app/console.php b/app/console.php index f622a08e..21b5a86f 100644 --- a/app/console.php +++ b/app/console.php @@ -2,20 +2,21 @@ declare(strict_types = 1); use DI\ContainerBuilder; -use PackageHealth\PHP\Application\Console\Packagist\GetDataCommand; use PackageHealth\PHP\Application\Console\Packagist\GetListCommand; +use PackageHealth\PHP\Application\Console\Packagist\GetPackageCommand; use PackageHealth\PHP\Application\Console\Packagist\GetUpdatesCommand; use PackageHealth\PHP\Application\Console\Packagist\MassImportCommand; use PackageHealth\PHP\Application\Console\Queue\ConsumeCommand; use PackageHealth\PHP\Application\Console\Queue\ListCommand; use PackageHealth\PHP\Application\Console\Queue\SendCommandCommand; use PackageHealth\PHP\Application\Console\Queue\SendEventCommand; + use function DI\autowire; return static function (ContainerBuilder $containerBuilder): void { $containerBuilder->addDefinitions( [ - GetDataCommand::class => autowire(GetDataCommand::class), + GetPackageCommand::class => autowire(GetPackageCommand::class), GetListCommand::class => autowire(GetListCommand::class), GetUpdatesCommand::class => autowire(GetUpdatesCommand::class), MassImportCommand::class => autowire(MassImportCommand::class), diff --git a/bin/console.php b/bin/console.php index 77e87cc0..76eb6723 100755 --- a/bin/console.php +++ b/bin/console.php @@ -12,8 +12,8 @@ use Composer\InstalledVersions; use DI\ContainerBuilder; -use PackageHealth\PHP\Application\Console\Packagist\GetDataCommand; use PackageHealth\PHP\Application\Console\Packagist\GetListCommand; +use PackageHealth\PHP\Application\Console\Packagist\GetPackageCommand; use PackageHealth\PHP\Application\Console\Packagist\GetUpdatesCommand; use PackageHealth\PHP\Application\Console\Packagist\MassImportCommand; use PackageHealth\PHP\Application\Console\Queue\ConsumeCommand; @@ -84,12 +84,12 @@ $app->setCommandLoader( new FactoryCommandLoader( [ - GetDataCommand::getDefaultName() => static function () use ($container): GetDataCommand { - return $container->get(GetDataCommand::class); - }, GetListCommand::getDefaultName() => static function () use ($container): GetListCommand { return $container->get(GetListCommand::class); }, + GetPackageCommand::getDefaultName() => static function () use ($container): GetPackageCommand { + return $container->get(GetPackageCommand::class); + }, GetUpdatesCommand::getDefaultName() => static function () use ($container): GetUpdatesCommand { return $container->get(GetUpdatesCommand::class); }, diff --git a/src/Application/Console/Packagist/GetDataCommand.php b/src/Application/Console/Packagist/GetDataCommand.php deleted file mode 100644 index 58d104d0..00000000 --- a/src/Application/Console/Packagist/GetDataCommand.php +++ /dev/null @@ -1,425 +0,0 @@ -addOption( - 'mirror', - 'm', - InputOption::VALUE_REQUIRED, - 'Packagist mirror url', - 'https://packagist.org' - ) - ->addArgument( - 'package', - InputArgument::REQUIRED, - 'The package name (e.g. symfony/console)' - ); - } - - /** - * Command execution. - * - * @param \Symfony\Component\Console\Input\InputInterface $input - * @param \Symfony\Component\Console\Output\OutputInterface $output - * - * @return int - */ - protected function execute(InputInterface $input, OutputInterface $output): int { - try { - // i/o styling - $io = new SymfonyStyle($input, $output); - $io->text( - sprintf( - '[%s] Started with pid %d', - date('H:i:s'), - posix_getpid() - ) - ); - - $mirror = $input->getOption('mirror'); - if (filter_var($mirror, FILTER_VALIDATE_URL) === false) { - throw new InvalidArgumentException('Invalid mirror option'); - } - - $packageName = $input->getArgument('package'); - - $metadata = $this->packagist->getPackageMetadataVersion1($packageName, $mirror); - - $packageCol = $this->packageRepository->find( - [ - 'name' => "{$vendor}/{$project}" - ], - 1 - ); - - if ($packageCol->isEmpty()) { - throw new PackageNotFoundException(); - } - - $package = $packageCol->first(); - $package = $package - ->withDescription($metadata['description'] ?? '') - ->withUrl($metadata['repository'] ?? ''); - $package = $this->packageRepository->update($package); - - // current latest version to check for updates - $latestVersion = $package->getLatestVersion(); - $latestVersionNormalized = $latestVersion ?? $this->versionParser->normalize($latestVersion); - - if ($this->statsRepository->exists($packageName)) { - $stats = $this->statsRepository->get($packageName); - $stats = $stats - ->withGithubStars($metadata['github_stars'] ?? 0) - ->withGithubWatchers($metadata['github_watchers'] ?? 0) - ->withGithubForks($metadata['github_forks'] ?? 0) - ->withDependents($metadata['dependents'] ?? 0) - ->withSuggesters($metadata['suggesters'] ?? 0) - ->withFavers($metadata['favers'] ?? 0) - ->withTotalDownloads($metadata['downloads']['total'] ?? 0) - ->withMonthlyDownloads($metadata['downloads']['monthly'] ?? 0) - ->withDailyDownloads($metadata['downloads']['daily'] ?? 0); - - $stats = $this->statsRepository->update($stats); - } else { - $stats = $this->statsRepository->create( - $packageName, - $metadata['github_stars'] ?? 0, - $metadata['github_watchers'] ?? 0, - $metadata['github_forks'] ?? 0, - $metadata['dependents'] ?? 0, - $metadata['suggesters'] ?? 0, - $metadata['favers'] ?? 0, - $metadata['downloads']['total'] ?? 0, - $metadata['downloads']['monthly'] ?? 0, - $metadata['downloads']['daily'] ?? 0 - ); - } - - if ($output->isVerbose()) { - $io->text( - sprintf( - '[%s] Found %d releases', - date('H:i:s'), - count($metadata['versions']) - ) - ); - } - - if (count($metadata['versions']) === 0) { - return Command::SUCCESS; - } - - foreach (array_reverse($metadata['versions']) as $release) { - // exclude branches from tagged releases (https://getcomposer.org/doc/articles/versions.md#branches) - $isBranch = preg_match('/^dev-|-dev$/', $release['version']) === 1; - - $versionCol = $this->versionRepository->find( - [ - 'package_id' => $package->getId(), - 'number' => $release['version'], - 'normalized' => $release['version_normalized'], - 'release' => $isBranch === false - ], - 1 - ); - - $version = $versionCol->first(); - if ($version === null) { - if ($output->isVeryVerbose()) { - $io->text( - sprintf( - '[%s] New %s %s found', - date('H:i:s'), - $isBranch ? 'branch' : 'version', - $release['version'] - ) - ); - } - - $version = $this->versionRepository->create( - $package->getId(), - $release['version'], - $release['version_normalized'], - $isBranch === false, - VersionStatusEnum::Unknown - ); - } - - // track new version releases - if ($isBranch === false && Comparator::greaterThan($release['version_normalized'], $latestVersionNormalized)) { - $latestVersion = $release['version']; - $latestVersionNormalized = $release['version_normalized']; - } - - // track "require" dependencies - $filteredRequire = array_filter( - $release['require'] ?? [], - static function (string $key): bool { - return preg_match('/^(php|hhvm|ext-.*|lib-.*|pear-.*)$/', $key) !== 1 && - preg_match('/^[^\/]+\/[^\/]+$/', $key) === 1; - }, - ARRAY_FILTER_USE_KEY - ); - - // flag packages without require dependencies with VersionStatusEnum::NoDeps - if (empty($filteredRequire)) { - if ($output->isDebug()) { - $io->text( - sprintf( - '[%s] %s %s has no required dependencies', - date('H:i:s'), - $isBranch ? 'Branch' : 'Version', - $version->getNumber() - ) - ); - } - - $version = $version->withStatus(VersionStatusEnum::NoDeps); - $version = $this->versionRepository->update($version); - } - - foreach ($filteredRequire as $dependencyName => $constraint) { - if ($constraint === 'self.version') { - // need to find out how to handle this - continue; - } - - if (isset($packageList[$dependencyName]) === false) { - $packageCol = $this->packageRepository->find( - [ - 'name' => $dependencyName - ], - 1 - ); - - $packageList[$dependencyName] = $packageCol->first()->getLatestVersion() ?? ''; - } - - $dependencyCol = $this->dependencyRepository->find( - [ - 'version_id' => $version->getId(), - 'name' => $dependencyName, - 'development' => false - ], - 1 - ); - - if ($dependencyCol->isEmpty() === false) { - $dependency = $dependencyCol->first(); - $dependency = $dependency - ->withConstraint($constraint) - ->withStatus( - empty($packageList[$dependencyName]) ? - DependencyStatusEnum::Unknown : - ( - Semver::satisfies($packageList[$dependencyName], $constraint) ? - DependencyStatusEnum::UpToDate : - DependencyStatusEnum::Outdated - ) - ); - $dependency = $this->dependencyRepository->update($dependency); - - continue; - } - - $dependency = $this->dependencyRepository->create( - $version->getId(), - $dependencyName, - $constraint, - false, - empty($packageList[$dependencyName]) ? - DependencyStatusEnum::Unknown : - ( - Semver::satisfies($packageList[$dependencyName], $constraint) ? - DependencyStatusEnum::UpToDate : - DependencyStatusEnum::Outdated - ) - ); - } - - // track "require-dev" dependencies - $filteredRequireDev = array_filter( - $release['require-dev'] ?? [], - static function (string $key): bool { - return preg_match('/^(php|hhvm|ext-.*|lib-.*|pear-.*)$/', $key) !== 1 && - preg_match('/^[^\/]+\/[^\/]+$/', $key) === 1; - }, - ARRAY_FILTER_USE_KEY - ); - - if (empty($filteredRequireDev) && $output->isDebug()) { - $io->text( - sprintf( - '[%s] %s %s has no required development dependencies', - date('H:i:s'), - $isBranch ? 'Branch' : 'Version', - $version->getNumber() - ) - ); - } - - foreach ($filteredRequireDev as $dependencyName => $constraint) { - if ($constraint === 'self.version') { - // need to find out how to handle this - continue; - } - - if (isset($packageList[$dependencyName]) === false) { - $packageCol = $this->packageRepository->find( - [ - 'name' => $dependencyName - ], - 1 - ); - - $packageList[$dependencyName] = $packageCol->first()->getLatestVersion() ?? ''; - } - - $dependencyCol = $this->dependencyRepository->find( - [ - 'version_id' => $version->getId(), - 'name' => $dependencyName, - 'development' => true - ], - 1 - ); - - if ($dependencyCol->isEmpty() === false) { - $dependency = $dependencyCol->first(); - $dependency = $dependency - ->withConstraint($constraint) - ->withStatus( - empty($packageList[$dependencyName]) ? - DependencyStatusEnum::Unknown : - ( - Semver::satisfies($packageList[$dependencyName], $constraint) ? - DependencyStatusEnum::UpToDate : - DependencyStatusEnum::Outdated - ) - ); - $dependency = $this->dependencyRepository->update($dependency); - - continue; - } - - $dependency = $this->dependencyRepository->create( - $version->getId(), - $dependencyName, - $constraint, - true, - empty($packageList[$dependencyName]) ? - DependencyStatusEnum::Unknown : - ( - Semver::satisfies($packageList[$dependencyName], $constraint) ? - DependencyStatusEnum::UpToDate : - DependencyStatusEnum::Outdated - ) - ); - } - } - - // update package latest tagged version if it was changed - $package = $package->withLatestVersion($latestVersion); - if ($package->isDirty()) { - if ($output->isVerbose()) { - $io->text( - sprintf( - '[%s] Updating latest package release to version %s', - date('H:i:s'), - $latestVersion - ) - ); - } - - $package = $this->packageRepository->update($package); - } - - $io->text( - sprintf( - '[%s] Done', - date('H:i:s') - ) - ); - } catch (Exception $exception) { - $io->error( - sprintf( - '[%s] %s', - date('H:i:s'), - $exception->getMessage() - ) - ); - if ($output->isDebug()) { - $io->listing(explode(PHP_EOL, $exception->getTraceAsString())); - } - - return Command::FAILURE; - } - - return Command::SUCCESS; - } - - public function __construct( - PackageRepositoryInterface $packageRepository, - VersionRepositoryInterface $versionRepository, - DependencyRepositoryInterface $dependencyRepository, - StatsRepositoryInterface $statsRepository, - Producer $producer, - VersionParser $versionParser, - Packagist $packagist - ) { - $this->packageRepository = $packageRepository; - $this->versionRepository = $versionRepository; - $this->dependencyRepository = $dependencyRepository; - $this->statsRepository = $statsRepository; - $this->producer = $producer; - $this->versionParser = $versionParser; - $this->packagist = $packagist; - - parent::__construct(); - } -} diff --git a/src/Application/Console/Packagist/GetPackageCommand.php b/src/Application/Console/Packagist/GetPackageCommand.php new file mode 100644 index 00000000..b0d2551f --- /dev/null +++ b/src/Application/Console/Packagist/GetPackageCommand.php @@ -0,0 +1,115 @@ +addOption( + 'mirror', + 'm', + InputOption::VALUE_REQUIRED, + 'Packagist mirror url', + 'https://packagist.org' + ) + ->addOption( + 'offline', + null, + InputOption::VALUE_NONE, + 'Work in offline mode' + ) + ->addArgument( + 'package', + InputArgument::REQUIRED, + 'The package name (e.g. symfony/console)' + ); + } + + /** + * Command execution. + * + * @param \Symfony\Component\Console\Input\InputInterface $input + * @param \Symfony\Component\Console\Output\OutputInterface $output + * + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output): int { + try { + // i/o styling + $io = new SymfonyStyle($input, $output); + $io->text( + sprintf( + '[%s] Started with pid %d', + date('H:i:s'), + posix_getpid() + ) + ); + + $mirror = $input->getOption('mirror'); + if (filter_var($mirror, FILTER_VALIDATE_URL) === false) { + throw new InvalidArgumentException('Invalid mirror option'); + } + + $workOffline = (bool)$input->getOption('offline'); + $packageName = $input->getArgument('package'); + + $this->producer->sendCommand( + new PackageDiscoveryCommand($packageName, workOffline: $workOffline) + ); + + $io->text( + sprintf( + '[%s] Done', + date('H:i:s') + ) + ); + } catch (Exception $exception) { + $io->error( + sprintf( + '[%s] %s', + date('H:i:s'), + $exception->getMessage() + ) + ); + if ($output->isDebug()) { + $io->listing(explode(PHP_EOL, $exception->getTraceAsString())); + } + + return Command::FAILURE; + } + + return Command::SUCCESS; + } + + public function __construct(Producer $producer) { + $this->producer = $producer; + + parent::__construct(); + } +} diff --git a/src/Application/Console/Packagist/MassImportCommand.php b/src/Application/Console/Packagist/MassImportCommand.php index 63c4233b..167fd304 100644 --- a/src/Application/Console/Packagist/MassImportCommand.php +++ b/src/Application/Console/Packagist/MassImportCommand.php @@ -67,7 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $pattern = $input->getArgument('pattern'); - $command = $this->getApplication()->find('packagist:get-data'); + $command = $this->getApplication()->find('packagist:get-package'); $packageCol = $this->packageRepository->findMatching( [