From 508ecaa4aab469d50f856502592a62ec6e89b4ec Mon Sep 17 00:00:00 2001 From: Alexander Piskun <13381981+bigcat88@users.noreply.github.com> Date: Thu, 28 Mar 2024 11:24:24 +0300 Subject: [PATCH] added "all" and "showonly" flags to occ app:update (#256) They do almost the same like the original flags from Server repo with one difference: `--showonly` flag can be specified only with `--all` flag. We can not easy make `--showonly` work for specified appid, cause we support updating ExApps with specifyng `json` or `xml` and not only by `appid`. Signed-off-by: Alexander Piskun --- CHANGELOG.md | 6 ++++- lib/Command/ExApp/Register.php | 7 ++---- lib/Command/ExApp/Update.php | 41 +++++++++++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50e15831..d48ab024 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). -## [2.3.2 - 2024-03-26] +## [2.3.2 - 2024-03-28] + +### Added + +- `--all` and `--showonly` flags to `occ app_api:app:update` command. #256 ### Fixed diff --git a/lib/Command/ExApp/Register.php b/lib/Command/ExApp/Register.php index 9e15cd40..73ac16f4 100644 --- a/lib/Command/ExApp/Register.php +++ b/lib/Command/ExApp/Register.php @@ -13,7 +13,6 @@ use OCA\AppAPI\Service\ExAppApiScopeService; use OCA\AppAPI\Service\ExAppScopesService; use OCA\AppAPI\Service\ExAppService; -use OCA\AppAPI\Service\ExAppUsersService; use OCP\IConfig; use OCP\Security\ISecureRandom; @@ -33,7 +32,6 @@ public function __construct( private readonly DaemonConfigService $daemonConfigService, private readonly ExAppScopesService $exAppScopesService, private readonly ExAppApiScopeService $exAppApiScopeService, - private readonly ExAppUsersService $exAppUsersService, private readonly DockerActions $dockerActions, private readonly ManualActions $manualActions, private readonly IConfig $config, @@ -134,7 +132,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $appInfo['port'] = $appInfo['port'] ?? $this->exAppService->getExAppFreePort(); $appInfo['secret'] = $appInfo['secret'] ?? $this->random->generate(128); $appInfo['daemon_config_name'] = $appInfo['daemon_config_name'] ?? $daemonConfigName; - $appInfo['api_scopes'] = $this->exAppApiScopeService->mapScopeNamesToNumbers($appInfo['external-app']['scopes']); + $appInfo['api_scopes'] = array_values($this->exAppApiScopeService->mapScopeNamesToNumbers($appInfo['external-app']['scopes'])); $exApp = $this->exAppService->registerExApp($appInfo); if (!$exApp) { $this->logger->error(sprintf('Error during registering ExApp %s.', $appId)); @@ -144,8 +142,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 3; } if (count($appInfo['external-app']['scopes']) > 0) { - if (!$this->exAppScopesService->registerExAppScopes($exApp, $appInfo['api_scopes']) - ) { + if (!$this->exAppScopesService->registerExAppScopes($exApp, $this->exAppApiScopeService->mapScopeNamesToNumbers($appInfo['external-app']['scopes']))) { $this->logger->error(sprintf('Error while registering API scopes for %s.', $appId)); if ($outputConsole) { $output->writeln(sprintf('Error while registering API scopes for %s.', $appId)); diff --git a/lib/Command/ExApp/Update.php b/lib/Command/ExApp/Update.php index 63baaca6..62e741e1 100644 --- a/lib/Command/ExApp/Update.php +++ b/lib/Command/ExApp/Update.php @@ -8,6 +8,7 @@ use OCA\AppAPI\DeployActions\DockerActions; use OCA\AppAPI\DeployActions\ManualActions; use OCA\AppAPI\Fetcher\ExAppArchiveFetcher; +use OCA\AppAPI\Fetcher\ExAppFetcher; use OCA\AppAPI\Service\AppAPIService; use OCA\AppAPI\Service\DaemonConfigService; use OCA\AppAPI\Service\ExAppApiScopeService; @@ -35,6 +36,7 @@ public function __construct( private readonly ManualActions $manualActions, private readonly LoggerInterface $logger, private readonly ExAppArchiveFetcher $exAppArchiveFetcher, + private readonly ExAppFetcher $exAppFetcher, ) { parent::__construct(); } @@ -43,19 +45,52 @@ protected function configure(): void { $this->setName('app_api:app:update'); $this->setDescription('Update ExApp'); - $this->addArgument('appid', InputArgument::REQUIRED); + $this->addArgument('appid', InputArgument::OPTIONAL, 'Update the specified app'); $this->addOption('info-xml', null, InputOption::VALUE_REQUIRED, 'Path to ExApp info.xml file (url or local absolute path)'); $this->addOption('json-info', null, InputOption::VALUE_REQUIRED, 'ExApp info.xml in JSON format'); $this->addOption('force-scopes', null, InputOption::VALUE_NONE, 'Force new ExApp scopes approval'); $this->addOption('wait-finish', null, InputOption::VALUE_NONE, 'Wait until finish'); $this->addOption('silent', null, InputOption::VALUE_NONE, 'Do not print to console'); + $this->addOption('all', null, InputOption::VALUE_NONE, 'Update all updatable apps'); + $this->addOption('showonly', null, InputOption::VALUE_NONE, 'Additional flag for "--all" to only show all updatable apps'); } protected function execute(InputInterface $input, OutputInterface $output): int { - $outputConsole = !$input->getOption('silent'); $appId = $input->getArgument('appid'); + if (empty($appId) && !$input->getOption('all')) { + $output->writeln("Please specify an app to update or \"--all\" to update all updatable apps"); + return 1; + } elseif (!empty($appId) && $input->getOption('all')) { + $output->writeln("The \"--all\" flag is mutually exclusive with specifying app"); + return 1; + } elseif ($input->getOption('all')) { + $apps = $this->exAppFetcher->get(); + $appsWithUpdates = array_filter($apps, function (array $app) { + $exApp = $this->exAppService->getExApp($app['id']); + $newestVersion = $app['releases'][0]['version']; + return $exApp !== null && isset($app['releases'][0]['version']) && version_compare($newestVersion, $exApp->getVersion(), '>'); + }); + if ($input->getOption('showonly')) { + foreach ($appsWithUpdates as $appWithUpdate) { + $output->writeln($appWithUpdate['id'] . ' new version available: ' . $appWithUpdate['releases'][0]['version']); + } + return 0; + } + $return = 0; + foreach ($appsWithUpdates as $appWithUpdate) { + $result = $this->updateExApp($input, $output, $appWithUpdate['id']); + if ($result > 0) { + $return = $result; + } + } + return $return; + } + return $this->updateExApp($input, $output, $appId); + } + private function updateExApp(InputInterface $input, OutputInterface $output, string $appId): int { + $outputConsole = !$input->getOption('silent'); $appInfo = $this->exAppService->getAppInfo( $appId, $input->getOption('info-xml'), $input->getOption('json-info') ); @@ -136,7 +171,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } } - $appInfo['api_scopes'] = $this->exAppApiScopeService->mapScopeNamesToNumbers($appInfo['external-app']['scopes']); + $appInfo['api_scopes'] = array_values($this->exAppApiScopeService->mapScopeNamesToNumbers($appInfo['external-app']['scopes'])); if (!$this->exAppService->updateExAppInfo($exApp, $appInfo)) { $this->logger->error(sprintf('Failed to update ExApp %s info', $appId)); if ($outputConsole) {