Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display the correct Console URL in the welcome command #1363

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 3 additions & 24 deletions src/Command/WebCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Platformsh\Cli\Command;

use GuzzleHttp\Exception\BadResponseException;
use Platformsh\Cli\Service\Url;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -41,31 +40,11 @@ protected function execute(InputInterface $input, OutputInterface $output)

if ($this->hasSelectedProject()) {
$project = $this->getSelectedProject();
if ($this->config()->has('service.console_url') && $this->config()->get('api.organizations')) {
// Load the organization name if possible.
$firstSegment = $organizationId = $project->getProperty('organization');
try {
$organization = $this->api()->getClient()->getOrganizationById($organizationId);
if ($organization) {
$firstSegment = $organization->name;
}
} catch (BadResponseException $e) {
if ($e->getResponse() && $e->getResponse()->getStatusCode() === 403) {
trigger_error($e->getMessage(), E_USER_WARNING);
} else {
throw $e;
}
}

$isConsole = true;
$url = ltrim($this->config()->get('service.console_url'), '/') . '/' . rawurlencode($firstSegment) . '/' . rawurlencode($project->id);
} else {
$subscription = $this->api()->getClient()->getSubscription($project->getSubscriptionId());
$url = $subscription->project_ui;
$isConsole = $this->config()->has('detection.console_domain') && parse_url($url, PHP_URL_HOST) === $this->config()->get('detection.console_domain');
}
$url = $this->api()->getConsoleURL($project);
if ($environmentId !== null) {
// Console links lack the /environments path component.
$isConsole = $this->config()->has('detection.console_domain')
&& parse_url($url, PHP_URL_HOST) === $this->config()->get('detection.console_domain');
if ($isConsole) {
$url .= '/' . rawurlencode($environmentId);
} else {
Expand Down
3 changes: 1 addition & 2 deletions src/Command/WelcomeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ private function defaultWelcome()
*/
private function welcomeForLocalProjectDir(Project $project)
{
$projectUri = $project->getLink('#ui');
$this->stdErr->writeln("Project title: <info>{$project->title}</info>");
$this->stdErr->writeln("Project ID: <info>{$project->id}</info>");
$this->stdErr->writeln("Project dashboard: <info>$projectUri</info>\n");
$this->stdErr->writeln("Project dashboard: <info>" . $this->api()->getConsoleURL($project) . "</info>\n");

// Show the environments.
$this->runOtherCommand('environments', [
Expand Down
55 changes: 55 additions & 0 deletions src/Service/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1555,4 +1555,59 @@ public function getUserRefLabel(UserRef $userRef, $tag = 'info')
}
return \sprintf($pattern, $tag, $name, $userRef->email);
}

/**
* Loads an organization by ID, with caching.
*
* @param string $id
* @param bool $reset
* @return Organization|false
*/
public function getOrganizationById($id, $reset = false)
{
$cacheKey = 'organization:' . $id;
if (!$reset && ($cached = $this->cache->fetch($cacheKey))) {
return new Organization($cached, $cached['_url'], $this->getHttpClient());
}
$organization = $this->getClient()->getOrganizationById($id);
if ($organization) {
$data = $organization->getData();
$data['_url'] = $organization->getUri();
$this->cache->save($cacheKey, $data, $this->config->getWithDefault('api.orgs_ttl', 3600));
}
return $organization;
}

/**
* Returns the Console URL for a project, with caching.
*
* @param Project $project
* @param bool $reset
*
* @return false|string
*/
public function getConsoleURL(Project $project, $reset = false)
{
if ($this->config->has('service.console_url') && $this->config->get('api.organizations')) {
// Load the organization name if possible.
$firstSegment = $organizationId = $project->getProperty('organization');
try {
$organization = $this->getOrganizationById($organizationId, $reset);
if ($organization) {
$firstSegment = $organization->name;
}
} catch (BadResponseException $e) {
if ($e->getResponse() && $e->getResponse()->getStatusCode() === 403) {
trigger_error($e->getMessage(), E_USER_WARNING);
} else {
throw $e;
}
}

return ltrim($this->config->get('service.console_url'), '/') . '/' . rawurlencode($firstSegment) . '/' . rawurlencode($project->id);
}
$subscription = $this->loadSubscription($project->getSubscriptionId(), $project);
return $subscription ? $subscription->project_ui : false;
}

}
Loading