From 8d8eb45278b94c5f2d0fb18ba64e823d83fb4ca8 Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Wed, 6 Dec 2023 11:07:26 +0000 Subject: [PATCH] Fix: always attempt to choose a default environment when asking interactively This should now apply again even when filtering is active, e.g. for the 'ssh' command. --- src/Command/CommandBase.php | 20 ++++++++---------- src/Command/CompletionCommand.php | 3 ++- .../Environment/EnvironmentPushCommand.php | 2 +- src/Service/Api.php | 21 ++++++++----------- 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/Command/CommandBase.php b/src/Command/CommandBase.php index d300c3e2ce..7cd6e19497 100644 --- a/src/Command/CommandBase.php +++ b/src/Command/CommandBase.php @@ -1153,7 +1153,8 @@ protected function selectEnvironment($environmentId = null, $required = true, $s if ($environmentId !== null) { if ($environmentId === self::DEFAULT_ENVIRONMENT_CODE) { $this->stdErr->writeln(sprintf('Selecting default environment (indicated by %s)', $environmentId)); - $environment = $this->api()->getDefaultEnvironment($this->project, true); + $environments = $this->api()->getEnvironments($this->project); + $environment = $this->api()->getDefaultEnvironment($environments, $this->project, true); if (!$environment) { throw new \RuntimeException('Default environment not found'); } @@ -1179,7 +1180,8 @@ protected function selectEnvironment($environmentId = null, $required = true, $s if ($selectDefaultEnv) { $this->debug('No environment specified or detected: finding a default...'); - $environment = $this->api()->getDefaultEnvironment($this->project); + $environments = $this->api()->getEnvironments($this->project); + $environment = $this->api()->getDefaultEnvironment($environments, $this->project); if ($environment) { $this->stdErr->writeln(\sprintf('Selected default environment: %s', $this->api()->getEnvironmentLabel($environment))); $this->printedSelectedEnvironment = true; @@ -1204,7 +1206,7 @@ protected function selectEnvironment($environmentId = null, $required = true, $s } if (count($environments) > 0) { $this->debug('No environment specified or detected: offering a choice...'); - $this->environment = $this->offerEnvironmentChoice($environments, $filter === null); + $this->environment = $this->offerEnvironmentChoice($environments); return; } } @@ -1498,24 +1500,20 @@ private function offerProjectChoice(array $projectInfos) * Offers a choice of environments. * * @param Environment[] $environments - * @param bool $autoDefault Whether to pick a default environment for the project. * * @return Environment */ - final protected function offerEnvironmentChoice(array $environments, $autoDefault = true) + final protected function offerEnvironmentChoice(array $environments) { if (!isset($this->input) || !isset($this->output) || !$this->input->isInteractive()) { throw new \BadMethodCallException('Not interactive: an environment choice cannot be offered.'); } + $defaultEnvironment = $this->api()->getDefaultEnvironment($environments, $this->project); + $defaultEnvironmentId = $defaultEnvironment ? $defaultEnvironment->id : null; + /** @var \Platformsh\Cli\Service\QuestionHelper $questionHelper */ $questionHelper = $this->getService('question_helper'); - if ($autoDefault) { - $defaultEnvironment = $this->api()->getDefaultEnvironment($this->project); - $defaultEnvironmentId = $defaultEnvironment ? $defaultEnvironment->id : null; - } else { - $defaultEnvironmentId = null; - } if (count($environments) > (new Terminal())->getHeight() / 2) { $ids = array_keys($environments); diff --git a/src/Command/CompletionCommand.php b/src/Command/CompletionCommand.php index c3bee0ad3a..cda28cb507 100644 --- a/src/Command/CompletionCommand.php +++ b/src/Command/CompletionCommand.php @@ -243,7 +243,8 @@ public function getAppNames() } } } elseif ($project = $this->getProject()) { - if ($environment = $this->api->getDefaultEnvironment($project, false, false)) { + $environments = $this->api->getEnvironments($project, false); + if ($environments && ($environment = $this->api->getDefaultEnvironment($environments, $project))) { $apps = array_keys($environment->getSshUrls()); } } diff --git a/src/Command/Environment/EnvironmentPushCommand.php b/src/Command/Environment/EnvironmentPushCommand.php index f9a8181109..e19fd1924e 100644 --- a/src/Command/Environment/EnvironmentPushCommand.php +++ b/src/Command/Environment/EnvironmentPushCommand.php @@ -478,7 +478,7 @@ private function findTargetParent(Project $project, $targetEnvironment) { if ($this->hasSelectedEnvironment()) { $defaultId = $this->getSelectedEnvironment()->id; } else { - $default = $this->api()->getDefaultEnvironment($project); + $default = $this->api()->getDefaultEnvironment($environments, $project); $defaultId = $default ? $default->id : null; } if (array_keys($environments) === [$defaultId]) { diff --git a/src/Service/Api.php b/src/Service/Api.php index 52a855c274..220334dd34 100644 --- a/src/Service/Api.php +++ b/src/Service/Api.php @@ -1341,23 +1341,25 @@ public function hasCachedCurrentDeployment(Environment $environment) * This may be the one set as the project's default_branch, or another * environment, e.g. if the user only has access to 1 environment. * - * @param Project $project + * @param Environment[] $envs + * @param Project $project * @param bool $onlyDefaultBranch Only use the default_branch. - * @param bool|null $refresh * * @return Environment|null */ - public function getDefaultEnvironment(Project $project, $onlyDefaultBranch = false, $refresh = null) + public function getDefaultEnvironment(array $envs, Project $project, $onlyDefaultBranch = false) { if ($project->default_branch === '') { throw new \RuntimeException('Default branch not set'); } - if ($env = $this->getEnvironment($project->default_branch, $project, $refresh)) { - return $env; - } elseif ($onlyDefaultBranch) { + foreach ($envs as $env) { + if ($env->id === $project->default_branch) { + return $env; + } + } + if ($onlyDefaultBranch) { return null; } - $envs = $this->getEnvironments($project, $refresh); // If there is only one environment, use that. if (count($envs) <= 1) { @@ -1380,11 +1382,6 @@ public function getDefaultEnvironment(Project $project, $onlyDefaultBranch = fal return \reset($main); } - // Select the environment matching the default branch. - if (isset($envs[$project->default_branch])) { - return $envs[$project->default_branch]; - } - return null; }