diff --git a/src/Command/CodeStudio/CodeStudioCiCdVariables.php b/src/Command/CodeStudio/CodeStudioCiCdVariables.php index f81893f59..571c4ea4f 100644 --- a/src/Command/CodeStudio/CodeStudioCiCdVariables.php +++ b/src/Command/CodeStudio/CodeStudioCiCdVariables.php @@ -16,7 +16,114 @@ public static function getList(): array { /** * @return array */ - public static function getDefaults(?string $cloudApplicationUuid = NULL, ?string $cloudKey = NULL, ?string $cloudSecret = NULL, ?string $projectAccessTokenName = NULL, ?string $projectAccessToken = NULL, ?string $phpVersion = NULL): array { + public static function getDefaults(?string $cloudApplicationUuid = NULL, ?string $cloudKey = NULL, ?string $cloudSecret = NULL, ?string $projectAccessTokenName = NULL, ?string $projectAccessToken = NULL, ?string $phpVersion = 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' => 'PHP_VERSION', + 'masked' => FALSE, + 'protected' => FALSE, + 'value' => $phpVersion, + 'variable_type' => 'env_var', + ], + [ + 'key' => 'NODE_VERSION', + 'masked' => FALSE, + 'protected' => FALSE, + 'value' => $nodeVersion, + 'variable_type' => 'env_var', + ], + ]; + } + + /** + * @return array + */ + 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' => FALSE, + 'protected' => FALSE, + 'value' => $nodeVersion, + 'variable_type' => 'env_var', + ], + ]; + } + + /** + * @return array + */ + 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', diff --git a/src/Command/CodeStudio/CodeStudioWizardCommand.php b/src/Command/CodeStudio/CodeStudioWizardCommand.php index 52675b696..098f07089 100644 --- a/src/Command/CodeStudio/CodeStudioWizardCommand.php +++ b/src/Command/CodeStudio/CodeStudioWizardCommand.php @@ -43,13 +43,30 @@ protected function execute(InputInterface $input, OutputInterface $output): int // So we reauthenticate to be sure we're using the provided credentials. $this->reAuthenticate($cloudKey, $cloudSecret, $this->cloudCredentials->getBaseUri(), $this->cloudCredentials->getAccountsUri()); - $phpVersions = [ - 'PHP_version_8.1' => "8.1", - 'PHP_version_8.2' => "8.2", + $projectType = [ + 'Drupal_project', + 'Node_project', ]; - $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]; + $projectSelected = $this->io->choice('Select a project type', $projectType, $projectType[0]); + echo "Selected project is, $projectSelected"; + + if ($projectSelected == '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]; + } elseif ($projectSelected == '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), $nodeVersions['NODE_version_20.5.1']); + $project = array_search($project, $nodeVersions, TRUE); + $nodeVersion = $nodeVersions[$project]; + } $appUuid = $this->determineCloudApplication(); @@ -97,7 +114,12 @@ 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); + if ($projectSelected == 'Drupal_project') { + $this->setGitLabCiCdVariablesForPhpProject($project, $appUuid, $cloudKey, $cloudSecret, $projectAccessTokenName, $projectAccessToken, $phpVersion); + + } elseif ($projectSelected == 'Node_project') { + $this->setGitLabCiCdVariablesForNodeProject($project, $appUuid, $cloudKey, $cloudSecret, $projectAccessTokenName, $projectAccessToken, $nodeVersion); + } $this->createScheduledPipeline($project); $this->io->success([ @@ -163,9 +185,37 @@ 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) { + if ($variable['key'] === 'NODE_VERSION') { + continue; + } + $this->checklist->addItem("Setting CI/CD variable {$variable['key']}"); + 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 = []; @@ -175,6 +225,9 @@ private function setGitLabCiCdVariables(array $project, string $cloudApplication } foreach ($gitlabCicdVariables as $variable) { + if ($variable['key'] === 'PHP_VERSION') { + continue; + } $this->checklist->addItem("Setting CI/CD variable {$variable['key']}"); if (!array_key_exists($variable['key'], $gitlabCicdExistingVariablesKeyed)) { $this->gitLabClient->projects()