113 lines
3 KiB
PHP
113 lines
3 KiB
PHP
<?php
|
|
|
|
namespace App\Lib;
|
|
|
|
use App\Objects\Gitlab\Schedule;
|
|
use GrahamCampbell\GitLab\GitLabManager;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class Gitlab
|
|
{
|
|
/**
|
|
* Class instance property
|
|
*
|
|
* @var mixed
|
|
*/
|
|
private static $instance;
|
|
|
|
/**
|
|
* The gitlab client
|
|
*
|
|
* @var \Gitlab\Client
|
|
*/
|
|
private static \Gitlab\Client $client;
|
|
|
|
/**
|
|
* __construct method
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
self::$client = app(GitLabManager::class);
|
|
}
|
|
|
|
/**
|
|
* getInstance method
|
|
*
|
|
* @return \App\Lib\Gitlab
|
|
*/
|
|
public static function getInstance(): \App\Lib\Gitlab
|
|
{
|
|
if (is_null(self::$instance)) {
|
|
self::$instance = new Gitlab();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
/**
|
|
* Save compiled vars on Gitlab project
|
|
*
|
|
* @param integer $projectId
|
|
* @param array<string,\App\Lib\CdEnvVariable|array<string,\App\Lib\CdEnvVariable>> $newVars
|
|
* @return boolean
|
|
*/
|
|
public static function saveProjectCdVarsToGitlab(int $projectId, array $newVars): bool
|
|
{
|
|
self::getInstance();
|
|
try {
|
|
$updateProjectVar = function (int $projectId, CdEnvVariable $var) {
|
|
Log::debug(
|
|
sprintf(
|
|
"Add variable %s with value %s",
|
|
$var->key,
|
|
$var->value
|
|
)
|
|
);
|
|
self::$client->projects()->addVariable(
|
|
$projectId,
|
|
$var->getFormatedGitlabKey(),
|
|
$var->value,
|
|
$var->protected,
|
|
$var->environment_scope->value,
|
|
[
|
|
'masked' => $var->masked,
|
|
'raw' => $var->raw
|
|
]
|
|
);
|
|
};
|
|
foreach ($newVars as $varOrVarList) {
|
|
if ($varOrVarList instanceof CdEnvVariable) {
|
|
$updateProjectVar($projectId, $varOrVarList);
|
|
continue;
|
|
}
|
|
foreach ($varOrVarList as $var) {
|
|
$updateProjectVar($projectId, $var);
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
Log::critical($e->getMessage());
|
|
return false;
|
|
} //end try
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* setPipelineSchedules method
|
|
*
|
|
* @param integer $projectId
|
|
* @param \App\Objects\Gitlab\Schedule $schedule
|
|
* @return boolean
|
|
*/
|
|
public static function setPipelineSchedules(int $projectId, Schedule $schedule): bool
|
|
{
|
|
self::getInstance();
|
|
try {
|
|
self::$client->schedules()->create($projectId, $schedule->toArray());
|
|
} catch (\Exception $e) {
|
|
Log::critical($e->getMessage());
|
|
return false;
|
|
} //end try
|
|
return true;
|
|
}
|
|
}
|