Skip to content

Commit

Permalink
Skip the cache when updating an org via the org:info command
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Oct 15, 2024
1 parent 0d48751 commit 4476026
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/Command/CommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2249,6 +2249,7 @@ protected function addOrganizationOptions($includeProjectOption = false)
* @param string $filterByCapability
* If no organization is specified, this filters the list of the organizations presented to those with the given
* capability.
* @param bool $skipCache
*
* @return Organization
* @throws NoOrganizationsException if the user does not have any organizations matching the filter
Expand All @@ -2257,7 +2258,7 @@ protected function addOrganizationOptions($includeProjectOption = false)
* @see CommandBase::addOrganizationOptions()
*
*/
protected function validateOrganizationInput(InputInterface $input, $filterByLink = '', $filterByCapability = '')
protected function validateOrganizationInput(InputInterface $input, $filterByLink = '', $filterByCapability = '', $skipCache = false)
{
if (!$this->config()->getWithDefault('api.organizations', false)) {
throw new \BadMethodCallException('Organizations are not enabled');
Expand All @@ -2273,9 +2274,9 @@ protected function validateOrganizationInput(InputInterface $input, $filterByLin
/** @link https://github.com/ulid/spec */
if (\preg_match('#^[0-9A-HJKMNP-TV-Z]{26}$#', $identifier) === 1) {
$this->debug('Detected organization ID format (ULID): ' . $identifier);
$organization = $this->api()->getOrganizationById($identifier);
$organization = $this->api()->getOrganizationById($identifier, $skipCache);
} else {
$organization = $this->api()->getOrganizationByName($identifier);
$organization = $this->api()->getOrganizationByName($identifier, $skipCache);
}
if (!$organization) {
throw new ConsoleInvalidArgumentException('Organization not found: ' . $identifier);
Expand All @@ -2294,15 +2295,15 @@ protected function validateOrganizationInput(InputInterface $input, $filterByLin
if ($this->hasSelectedProject()) {
$project = $this->getSelectedProject();
$this->ensurePrintSelectedProject();
$organization = $this->api()->getOrganizationById($project->getProperty('organization'));
$organization = $this->api()->getOrganizationById($project->getProperty('organization'), $skipCache);
if ($organization) {
$this->stdErr->writeln(\sprintf('Project organization: %s', $this->api()->getOrganizationLabel($organization)));
return $organization;
}
} elseif (($currentProject = $this->getCurrentProject(true)) && $currentProject->hasProperty('organization')) {
$organizationId = $currentProject->getProperty('organization');
try {
$organization = $this->api()->getOrganizationById($organizationId);
$organization = $this->api()->getOrganizationById($organizationId, $skipCache);
} catch (BadResponseException $e) {
$this->debug('Error when fetching project organization: ' . $e->getMessage());
$organization = false;
Expand Down
12 changes: 8 additions & 4 deletions src/Command/Organization/OrganizationInfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Platformsh\Client\Model\Organization\Organization;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class OrganizationInfoCommand extends OrganizationCommandBase
Expand All @@ -19,7 +20,8 @@ protected function configure()
->setDescription('View or change organization details')
->addOrganizationOptions(true)
->addArgument('property', InputArgument::OPTIONAL, 'The name of a property to view or change')
->addArgument('value', InputArgument::OPTIONAL, 'A new value for the property');
->addArgument('value', InputArgument::OPTIONAL, 'A new value for the property')
->addOption('refresh', null, InputOption::VALUE_NONE, 'Refresh the cache');
PropertyFormatter::configureInput($this->getDefinition());
Table::configureInput($this->getDefinition());
$this->addExample('View the organization "acme"', '--org acme')
Expand All @@ -29,18 +31,19 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$organization = $this->validateOrganizationInput($input);
$property = $input->getArgument('property');
$value = $input->getArgument('value');
$skipCache = $value !== null || $input->getOption('refresh');
$organization = $this->validateOrganizationInput($input, '', '', $skipCache);

/** @var PropertyFormatter $formatter */
$formatter = $this->getService('property_formatter');

$property = $input->getArgument('property');
if ($property === null) {
$this->listProperties($organization);
return 0;
}

$value = $input->getArgument('value');
if ($value === null) {
$formatter->displayData($output, $this->getProperties($organization), $property);
return 0;
Expand Down Expand Up @@ -119,6 +122,7 @@ protected function setProperty($property, $value, Organization $organization)
}
throw $e;
}
$this->api()->clearOrganizationCache($organization);
$this->stdErr->writeln(sprintf(
'Property <info>%s</info> set to: %s',
$property,
Expand Down
12 changes: 12 additions & 0 deletions src/Service/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,18 @@ public function getOrganizationByName($name, $reset = false)
return $this->getOrganizationById('name=' . $name, $reset);
}

/**
* Clears the cache for an organization.
*
* @param Organization $org
* @return void
*/
public function clearOrganizationCache(Organization $org)
{
$this->cache->delete('organization:' . $org->id);
$this->cache->delete('organization:name=' . $org->name);
}

/**
* Returns the Console URL for a project, with caching.
*
Expand Down

0 comments on commit 4476026

Please sign in to comment.