Skip to content

Commit

Permalink
GL-2039: Added prompt for nodejs and php project selection. (#1683)
Browse files Browse the repository at this point in the history
* GL-2039: Added prompt for nodejs and php project selection.

* GL-2039: Updated getlist for pipeline migrate command to use drupal project as default.

* GL-2039: Updated file improving test checks.

* GL-2039: Updated test file to resolve error in test checks.

* GL-2039: Resolved review commnets and added testcases.

* kill mutant

* GL-2039: Replace if-else with sw-case to resolve mutation warning.

* GL-2039: Tried adding testcase to kill mutant.

* GL-2039: Updated testcases and alternate logic.

* GL-2039: Add assertion to kill mutant FalseValue and TrueValue.

* GL-2039: Applied fix for mutant TRUEVAL and FLASEVAL.

* Revert "GL-2039: Applied fix for mutant TRUEVAL and FLASEVAL."

This reverts commit 6ed1e9f.

* GL-2039: Applied fix for mutant TRUEVAL and FLASEVAL.

* Revert "GL-2039: Applied fix for mutant TRUEVAL and FLASEVAL."

This reverts commit 0dac450.

* GL-2039-1: Added test file for ci-cd variables.

* GL-2039-1: Refactor testcase.

* GL-2039-1: Addressed integer increment mutant.

* GL-2039-1: Added default value instead of fetching it from array.

* GL-2039-1: Added assertion to kill MethodCallRemoval mutant.

* GL-2039: modified testcase.

---------

Co-authored-by: Dane Powell <[email protected]>
  • Loading branch information
akashkska and danepowell authored Mar 4, 2024
1 parent d7af9cf commit babf3ba
Show file tree
Hide file tree
Showing 7 changed files with 327 additions and 23 deletions.
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 {
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 @@ protected function execute(InputInterface $input, OutputInterface $output): int
// 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 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$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 @@ private function getGitLabProjectAccessTokenByName(array $project, string $name)
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 @@ private function createProjectAccessToken(array $project, string $projectAccessT
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);
$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>");
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();
}
}

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

0 comments on commit babf3ba

Please sign in to comment.