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

GL-2039: Added prompt for nodejs and php project selection. #1683

Merged
merged 25 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a367e98
GL-2039: Added prompt for nodejs and php project selection.
akashkska Feb 14, 2024
75a720c
GL-2039: Updated getlist for pipeline migrate command to use drupal p…
akashkska Feb 14, 2024
5b50c28
GL-2039: Updated file improving test checks.
akashkska Feb 14, 2024
6141704
GL-2039: Updated test file to resolve error in test checks.
akashkska Feb 14, 2024
60bf6c6
GL-2039: Resolved review commnets and added testcases.
akashkska Feb 21, 2024
34b0461
Merge branch 'main' into GL-2039
akashkska Feb 21, 2024
8c0d2da
kill mutant
danepowell Feb 21, 2024
51e08a2
GL-2039: Replace if-else with sw-case to resolve mutation warning.
akashkska Feb 23, 2024
f50c6e7
Merge branch 'main' into GL-2039
akashkska Feb 27, 2024
3f0507d
GL-2039: Tried adding testcase to kill mutant.
akashkska Feb 27, 2024
828996e
GL-2039: Updated testcases and alternate logic.
akashkska Feb 27, 2024
931563e
GL-2039: Add assertion to kill mutant FalseValue and TrueValue.
akashkska Feb 28, 2024
6ed1e9f
GL-2039: Applied fix for mutant TRUEVAL and FLASEVAL.
akashkska Feb 28, 2024
56167cf
Revert "GL-2039: Applied fix for mutant TRUEVAL and FLASEVAL."
akashkska Feb 28, 2024
0dac450
GL-2039: Applied fix for mutant TRUEVAL and FLASEVAL.
akashkska Feb 28, 2024
84f8b60
Revert "GL-2039: Applied fix for mutant TRUEVAL and FLASEVAL."
akashkska Feb 28, 2024
c5d2a07
GL-2039-1: Added test file for ci-cd variables.
akashkska Feb 29, 2024
badf8a5
GL-2039-1: Refactor testcase.
akashkska Feb 29, 2024
c44450f
GL-2039-1: Addressed integer increment mutant.
akashkska Feb 29, 2024
64950f8
GL-2039-1: Added default value instead of fetching it from array.
akashkska Feb 29, 2024
d4c44fb
GL-2039-1: Added assertion to kill MethodCallRemoval mutant.
akashkska Mar 1, 2024
885b197
Merge pull request #3 from akashkska/GL-2039-2
akashkska Mar 4, 2024
529da4e
GL-2039: modified testcase.
akashkska Mar 4, 2024
ecac0b7
Merge pull request #4 from akashkska/GL-2039-2
akashkska Mar 4, 2024
e878acf
Merge branch 'main' into GL-2039
danepowell Mar 4, 2024
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
61 changes: 56 additions & 5 deletions src/Command/CodeStudio/CodeStudioCiCdVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,64 @@ class CodeStudioCiCdVariables {
* @return array<mixed>
*/
public static function getList(): array {
return array_column(self::getDefaults(), 'key');
//getlist is being utilised in pipeline-migrate command. By default command is supporting drupal project but going forward need to support both drupal and nodejs project.
return array_column(self::getDefaultsForPhp(), 'key');
}

/**
* @return array<mixed>
*/
public static function getDefaults(?string $cloudApplicationUuid = NULL, ?string $cloudKey = NULL, ?string $cloudSecret = NULL, ?string $projectAccessTokenName = NULL, ?string $projectAccessToken = NULL, ?string $phpVersion = NULL): array {
* @return array<mixed>
*/
public static function getDefaultsForNode(?string $cloudApplicationUuid = NULL, ?string $cloudKey = NULL, ?string $cloudSecret = NULL, ?string $projectAccessTokenName = NULL, ?string $projectAccessToken = NULL, ?string $nodeVersion = NULL): array {
danepowell marked this conversation as resolved.
Show resolved Hide resolved
return [
[
'key' => 'ACQUIA_APPLICATION_UUID',
'masked' => TRUE,
'protected' => FALSE,
'value' => $cloudApplicationUuid,
'variable_type' => 'env_var',
],
[
'key' => 'ACQUIA_CLOUD_API_TOKEN_KEY',
'masked' => TRUE,
'protected' => FALSE,
'value' => $cloudKey,
'variable_type' => 'env_var',
],
[
'key' => 'ACQUIA_CLOUD_API_TOKEN_SECRET',
'masked' => TRUE,
'protected' => FALSE,
'value' => $cloudSecret,
'variable_type' => 'env_var',
],
[
'key' => 'ACQUIA_GLAB_TOKEN_NAME',
'masked' => TRUE,
'protected' => FALSE,
'value' => $projectAccessTokenName,
'variable_type' => 'env_var',
],
[
'key' => 'ACQUIA_GLAB_TOKEN_SECRET',
'masked' => TRUE,
'protected' => FALSE,
'value' => $projectAccessToken,
'variable_type' => 'env_var',
],
[
'key' => 'NODE_VERSION',
'masked' => TRUE,
'protected' => FALSE,
'value' => $nodeVersion,
'variable_type' => 'env_var',
],
];
}

/**
* @return array<mixed>
*/
public static function getDefaultsForPhp(?string $cloudApplicationUuid = NULL, ?string $cloudKey = NULL, ?string $cloudSecret = NULL, ?string $projectAccessTokenName = NULL, ?string $projectAccessToken = NULL, ?string $phpVersion = NULL): array {
return [
[
'key' => 'ACQUIA_APPLICATION_UUID',
Expand Down Expand Up @@ -55,7 +106,7 @@ public static function getDefaults(?string $cloudApplicationUuid = NULL, ?string
],
[
'key' => 'PHP_VERSION',
'masked' => FALSE,
'masked' => TRUE,
'protected' => FALSE,
'value' => $phpVersion,
'variable_type' => 'env_var',
Expand Down
80 changes: 70 additions & 10 deletions src/Command/CodeStudio/CodeStudioWizardCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,31 @@
// But, we specifically need an API Token key-pair of Code Studio.
// So we reauthenticate to be sure we're using the provided credentials.
$this->reAuthenticate($cloudKey, $cloudSecret, $this->cloudCredentials->getBaseUri(), $this->cloudCredentials->getAccountsUri());
$phpVersion = NULL;
$nodeVersion = NULL;
$projectType = $this->getListOfProjectType();
$projectSelected = $this->io->choice('Select a project type', $projectType, "Drupal_project");

$phpVersions = [
'PHP_version_8.1' => "8.1",
'PHP_version_8.2' => "8.2",
];
$project = $this->io->choice('Select a PHP version', array_values($phpVersions), $phpVersions['PHP_version_8.1']);
$project = array_search($project, $phpVersions, TRUE);
$phpVersion = $phpVersions[$project];
switch ($projectSelected) {
case "Drupal_project":
$phpVersions = [
'PHP_version_8.1' => "8.1",
'PHP_version_8.2' => "8.2",
];
$project = $this->io->choice('Select a PHP version', array_values($phpVersions), "8.1");
$project = array_search($project, $phpVersions, TRUE);
$phpVersion = $phpVersions[$project];
break;
case "Node_project":
$nodeVersions = [
'NODE_version_18.17.1' => "18.17.1",
'NODE_version_20.5.1' => "20.5.1",
];
$project = $this->io->choice('Select a NODE version', array_values($nodeVersions), "18.17.1");
$project = array_search($project, $nodeVersions, TRUE);
$nodeVersion = $nodeVersions[$project];
break;
}

$appUuid = $this->determineCloudApplication();

Expand Down Expand Up @@ -97,7 +114,14 @@
$projectAccessTokenName = 'acquia-codestudio';
$projectAccessToken = $this->createProjectAccessToken($project, $projectAccessTokenName);
$this->updateGitLabProject($project);
$this->setGitLabCiCdVariables($project, $appUuid, $cloudKey, $cloudSecret, $projectAccessTokenName, $projectAccessToken, $phpVersion);
switch ($projectSelected) {
case "Drupal_project":
$this->setGitLabCiCdVariablesForPhpProject($project, $appUuid, $cloudKey, $cloudSecret, $projectAccessTokenName, $projectAccessToken, $phpVersion);
break;
case "Node_project":
$this->setGitLabCiCdVariablesForNodeProject($project, $appUuid, $cloudKey, $cloudSecret, $projectAccessTokenName, $projectAccessToken, $nodeVersion);
break;
}
$this->createScheduledPipeline($project);

$this->io->success([
Expand Down Expand Up @@ -143,6 +167,17 @@
return NULL;
}

/**
* @return array<mixed>|null ?
*/
private function getListOfProjectType(): ?array {
$array = [
'Drupal_project',
'Node_project',
];
return $array;
}

private function createProjectAccessToken(array $project, string $projectAccessTokenName): string {
$this->io->writeln("Creating project access token...");

Expand All @@ -163,9 +198,34 @@
return $projectAccessToken['token'];
}

private function setGitLabCiCdVariables(array $project, string $cloudApplicationUuid, string $cloudKey, string $cloudSecret, string $projectAccessTokenName, string $projectAccessToken, string $phpVersion): void {
private function setGitLabCiCdVariablesForPhpProject(array $project, string $cloudApplicationUuid, string $cloudKey, string $cloudSecret, string $projectAccessTokenName, string $projectAccessToken, string $phpVersion): void {
$this->io->writeln("Setting GitLab CI/CD variables for {$project['path_with_namespace']}..");
$gitlabCicdVariables = CodeStudioCiCdVariables::getDefaultsForPhp($cloudApplicationUuid, $cloudKey, $cloudSecret, $projectAccessTokenName, $projectAccessToken, $phpVersion);
danepowell marked this conversation as resolved.
Show resolved Hide resolved
$gitlabCicdExistingVariables = $this->gitLabClient->projects()
->variables($project['id']);
$gitlabCicdExistingVariablesKeyed = [];
foreach ($gitlabCicdExistingVariables as $variable) {
$key = $variable['key'];
$gitlabCicdExistingVariablesKeyed[$key] = $variable;
}

foreach ($gitlabCicdVariables as $variable) {
$this->checklist->addItem("Setting GitLab CI/CD variables for <comment>{$variable['key']}</comment>");

Check warning on line 213 in src/Command/CodeStudio/CodeStudioWizardCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ $gitlabCicdExistingVariablesKeyed[$key] = $variable; } foreach ($gitlabCicdVariables as $variable) { - $this->checklist->addItem("Setting GitLab CI/CD variables for <comment>{$variable['key']}</comment>"); + if (!array_key_exists($variable['key'], $gitlabCicdExistingVariablesKeyed)) { $this->gitLabClient->projects()->addVariable($project['id'], $variable['key'], $variable['value'], $variable['protected'], NULL, ['masked' => $variable['masked'], 'variable_type' => $variable['variable_type']]); } else {
if (!array_key_exists($variable['key'], $gitlabCicdExistingVariablesKeyed)) {
$this->gitLabClient->projects()
->addVariable($project['id'], $variable['key'], $variable['value'], $variable['protected'], NULL, ['masked' => $variable['masked'], 'variable_type' => $variable['variable_type']]);
}
else {
$this->gitLabClient->projects()
->updateVariable($project['id'], $variable['key'], $variable['value'], $variable['protected'], NULL, ['masked' => $variable['masked'], 'variable_type' => $variable['variable_type']]);
}
$this->checklist->completePreviousItem();

Check warning on line 222 in src/Command/CodeStudio/CodeStudioWizardCommand.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ } else { $this->gitLabClient->projects()->updateVariable($project['id'], $variable['key'], $variable['value'], $variable['protected'], NULL, ['masked' => $variable['masked'], 'variable_type' => $variable['variable_type']]); } - $this->checklist->completePreviousItem(); + } } private function setGitLabCiCdVariablesForNodeProject(array $project, string $cloudApplicationUuid, string $cloudKey, string $cloudSecret, string $projectAccessTokenName, string $projectAccessToken, string $nodeVersion) : void
}
}

private function setGitLabCiCdVariablesForNodeProject(array $project, string $cloudApplicationUuid, string $cloudKey, string $cloudSecret, string $projectAccessTokenName, string $projectAccessToken, string $nodeVersion): void {
$this->io->writeln("Setting GitLab CI/CD variables for {$project['path_with_namespace']}..");
$gitlabCicdVariables = CodeStudioCiCdVariables::getDefaults($cloudApplicationUuid, $cloudKey, $cloudSecret, $projectAccessTokenName, $projectAccessToken, $phpVersion);
$gitlabCicdVariables = CodeStudioCiCdVariables::getDefaultsForNode($cloudApplicationUuid, $cloudKey, $cloudSecret, $projectAccessTokenName, $projectAccessToken, $nodeVersion);
$gitlabCicdExistingVariables = $this->gitLabClient->projects()
->variables($project['id']);
$gitlabCicdExistingVariablesKeyed = [];
Expand Down
11 changes: 9 additions & 2 deletions tests/phpunit/src/CommandTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,15 +648,22 @@ protected function getMockGitLabVariables(): array {
'key' => 'ACQUIA_APPLICATION_UUID',
'masked' => TRUE,
'protected' => FALSE,
'value' => '2b3f7cf0-6602-4590-948b-3b07b1b005ef',
'value' => 'a47ac10b-58cc-4372-a567-0e02b2c3d470',
'variable_type' => 'env_var',
],
1 => [
'environment_scope' => '*',
'key' => 'ACQUIA_CLOUD_API_TOKEN_KEY',
'masked' => TRUE,
'protected' => FALSE,
'value' => '111aae74-e81a-4052-b4b9-a27a62e6b6a6',
'value' => '17feaf34-5d04-402b-9a67-15d5161d24e1',
'variable_type' => 'env_var',
],
2 => [
'key' => 'ACQUIA_CLOUD_API_TOKEN_SECRET',
'masked' => TRUE,
'protected' => FALSE,
'value' => 'X1u\/PIQXtYaoeui.4RJSJpGZjwmWYmfl5AUQkAebYE=',
'variable_type' => 'env_var',
],
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types = 1);

namespace Acquia\Cli\Tests\Commands\CodeStudio;

use Acquia\Cli\Command\CodeStudio\CodeStudioCiCdVariables;
use Acquia\Cli\Tests\TestBase;

class CodeStudioCiCdVariablesTest extends TestBase {

public function testGetDefaultsForNode(): void {
$codeStudioCiCdVariablesObj = new CodeStudioCiCdVariables();
$variables = $codeStudioCiCdVariablesObj->getDefaultsForNode();
$this->testBooleanValues($variables);
$variables = $codeStudioCiCdVariablesObj->getDefaultsForPhp();
$this->testBooleanValues($variables);
}

protected function testBooleanValues(array $variables): void {
foreach ($variables as $variable) {
$maskedValue = $variable['masked'];
$this->assertEquals(TRUE, $maskedValue);
$protectedValue = $variable['protected'];
$this->assertEquals(FALSE, $protectedValue);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Acquia\Cli\Tests\Commands\CodeStudio;

use Acquia\Cli\Command\CodeStudio\CodeStudioCiCdVariables;
use Acquia\Cli\Command\CodeStudio\CodeStudioPipelinesMigrateCommand;
use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Tests\Commands\Ide\IdeRequiredTestTrait;
Expand Down Expand Up @@ -92,7 +91,51 @@ public function testCommand(mixed $mockedGitlabProjects, mixed $inputs, mixed $a
$this->mockRequest('getAccount');
$this->mockGitLabPermissionsRequest($this::$applicationUuid);
$projects = $this->mockGetGitLabProjects($this::$applicationUuid, $this->gitLabProjectId, $mockedGitlabProjects);
$projects->variables($this->gitLabProjectId)->willReturn(CodeStudioCiCdVariables::getDefaults());
$gitlabCicdVariables = [
[
'key' => 'ACQUIA_APPLICATION_UUID',
'masked' => TRUE,
'protected' => FALSE,
'value' => NULL,
'variable_type' => 'env_var',
],
[
'key' => 'ACQUIA_CLOUD_API_TOKEN_KEY',
'masked' => TRUE,
'protected' => FALSE,
'value' => NULL,
'variable_type' => 'env_var',
],
[
'key' => 'ACQUIA_CLOUD_API_TOKEN_SECRET',
'masked' => TRUE,
'protected' => FALSE,
'value' => NULL,
'variable_type' => 'env_var',
],
[
'key' => 'ACQUIA_GLAB_TOKEN_NAME',
'masked' => TRUE,
'protected' => FALSE,
'value' => NULL,
'variable_type' => 'env_var',
],
[
'key' => 'ACQUIA_GLAB_TOKEN_SECRET',
'masked' => TRUE,
'protected' => FALSE,
'value' => NULL,
'variable_type' => 'env_var',
],
[
'key' => 'PHP_VERSION',
'masked' => FALSE,
'protected' => FALSE,
'value' => NULL,
'variable_type' => 'env_var',
],
];
$projects->variables($this->gitLabProjectId)->willReturn($gitlabCicdVariables);
$projects->update($this->gitLabProjectId, Argument::type('array'));
$gitlabClient->projects()->willReturn($projects);
$localMachineHelper->getFilesystem()->willReturn(new Filesystem())->shouldBeCalled();
Expand Down
Loading