diff --git a/src/Appwrite/Deployment/Deployments.php b/src/Appwrite/Deployment/Deployments.php index 612799075b..44a6dce9bb 100644 --- a/src/Appwrite/Deployment/Deployments.php +++ b/src/Appwrite/Deployment/Deployments.php @@ -165,6 +165,8 @@ readonly class Deployments string $ref, string $rootDirectory = '', ): Document { + $deployment->setAttribute('providerRootDirectory', $rootDirectory); + if ($vcs->supportsRepositoryArchives()) { return $this->createFromUrl( $resource, diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php index 437c7b3fc1..f5842b298d 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php @@ -167,7 +167,7 @@ class Create extends Action $owner, $repository, $ref, - $deployment->getAttribute('providerRootDirectory', ''), + $function->getAttribute('providerRootDirectory', ''), ); } else { // Public template repo: providerBranch holds the resolved ref, diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php index 7c703621db..c7348f315c 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php @@ -192,7 +192,7 @@ class Create extends Action $owner, $repository, $ref, - $deployment->getAttribute('providerRootDirectory', ''), + $site->getAttribute('providerRootDirectory', ''), ); } else { // Public template repo: providerBranch holds the resolved ref, diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php index 8db0756915..cea9401442 100644 --- a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php @@ -110,6 +110,75 @@ final class VCSGiteaConsoleClientTest extends Scope $this->assertEventually(fn () => $this->assertExecutionOutputHelper($functionId, 'gitea-v2'), 30000, 1000); } + public function testCreateDuplicateDeploymentWithRootDirectory(): void + { + $projectId = $this->getProject()['$id']; + $headers = \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()); + $installationId = $this->createInstallationHelper()['$id']; + + $repository = $this->giteaApiHelper(Client::METHOD_POST, '/api/v1/user/repos', [ + 'name' => 'function-' . \uniqid(), + 'auto_init' => true, + 'default_branch' => 'main', + 'private' => false, + ]); + $this->assertEquals(201, $repository['headers']['status-code'], \json_encode($repository['body'])); + + $workdir = \sys_get_temp_dir() . '/vcs-gitea-' . \uniqid(); + $endpoint = System::getEnv('_APP_VCS_GITEA_ENDPOINT', 'http://gitea:3000'); + $remote = \str_replace('://', '://' . self::GITEA_USERNAME . ':' . self::GITEA_PASSWORD . '@', $endpoint) + . '/' . self::GITEA_USERNAME . '/' . $repository['body']['name'] . '.git'; + + $this->gitHelper("git clone {$remote} {$workdir}", \sys_get_temp_dir()); + foreach (['api', 'web'] as $directory) { + \mkdir($workdir . '/functions/' . $directory, 0o777, true); + \file_put_contents($workdir . '/functions/' . $directory . '/index.js', "module.exports = async (context) => context.res.send('{$directory}:' + process.env.APPWRITE_VCS_ROOT_DIRECTORY);\n"); + } + $this->gitHelper('git add functions && git commit -m "Add functions"', $workdir); + $this->gitHelper('git push origin main', $workdir); + + $function = $this->client->call(Client::METHOD_POST, '/functions', $headers, [ + 'functionId' => ID::unique(), + 'name' => 'Gitea root directory', + 'execute' => [Role::any()->toString()], + 'runtime' => 'node-22', + 'entrypoint' => 'index.js', + 'timeout' => 15, + 'installationId' => $installationId, + 'providerRepositoryId' => (string) $repository['body']['id'], + 'providerBranch' => 'main', + 'providerRootDirectory' => 'functions/api', + ]); + $this->assertEquals(201, $function['headers']['status-code'], \json_encode($function['body'])); + $functionId = $function['body']['$id']; + + $deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments/vcs', $headers, [ + 'type' => 'branch', + 'reference' => 'main', + 'activate' => true, + ]); + $this->assertEquals(202, $deployment['headers']['status-code'], \json_encode($deployment['body'])); + $this->waitForDeploymentReadyHelper($functionId, $deployment['body']['$id']); + + $function = $this->client->call(Client::METHOD_PUT, '/functions/' . $functionId, $headers, [ + 'name' => 'Gitea root directory', + 'execute' => [Role::any()->toString()], + 'providerRootDirectory' => 'functions/web', + ]); + $this->assertEquals(200, $function['headers']['status-code'], \json_encode($function['body'])); + + $duplicate = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments/duplicate', $headers, [ + 'deploymentId' => $deployment['body']['$id'], + ]); + $this->assertEquals(202, $duplicate['headers']['status-code'], \json_encode($duplicate['body'])); + $this->waitForDeploymentReadyHelper($functionId, $duplicate['body']['$id']); + + $this->assertEventually(fn () => $this->assertExecutionOutputHelper($functionId, 'web:functions/web'), 30000, 1000); + } + public function testClosePullRequestRemovesAuthorization(): void { /**