diff --git a/.env b/.env index 4a4b7245a1..8aa4e80ccb 100644 --- a/.env +++ b/.env @@ -154,13 +154,13 @@ _APP_VCS_GITHUB_CLIENT_ID= _APP_VCS_GITHUB_CLIENT_SECRET= _APP_VCS_GITHUB_PRIVATE_KEY=disabled _APP_VCS_GITHUB_WEBHOOK_SECRET= -_APP_VCS_GITEA_ENDPOINT= -_APP_VCS_GITEA_BROWSER_ENDPOINT= +_APP_VCS_GITEA_ENDPOINT=http://gitea:3000 +_APP_VCS_GITEA_BROWSER_ENDPOINT=http://localhost:9515 _APP_VCS_GITEA_CLIENT_ID= _APP_VCS_GITEA_CLIENT_SECRET= -_APP_VCS_GITEA_WEBHOOK_SECRET= +_APP_VCS_GITEA_WEBHOOK_SECRET=gitea-webhook-secret _APP_VCS_GITLAB_CLIENT_ID= _APP_VCS_GITLAB_CLIENT_SECRET= _APP_VCS_GITLAB_WEBHOOK_SECRET= -_APP_VCS_WEBHOOK_URL= +_APP_VCS_WEBHOOK_URL=http://appwrite/v1 _APP_WEBHOOK_MAX_FAILED_ATTEMPTS=10 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7f20ef876f..a3949e832f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -214,7 +214,8 @@ jobs: { name: 'Users', runner: runner4, functional: true, processes: null }, { name: 'ProjectWebhooks', runner: runner4, functional: false, processes: null }, { name: 'Webhooks', runner: runner4, functional: true, processes: null }, - { name: 'VCS', runner: runner4, functional: true, processes: null }, + { name: 'VCSGitHub', runner: runner4, functional: true, processes: null }, + { name: 'VCSGitea', runner: runner4, functional: false, processes: null }, { name: 'Messaging', runner: runner4, functional: true, processes: null }, { name: 'Notifications', runner: runner4, functional: true, processes: null }, { name: 'Migrations', runner: runner4, functional: true, processes: 1 }, @@ -435,22 +436,29 @@ jobs: echo "_APP_OPTIONS_ROUTER_PROTECTION=enabled" >> $GITHUB_ENV if [ "${{ matrix.database }}" = "MariaDB" ]; then - echo "COMPOSE_PROFILES=mariadb" >> $GITHUB_ENV + COMPOSE_PROFILES=mariadb echo "_APP_DB_ADAPTER=mariadb" >> $GITHUB_ENV echo "_APP_DB_HOST=mariadb" >> $GITHUB_ENV echo "_APP_DB_PORT=3306" >> $GITHUB_ENV elif [ "${{ matrix.database }}" = "MongoDB" ]; then - echo "COMPOSE_PROFILES=mongodb" >> $GITHUB_ENV + COMPOSE_PROFILES=mongodb echo "_APP_DB_ADAPTER=mongodb" >> $GITHUB_ENV echo "_APP_DB_HOST=mongodb" >> $GITHUB_ENV echo "_APP_DB_PORT=27017" >> $GITHUB_ENV elif [ "${{ matrix.database }}" = "PostgreSQL" ]; then - echo "COMPOSE_PROFILES=postgresql" >> $GITHUB_ENV + COMPOSE_PROFILES=postgresql echo "_APP_DB_ADAPTER=postgresql" >> $GITHUB_ENV echo "_APP_DB_HOST=postgresql" >> $GITHUB_ENV echo "_APP_DB_PORT=5432" >> $GITHUB_ENV fi + # Gitea runs alongside Appwrite only where VCS tests need it. + if [ "${{ matrix.service.name }}" = "VCSGitea" ]; then + COMPOSE_PROFILES="${COMPOSE_PROFILES},gitea" + fi + + echo "COMPOSE_PROFILES=${COMPOSE_PROFILES}" >> $GITHUB_ENV + - name: Login to Docker Hub if: ${{ env.DOCKERHUB_AUTH_AVAILABLE == 'true' }} uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0 @@ -481,6 +489,18 @@ jobs: sleep 15 done + - name: Provision Gitea + if: matrix.service.name == 'VCSGitea' + timeout-minutes: 5 + run: | + docker compose up -d --wait gitea + docker compose exec -T gitea /setup.sh + CREDS=$(docker compose exec -T gitea cat /data/gitea/oauth.json) + CLIENT_SECRET=$(echo "$CREDS" | jq -r .client_secret) + echo "::add-mask::$CLIENT_SECRET" + echo "_APP_VCS_GITEA_CLIENT_ID=$(echo "$CREDS" | jq -r .client_id)" >> $GITHUB_ENV + echo "_APP_VCS_GITEA_CLIENT_SECRET=$CLIENT_SECRET" >> $GITHUB_ENV + - name: Load and Start Appwrite timeout-minutes: 5 env: diff --git a/dev/gitea/setup.sh b/dev/gitea/setup.sh new file mode 100755 index 0000000000..204737861c --- /dev/null +++ b/dev/gitea/setup.sh @@ -0,0 +1,12 @@ +#!/bin/sh +set -eu + +su git -c "gitea admin user create --username $GITEA_ADMIN_USERNAME --password $GITEA_ADMIN_PASSWORD --email $GITEA_ADMIN_EMAIL --admin --must-change-password=false" || true + +if [ ! -f /data/gitea/oauth.json ]; then + curl -sf -X POST -u "$GITEA_ADMIN_USERNAME:$GITEA_ADMIN_PASSWORD" \ + -H 'Content-Type: application/json' \ + -d '{"name":"Appwrite","redirect_uris":["http://localhost/v1/vcs/gitea/callback"],"confidential_client":true}' \ + http://gitea:3000/api/v1/user/applications/oauth2 > /tmp/oauth.json + mv /tmp/oauth.json /data/gitea/oauth.json +fi diff --git a/docker-compose.override.yml b/docker-compose.override.yml index b4a2c50993..5049c71a7a 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -331,6 +331,48 @@ services: - 9507:5000 networks: - appwrite + gitea: + profiles: + - gitea + image: gitea/gitea:1.21.5 + container_name: appwrite-gitea + logging: + driver: json-file + options: + max-file: "5" + max-size: 10m + networks: + - appwrite + ports: + - 9515:3000 + volumes: + - appwrite-gitea:/data + - ./dev/gitea/setup.sh:/setup.sh:ro + environment: + - USER_UID=1000 + - USER_GID=1000 + - GITEA_ADMIN_USERNAME=${GITEA_ADMIN_USERNAME:-appwrite} + - GITEA_ADMIN_PASSWORD=${GITEA_ADMIN_PASSWORD:-password} + - GITEA_ADMIN_EMAIL=${GITEA_ADMIN_EMAIL:-gitea@appwrite.io} + - GITEA__database__DB_TYPE=sqlite3 + - GITEA__security__INSTALL_LOCK=true + - GITEA__service__DISABLE_REGISTRATION=false + - GITEA__repository__DEFAULT_BRANCH=main + - GITEA__webhook__ALLOWED_HOST_LIST=* + - GITEA__webhook__SKIP_TLS_VERIFY=true + - GITEA__webhook__DELIVER_TIMEOUT=10 + - GITEA__server__ROOT_URL=http://gitea:3000/ + - GITEA__server__LOCAL_ROOT_URL=http://gitea:3000/ + healthcheck: + test: + - CMD + - curl + - -f + - http://localhost:3000/api/healthz + interval: 10s + timeout: 5s + retries: 10 + start_period: 10s appwrite-mongo-express: profiles: - mongodb @@ -522,3 +564,5 @@ networks: ipam: config: - subnet: 172.16.238.0/24 +volumes: + appwrite-gitea: null diff --git a/tests/e2e/Services/VCS/VCSBase.php b/tests/e2e/Services/VCSGitHub/VCSGitHubBase.php similarity index 81% rename from tests/e2e/Services/VCS/VCSBase.php rename to tests/e2e/Services/VCSGitHub/VCSGitHubBase.php index a65ebcd1d3..f485c96980 100644 --- a/tests/e2e/Services/VCS/VCSBase.php +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubBase.php @@ -1,10 +1,10 @@ markTestSkipped('Gitea VCS is not configured.'); + } + } +} diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php new file mode 100644 index 0000000000..3b65774a78 --- /dev/null +++ b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php @@ -0,0 +1,332 @@ +createInstallationHelper(); + + $this->assertNotEmpty($installation['$id']); + $this->assertEquals('gitea', $installation['provider']); + $this->assertEquals(self::GITEA_USERNAME, $installation['organization']); + $this->assertNotEmpty($installation['providerInstallationId']); + } + + public function testCreateDeploymentFromGitPush(): void + { + $projectId = $this->getProject()['$id']; + $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'])); + $repositoryName = $repository['body']['name']; + + $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 . '/' . $repositoryName . '.git'; + + $this->gitHelper("git clone {$remote} {$workdir}", \sys_get_temp_dir()); + $this->writeFunctionHelper($workdir, 'gitea-v1'); + $this->gitHelper('git add index.js && git commit -m "Add function"', $workdir); + $this->gitHelper('git push origin main', $workdir); + + $function = $this->client->call(Client::METHOD_POST, '/functions', \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), [ + 'functionId' => ID::unique(), + 'name' => 'Gitea VCS', + 'execute' => [Role::any()->toString()], + 'runtime' => 'node-22', + 'entrypoint' => 'index.js', + 'timeout' => 15, + 'installationId' => $installationId, + 'providerRepositoryId' => (string) $repository['body']['id'], + 'providerBranch' => 'main', + ]); + $this->assertEquals(201, $function['headers']['status-code'], \json_encode($function['body'])); + $functionId = $function['body']['$id']; + + // Connecting the repository must register a push webhook in Gitea + $hooks = $this->giteaApiHelper(Client::METHOD_GET, '/api/v1/repos/' . self::GITEA_USERNAME . '/' . $repositoryName . '/hooks'); + $this->assertEquals(200, $hooks['headers']['status-code']); + $matching = \array_filter($hooks['body'], fn ($hook) => \str_contains($hook['config']['url'] ?? '', '/v1/vcs/gitea/events')); + $this->assertNotEmpty($matching, 'No Appwrite webhook registered in Gitea: ' . \json_encode($hooks['body'])); + + $deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments/vcs', \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), [ + 'type' => 'branch', + 'reference' => 'main', + 'activate' => true, + ]); + $this->assertEquals(202, $deployment['headers']['status-code'], \json_encode($deployment['body'])); + + $this->waitForDeploymentReadyHelper($functionId, $deployment['body']['$id']); + $this->assertEventually(fn () => $this->assertExecutionOutputHelper($functionId, 'gitea-v1'), 30000, 1000); + + // Connecting the repository can auto-create a deployment alongside the + // explicit one, so collect every pre-push deployment; only a deployment + // absent from this list can be the webhook's. + $knownIds = $this->listDeploymentIdsHelper($functionId); + + $this->writeFunctionHelper($workdir, 'gitea-v2'); + $this->gitHelper('git add index.js && git commit -m "Update function"', $workdir); + $this->gitHelper('git push origin main', $workdir); + + $webhookDeploymentId = $this->waitForNewDeploymentReadyHelper($functionId, $knownIds); + $this->assertNotContains($webhookDeploymentId, $knownIds); + $this->assertEventually(fn () => $this->assertExecutionOutputHelper($functionId, 'gitea-v2'), 30000, 1000); + } + + /** + * Walk the full OAuth2 dance against the local Gitea and return the + * resulting installation, asserting every hop on the way. + */ + private function createInstallationHelper(): array + { + $projectId = $this->getProject()['$id']; + $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; + + $authorize = $this->client->call(Client::METHOD_GET, '/vcs/gitea/authorize', \array_merge([ + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), [ + 'success' => $consoleUrl, + 'failure' => $consoleUrl, + ], true, false); + + $this->assertEquals(301, $authorize['headers']['status-code']); + + $loginUrl = $authorize['headers']['location'] ?? ''; + $this->assertStringContainsString('/login/oauth/authorize', (string) $loginUrl); + + $query = []; + \parse_str(\parse_url($loginUrl, PHP_URL_QUERY) ?: '', $query); + $this->assertNotEmpty($query['client_id'] ?? ''); + $this->assertNotEmpty($query['redirect_uri'] ?? ''); + $this->assertNotEmpty($query['state'] ?? ''); + $this->assertSame('code', $query['response_type'] ?? ''); + + // Location targets the browser-facing endpoint, unreachable in-container; reuse only its path and query + $gitea = new Client(); + $gitea->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', 'http://gitea:3000')); + + $this->giteaCallHelper($gitea, Client::METHOD_GET, '/user/login'); + $this->assertNotEmpty($this->giteaCookies['_csrf'] ?? '', 'Gitea did not issue a CSRF cookie.'); + + $login = $this->giteaCallHelper($gitea, Client::METHOD_POST, '/user/login', [ + '_csrf' => $this->giteaCookies['_csrf'], + 'user_name' => self::GITEA_USERNAME, + 'password' => self::GITEA_PASSWORD, + ]); + $this->assertContains($login['headers']['status-code'], [302, 303], 'Gitea login failed.'); + $this->assertNotEmpty($this->giteaCookies['i_like_gitea'] ?? '', 'Gitea did not issue a session cookie.'); + + // Gitea stores client_id, state and redirect_uri in the session here; the grant must match them + $authorizePath = \parse_url($loginUrl, PHP_URL_PATH) . '?' . \parse_url($loginUrl, PHP_URL_QUERY); + $consent = $this->giteaCallHelper($gitea, Client::METHOD_GET, $authorizePath); + + if (\in_array($consent['headers']['status-code'], [302, 303])) { + // Already granted: Gitea redirects straight back with a code + $redirect = $consent['headers']['location'] ?? ''; + } else { + $this->assertEquals(200, $consent['headers']['status-code']); + + $grant = $this->giteaCallHelper($gitea, Client::METHOD_POST, '/login/oauth/grant', [ + '_csrf' => $this->giteaCookies['_csrf'], + 'client_id' => $query['client_id'], + 'state' => $query['state'], + 'scope' => $query['scope'] ?? '', + 'nonce' => '', + 'redirect_uri' => $query['redirect_uri'], + ]); + $this->assertEquals(303, $grant['headers']['status-code']); + + $redirect = $grant['headers']['location'] ?? ''; + } + + $this->assertStringContainsString('/v1/vcs/gitea/callback', (string) $redirect); + + $callbackQuery = []; + \parse_str(\parse_url($redirect, PHP_URL_QUERY) ?: '', $callbackQuery); + $this->assertNotEmpty($callbackQuery['code'] ?? ''); + $this->assertNotEmpty($callbackQuery['state'] ?? ''); + + $callback = $this->client->call(Client::METHOD_GET, '/vcs/gitea/callback', \array_merge([ + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), [ + 'code' => $callbackQuery['code'], + 'state' => $callbackQuery['state'], + ], true, false); + + $this->assertEquals(301, $callback['headers']['status-code']); + $this->assertEquals($consoleUrl, $callback['headers']['location'] ?? ''); + + $installations = $this->client->call(Client::METHOD_GET, '/vcs/installations', \array_merge([ + 'x-appwrite-project' => $projectId, + ], $this->getHeaders())); + + $this->assertEquals(200, $installations['headers']['status-code']); + $this->assertGreaterThanOrEqual(1, $installations['body']['total']); + + foreach ($installations['body']['installations'] as $installation) { + if ($installation['provider'] === 'gitea') { + return $installation; + } + } + + $this->fail('Gitea installation not found in listInstallations.'); + } + + // Client does not persist cookies between calls, so carry them manually and never follow redirects + private function giteaCallHelper(Client $gitea, string $method, string $path, array $params = []): array + { + $headers = []; + + if (!empty($this->giteaCookies)) { + $headers['cookie'] = \implode('; ', \array_map( + fn (string $name) => $name . '=' . $this->giteaCookies[$name], + \array_keys($this->giteaCookies) + )); + } + + if ($method !== Client::METHOD_GET) { + $headers['content-type'] = 'application/x-www-form-urlencoded'; + } + + $response = $gitea->call($method, $path, $headers, $params, false, false); + + $this->giteaCookies = \array_merge($this->giteaCookies, $response['cookies']); + + return $response; + } + + private function giteaApiHelper(string $method, string $path, array $params = []): array + { + $gitea = new Client(); + $gitea->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', 'http://gitea:3000')); + + return $gitea->call($method, $path, [ + 'content-type' => 'application/json', + 'authorization' => 'Basic ' . \base64_encode(self::GITEA_USERNAME . ':' . self::GITEA_PASSWORD), + ], $params); + } + + private function gitHelper(string $command, string $directory): void + { + $identity = '-c user.email=gitea@appwrite.io -c user.name=' . self::GITEA_USERNAME; + $command = \str_replace('git commit', "git {$identity} commit", $command); + + \exec('cd ' . \escapeshellarg($directory) . ' && ' . $command . ' 2>&1', $output, $exitCode); + + $this->assertSame(0, $exitCode, "Git command failed: {$command}\n" . \implode("\n", $output)); + } + + private function writeFunctionHelper(string $workdir, string $output): void + { + \file_put_contents($workdir . '/index.js', "module.exports = async (context) => context.res.send('{$output}');\n"); + } + + private function waitForDeploymentReadyHelper(string $functionId, string $deploymentId): void + { + $this->assertEventually(function () use ($functionId, $deploymentId) { + $response = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/deployments/' . $deploymentId, \array_merge([ + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(200, $response['headers']['status-code']); + + if (($response['body']['status'] ?? '') === 'failed') { + throw new Critical('Deployment build failed: ' . ($response['body']['buildLogs'] ?? 'no logs')); + } + + $this->assertEquals('ready', $response['body']['status'] ?? '', \json_encode($response['body'])); + }, 300000, 2000); + } + + private function listDeploymentIdsHelper(string $functionId): array + { + return \array_column($this->listDeploymentsHelper($functionId), '$id'); + } + + private function waitForNewDeploymentReadyHelper(string $functionId, array $knownIds): string + { + $deploymentId = ''; + + $this->assertEventually(function () use ($functionId, $knownIds, &$deploymentId) { + foreach ($this->listDeploymentsHelper($functionId) as $candidate) { + if (\in_array($candidate['$id'], $knownIds, true)) { + continue; + } + + if (($candidate['status'] ?? '') === 'failed') { + throw new Critical('Webhook deployment build failed: ' . ($candidate['buildLogs'] ?? 'no logs')); + } + + if (($candidate['status'] ?? '') === 'ready') { + $deploymentId = $candidate['$id']; + return; + } + } + + $this->fail('Webhook deployment is not ready yet.'); + }, 300000, 2000); + + return $deploymentId; + } + + private function listDeploymentsHelper(string $functionId): array + { + $response = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/deployments', \array_merge([ + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(200, $response['headers']['status-code']); + + return $response['body']['deployments'] ?? []; + } + + private function assertExecutionOutputHelper(string $functionId, string $output): void + { + $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'async' => false, + ]); + + $this->assertEquals(201, $execution['headers']['status-code'], \json_encode($execution['body'])); + $this->assertEquals('completed', $execution['body']['status'] ?? '', \json_encode($execution['body'])); + $this->assertEquals($output, $execution['body']['responseBody'] ?? ''); + } +}