1
0
Fork 0
mirror of synced 2024-10-01 01:37:56 +13:00

WIP: Build from template

This commit is contained in:
Khushboo Verma 2023-06-17 17:42:21 +05:30
parent ec6a817aa4
commit 860b4fb1c0
3 changed files with 35 additions and 5 deletions

View file

@ -47,7 +47,7 @@ use Utopia\Database\Exception\Duplicate as DuplicateException;
include_once __DIR__ . '/../shared/api.php';
$redeployVcsLogic = function (Document $function, Document $project, Document $installation, Database $dbForProject) {
$redeployVcsLogic = function (Document $function, Document $project, Document $installation, Document $template, Database $dbForProject) {
$deploymentId = ID::unique();
$entrypoint = $function->getAttribute('entrypoint', '');
$deployment = $dbForProject->createDocument('deployments', new Document([
@ -79,6 +79,7 @@ $redeployVcsLogic = function (Document $function, Document $project, Document $i
->setType(BUILD_TYPE_DEPLOYMENT)
->setResource($function)
->setDeployment($deployment)
->setTemplate($template)
->setProject($project)
->trigger();
};
@ -114,15 +115,26 @@ App::post('/v1/functions')
->param('vcsBranch', '', new Text(128), 'Production branch for the repo linked to the function', true)
->param('vcsSilentMode', false, new Boolean(), 'Is VCS connection in silent mode for the repo linked to the function?', true)
->param('vcsRootDirectory', '', new Text(128), 'Path to function code in the linked repo', true)
->param('templateRepositoryName', '', new Text(128), 'Repository name of the template', true)
->param('templateOwnerName', '', new Text(128), 'Owner name of the template', true)
->param('templateDirectory', '', new Text(128), 'Path to function code in the template repo', true)
->inject('response')
->inject('dbForProject')
->inject('project')
->inject('user')
->inject('events')
->inject('dbForConsole')
->action(function (string $functionId, string $name, string $runtime, array $execute, array $events, string $schedule, int $timeout, bool $enabled, bool $logging, string $entrypoint, string $buildCommand, string $installCommand, string $vcsInstallationId, string $vcsRepositoryId, string $vcsBranch, bool $vcsSilentMode, string $vcsRootDirectory, Response $response, Database $dbForProject, Document $project, Document $user, Event $eventsInstance, Database $dbForConsole) use ($redeployVcsLogic) {
->action(function (string $functionId, string $name, string $runtime, array $execute, array $events, string $schedule, int $timeout, bool $enabled, bool $logging, string $entrypoint, string $buildCommand, string $installCommand, string $vcsInstallationId, string $vcsRepositoryId, string $vcsBranch, bool $vcsSilentMode, string $vcsRootDirectory, string $templateRepositoryName, string $templateOwnerName, string $templateDirectory, Response $response, Database $dbForProject, Document $project, Document $user, Event $eventsInstance, Database $dbForConsole) use ($redeployVcsLogic) {
$functionId = ($functionId == 'unique()') ? ID::unique() : $functionId;
// build from template
$template = new Document([]);
if (!empty($templateRepositoryName) && !empty($templateOwnerName)) {
$template->setAttribute('templateRepositoryName', $templateRepositoryName)
->setAttribute('templateOwnerName', $templateOwnerName)
->setAttribute('templateDirectory', $templateDirectory);
}
$installation = $dbForConsole->getDocument('vcsInstallations', $vcsInstallationId, [
Query::equal('projectInternalId', [$project->getInternalId()])
]);
@ -201,7 +213,7 @@ App::post('/v1/functions')
// Redeploy vcs logic
if (!empty($vcsRepositoryId)) {
$redeployVcsLogic($function, $project, $installation, $dbForProject);
$redeployVcsLogic($function, $project, $installation, $template, $dbForProject);
}
$functionsDomain = App::getEnv('_APP_DOMAIN_FUNCTIONS', 'disabled');

View file

@ -47,6 +47,7 @@ class BuildsV1 extends Worker
$project = new Document($this->args['project'] ?? []);
$resource = new Document($this->args['resource'] ?? []);
$deployment = new Document($this->args['deployment'] ?? []);
$template = new Document($this->args['template'] ?? []);
$SHA = $this->args['SHA'] ?? '';
$targetUrl = $this->args['targetUrl'] ?? '';
@ -55,7 +56,7 @@ class BuildsV1 extends Worker
case BUILD_TYPE_RETRY:
Console::info('Creating build for deployment: ' . $deployment->getId());
$github = new GitHub($this->getCache());
$this->buildDeployment($github, $project, $resource, $deployment, $SHA, $targetUrl);
$this->buildDeployment($github, $project, $resource, $deployment, $template, $SHA, $targetUrl);
break;
default:
@ -64,8 +65,10 @@ class BuildsV1 extends Worker
}
}
protected function buildDeployment(GitHub $github, Document $project, Document $function, Document $deployment, string $SHA = '', string $targetUrl = '')
protected function buildDeployment(GitHub $github, Document $project, Document $function, Document $deployment, Document $template, string $SHA = '', string $targetUrl = '')
{
$templateRepositoryNmae = $template->getAttribute('templateRepositoryName');
$templateOwnerName = $template->getAttribute('templateOwnerName');
global $register;
$dbForProject = $this->getProjectDB($project);

View file

@ -10,6 +10,7 @@ class Build extends Event
protected string $type = '';
protected ?Document $resource = null;
protected ?Document $deployment = null;
protected ?Document $template = null;
protected string $SHA = '';
protected string $targetUrl = '';
@ -18,6 +19,19 @@ class Build extends Event
parent::__construct(Event::BUILDS_QUEUE_NAME, Event::BUILDS_CLASS_NAME);
}
/**
* Sets template for the build event.
*
* @param Document $template
* @return self
*/
public function setTemplate(Document $template): self
{
$this->template = $template;
return $this;
}
/**
* Sets commit SHA for the build event.
*
@ -126,6 +140,7 @@ class Build extends Event
'resource' => $this->resource,
'deployment' => $this->deployment,
'type' => $this->type,
'template' => $this->template,
'SHA' => $this->SHA,
'targetUrl' => $this->targetUrl
]);