-
Notifications
You must be signed in to change notification settings - Fork 6
Eliminate duplication in Forgejo adapter and tests #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
15dde10
Add Forgejo adapter, tests, and docker-compose support
Copilot d96a96a
Fix Forgejo token path to use /data/gitea/ directory (Forgejo 9 uses …
Copilot b6a674a
Merge branch 'main' into copilot/add-forgejo-adapter-and-tests
Meldiron 279ffa3
Plan: extract getHookType() + fix PSR-4 + refactor webhook header pro…
Copilot e854b03
Extract getHookType() in Gitea; override in Forgejo; remove duplicate…
Copilot 3d133cc
Revert unwatned changes
Meldiron ca20dfa
Fix test namespaces
Meldiron 1ec6598
Fix avatar tests
Meldiron a282d25
Fix tests
Meldiron b59f772
Merge branch 'main' into copilot/add-forgejo-adapter-and-tests
Meldiron 5dc55f3
Fix linter
Meldiron 89ede13
fix tests
Meldiron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; | ||
|
|
||
| /** | ||
| * 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); | ||
| } | ||
|
Meldiron marked this conversation as resolved.
Outdated
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.