From f277bce176f1048c75b3ba57dd3ea8ba10a51c38 Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Mon, 30 Sep 2024 10:08:42 +0100 Subject: [PATCH] Fix and improve project suspension warnings (#1486) --- composer.json | 2 +- composer.lock | 14 +++---- src/Command/CommandBase.php | 27 +++++++++++++ .../Environment/EnvironmentSshCommand.php | 6 +++ src/Command/WelcomeCommand.php | 39 +++++++------------ 5 files changed, 55 insertions(+), 33 deletions(-) diff --git a/composer.json b/composer.json index 1e279ce85..7645b10b0 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "guzzlehttp/guzzle": "^5.3", "guzzlehttp/ringphp": "^1.1", "platformsh/console-form": ">=0.0.37 <2.0", - "platformsh/client": ">=0.85.0 <2.0", + "platformsh/client": ">=0.85.1 <2.0", "symfony/console": "^3.0 >=3.2", "symfony/yaml": "^3.0 || ^2.6", "symfony/finder": "^3.0", diff --git a/composer.lock b/composer.lock index bed6f5be0..50623f76f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "cd92506c5bd717cea914d8fdf106fd8d", + "content-hash": "7255190656bf6176881b02e6a6ec9dda", "packages": [ { "name": "cocur/slugify", @@ -921,16 +921,16 @@ }, { "name": "platformsh/client", - "version": "0.85.0", + "version": "0.85.1", "source": { "type": "git", "url": "https://github.com/platformsh/platformsh-client-php.git", - "reference": "36425b70260021b230352a05b572ed15e2eb5bf2" + "reference": "fcd1969473031cbef7f3f9c8177a1d9487a5e924" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/platformsh/platformsh-client-php/zipball/36425b70260021b230352a05b572ed15e2eb5bf2", - "reference": "36425b70260021b230352a05b572ed15e2eb5bf2", + "url": "https://api.github.com/repos/platformsh/platformsh-client-php/zipball/fcd1969473031cbef7f3f9c8177a1d9487a5e924", + "reference": "fcd1969473031cbef7f3f9c8177a1d9487a5e924", "shasum": "" }, "require": { @@ -962,9 +962,9 @@ "description": "Platform.sh API client", "support": { "issues": "https://github.com/platformsh/platformsh-client-php/issues", - "source": "https://github.com/platformsh/platformsh-client-php/tree/0.85.0" + "source": "https://github.com/platformsh/platformsh-client-php/tree/0.85.1" }, - "time": "2024-07-01T19:03:46+00:00" + "time": "2024-09-26T12:20:25+00:00" }, { "name": "platformsh/console-form", diff --git a/src/Command/CommandBase.php b/src/Command/CommandBase.php index 21be74e2b..0b32812bd 100644 --- a/src/Command/CommandBase.php +++ b/src/Command/CommandBase.php @@ -2472,4 +2472,31 @@ protected function validateResourcesInitInput(InputInterface $input, Project $pr } return $resourcesInit; } + + /** + * Warn the user if a project is suspended. + * + * @param \Platformsh\Client\Model\Project $project + */ + protected function warnIfSuspended(Project $project) + { + if ($project->isSuspended()) { + $this->stdErr->writeln('This project is suspended.'); + if ($this->config()->getWithDefault('warnings.project_suspended_payment', true)) { + $orgId = $project->getProperty('organization', false); + if ($orgId) { + try { + $organization = $this->api()->getClient()->getOrganizationById($orgId); + } catch (BadResponseException $e) { + $organization = false; + } + if ($organization && $organization->hasLink('payment-source')) { + $this->stdErr->writeln(sprintf('To re-activate it, update the payment details for your organization, %s.', $this->api()->getOrganizationLabel($organization, 'comment'))); + } + } elseif ($project->owner === $this->api()->getMyUserId()) { + $this->stdErr->writeln('To re-activate it, update your payment details.'); + } + } + } + } } diff --git a/src/Command/Environment/EnvironmentSshCommand.php b/src/Command/Environment/EnvironmentSshCommand.php index 9d66b2cd8..59fa92b3b 100644 --- a/src/Command/Environment/EnvironmentSshCommand.php +++ b/src/Command/Environment/EnvironmentSshCommand.php @@ -91,6 +91,12 @@ protected function execute(InputInterface $input, OutputInterface $output) $exitCode = $shell->executeSimple($command, null, $ssh->getEnv()); if ($exitCode !== 0) { + if ($this->getSelectedProject()->isSuspended()) { + $this->stdErr->writeln(''); + $this->warnIfSuspended($this->getSelectedProject()); + return $exitCode; + } + /** @var \Platformsh\Cli\Service\SshDiagnostics $diagnostics */ $diagnostics = $this->getService('ssh_diagnostics'); $diagnostics->diagnoseFailureWithTest($sshUrl, $start, $exitCode); diff --git a/src/Command/WelcomeCommand.php b/src/Command/WelcomeCommand.php index 97e7a2509..87f5d33ba 100644 --- a/src/Command/WelcomeCommand.php +++ b/src/Command/WelcomeCommand.php @@ -2,6 +2,7 @@ namespace Platformsh\Cli\Command; +use GuzzleHttp\Exception\BadResponseException; use Platformsh\Client\Model\Project; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -70,32 +71,17 @@ private function welcomeForLocalProjectDir(Project $project) $this->stdErr->writeln("Project ID: {$project->id}"); $this->stdErr->writeln("Project dashboard: " . $this->api()->getConsoleURL($project) . "\n"); - // Show the environments. - $this->runOtherCommand('environments', [ - '--project' => $project->id, - ]); - $executable = $this->config()->get('application.executable'); - $this->stdErr->writeln("\nYou can list other projects by running $executable projects"); - } - - /** - * Warn the user if a project is suspended. - * - * @param \Platformsh\Client\Model\Project $project - */ - private function warnIfSuspended(Project $project) - { if ($project->isSuspended()) { - $messages = []; - $messages[] = 'This project is suspended.'; - if ($this->config()->getWithDefault('warnings.project_suspended_payment', true)) { - if ($project->owner === $this->api()->getMyUserId()) { - $messages[] = 'Update your payment details to re-activate it'; - } - } - $messages[] = ''; - $this->stdErr->writeln($messages); + $this->warnIfSuspended($project); + } else { + // Show the environments. + $this->runOtherCommand('environments', [ + '--project' => $project->id, + ]); } + + $executable = $this->config()->get('application.executable'); + $this->stdErr->writeln("\nYou can list other projects by running $executable projects"); } /** @@ -128,7 +114,10 @@ private function welcomeOnContainer() $this->stdErr->writeln('Application name: ' . $appName . ''); } - $this->warnIfSuspended($project); + if ($project->isSuspended()) { + $this->warnIfSuspended($project); + return; + } } else { $this->stdErr->writeln('Project ID: ' . $projectId . ''); if ($environmentId) {