From 916a3a5aa9a0c0ae2ba0f80d55df7c6a919dff05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 23 Jul 2026 16:08:32 +0200 Subject: [PATCH 1/9] Introdice gitea container locally and in CICD --- .env | 6 ++-- .github/workflows/ci.yml | 13 +++++-- docker-compose.override.yml | 70 ++++++++++++++++++++++++++++++++++++- 3 files changed, 82 insertions(+), 7 deletions(-) diff --git a/.env b/.env index b48a023df1..6262c41f20 100644 --- a/.env +++ b/.env @@ -155,11 +155,11 @@ _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= diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8eb89ceb31..3b9060a080 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -435,22 +435,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 }}" = "VCS" ]; 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 diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 2613f5f243..aab3eede66 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -19,8 +19,13 @@ services: - ./public:/usr/src/code/public - ./src:/usr/src/code/src - ./dev:/usr/src/code/dev + - appwrite-gitea:/gitea-data:ro depends_on: - - coredns + coredns: + condition: service_started + gitea-bootstrap: + condition: service_completed_successfully + required: false ports: - 9501:80 dns: @@ -331,6 +336,67 @@ 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 + environment: + - USER_UID=1000 + - USER_GID=1000 + - GITEA__database__DB_TYPE=sqlite3 + - GITEA__security__INSTALL_LOCK=true + - 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 + gitea-bootstrap: + profiles: + - gitea + image: gitea/gitea:1.21.5 + container_name: appwrite-gitea-bootstrap + networks: + - appwrite + volumes: + - appwrite-gitea:/data + depends_on: + gitea: + condition: service_healthy + environment: + - GITEA_ADMIN_USERNAME=${GITEA_ADMIN_USERNAME:-appwrite} + - GITEA_ADMIN_PASSWORD=${GITEA_ADMIN_PASSWORD:-password} + - GITEA_ADMIN_EMAIL=${GITEA_ADMIN_EMAIL:-gitea@appwrite.io} + entrypoint: /bin/sh + command: > + -c " + 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/token.txt ]; then + TOKEN=$$(su git -c \"gitea admin user generate-access-token --username $$GITEA_ADMIN_USERNAME --token-name $$GITEA_ADMIN_USERNAME-token --scopes all --raw\") && + echo $$TOKEN > /data/gitea/token.txt; + fi + " appwrite-mongo-express: profiles: - mongodb @@ -514,3 +580,5 @@ networks: ipam: config: - subnet: 172.16.238.0/24 +volumes: + appwrite-gitea: null From a35e0b6a2bcf12a950e547bc873d64191874c8f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 23 Jul 2026 16:17:23 +0200 Subject: [PATCH 2/9] Add E2E gitea tests --- tests/e2e/Services/VCSGitea/VCSGiteaBase.php | 17 +++ .../VCSGitea/VCSGiteaConsoleClientTest.php | 144 ++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 tests/e2e/Services/VCSGitea/VCSGiteaBase.php create mode 100644 tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaBase.php b/tests/e2e/Services/VCSGitea/VCSGiteaBase.php new file mode 100644 index 0000000000..34a63e1010 --- /dev/null +++ b/tests/e2e/Services/VCSGitea/VCSGiteaBase.php @@ -0,0 +1,17 @@ +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..a719709576 --- /dev/null +++ b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php @@ -0,0 +1,144 @@ +getProject()['$id']; + + $authorize = $this->client->call(Client::METHOD_GET, '/vcs/gitea/authorize', array_merge([ + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), [], 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->giteaCall($gitea, Client::METHOD_GET, '/user/login'); + $this->assertNotEmpty($this->giteaCookies['_csrf'] ?? '', 'Gitea did not issue a CSRF cookie.'); + + $login = $this->giteaCall($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->giteaCall($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->giteaCall($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->assertStringContainsString('git-installations', (string) ($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']); + + $installation = null; + foreach ($installations['body']['installations'] as $candidate) { + if ($candidate['provider'] === 'gitea') { + $installation = $candidate; + break; + } + } + + $this->assertNotNull($installation, 'Gitea installation not found in listInstallations.'); + $this->assertNotEmpty($installation['$id']); + $this->assertEquals('gitea', $installation['provider']); + $this->assertEquals(self::GITEA_USERNAME, $installation['organization']); + $this->assertTrue($installation['personal']); + } + + // Client does not persist cookies between calls, so carry them manually and never follow redirects + private function giteaCall(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; + } +} From 732c4cc1abcc6f94e21c8bc0e20fb69b0e5bb5d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 23 Jul 2026 16:27:35 +0200 Subject: [PATCH 3/9] Fix failing tests --- .../Services/VCSGitea/VCSGiteaConsoleClientTest.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php index a719709576..a9739ef55a 100644 --- a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php @@ -25,10 +25,14 @@ final class VCSGiteaConsoleClientTest extends Scope public function testCreateInstallation(): void { $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()), [], true, false); + ], $this->getHeaders()), [ + 'success' => $consoleUrl, + 'failure' => $consoleUrl, + ], true, false); $this->assertEquals(301, $authorize['headers']['status-code']); @@ -95,7 +99,7 @@ final class VCSGiteaConsoleClientTest extends Scope ], true, false); $this->assertEquals(301, $callback['headers']['status-code']); - $this->assertStringContainsString('git-installations', (string) ($callback['headers']['location'] ?? '')); + $this->assertEquals($consoleUrl, $callback['headers']['location'] ?? ''); $installations = $this->client->call(Client::METHOD_GET, '/vcs/installations', array_merge([ 'x-appwrite-project' => $projectId, @@ -116,7 +120,7 @@ final class VCSGiteaConsoleClientTest extends Scope $this->assertNotEmpty($installation['$id']); $this->assertEquals('gitea', $installation['provider']); $this->assertEquals(self::GITEA_USERNAME, $installation['organization']); - $this->assertTrue($installation['personal']); + $this->assertNotEmpty($installation['providerInstallationId']); } // Client does not persist cookies between calls, so carry them manually and never follow redirects From 956adbf4f90dfb7b94127ba6793aba980543b71b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 23 Jul 2026 16:34:43 +0200 Subject: [PATCH 4/9] Improve gitea setup quality --- .env | 1 + docker-compose.override.yml | 9 +++------ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/.env b/.env index 6262c41f20..f060ec0714 100644 --- a/.env +++ b/.env @@ -160,6 +160,7 @@ _APP_VCS_GITEA_BROWSER_ENDPOINT=http://localhost:9515 _APP_VCS_GITEA_CLIENT_ID= _APP_VCS_GITEA_CLIENT_SECRET= _APP_VCS_GITEA_WEBHOOK_SECRET=gitea-webhook-secret +_APP_VCS_WEBHOOK_URL=http://appwrite/v1 _APP_VCS_GITLAB_CLIENT_ID= _APP_VCS_GITLAB_CLIENT_SECRET= _APP_VCS_GITLAB_WEBHOOK_SECRET= diff --git a/docker-compose.override.yml b/docker-compose.override.yml index aab3eede66..3c14bea4c6 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -19,7 +19,6 @@ services: - ./public:/usr/src/code/public - ./src:/usr/src/code/src - ./dev:/usr/src/code/dev - - appwrite-gitea:/gitea-data:ro depends_on: coredns: condition: service_started @@ -357,6 +356,8 @@ services: - USER_GID=1000 - 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 @@ -391,11 +392,7 @@ services: entrypoint: /bin/sh command: > -c " - 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/token.txt ]; then - TOKEN=$$(su git -c \"gitea admin user generate-access-token --username $$GITEA_ADMIN_USERNAME --token-name $$GITEA_ADMIN_USERNAME-token --scopes all --raw\") && - echo $$TOKEN > /data/gitea/token.txt; - fi + 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 " appwrite-mongo-express: profiles: From f9876aba8ff2e4ff45f327bcb570b7b207c236ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 23 Jul 2026 16:39:46 +0200 Subject: [PATCH 5/9] rename tests for better categorization --- .env | 3 +-- .github/workflows/ci.yml | 5 +++-- .../{VCS/VCSBase.php => VCSGitHub/VCSGitHubBase.php} | 4 ++-- .../VCSGitHubConsoleClientTest.php} | 6 +++--- .../VCSGitHubCustomClientTest.php} | 6 +++--- .../VCSGitHubCustomServerTest.php} | 6 +++--- 6 files changed, 15 insertions(+), 15 deletions(-) rename tests/e2e/Services/{VCS/VCSBase.php => VCSGitHub/VCSGitHubBase.php} (81%) rename tests/e2e/Services/{VCS/VCSConsoleClientTest.php => VCSGitHub/VCSGitHubConsoleClientTest.php} (99%) rename tests/e2e/Services/{VCS/VCSCustomClientTest.php => VCSGitHub/VCSGitHubCustomClientTest.php} (83%) rename tests/e2e/Services/{VCS/VCSCustomServerTest.php => VCSGitHub/VCSGitHubCustomServerTest.php} (85%) diff --git a/.env b/.env index f060ec0714..dc5459551b 100644 --- a/.env +++ b/.env @@ -160,9 +160,8 @@ _APP_VCS_GITEA_BROWSER_ENDPOINT=http://localhost:9515 _APP_VCS_GITEA_CLIENT_ID= _APP_VCS_GITEA_CLIENT_SECRET= _APP_VCS_GITEA_WEBHOOK_SECRET=gitea-webhook-secret -_APP_VCS_WEBHOOK_URL=http://appwrite/v1 _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 3b9060a080..f72d0d9107 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: true, 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 }, @@ -452,7 +453,7 @@ jobs: fi # Gitea runs alongside Appwrite only where VCS tests need it. - if [ "${{ matrix.service.name }}" = "VCS" ]; then + if [ "${{ matrix.service.name }}" = "VCSGitea" ]; then COMPOSE_PROFILES="${COMPOSE_PROFILES},gitea" fi 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 @@ Date: Thu, 23 Jul 2026 16:44:10 +0200 Subject: [PATCH 6/9] Improve CICD gitea app setup --- .github/workflows/ci.yml | 11 +++++++++++ dev/gitea/setup.sh | 12 ++++++++++++ docker-compose.override.yml | 6 ++---- 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100755 dev/gitea/setup.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f72d0d9107..c64cdcbf6d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -489,6 +489,17 @@ jobs: sleep 15 done + - name: Provision Gitea + if: matrix.service.name == 'VCSGitea' + timeout-minutes: 5 + run: | + docker compose up -d --wait gitea gitea-bootstrap + 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 3c14bea4c6..069296956b 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -382,6 +382,7 @@ services: - appwrite volumes: - appwrite-gitea:/data + - ./dev/gitea/setup.sh:/setup.sh:ro depends_on: gitea: condition: service_healthy @@ -390,10 +391,7 @@ services: - GITEA_ADMIN_PASSWORD=${GITEA_ADMIN_PASSWORD:-password} - GITEA_ADMIN_EMAIL=${GITEA_ADMIN_EMAIL:-gitea@appwrite.io} entrypoint: /bin/sh - command: > - -c " - 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 - " + command: /setup.sh appwrite-mongo-express: profiles: - mongodb From fca5b718a3f3925a76977bb5fd944dab762d3513 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 23 Jul 2026 16:54:11 +0200 Subject: [PATCH 7/9] Add function E2E vcs test --- .../VCSGitea/VCSGiteaConsoleClientTest.php | 220 ++++++++++++++++-- 1 file changed, 202 insertions(+), 18 deletions(-) diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php index a9739ef55a..3b65774a78 100644 --- a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php @@ -4,10 +4,13 @@ declare(strict_types=1); namespace Tests\E2E\Services\VCSGitea; +use Appwrite\Tests\Async\Exceptions\Critical; use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideConsole; +use Utopia\Database\Helpers\ID; +use Utopia\Database\Helpers\Role; use Utopia\System\System; final class VCSGiteaConsoleClientTest extends Scope @@ -23,11 +26,99 @@ final class VCSGiteaConsoleClientTest extends Scope private array $giteaCookies = []; public function testCreateInstallation(): void + { + $installation = $this->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([ + $authorize = $this->client->call(Client::METHOD_GET, '/vcs/gitea/authorize', \array_merge([ 'x-appwrite-project' => $projectId, ], $this->getHeaders()), [ 'success' => $consoleUrl, @@ -50,10 +141,10 @@ final class VCSGiteaConsoleClientTest extends Scope $gitea = new Client(); $gitea->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', 'http://gitea:3000')); - $this->giteaCall($gitea, Client::METHOD_GET, '/user/login'); + $this->giteaCallHelper($gitea, Client::METHOD_GET, '/user/login'); $this->assertNotEmpty($this->giteaCookies['_csrf'] ?? '', 'Gitea did not issue a CSRF cookie.'); - $login = $this->giteaCall($gitea, Client::METHOD_POST, '/user/login', [ + $login = $this->giteaCallHelper($gitea, Client::METHOD_POST, '/user/login', [ '_csrf' => $this->giteaCookies['_csrf'], 'user_name' => self::GITEA_USERNAME, 'password' => self::GITEA_PASSWORD, @@ -63,7 +154,7 @@ final class VCSGiteaConsoleClientTest extends Scope // 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->giteaCall($gitea, Client::METHOD_GET, $authorizePath); + $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 @@ -71,7 +162,7 @@ final class VCSGiteaConsoleClientTest extends Scope } else { $this->assertEquals(200, $consent['headers']['status-code']); - $grant = $this->giteaCall($gitea, Client::METHOD_POST, '/login/oauth/grant', [ + $grant = $this->giteaCallHelper($gitea, Client::METHOD_POST, '/login/oauth/grant', [ '_csrf' => $this->giteaCookies['_csrf'], 'client_id' => $query['client_id'], 'state' => $query['state'], @@ -91,7 +182,7 @@ final class VCSGiteaConsoleClientTest extends Scope $this->assertNotEmpty($callbackQuery['code'] ?? ''); $this->assertNotEmpty($callbackQuery['state'] ?? ''); - $callback = $this->client->call(Client::METHOD_GET, '/vcs/gitea/callback', array_merge([ + $callback = $this->client->call(Client::METHOD_GET, '/vcs/gitea/callback', \array_merge([ 'x-appwrite-project' => $projectId, ], $this->getHeaders()), [ 'code' => $callbackQuery['code'], @@ -101,30 +192,24 @@ final class VCSGiteaConsoleClientTest extends Scope $this->assertEquals(301, $callback['headers']['status-code']); $this->assertEquals($consoleUrl, $callback['headers']['location'] ?? ''); - $installations = $this->client->call(Client::METHOD_GET, '/vcs/installations', array_merge([ + $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']); - $installation = null; - foreach ($installations['body']['installations'] as $candidate) { - if ($candidate['provider'] === 'gitea') { - $installation = $candidate; - break; + foreach ($installations['body']['installations'] as $installation) { + if ($installation['provider'] === 'gitea') { + return $installation; } } - $this->assertNotNull($installation, 'Gitea installation not found in listInstallations.'); - $this->assertNotEmpty($installation['$id']); - $this->assertEquals('gitea', $installation['provider']); - $this->assertEquals(self::GITEA_USERNAME, $installation['organization']); - $this->assertNotEmpty($installation['providerInstallationId']); + $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 giteaCall(Client $gitea, string $method, string $path, array $params = []): array + private function giteaCallHelper(Client $gitea, string $method, string $path, array $params = []): array { $headers = []; @@ -145,4 +230,103 @@ final class VCSGiteaConsoleClientTest extends Scope 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'] ?? ''); + } } From 315a29c2fde8a4be88aac894cda60eaeb94f242d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 23 Jul 2026 17:08:25 +0200 Subject: [PATCH 8/9] Simplify gitea setup --- .github/workflows/ci.yml | 3 ++- docker-compose.override.yml | 29 +++++------------------------ 2 files changed, 7 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c64cdcbf6d..6287a2a669 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -493,7 +493,8 @@ jobs: if: matrix.service.name == 'VCSGitea' timeout-minutes: 5 run: | - docker compose up -d --wait gitea gitea-bootstrap + 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" diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 069296956b..b8b09cfc18 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -20,11 +20,7 @@ services: - ./src:/usr/src/code/src - ./dev:/usr/src/code/dev depends_on: - coredns: - condition: service_started - gitea-bootstrap: - condition: service_completed_successfully - required: false + - coredns ports: - 9501:80 dns: @@ -351,9 +347,13 @@ services: - 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 @@ -373,25 +373,6 @@ services: timeout: 5s retries: 10 start_period: 10s - gitea-bootstrap: - profiles: - - gitea - image: gitea/gitea:1.21.5 - container_name: appwrite-gitea-bootstrap - networks: - - appwrite - volumes: - - appwrite-gitea:/data - - ./dev/gitea/setup.sh:/setup.sh:ro - depends_on: - gitea: - condition: service_healthy - environment: - - GITEA_ADMIN_USERNAME=${GITEA_ADMIN_USERNAME:-appwrite} - - GITEA_ADMIN_PASSWORD=${GITEA_ADMIN_PASSWORD:-password} - - GITEA_ADMIN_EMAIL=${GITEA_ADMIN_EMAIL:-gitea@appwrite.io} - entrypoint: /bin/sh - command: /setup.sh appwrite-mongo-express: profiles: - mongodb From c14717443d6a230b1820f8741c8a231b6ccdbf3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 23 Jul 2026 17:22:09 +0200 Subject: [PATCH 9/9] Fix CICD test --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6287a2a669..f64042b4e7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -215,7 +215,7 @@ jobs: { name: 'ProjectWebhooks', runner: runner4, functional: false, processes: null }, { name: 'Webhooks', runner: runner4, functional: true, processes: null }, { name: 'VCSGitHub', runner: runner4, functional: true, processes: null }, - { name: 'VCSGitea', 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 },