Skip to content

Commit 908d80e

Browse files
authored
Merge pull request #9804 from appwrite/fix-function-create-compatibility
Fix: backwards compatibility for function creation
2 parents 08e9399 + 02b7e0d commit 908d80e

File tree

6 files changed

+211
-20
lines changed

6 files changed

+211
-20
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"utopia-php/platform": "0.7.*",
6868
"utopia-php/pools": "0.8.*",
6969
"utopia-php/preloader": "0.2.*",
70-
"utopia-php/queue": "0.9.*",
70+
"utopia-php/queue": "0.10.*",
7171
"utopia-php/registry": "0.5.*",
7272
"utopia-php/storage": "0.18.*",
7373
"utopia-php/swoole": "0.8.*",

composer.lock

Lines changed: 19 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php

Lines changed: 158 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace Appwrite\Platform\Modules\Functions\Http\Functions;
44

5+
use Appwrite\Event\Build;
56
use Appwrite\Event\Event;
7+
use Appwrite\Event\Func;
8+
use Appwrite\Event\Realtime;
69
use Appwrite\Event\Validator\FunctionEvent;
10+
use Appwrite\Event\Webhook;
711
use Appwrite\Extend\Exception;
812
use Appwrite\Platform\Modules\Compute\Base;
913
use Appwrite\Platform\Modules\Compute\Validator\Specification;
@@ -13,6 +17,7 @@
1317
use Appwrite\Task\Validator\Cron;
1418
use Appwrite\Utopia\Database\Validator\CustomId;
1519
use Appwrite\Utopia\Response;
20+
use Appwrite\Utopia\Response\Model\Rule;
1621
use Utopia\Abuse\Abuse;
1722
use Utopia\Config\Config;
1823
use Utopia\Database\Database;
@@ -25,12 +30,14 @@
2530
use Utopia\Database\Validator\Roles;
2631
use Utopia\Platform\Action;
2732
use Utopia\Platform\Scope\HTTP;
33+
use Utopia\Request;
2834
use Utopia\System\System;
2935
use Utopia\Validator\ArrayList;
3036
use Utopia\Validator\Boolean;
3137
use Utopia\Validator\Range;
3238
use Utopia\Validator\Text;
3339
use Utopia\Validator\WhiteList;
40+
use Utopia\VCS\Adapter\Git\GitHub;
3441

3542
class Create extends Base
3643
{
@@ -91,12 +98,22 @@ public function __construct()
9198
System::getEnv('_APP_COMPUTE_CPUS', 0),
9299
System::getEnv('_APP_COMPUTE_MEMORY', 0)
93100
), 'Runtime specification for the function and builds.', true, ['plan'])
101+
->param('templateRepository', '', new Text(128, 0), 'Repository name of the template.', true, deprecated: true)
102+
->param('templateOwner', '', new Text(128, 0), 'The name of the owner of the template.', true, deprecated: true)
103+
->param('templateRootDirectory', '', new Text(128, 0), 'Path to function code in the template repo.', true, deprecated: true)
104+
->param('templateVersion', '', new Text(128, 0), 'Version (tag) for the repo linked to the function template.', true, deprecated: true)
94105
->inject('response')
95106
->inject('dbForProject')
96107
->inject('timelimit')
97108
->inject('project')
98109
->inject('queueForEvents')
110+
->inject('queueForBuilds')
111+
->inject('queueForRealtime')
112+
->inject('queueForWebhooks')
113+
->inject('queueForFunctions')
99114
->inject('dbForPlatform')
115+
->inject('request')
116+
->inject('gitHub')
100117
->callback([$this, 'action']);
101118
}
102119

@@ -119,12 +136,22 @@ public function action(
119136
bool $providerSilentMode,
120137
string $providerRootDirectory,
121138
string $specification,
139+
string $templateRepository,
140+
string $templateOwner,
141+
string $templateRootDirectory,
142+
string $templateVersion,
122143
Response $response,
123144
Database $dbForProject,
124145
callable $timelimit,
125146
Document $project,
126147
Event $queueForEvents,
127-
Database $dbForPlatform
148+
Build $queueForBuilds,
149+
Realtime $queueForRealtime,
150+
Webhook $queueForWebhooks,
151+
Func $queueForFunctions,
152+
Database $dbForPlatform,
153+
Request $request,
154+
GitHub $github
128155
) {
129156

130157
// Temporary abuse check
@@ -251,6 +278,136 @@ public function action(
251278

252279
$function = $dbForProject->updateDocument('functions', $function->getId(), $function);
253280

281+
// Backwards compatibility with 1.6 behaviour
282+
$requestFormat = $request->getHeader('x-appwrite-response-format', System::getEnv('_APP_SYSTEM_RESPONSE_FORMAT', ''));
283+
if ($requestFormat && version_compare($requestFormat, '1.7.0', '<')) {
284+
// build from template
285+
$template = new Document([]);
286+
if (
287+
!empty($templateRepository)
288+
&& !empty($templateOwner)
289+
&& !empty($templateRootDirectory)
290+
&& !empty($templateVersion)
291+
) {
292+
$template->setAttribute('repositoryName', $templateRepository)
293+
->setAttribute('ownerName', $templateOwner)
294+
->setAttribute('rootDirectory', $templateRootDirectory)
295+
->setAttribute('version', $templateVersion);
296+
}
297+
298+
if (!empty($providerRepositoryId)) {
299+
// Deploy VCS
300+
$template = new Document();
301+
302+
$installation = $dbForPlatform->getDocument('installations', $function->getAttribute('installationId'));
303+
$deployment = $this->redeployVcsFunction(
304+
request: $request,
305+
function: $function,
306+
project: $project,
307+
installation: $installation,
308+
dbForProject: $dbForProject,
309+
queueForBuilds: $queueForBuilds,
310+
template: $template,
311+
github: $github,
312+
activate: true,
313+
reference: $providerBranch,
314+
referenceType: 'branch'
315+
);
316+
317+
$function = $function
318+
->setAttribute('latestDeploymentId', $deployment->getId())
319+
->setAttribute('latestDeploymentInternalId', $deployment->getInternalId())
320+
->setAttribute('latestDeploymentCreatedAt', $deployment->getCreatedAt())
321+
->setAttribute('latestDeploymentStatus', $deployment->getAttribute('status', ''));
322+
$dbForProject->updateDocument('functions', $function->getId(), $function);
323+
} elseif (!$template->isEmpty()) {
324+
// Deploy non-VCS from template
325+
$deploymentId = ID::unique();
326+
$deployment = $dbForProject->createDocument('deployments', new Document([
327+
'$id' => $deploymentId,
328+
'$permissions' => [
329+
Permission::read(Role::any()),
330+
Permission::update(Role::any()),
331+
Permission::delete(Role::any()),
332+
],
333+
'resourceId' => $function->getId(),
334+
'resourceInternalId' => $function->getInternalId(),
335+
'resourceType' => 'functions',
336+
'entrypoint' => $function->getAttribute('entrypoint', ''),
337+
'buildCommands' => $function->getAttribute('commands', ''),
338+
'type' => 'manual',
339+
'activate' => true,
340+
]));
341+
342+
$function = $function
343+
->setAttribute('latestDeploymentId', $deployment->getId())
344+
->setAttribute('latestDeploymentInternalId', $deployment->getInternalId())
345+
->setAttribute('latestDeploymentCreatedAt', $deployment->getCreatedAt())
346+
->setAttribute('latestDeploymentStatus', $deployment->getAttribute('status', ''));
347+
$dbForProject->updateDocument('functions', $function->getId(), $function);
348+
349+
$queueForBuilds
350+
->setType(BUILD_TYPE_DEPLOYMENT)
351+
->setResource($function)
352+
->setDeployment($deployment)
353+
->setTemplate($template);
354+
}
355+
356+
$functionsDomain = System::getEnv('_APP_DOMAIN_FUNCTIONS', '');
357+
if (!empty($functionsDomain)) {
358+
$routeSubdomain = ID::unique();
359+
$domain = "{$routeSubdomain}.{$functionsDomain}";
360+
// TODO: @christyjacob remove once we migrate the rules in 1.7.x
361+
$ruleId = System::getEnv('_APP_RULES_FORMAT') === 'md5' ? md5($domain) : ID::unique();
362+
363+
$rule = Authorization::skip(
364+
fn () => $dbForPlatform->createDocument('rules', new Document([
365+
'$id' => $ruleId,
366+
'projectId' => $project->getId(),
367+
'projectInternalId' => $project->getInternalId(),
368+
'domain' => $domain,
369+
'status' => 'verified',
370+
'type' => 'deployment',
371+
'trigger' => 'manual',
372+
'deploymentId' => !isset($deployment) || $deployment->isEmpty() ? '' : $deployment->getId(),
373+
'deploymentInternalId' => !isset($deployment) || $deployment->isEmpty() ? '' : $deployment->getInternalId(),
374+
'deploymentResourceType' => 'function',
375+
'deploymentResourceId' => $function->getId(),
376+
'deploymentResourceInternalId' => $function->getInternalId(),
377+
'deploymentVcsProviderBranch' => '',
378+
'certificateId' => '',
379+
'search' => implode(' ', [$ruleId, $domain]),
380+
'owner' => 'Appwrite',
381+
'region' => $project->getAttribute('region')
382+
]))
383+
);
384+
385+
$ruleModel = new Rule();
386+
$ruleCreate =
387+
$queueForEvents
388+
->setProject($project)
389+
->setEvent('rules.[ruleId].create')
390+
->setParam('ruleId', $rule->getId())
391+
->setPayload($rule->getArrayCopy(array_keys($ruleModel->getRules())));
392+
393+
/** Trigger Webhook */
394+
$queueForWebhooks
395+
->from($ruleCreate)
396+
->trigger();
397+
398+
/** Trigger Functions */
399+
$queueForFunctions
400+
->from($ruleCreate)
401+
->trigger();
402+
403+
/** Trigger Realtime Events */
404+
$queueForRealtime
405+
->from($ruleCreate)
406+
->setSubscribers(['console', $project->getId()])
407+
->trigger();
408+
}
409+
}
410+
254411
$queueForEvents->setParam('functionId', $function->getId());
255412

256413
$response

src/Appwrite/SDK/Specification/Format/OpenAPI3.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@ public function parse(): array
309309
$bodyRequired = [];
310310

311311
foreach ($route->getParams() as $name => $param) { // Set params
312+
if ($param['deprecated']) {
313+
continue;
314+
}
315+
312316
/**
313317
* @var \Utopia\Validator $validator
314318
*/

src/Appwrite/SDK/Specification/Format/Swagger2.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,10 @@ public function parse(): array
314314
);
315315

316316
foreach ($parameters as $name => $param) { // Set params
317+
if ($param['deprecated']) {
318+
continue;
319+
}
320+
317321
/** @var Validator $validator */
318322
$validator = (\is_callable($param['validator']))
319323
? ($param['validator'])(...$this->app->getResources($param['injections']))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy