Skip to content
Merged
59 changes: 55 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ services:
- ./tests:/usr/local/src/tests
- ./phpunit.xml:/usr/local/src/phpunit.xml
- gitea-data:/data:ro
- forgejo-data:/forgejo-data:ro
environment:
- TESTS_GITHUB_PRIVATE_KEY
- TESTS_GITHUB_APP_IDENTIFIER
- TESTS_GITHUB_INSTALLATION_ID
- TESTS_GITEA_URL=http://gitea:3000
- TESTS_GITEA_REQUEST_CATCHER_URL=http://request-catcher:5000
- TESTS_GITEA_REQUEST_CATCHER_URL=http://request-catcher:5000
- TESTS_FORGEJO_URL=http://forgejo:3000
depends_on:
gitea:
condition: service_healthy
gitea-bootstrap:
condition: service_completed_successfully
forgejo:
condition: service_healthy
forgejo-bootstrap:
condition: service_completed_successfully
request-catcher:
condition: service_started

Expand Down Expand Up @@ -58,12 +64,57 @@ services:
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 &&
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
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
"
request-catcher:
image: appwrite/requestcatcher:1.1.0
ports:
- "5000:5000"

forgejo:
image: codeberg.org/forgejo/forgejo:9
environment:
- USER_UID=1000
- USER_GID=1000
- FORGEJO__database__DB_TYPE=sqlite3
- FORGEJO__security__INSTALL_LOCK=true
- FORGEJO__webhook__ALLOWED_HOST_LIST=*
- FORGEJO__webhook__SKIP_TLS_VERIFY=true
- FORGEJO__webhook__DELIVER_TIMEOUT=10
- FORGEJO__server__LOCAL_ROOT_URL=http://forgejo:3000/
volumes:
- forgejo-data:/data
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/api/healthz"]
interval: 10s
timeout: 5s
retries: 10
start_period: 10s

forgejo-bootstrap:
image: codeberg.org/forgejo/forgejo:9
volumes:
- forgejo-data:/data
depends_on:
forgejo:
condition: service_healthy
entrypoint: /bin/sh
environment:
- FORGEJO_ADMIN_USERNAME=${FORGEJO_ADMIN_USERNAME:-utopia}
- FORGEJO_ADMIN_PASSWORD=${FORGEJO_ADMIN_PASSWORD:-password}
- FORGEJO_ADMIN_EMAIL=${FORGEJO_ADMIN_EMAIL:-utopia@example.com}
command: >
-c "
su git -c \"forgejo admin user create --username $$FORGEJO_ADMIN_USERNAME --password $$FORGEJO_ADMIN_PASSWORD --email $$FORGEJO_ADMIN_EMAIL --admin --must-change-password=false\" || true &&
if [ ! -f /data/gitea/token.txt ]; then
TOKEN=$$(su git -c \"forgejo admin user generate-access-token --username $$FORGEJO_ADMIN_USERNAME --token-name $$FORGEJO_ADMIN_USERNAME-token --scopes all --raw\") &&
echo $$TOKEN > /data/gitea/token.txt;
fi
"

volumes:
gitea-data:
gitea-data:
forgejo-data:
23 changes: 23 additions & 0 deletions src/VCS/Adapter/Git/Forgejo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Utopia\VCS\Adapter\Git;

class Forgejo extends Gitea
{
protected string $endpoint = 'http://forgejo:3000/api/v1';

/**
* Get Adapter Name
*
* @return string
*/
public function getName(): string
{
return 'forgejo';
}

protected function getHookType(): string
{
return 'forgejo';
}
}
7 changes: 6 additions & 1 deletion src/VCS/Adapter/Git/Gitea.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,11 @@ public function createPullRequest(string $owner, string $repositoryName, string
return $responseBody;
}

protected function getHookType(): string
{
return 'gitea';
}

/**
* Create a webhook on a repository
*
Expand All @@ -511,7 +516,7 @@ public function createWebhook(string $owner, string $repositoryName, string $url
"/repos/{$owner}/{$repositoryName}/hooks",
['Authorization' => "token $this->accessToken"],
[
'type' => 'gitea',
'type' => $this->getHookType(),
'active' => true,
'events' => $events,
'config' => [
Expand Down
62 changes: 62 additions & 0 deletions tests/VCS/Adapter/ForgejoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Utopia\Tests\Adapter;

use Utopia\Cache\Adapter\None;
use Utopia\Cache\Cache;
use Utopia\System\System;
use Utopia\VCS\Adapter\Git;
use Utopia\VCS\Adapter\Git\Forgejo;

class ForgejoTest extends GiteaTest
{
protected static string $accessToken = '';

protected static string $owner = '';

protected string $webhookEventHeader = 'X-Forgejo-Event';
protected string $webhookSignatureHeader = 'X-Forgejo-Signature';
protected string $avatarDomain = 'http://localhost:3000/avatars/';

protected function createVCSAdapter(): Git
{
return new Forgejo(new Cache(new None()));
}

public function setUp(): void
{
if (empty(static::$accessToken)) {
$this->setupForgejo();
}

$adapter = new Forgejo(new Cache(new None()));
$forgejoUrl = System::getEnv('TESTS_FORGEJO_URL', 'http://forgejo:3000');

$adapter->initializeVariables(
installationId: '',
privateKey: '',
appId: '',
accessToken: static::$accessToken,
refreshToken: ''
);
$adapter->setEndpoint($forgejoUrl);
if (empty(static::$owner)) {
$orgName = 'test-org-' . \uniqid();
static::$owner = $adapter->createOrganization($orgName);
}

$this->vcsAdapter = $adapter;
}

protected function setupForgejo(): void
{
$tokenFile = '/forgejo-data/gitea/token.txt';

if (file_exists($tokenFile)) {
$contents = file_get_contents($tokenFile);
if ($contents !== false) {
static::$accessToken = trim($contents);
}
}
}
}
2 changes: 1 addition & 1 deletion tests/VCS/Adapter/GitHubTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Utopia\Tests\VCS\Adapter;
namespace Utopia\Tests\Adapter;

use Utopia\Cache\Adapter\None;
use Utopia\Cache\Cache;
Expand Down
26 changes: 15 additions & 11 deletions tests/VCS/Adapter/GiteaTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Utopia\Tests\VCS\Adapter;
namespace Utopia\Tests\Adapter;

use Utopia\Cache\Adapter\None;
use Utopia\Cache\Cache;
Expand All @@ -15,6 +15,10 @@ class GiteaTest extends Base
protected static string $owner = '';
protected static string $defaultBranch = 'main';

protected string $webhookEventHeader = 'X-Gitea-Event';
protected string $webhookSignatureHeader = 'X-Gitea-Signature';
protected string $avatarDomain = 'gravatar.com';

protected function createVCSAdapter(): Git
{
return new Gitea(new Cache(new None()));
Expand Down Expand Up @@ -45,7 +49,7 @@ public function setUp(): void
$this->vcsAdapter = $adapter;
}

private function setupGitea(): void
protected function setupGitea(): void
{
$tokenFile = '/data/gitea/token.txt';

Expand Down Expand Up @@ -747,7 +751,7 @@ public function testGetCommit(): void
$this->assertSame($commitHash, $result['commitHash']);
$this->assertSame('utopia', $result['commitAuthor']);
$this->assertStringStartsWith($customMessage, $result['commitMessage']);
$this->assertStringContainsString('gravatar.com', $result['commitAuthorAvatar']);
$this->assertStringContainsString($this->avatarDomain, $result['commitAuthorAvatar']);
$this->assertNotEmpty($result['commitUrl']);

$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
Expand Down Expand Up @@ -775,7 +779,7 @@ public function testGetLatestCommit(): void
$this->assertNotEmpty($commit1['commitHash']);
$this->assertSame('utopia', $commit1['commitAuthor']);
$this->assertStringStartsWith($firstMessage, $commit1['commitMessage']);
$this->assertStringContainsString('gravatar.com', $commit1['commitAuthorAvatar']);
$this->assertStringContainsString($this->avatarDomain, $commit1['commitAuthorAvatar']);
$this->assertNotEmpty($commit1['commitUrl']);

$commit1Hash = $commit1['commitHash'];
Expand Down Expand Up @@ -1499,14 +1503,14 @@ public function testWebhookPushEvent(): void
$webhookData = $this->getLastWebhookRequest();
$this->assertNotEmpty($webhookData, 'No webhook received');
$this->assertNotEmpty($webhookData['data'] ?? '', 'Webhook payload is empty');
$this->assertSame('push', $webhookData['headers']['X-Gitea-Event'] ?? '', 'Expected push event');
$this->assertSame('push', $webhookData['headers'][$this->webhookEventHeader] ?? '', 'Expected push event');
}, 15000, 500);

$payload = $webhookData['data'];
$headers = $webhookData['headers'] ?? [];
$signature = $headers['X-Gitea-Signature'] ?? '';
$signature = $headers[$this->webhookSignatureHeader] ?? '';

$this->assertNotEmpty($signature, 'Missing X-Gitea-Signature header');
$this->assertNotEmpty($signature, 'Missing ' . $this->webhookSignatureHeader . ' header');
$this->assertTrue(
$this->vcsAdapter->validateWebhookEvent($payload, $signature, $secret),
'Webhook signature validation failed'
Expand Down Expand Up @@ -1538,7 +1542,7 @@ public function testWebhookPullRequestEvent(): void
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'feature.txt', 'content', 'Add feature', 'feature-branch');

$catcherUrl = System::getEnv('TESTS_GITEA_REQUEST_CATCHER_URL', 'http://request-catcher:5000');
$this->vcsAdapter->createWebhook(static::$owner, $repositoryName, $catcherUrl . '/webhook', $secret);
$this->vcsAdapter->createWebhook(static::$owner, $repositoryName, $catcherUrl . '/webhook', $secret, ['pull_request']);

// Clear after setup so only PR event will arrive
$this->deleteLastWebhookRequest();
Expand All @@ -1558,14 +1562,14 @@ public function testWebhookPullRequestEvent(): void
$webhookData = $this->getLastWebhookRequest();
$this->assertNotEmpty($webhookData, 'No webhook received');
$this->assertNotEmpty($webhookData['data'] ?? '', 'Webhook payload is empty');
$this->assertSame('pull_request', $webhookData['headers']['X-Gitea-Event'] ?? '', 'Expected pull_request event');
$this->assertSame('pull_request', $webhookData['headers'][$this->webhookEventHeader] ?? '', 'Expected pull_request event');
}, 15000, 500);

$payload = $webhookData['data'];
$headers = $webhookData['headers'] ?? [];
$signature = $headers['X-Gitea-Signature'] ?? '';
$signature = $headers[$this->webhookSignatureHeader] ?? '';

$this->assertNotEmpty($signature, 'Missing X-Gitea-Signature header');
$this->assertNotEmpty($signature, 'Missing ' . $this->webhookSignatureHeader . ' header');
$this->assertTrue(
$this->vcsAdapter->validateWebhookEvent($payload, $signature, $secret),
'Webhook signature validation failed'
Expand Down
Loading