diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 39b613c3c8..fd94c746ad 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -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'); diff --git a/app/workers/builds.php b/app/workers/builds.php index e124cb3a77..7958da8894 100644 --- a/app/workers/builds.php +++ b/app/workers/builds.php @@ -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); diff --git a/src/Appwrite/Event/Build.php b/src/Appwrite/Event/Build.php index 8ffc421a56..e868c9e9e2 100644 --- a/src/Appwrite/Event/Build.php +++ b/src/Appwrite/Event/Build.php @@ -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 ]);