Skip to content

Commit

Permalink
added "all" and "showonly" flags to occ app:update (#256)
Browse files Browse the repository at this point in the history
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 <bigcat88@icloud.com>
  • Loading branch information
bigcat88 authored Mar 28, 2024
1 parent c4e56dc commit 508ecaa
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 2 additions & 5 deletions lib/Command/ExApp/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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));
Expand All @@ -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));
Expand Down
41 changes: 38 additions & 3 deletions lib/Command/ExApp/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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("<error>Please specify an app to update or \"--all\" to update all updatable apps</error>");
return 1;
} elseif (!empty($appId) && $input->getOption('all')) {
$output->writeln("<error>The \"--all\" flag is mutually exclusive with specifying app</error>");
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')
);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 508ecaa

Please sign in to comment.