From 534b695dad6ed7a714f1ea8319170199cd85cd8e Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 14 Jul 2026 18:49:31 +0530 Subject: [PATCH] Collapse envVariables/requiredEnvVariables into per-field variables The two arrays were both keyed by the same semantic names and had to be kept manually in sync -- adding a field to one but forgetting the other (or a typo) would silently make getEnv() return empty and isConfigured() silently treat the whole provider as unconfigured, with no error anywhere. Each field is now self-contained: name => {required, envVariable}. --- app/config/vcs.php | 30 ++++++++++++++---------------- src/Appwrite/Vcs/Factory.php | 14 +++++++------- tests/unit/Vcs/FactoryTest.php | 26 +++++++++++++------------- 3 files changed, 34 insertions(+), 36 deletions(-) diff --git a/app/config/vcs.php b/app/config/vcs.php index f456f80d96..6df0cc924d 100644 --- a/app/config/vcs.php +++ b/app/config/vcs.php @@ -10,26 +10,24 @@ use Utopia\VCS\Adapter\Git\GitHub; return [ 'github' => [ 'adapter' => GitHub::class, - 'envVariables' => [ - 'APP_NAME' => '_APP_VCS_GITHUB_APP_NAME', - 'PRIVATE_KEY' => '_APP_VCS_GITHUB_PRIVATE_KEY', - 'APP_ID' => '_APP_VCS_GITHUB_APP_ID', - 'CLIENT_ID' => '_APP_VCS_GITHUB_CLIENT_ID', - 'CLIENT_SECRET' => '_APP_VCS_GITHUB_CLIENT_SECRET', - 'WEBHOOK_SECRET' => '_APP_VCS_GITHUB_WEBHOOK_SECRET', + 'variables' => [ + 'appName' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_APP_NAME'], + 'privateKey' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_PRIVATE_KEY'], + 'appId' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_APP_ID'], + 'clientId' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_CLIENT_ID'], + 'clientSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_CLIENT_SECRET'], + 'webhookSecret' => ['required' => false, 'envVariable' => '_APP_VCS_GITHUB_WEBHOOK_SECRET'], ], - 'requiredEnvVariables' => ['APP_NAME', 'PRIVATE_KEY', 'APP_ID', 'CLIENT_ID', 'CLIENT_SECRET'], ], 'gitea' => [ 'adapter' => Gitea::class, - 'envVariables' => [ - 'ENDPOINT' => '_APP_VCS_GITEA_ENDPOINT', - 'CLIENT_ID' => '_APP_VCS_GITEA_CLIENT_ID', - 'CLIENT_SECRET' => '_APP_VCS_GITEA_CLIENT_SECRET', - 'WEBHOOK_SECRET' => '_APP_VCS_GITEA_WEBHOOK_SECRET', + 'variables' => [ + 'endpoint' => ['required' => true, 'envVariable' => '_APP_VCS_GITEA_ENDPOINT'], + 'clientId' => ['required' => true, 'envVariable' => '_APP_VCS_GITEA_CLIENT_ID'], + 'clientSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITEA_CLIENT_SECRET'], + // Unlike GitHub's legacy optional secret, Gitea webhooks must + // always have a shared secret because Appwrite creates them directly. + 'webhookSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITEA_WEBHOOK_SECRET'], ], - // Unlike GitHub's legacy optional secret, Gitea webhooks must always - // have a shared secret because Appwrite creates them directly. - 'requiredEnvVariables' => ['ENDPOINT', 'CLIENT_ID', 'CLIENT_SECRET', 'WEBHOOK_SECRET'], ], ]; diff --git a/src/Appwrite/Vcs/Factory.php b/src/Appwrite/Vcs/Factory.php index 58a5e06c13..d654875144 100644 --- a/src/Appwrite/Vcs/Factory.php +++ b/src/Appwrite/Vcs/Factory.php @@ -40,8 +40,8 @@ class Factory return false; } - foreach ($this->registry[$key]['requiredEnvVariables'] ?? [] as $name) { - if (empty($this->getEnv($key, $name))) { + foreach ($this->registry[$key]['variables'] ?? [] as $name => $variable) { + if (($variable['required'] ?? false) && empty($this->getEnv($key, $name))) { return false; } } @@ -57,7 +57,7 @@ class Factory $adapter = new ($this->registry[$key]['adapter'])($this->cache); - $endpoint = $this->getEnv($key, 'ENDPOINT'); + $endpoint = $this->getEnv($key, 'endpoint'); if (!empty($endpoint) && \method_exists($adapter, 'setEndpoint')) { $adapter->setEndpoint(\rtrim($endpoint, '/')); } @@ -80,8 +80,8 @@ class Factory $adapter->initializeVariables( $installation->getAttribute('providerInstallationId', ''), - $this->getEnv($provider, 'PRIVATE_KEY'), - $this->getEnv($provider, 'APP_ID'), + $this->getEnv($provider, 'privateKey'), + $this->getEnv($provider, 'appId'), $installation->getAttribute('personalAccessToken', ''), $installation->getAttribute('personalRefreshToken', ''), ); @@ -91,12 +91,12 @@ class Factory public function getWebhookSecret(string $key): string { - return $this->getEnv($key, 'WEBHOOK_SECRET'); + return $this->getEnv($key, 'webhookSecret'); } protected function getEnv(string $key, string $name): string { - $variable = $this->registry[$key]['envVariables'][$name] ?? ''; + $variable = $this->registry[$key]['variables'][$name]['envVariable'] ?? ''; return empty($variable) ? '' : System::getEnv($variable, ''); } diff --git a/tests/unit/Vcs/FactoryTest.php b/tests/unit/Vcs/FactoryTest.php index ec38c4149e..db89369252 100644 --- a/tests/unit/Vcs/FactoryTest.php +++ b/tests/unit/Vcs/FactoryTest.php @@ -28,9 +28,9 @@ final class FactoryTest extends TestCase foreach ($registry as $key => $entry) { $this->assertTrue(\is_subclass_of($entry['adapter'], Git::class), "Adapter for '{$key}' must extend Git"); - $this->assertNotEmpty($entry['requiredEnvVariables'], "Required env variables missing for '{$key}'"); - foreach ($entry['requiredEnvVariables'] as $required) { - $this->assertStringStartsWith('_APP_VCS_', $entry['envVariables'][$required] ?? '', "Env variable for required key '{$required}' missing or invalid for '{$key}'"); + $this->assertNotEmpty($entry['variables'], "Variables missing for '{$key}'"); + foreach ($entry['variables'] as $name => $variable) { + $this->assertStringStartsWith('_APP_VCS_', $variable['envVariable'] ?? '', "Env variable for '{$name}' missing or invalid for '{$key}'"); } } } @@ -74,8 +74,9 @@ final class FactoryTest extends TestCase { $entry = [ 'adapter' => GitHub::class, - 'envVariables' => ['TOKEN' => '_APP_VCS_TEST_TOKEN'], - 'requiredEnvVariables' => ['TOKEN'], + 'variables' => [ + 'token' => ['required' => true, 'envVariable' => '_APP_VCS_TEST_TOKEN'], + ], ]; $factory = new Factory($this->cache(), ['test' => $entry]); @@ -111,15 +112,14 @@ final class FactoryTest extends TestCase { return [ 'adapter' => GitHub::class, - 'envVariables' => [ - 'APP_NAME' => '_APP_VCS_GITHUB_APP_NAME', - 'PRIVATE_KEY' => '_APP_VCS_GITHUB_PRIVATE_KEY', - 'APP_ID' => '_APP_VCS_GITHUB_APP_ID', - 'CLIENT_ID' => '_APP_VCS_GITHUB_CLIENT_ID', - 'CLIENT_SECRET' => '_APP_VCS_GITHUB_CLIENT_SECRET', - 'WEBHOOK_SECRET' => '_APP_VCS_GITHUB_WEBHOOK_SECRET', + 'variables' => [ + 'appName' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_APP_NAME'], + 'privateKey' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_PRIVATE_KEY'], + 'appId' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_APP_ID'], + 'clientId' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_CLIENT_ID'], + 'clientSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_CLIENT_SECRET'], + 'webhookSecret' => ['required' => false, 'envVariable' => '_APP_VCS_GITHUB_WEBHOOK_SECRET'], ], - 'requiredEnvVariables' => ['APP_NAME', 'PRIVATE_KEY', 'APP_ID', 'CLIENT_ID', 'CLIENT_SECRET'], ]; } }