Skip to content

Commit db1b87e

Browse files
committed
Add env var value detection
1 parent 831a261 commit db1b87e

6 files changed

Lines changed: 101 additions & 26 deletions

File tree

app/controllers/api/vcs.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -966,14 +966,14 @@
966966
}
967967

968968
$wg = new WaitGroup();
969-
$envNames = [];
969+
$envs = [];
970970
foreach ($files as $file) {
971971
if (!(\str_starts_with($file, '.env'))) {
972972
continue;
973973
}
974974

975975
$wg->add();
976-
go(function () use ($github, $owner, $repositoryName, $providerRootDirectory, $file, $wg, &$envNames) {
976+
go(function () use ($github, $owner, $repositoryName, $providerRootDirectory, $file, $wg, &$envs) {
977977
try {
978978
$contentResponse = $github->getRepositoryContent($owner, $repositoryName, \rtrim($providerRootDirectory, '/') . '/' . $file);
979979
$envFile = $contentResponse['content'] ?? '';
@@ -982,8 +982,9 @@
982982
foreach ($envLines as $line) {
983983
$parts = \explode('=', $line, 2);
984984
$envName = \trim($parts[0] ?? '');
985+
$envValue = \trim($parts[1] ?? '');
985986
if (!empty($envName)) {
986-
$envNames[] = $envName;
987+
$envs[$envName] = $envValue;
987988
}
988989
}
989990
} finally {
@@ -993,7 +994,15 @@
993994
}
994995
$wg->wait();
995996

996-
$output->setAttribute('variables', \array_unique($envNames));
997+
$variables = [];
998+
foreach ($envs as $key => $value) {
999+
$variables[] = [
1000+
'name' => $key,
1001+
'value' => $value,
1002+
];
1003+
}
1004+
1005+
$output->setAttribute('variables', $variables);
9971006

9981007
$response->dynamic($output, $type === 'framework' ? Response::MODEL_DETECTION_FRAMEWORK : Response::MODEL_DETECTION_RUNTIME);
9991008
});
@@ -1171,14 +1180,14 @@
11711180
}
11721181

11731182
$wg = new WaitGroup();
1174-
$envNames = [];
1183+
$envs = [];
11751184
foreach ($files as $file) {
11761185
if (!(\str_starts_with($file, '.env'))) {
11771186
continue;
11781187
}
11791188

11801189
$wg->add();
1181-
go(function () use ($github, $repo, $file, $wg, &$envNames) {
1190+
go(function () use ($github, $repo, $file, $wg, &$envs) {
11821191
try {
11831192
$contentResponse = $github->getRepositoryContent($repo['organization'], $repo['name'], $file);
11841193
$envFile = $contentResponse['content'] ?? '';
@@ -1187,8 +1196,9 @@
11871196
foreach ($envLines as $line) {
11881197
$parts = \explode('=', $line, 2);
11891198
$envName = \trim($parts[0] ?? '');
1199+
$envValue = \trim($parts[1] ?? '');
11901200
if (!empty($envName)) {
1191-
$envNames[] = $envName;
1201+
$envs[$envName] = $envValue;
11921202
}
11931203
}
11941204
} finally {
@@ -1198,7 +1208,13 @@
11981208
}
11991209
$wg->wait();
12001210

1201-
$repo['variables'] = \array_unique($envNames);
1211+
$repo['variables'] = [];
1212+
foreach ($envs as $key => $value) {
1213+
$repo['variables'][] = [
1214+
'name' => $key,
1215+
'value' => $value,
1216+
];
1217+
}
12021218

12031219
return $repo;
12041220
};

src/Appwrite/Utopia/Response.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
use Appwrite\Utopia\Response\Model\Deployment;
6060
use Appwrite\Utopia\Response\Model\DetectionFramework;
6161
use Appwrite\Utopia\Response\Model\DetectionRuntime;
62+
use Appwrite\Utopia\Response\Model\DetectionVariable;
6263
use Appwrite\Utopia\Response\Model\DevKey;
6364
use Appwrite\Utopia\Response\Model\Document as ModelDocument;
6465
use Appwrite\Utopia\Response\Model\Error;
@@ -316,6 +317,7 @@ class Response extends SwooleResponse
316317
public const MODEL_BRANCH = 'branch';
317318
public const MODEL_BRANCH_LIST = 'branchList';
318319
public const MODEL_DETECTION_FRAMEWORK = 'detectionFramework';
320+
public const MODEL_DETECTION_VARIABLE = 'detectionVariable';
319321
public const MODEL_DETECTION_RUNTIME = 'detectionRuntime';
320322
public const MODEL_VCS_CONTENT = 'vcsContent';
321323
public const MODEL_VCS_CONTENT_LIST = 'vcsContentList';
@@ -563,6 +565,7 @@ public function __construct(SwooleHTTPResponse $response)
563565
->setModel(new ProviderRepositoryRuntime())
564566
->setModel(new DetectionFramework())
565567
->setModel(new DetectionRuntime())
568+
->setModel(new DetectionVariable())
566569
->setModel(new VcsContent())
567570
->setModel(new Branch())
568571
->setModel(new Runtime())
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Appwrite\Utopia\Response\Model;
4+
5+
use Appwrite\Utopia\Response;
6+
use Appwrite\Utopia\Response\Model;
7+
8+
abstract class Detection extends Model
9+
{
10+
public function __construct()
11+
{
12+
$this
13+
->addRule('variables', [
14+
'type' => Response::MODEL_DETECTION_VARIABLE,
15+
'description' => 'Environment variables found in .env files',
16+
'required' => false,
17+
'default' => [],
18+
'example' => new \stdClass(),
19+
'array' => true,
20+
]);
21+
}
22+
}

src/Appwrite/Utopia/Response/Model/DetectionFramework.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace Appwrite\Utopia\Response\Model;
44

55
use Appwrite\Utopia\Response;
6-
use Appwrite\Utopia\Response\Model;
76

8-
class DetectionFramework extends Model
7+
class DetectionFramework extends Detection
98
{
109
public function __construct()
1110
{
11+
parent::__construct();
12+
1213
$this
1314
->addRule('framework', [
1415
'type' => self::TYPE_STRING,
@@ -33,13 +34,6 @@ public function __construct()
3334
'description' => 'Site Output Directory',
3435
'default' => '',
3536
'example' => 'dist',
36-
])
37-
->addRule('variables', [
38-
'type' => self::TYPE_STRING,
39-
'description' => 'Environment variables found in .env files',
40-
'default' => [],
41-
'array' => true,
42-
'example' => ['PORT', 'NODE_ENV'],
4337
]);
4438
}
4539

src/Appwrite/Utopia/Response/Model/DetectionRuntime.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace Appwrite\Utopia\Response\Model;
44

55
use Appwrite\Utopia\Response;
6-
use Appwrite\Utopia\Response\Model;
76

8-
class DetectionRuntime extends Model
7+
class DetectionRuntime extends Detection
98
{
109
public function __construct()
1110
{
11+
parent::__construct();
12+
1213
$this
1314
->addRule('runtime', [
1415
'type' => self::TYPE_STRING,
@@ -27,13 +28,6 @@ public function __construct()
2728
'description' => 'Function install and build commands',
2829
'default' => '',
2930
'example' => 'npm install && npm run build',
30-
])
31-
->addRule('variables', [
32-
'type' => self::TYPE_STRING,
33-
'description' => 'Environment variables found in .env files',
34-
'default' => [],
35-
'array' => true,
36-
'example' => ['PORT', 'NODE_ENV'],
3731
]);
3832
}
3933

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Appwrite\Utopia\Response\Model;
4+
5+
use Appwrite\Utopia\Response;
6+
use Appwrite\Utopia\Response\Model;
7+
8+
class DetectionVariable extends Model
9+
{
10+
public function __construct()
11+
{
12+
$this
13+
->addRule('name', [
14+
'type' => self::TYPE_STRING,
15+
'description' => 'Name of environment variable',
16+
'default' => '',
17+
'example' => 'NODE_ENV',
18+
])
19+
->addRule('value', [
20+
'type' => self::TYPE_STRING,
21+
'description' => 'Value of environment variable',
22+
'default' => '',
23+
'example' => 'production',
24+
]);
25+
}
26+
27+
/**
28+
* Get Name
29+
*
30+
* @return string
31+
*/
32+
public function getName(): string
33+
{
34+
return 'DetectionVariable';
35+
}
36+
37+
/**
38+
* Get Type
39+
*
40+
* @return string
41+
*/
42+
public function getType(): string
43+
{
44+
return Response::MODEL_DETECTION_VARIABLE;
45+
}
46+
}

0 commit comments

Comments
 (0)