Skip to content
Merged
51 changes: 49 additions & 2 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 @@ -65,5 +71,46 @@ services:
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 &&
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
"

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

namespace Utopia\VCS\Adapter\Git;

use Exception;

class Forgejo extends Gitea
{
protected string $endpoint = 'http://forgejo:3000/api/v1';
Comment thread
Meldiron marked this conversation as resolved.

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

/**
* Create a webhook on a repository
*
* @param string $owner Owner of the repository
* @param string $repositoryName Name of the repository
* @param string $url Webhook URL to send events to
* @param string $secret Webhook secret for signature validation
* @param array<string> $events Events to trigger the webhook
* @return int Webhook ID
*/
public function createWebhook(string $owner, string $repositoryName, string $url, string $secret, array $events = ['push', 'pull_request']): int
{
$response = $this->call(
self::METHOD_POST,
"/repos/{$owner}/{$repositoryName}/hooks",
['Authorization' => "token $this->accessToken"],
[
'type' => 'forgejo',
'active' => true,
'events' => $events,
'config' => [
'url' => $url,
'content_type' => 'json',
'secret' => $secret,
],
]
);

$responseHeaders = $response['headers'] ?? [];
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
if ($responseHeadersStatusCode >= 400) {
throw new Exception("Failed to create webhook: HTTP {$responseHeadersStatusCode}");
}

return (int) ($response['body']['id'] ?? 0);
}
Comment thread
Meldiron marked this conversation as resolved.
Outdated
}
169 changes: 169 additions & 0 deletions tests/VCS/Adapter/ForgejoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

namespace Utopia\Tests\VCS\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 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);
}
}
}

public function testWebhookPushEvent(): void
{
$repositoryName = 'test-webhook-push-' . \uniqid();
$secret = 'test-webhook-secret-' . \uniqid();

$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);

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

// Trigger a real push by creating a file
$this->vcsAdapter->createFile(
static::$owner,
$repositoryName,
'README.md',
'# Webhook Test',
'Initial commit'
);

// Wait for push webhook to arrive automatically
$webhookData = [];
$this->assertEventually(function () use (&$webhookData) {
$webhookData = $this->getLastWebhookRequest();
$this->assertNotEmpty($webhookData, 'No webhook received');
$this->assertNotEmpty($webhookData['data'] ?? '', 'Webhook payload is empty');
$this->assertSame('push', $webhookData['headers']['X-Forgejo-Event'] ?? '', 'Expected push event');
}, 15000, 500);

$payload = $webhookData['data'];
$headers = $webhookData['headers'] ?? [];
$signature = $headers['X-Forgejo-Signature'] ?? '';

$this->assertNotEmpty($signature, 'Missing X-Forgejo-Signature header');
$this->assertTrue(
$this->vcsAdapter->validateWebhookEvent($payload, $signature, $secret),
'Webhook signature validation failed'
);

$event = $this->vcsAdapter->getEvent('push', $payload);
$this->assertIsArray($event);
$this->assertSame('main', $event['branch']);
$this->assertSame($repositoryName, $event['repositoryName']);
$this->assertSame(static::$owner, $event['owner']);
$this->assertNotEmpty($event['commitHash']);
} finally {
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
}
}

public function testWebhookPullRequestEvent(): void
{
$repositoryName = 'test-webhook-pr-' . \uniqid();
$secret = 'test-webhook-secret-' . \uniqid();

$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);

try {
// Create all files BEFORE configuring webhook
// so those push events don't pollute the catcher
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'feature-branch', 'main');
$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);

// Clear after setup so only PR event will arrive
$this->deleteLastWebhookRequest();

// Trigger real PR event
$this->vcsAdapter->createPullRequest(
static::$owner,
$repositoryName,
'Test Webhook PR',
'feature-branch',
'main'
);

// Wait for pull_request webhook to arrive automatically
$webhookData = [];
$this->assertEventually(function () use (&$webhookData) {
$webhookData = $this->getLastWebhookRequest();
$this->assertNotEmpty($webhookData, 'No webhook received');
$this->assertNotEmpty($webhookData['data'] ?? '', 'Webhook payload is empty');
$this->assertSame('pull_request', $webhookData['headers']['X-Forgejo-Event'] ?? '', 'Expected pull_request event');
}, 15000, 500);

$payload = $webhookData['data'];
$headers = $webhookData['headers'] ?? [];
$signature = $headers['X-Forgejo-Signature'] ?? '';

$this->assertNotEmpty($signature, 'Missing X-Forgejo-Signature header');
$this->assertTrue(
$this->vcsAdapter->validateWebhookEvent($payload, $signature, $secret),
'Webhook signature validation failed'
);

$event = $this->vcsAdapter->getEvent('pull_request', $payload);
$this->assertIsArray($event);
$this->assertSame('feature-branch', $event['branch']);
$this->assertSame($repositoryName, $event['repositoryName']);
$this->assertSame(static::$owner, $event['owner']);
$this->assertContains($event['action'], ['opened', 'synchronized']);
$this->assertGreaterThan(0, $event['pullRequestNumber']);
} finally {
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
}
}
}
Loading