Skip to content

Commit

Permalink
use gitlab-php-client
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Mar 29, 2024
1 parent 1d0708b commit 2e087b1
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 43 deletions.
15 changes: 5 additions & 10 deletions src/Command/CodeStudio/CodeStudioWizardCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
// Get Cloud application.
$cloudApplication = $this->getCloudApplication($appUuid);
$project = $this->determineGitLabProject($cloudApplication);
$projectId = $project['id'];

$this->io->writeln([
"",
Expand All @@ -115,15 +114,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$projectAccessTokenName = 'acquia-codestudio';
$projectAccessToken = $this->createProjectAccessToken($project, $projectAccessTokenName);
$this->updateGitLabProject($project);
$ciPath = 'gitlab-ci/Auto-DevOps.acquia.gitlab-ci.yml@acquia/node-template';
$hostPath = $this->getGitLabHost();
$curlCommand = $this->getCurlCommand($projectAccessToken, $hostPath, $projectId, $ciPath);
switch ($projectSelected) {
case "Drupal_project":
$this->setGitLabCiCdVariablesForPhpProject($project, $appUuid, $cloudKey, $cloudSecret, $projectAccessTokenName, $projectAccessToken, $phpVersion);
break;
case "Node_project":
$this->invokeCurlCommand($curlCommand);
$parameters = [
'ci_config_path' => 'gitlab-ci/Auto-DevOps.acquia.gitlab-ci.yml@acquia/node-template',
];
$client = $this->getGitLabClient();
$client->projects()->update($project['id'], $parameters);
$this->setGitLabCiCdVariablesForNodeProject($project, $appUuid, $cloudKey, $cloudSecret, $projectAccessTokenName, $projectAccessToken, $nodeVersion);
break;
}
Expand Down Expand Up @@ -181,11 +181,6 @@ private function getListOfProjectType(): ?array {
return $array;
}

public function getCurlCommand(string $projectAccessToken, string $hostPath, int $projectId, string $ciPath): string {
$curlCommandString = 'curl -s -N -k -L --request PUT --header "PRIVATE-TOKEN: ' . $projectAccessToken . '" --url ' . $hostPath . '/api/v4/projects/' . $projectId . ' --data "ci_config_path=' . $ciPath . '"';
return $curlCommandString;
}

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

Expand Down
8 changes: 0 additions & 8 deletions src/Command/CommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1572,14 +1572,6 @@ protected function createMySqlDumpOnLocal(string $dbHost, string $dbUser, string
return $localFilepath;
}

protected function invokeCurlCommand(string $command, Closure $outputCallback = NULL): void {
$this->localMachineHelper->checkRequiredBinariesExist(['curl']);
$process = $this->localMachineHelper->executeFromCmd($command, $outputCallback, NULL, ($this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL));
if (!$process->isSuccessful()) {
throw new AcquiaCliException('Unable to execute curl command. {error_message}', ['error_message' => $process->getErrorOutput()]);
}
}

protected function promptOpenBrowserToCreateToken(
InputInterface $input
): void {
Expand Down
9 changes: 0 additions & 9 deletions tests/phpunit/src/CommandTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,6 @@ protected function mockCreateMySqlDumpOnLocal(ObjectProphecy $localMachineHelper
->shouldBeCalled();
}

protected function mockCurlCommand(ObjectProphecy $localMachineHelper): void {
$localMachineHelper->checkRequiredBinariesExist(["curl"])->shouldBeCalled();
$process = $this->mockProcess();
$process->getOutput()->willReturn('');
$command = $this->getCurlString();
$localMachineHelper->executeFromCmd($command, Argument::type('callable'), NULL, TRUE)->willReturn($process->reveal())
->shouldBeCalled();
}

protected function mockExecutePvExists(
ObjectProphecy $localMachineHelper,
bool $pvExists = TRUE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class CodeStudioWizardCommandTest extends WizardTestBase {

private string $gitLabHost = 'gitlabhost';
private string $gitLabToken = 'gitlabtoken';
private string $ciPath = 'ciPath';

private int $gitLabProjectId = 33;
private int $gitLabTokenId = 118;
Expand Down Expand Up @@ -315,6 +314,13 @@ public function testCommand(array $mockedGitlabProjects, array $inputs, array $a
Argument::type('string'),
)->shouldBeCalled();
$this->mockGitLabVariables($this->gitLabProjectId, $projects);

if ($inputs[0] === 'y' && ($inputs[1] === '1' || (array_key_exists(3, $inputs) && $inputs[3] === '1'))) {
$parameters = [
'ci_config_path' => 'gitlab-ci/Auto-DevOps.acquia.gitlab-ci.yml@acquia/node-template',
];
$projects->update($this->gitLabProjectId, $parameters)->shouldBeCalled();
}
$schedules = $this->prophet->prophesize(Schedules::class);
$schedules->showAll($this->gitLabProjectId)->willReturn([]);
$pipeline = ['id' => 1];
Expand Down Expand Up @@ -349,10 +355,6 @@ public function testCommand(array $mockedGitlabProjects, array $inputs, array $a
$this->mockGitlabGetHost($localMachineHelper, $this->gitLabHost);
$this->mockGitlabGetToken($localMachineHelper, $this->gitLabToken, $this->gitLabHost);

if ($inputs[0] == 'y' && $inputs[1] == '1' ) {
$this->mockCurlCommand($localMachineHelper);
}

/** @var Filesystem|ObjectProphecy $fileSystem */
$fileSystem = $this->prophet->prophesize(Filesystem::class);
// Set properties and execute.
Expand All @@ -364,9 +366,6 @@ public function testCommand(array $mockedGitlabProjects, array $inputs, array $a
}

// Assertions.
$curlCommand = $this->command->getCurlCommand($this->gitLabToken, $this->gitLabHost, $this->gitLabProjectId, $this->ciPath);
$curlString = $this->getCurlString();
self::assertStringContainsString($curlString, $curlCommand);
$this->assertEquals(0, $this->getStatusCode());
}

Expand Down
8 changes: 0 additions & 8 deletions tests/phpunit/src/Commands/WizardTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,4 @@ protected function getOutputStrings(): array {
];
}

/**
* @return string
* A strings to inspect the output for.
*/
protected function getCurlString(): string {
return 'curl -s -N -k -L --request PUT --header "PRIVATE-TOKEN: gitlabtoken" --url gitlabhost/api/v4/projects/33 --data "ci_config_path=ciPath"';
}

}

0 comments on commit 2e087b1

Please sign in to comment.