From 44760267e6bc8a13cc40521a45be6d87c36f8646 Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Sun, 13 Oct 2024 19:47:12 +0100 Subject: [PATCH] Skip the cache when updating an org via the org:info command --- src/Command/CommandBase.php | 11 ++++++----- src/Command/Organization/OrganizationInfoCommand.php | 12 ++++++++---- src/Service/Api.php | 12 ++++++++++++ 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/Command/CommandBase.php b/src/Command/CommandBase.php index 4e759631a..36c274802 100644 --- a/src/Command/CommandBase.php +++ b/src/Command/CommandBase.php @@ -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 @@ -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'); @@ -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); @@ -2294,7 +2295,7 @@ 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; @@ -2302,7 +2303,7 @@ protected function validateOrganizationInput(InputInterface $input, $filterByLin } 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; diff --git a/src/Command/Organization/OrganizationInfoCommand.php b/src/Command/Organization/OrganizationInfoCommand.php index 1d4252c62..b179bf61c 100644 --- a/src/Command/Organization/OrganizationInfoCommand.php +++ b/src/Command/Organization/OrganizationInfoCommand.php @@ -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 @@ -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') @@ -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; @@ -119,6 +122,7 @@ protected function setProperty($property, $value, Organization $organization) } throw $e; } + $this->api()->clearOrganizationCache($organization); $this->stdErr->writeln(sprintf( 'Property %s set to: %s', $property, diff --git a/src/Service/Api.php b/src/Service/Api.php index b95f276c7..04b9e92e5 100644 --- a/src/Service/Api.php +++ b/src/Service/Api.php @@ -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. *