Skip to content

Commit

Permalink
Fix: always attempt to choose a default environment when asking inter…
Browse files Browse the repository at this point in the history
…actively

This should now apply again even when filtering is active, e.g. for the 'ssh'
command.
  • Loading branch information
pjcdawkins committed Dec 6, 2023
1 parent 592e74a commit 8d8eb45
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
20 changes: 9 additions & 11 deletions src/Command/CommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <info>%s</info>)', $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');
}
Expand All @@ -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;
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/Command/CompletionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Environment/EnvironmentPushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down
21 changes: 9 additions & 12 deletions src/Service/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down

0 comments on commit 8d8eb45

Please sign in to comment.