From 56b2b3b2bcbd6797177a21d06efb3a84c3680ac5 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Sat, 6 Jan 2024 18:00:09 +0100 Subject: [PATCH] fix(package-manager): bugs --- .../src/admin/components/AuthMethodModal.tsx | 4 +-- .../js/src/admin/components/ConfigureAuth.tsx | 25 ++++++++++++--- .../admin/components/ConfigureComposer.tsx | 9 +++++- .../js/src/admin/components/Pagination.tsx | 4 +-- .../js/src/admin/components/QueueSection.tsx | 2 ++ .../src/admin/components/TaskOutputModal.tsx | 16 +++++----- .../js/src/admin/states/QueueState.ts | 2 ++ .../ConfigureComposerController.php | 31 ++++++++++++++++--- .../Api/Controller/ListTasksController.php | 2 +- .../src/Composer/ComposerAdapter.php | 20 ++++++++---- .../src/Job/ComposerCommandJob.php | 2 +- 11 files changed, 88 insertions(+), 29 deletions(-) diff --git a/extensions/package-manager/js/src/admin/components/AuthMethodModal.tsx b/extensions/package-manager/js/src/admin/components/AuthMethodModal.tsx index 1629c35b4a..55d1c99fef 100644 --- a/extensions/package-manager/js/src/admin/components/AuthMethodModal.tsx +++ b/extensions/package-manager/js/src/admin/components/AuthMethodModal.tsx @@ -64,12 +64,12 @@ export default class AuthMethodModal this.token((e.target as HTMLTextAreaElement).value)} rows="6" placeholder={ - this.token() === '***' + this.token().startsWith('unchanged:') ? extractText(app.translator.trans('flarum-package-manager.admin.auth_config.add_modal.unchanged_token_placeholder')) : '' } > - {this.token() === '***' ? '' : this.token()} + {this.token().startsWith('unchanged:') ? '' : this.token()}
diff --git a/extensions/package-manager/js/src/admin/components/ConfigureAuth.tsx b/extensions/package-manager/js/src/admin/components/ConfigureAuth.tsx index 7e285048fc..fa48677235 100644 --- a/extensions/package-manager/js/src/admin/components/ConfigureAuth.tsx +++ b/extensions/package-manager/js/src/admin/components/ConfigureAuth.tsx @@ -18,10 +18,17 @@ export default class ConfigureAuth extends ConfigureJson { content(): Mithril.Children { const authSettings = Object.keys(this.settings); + const hasAuthSettings = + authSettings.length && + authSettings.every((type) => { + const data = this.settings[type](); + + return Array.isArray(data) ? data.length : Object.keys(data).length; + }); return (
- {authSettings.length ? ( + {hasAuthSettings ? ( authSettings.map((type) => { const hosts = this.settings[type](); @@ -42,7 +49,7 @@ export default class ConfigureAuth extends ConfigureJson { type, host, token: data, - onsubmit: this.onchange.bind(this), + onsubmit: this.onchange.bind(this, host), }) } > @@ -88,7 +95,7 @@ export default class ConfigureAuth extends ConfigureJson { loading={this.loading} onclick={() => app.modal.show(AuthMethodModal, { - onsubmit: this.onchange.bind(this), + onsubmit: this.onchange.bind(this, null), }) } > @@ -99,7 +106,15 @@ export default class ConfigureAuth extends ConfigureJson { return items; } - onchange(type: string, host: string, token: string) { - this.setting(type)({ ...this.setting(type)(), [host]: token }); + onchange(oldHost: string | null, type: string, host: string, token: string) { + const data = { ...this.setting(type)() }; + + if (oldHost) { + delete data[oldHost]; + } + + data[host] = token; + + this.setting(type)(data); } } diff --git a/extensions/package-manager/js/src/admin/components/ConfigureComposer.tsx b/extensions/package-manager/js/src/admin/components/ConfigureComposer.tsx index e6e8da6f10..e502a7bb9f 100644 --- a/extensions/package-manager/js/src/admin/components/ConfigureComposer.tsx +++ b/extensions/package-manager/js/src/admin/components/ConfigureComposer.tsx @@ -59,7 +59,14 @@ export default class ConfigureComposer extends ConfigureJson { app.modal.show(RepositoryModal, { name, repository, - onsubmit: this.onchange.bind(this), + onsubmit: (repository: Repository, newName: string) => { + const repositories = this.setting('repositories')(); + delete repositories[name]; + + this.setting('repositories')(repositories); + + this.onchange(repository, newName); + }, }) } > diff --git a/extensions/package-manager/js/src/admin/components/Pagination.tsx b/extensions/package-manager/js/src/admin/components/Pagination.tsx index 8d87d08417..90b77281ee 100644 --- a/extensions/package-manager/js/src/admin/components/Pagination.tsx +++ b/extensions/package-manager/js/src/admin/components/Pagination.tsx @@ -15,7 +15,7 @@ export default class Pagination extends Component { return (
@@ -147,6 +148,7 @@ export default class QueueSection extends Component<{}> { // @todo fix in core // @ts-ignore onclick={() => app.modal.show(TaskOutputModal, { task })} + disabled={['pending', 'running'].includes(task.status())} /> ), className: 'Table-controls', diff --git a/extensions/package-manager/js/src/admin/components/TaskOutputModal.tsx b/extensions/package-manager/js/src/admin/components/TaskOutputModal.tsx index aa509dcc16..8922d605f9 100644 --- a/extensions/package-manager/js/src/admin/components/TaskOutputModal.tsx +++ b/extensions/package-manager/js/src/admin/components/TaskOutputModal.tsx @@ -19,14 +19,16 @@ export default class TaskOutputModal
-
- -
- {(this.attrs.task.guessedCause() && - app.translator.trans('flarum-package-manager.admin.exceptions.guessed_cause.' + this.attrs.task.guessedCause())) || - app.translator.trans('flarum-package-manager.admin.sections.queue.output_modal.cause_unknown')} + {this.attrs.task.status() === 'failure' && ( +
+ +
+ {(this.attrs.task.guessedCause() && + app.translator.trans('flarum-package-manager.admin.exceptions.guessed_cause.' + this.attrs.task.guessedCause())) || + app.translator.trans('flarum-package-manager.admin.sections.queue.output_modal.cause_unknown')} +
-
+ )}
diff --git a/extensions/package-manager/js/src/admin/states/QueueState.ts b/extensions/package-manager/js/src/admin/states/QueueState.ts index 7d0e8a009c..9c54ba8e53 100644 --- a/extensions/package-manager/js/src/admin/states/QueueState.ts +++ b/extensions/package-manager/js/src/admin/states/QueueState.ts @@ -36,6 +36,8 @@ export default class QueueState { // Refresh the page window.location.reload(); + } else if (app.packageManager.control.isLoading()) { + app.packageManager.control.setLoading(null); } return data; diff --git a/extensions/package-manager/src/Api/Controller/ConfigureComposerController.php b/extensions/package-manager/src/Api/Controller/ConfigureComposerController.php index 6935b122a6..179369a8dc 100755 --- a/extensions/package-manager/src/Api/Controller/ConfigureComposerController.php +++ b/extensions/package-manager/src/Api/Controller/ConfigureComposerController.php @@ -16,6 +16,7 @@ use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Arr; +use Illuminate\Support\Str; use Laminas\Diactoros\Response\JsonResponse; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -102,6 +103,14 @@ protected function composerConfig(ServerRequestInterface $request): array $this->composerJson->set($composerJson); } + $default = [ + 'minimum-stability' => 'stable', + ]; + + foreach ($this->configurable as $key) { + $composerJson[$key] = Arr::get($composerJson, $key, Arr::get($default, $key)); + } + return Arr::only($composerJson, $this->configurable); } @@ -125,9 +134,23 @@ protected function authConfig(ServerRequestInterface $request): array continue; } - $data[$type][$host] = $token === '***' - ? $authJson[$type][$host] - : $token; + if (str_starts_with($token, 'unchanged:')) { + $old = Str::of($token)->explode(':')->skip(1)->values()->all(); + + if (count($old) !== 2) { + continue; + } + + [$oldType, $oldHost] = $old; + + if (! isset($authJson[$oldType][$oldHost])) { + continue; + } + + $data[$type][$host] = $authJson[$oldType][$oldHost]; + } else { + $data[$type][$host] = $token; + } } } @@ -138,7 +161,7 @@ protected function authConfig(ServerRequestInterface $request): array // Remove tokens from response. foreach ($authJson as $type => $hosts) { foreach ($hosts as $host => $token) { - $authJson[$type][$host] = '***'; + $authJson[$type][$host] = "unchanged:$type:$host"; } } diff --git a/extensions/package-manager/src/Api/Controller/ListTasksController.php b/extensions/package-manager/src/Api/Controller/ListTasksController.php index 5bcf475073..71e5644bcf 100644 --- a/extensions/package-manager/src/Api/Controller/ListTasksController.php +++ b/extensions/package-manager/src/Api/Controller/ListTasksController.php @@ -44,7 +44,7 @@ protected function data(ServerRequestInterface $request, Document $document) $offset = $this->extractOffset($request); $results = Task::query() - ->latest() + ->latest('id') ->offset($offset) ->limit($limit) ->get(); diff --git a/extensions/package-manager/src/Composer/ComposerAdapter.php b/extensions/package-manager/src/Composer/ComposerAdapter.php index f0e535493f..8817772051 100644 --- a/extensions/package-manager/src/Composer/ComposerAdapter.php +++ b/extensions/package-manager/src/Composer/ComposerAdapter.php @@ -38,6 +38,11 @@ class ComposerAdapter */ private $paths; + /** + * @var BufferedOutput|null + */ + private $output = null; + public function __construct(Application $application, OutputLogger $logger, Paths $paths) { $this->application = $application; @@ -49,24 +54,27 @@ public function run(InputInterface $input, ?Task $task = null): ComposerOutput { $this->application->resetComposer(); - $output = new BufferedOutput(); + $this->output = $this->output ?? new BufferedOutput(); // This hack is necessary so that relative path repositories are resolved properly. $currDir = getcwd(); chdir($this->paths->base); - $exitCode = $this->application->run($input, $output); + $exitCode = $this->application->run($input, $this->output); chdir($currDir); $command = Util::readableConsoleInput($input); - $output = $output->fetch(); + $outputContent = $this->output->fetch(); if ($task) { - $task->update(compact('command', 'output')); + $task->update([ + 'command' => $command, + 'output' => $outputContent, + ]); } else { - $this->logger->log($command, $output, $exitCode); + $this->logger->log($command, $outputContent, $exitCode); } - return new ComposerOutput($exitCode, $output); + return new ComposerOutput($exitCode, $outputContent); } public static function setPhpVersion(string $phpVersion) diff --git a/extensions/package-manager/src/Job/ComposerCommandJob.php b/extensions/package-manager/src/Job/ComposerCommandJob.php index a4e0befd12..205d7ce574 100644 --- a/extensions/package-manager/src/Job/ComposerCommandJob.php +++ b/extensions/package-manager/src/Job/ComposerCommandJob.php @@ -53,7 +53,7 @@ public function handle(Dispatcher $bus) public function abort(Throwable $exception) { - if (! $this->command->task->output) { + if (empty($this->command->task->output)) { $this->command->task->output = $exception->getMessage(); }