mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #12894 from appwrite/feat/vcs-gitlab
feat: add GitLab VCS provider
This commit is contained in:
23 files changed
+701
-95
No files matched your search
@@ -160,5 +160,8 @@ _APP_VCS_GITEA_BROWSER_ENDPOINT=
|
||||
_APP_VCS_GITEA_CLIENT_ID=
|
||||
_APP_VCS_GITEA_CLIENT_SECRET=
|
||||
_APP_VCS_GITEA_WEBHOOK_SECRET=
|
||||
_APP_VCS_GITLAB_CLIENT_ID=
|
||||
_APP_VCS_GITLAB_CLIENT_SECRET=
|
||||
_APP_VCS_GITLAB_WEBHOOK_SECRET=
|
||||
_APP_VCS_WEBHOOK_URL=
|
||||
_APP_WEBHOOK_MAX_FAILED_ATTEMPTS=10
|
||||
@@ -1311,6 +1311,33 @@ return [
|
||||
'question' => '',
|
||||
'filter' => ''
|
||||
],
|
||||
[
|
||||
'name' => '_APP_VCS_GITLAB_CLIENT_ID',
|
||||
'description' => 'GitLab OAuth2 application client ID. You can generate one in your GitLab instance under Settings > Applications.',
|
||||
'introduction' => '2.0.0',
|
||||
'default' => '',
|
||||
'required' => false,
|
||||
'question' => '',
|
||||
'filter' => ''
|
||||
],
|
||||
[
|
||||
'name' => '_APP_VCS_GITLAB_CLIENT_SECRET',
|
||||
'description' => 'GitLab OAuth2 application client secret. You can generate one in your GitLab instance under Settings > Applications.',
|
||||
'introduction' => '2.0.0',
|
||||
'default' => '',
|
||||
'required' => false,
|
||||
'question' => '',
|
||||
'filter' => ''
|
||||
],
|
||||
[
|
||||
'name' => '_APP_VCS_GITLAB_WEBHOOK_SECRET',
|
||||
'description' => 'Secret used to validate incoming GitLab webhook payloads.',
|
||||
'introduction' => '2.0.0',
|
||||
'default' => '',
|
||||
'required' => false,
|
||||
'question' => '',
|
||||
'filter' => ''
|
||||
],
|
||||
[
|
||||
'name' => '_APP_VCS_WEBHOOK_URL',
|
||||
'description' => 'Base URL Appwrite advertises to self-hosted VCS providers when registering repository webhooks, if different from the public domain (e.g. a Docker-internal address). Falls back to the public domain when unset.',
|
||||
|
||||
@@ -4,12 +4,20 @@
|
||||
* VCS provider registry, read by Appwrite\Vcs\Factory.
|
||||
*/
|
||||
|
||||
use Appwrite\Auth\OAuth2\Gitea as OAuth2Gitea;
|
||||
use Appwrite\Auth\OAuth2\Github as OAuth2Github;
|
||||
use Appwrite\Auth\OAuth2\Gitlab as OAuth2Gitlab;
|
||||
use Utopia\VCS\Adapter\Git\Gitea;
|
||||
use Utopia\VCS\Adapter\Git\GitHub;
|
||||
use Utopia\VCS\Adapter\Git\GitLab;
|
||||
|
||||
return [
|
||||
'github' => [
|
||||
'adapter' => GitHub::class,
|
||||
// Each provider owns its own construction quirks (Gitea calls
|
||||
// setEndpoint(), GitLab JSON-encodes its secret); Factory just
|
||||
// supplies the resolved env values, so no per-provider branching lives there.
|
||||
'oauth2' => fn (string $clientId, string $clientSecret, string $endpoint) => new OAuth2Github($clientId, $clientSecret, ''),
|
||||
'variables' => [
|
||||
'appName' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_APP_NAME'],
|
||||
'privateKey' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_PRIVATE_KEY'],
|
||||
@@ -21,6 +29,11 @@ return [
|
||||
],
|
||||
'gitea' => [
|
||||
'adapter' => Gitea::class,
|
||||
'oauth2' => function (string $clientId, string $clientSecret, string $endpoint) {
|
||||
$oauth2 = new OAuth2Gitea($clientId, $clientSecret, '');
|
||||
$oauth2->setEndpoint($endpoint);
|
||||
return $oauth2;
|
||||
},
|
||||
'variables' => [
|
||||
'endpoint' => ['required' => true, 'envVariable' => '_APP_VCS_GITEA_ENDPOINT'],
|
||||
'clientId' => ['required' => true, 'envVariable' => '_APP_VCS_GITEA_CLIENT_ID'],
|
||||
@@ -30,4 +43,21 @@ return [
|
||||
'webhookSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITEA_WEBHOOK_SECRET'],
|
||||
],
|
||||
],
|
||||
'gitlab' => [
|
||||
'adapter' => GitLab::class,
|
||||
// Auth\OAuth2\Gitlab is shared with the "Sign in with GitLab" account-login
|
||||
// provider, which JSON-encodes its secret as {"clientSecret","endpoint"}
|
||||
// to support a per-project self-hosted endpoint -- match that shape here too.
|
||||
'oauth2' => fn (string $clientId, string $clientSecret, string $endpoint) => new OAuth2Gitlab($clientId, \json_encode([
|
||||
'clientSecret' => $clientSecret,
|
||||
'endpoint' => $endpoint,
|
||||
]), ''),
|
||||
// Only official gitlab.com is supported -- fixed, not configurable.
|
||||
'endpoint' => 'https://gitlab.com',
|
||||
'variables' => [
|
||||
'clientId' => ['required' => true, 'envVariable' => '_APP_VCS_GITLAB_CLIENT_ID'],
|
||||
'clientSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITLAB_CLIENT_SECRET'],
|
||||
'webhookSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITLAB_WEBHOOK_SECRET'],
|
||||
],
|
||||
],
|
||||
];
|
||||
@@ -295,62 +295,6 @@ Http::get('/v1/mock/github/callback')
|
||||
]);
|
||||
});
|
||||
|
||||
Http::get('/v1/mock/gitea/callback')
|
||||
->desc('Create installation document using Gitea OAuth2 tokens')
|
||||
->groups(['mock', 'api', 'vcs'])
|
||||
->label('scope', 'public')
|
||||
->label('docs', false)
|
||||
->param('projectId', '', new UID(), 'Project ID of the project where Gitea is to be installed')
|
||||
->param('giteaUserId', '', new Text(256), 'Gitea user ID')
|
||||
->param('organization', '', new Text(256), 'Gitea username or organization')
|
||||
->param('accessToken', '', new Text(2048), 'Gitea access token')
|
||||
->inject('response')
|
||||
->inject('dbForPlatform')
|
||||
->action(function (string $projectId, string $giteaUserId, string $organization, string $accessToken, Response $response, Database $dbForPlatform) {
|
||||
$isDevelopment = System::getEnv('_APP_ENV', 'development') === 'development';
|
||||
|
||||
if (!$isDevelopment) {
|
||||
throw new Exception(Exception::GENERAL_NOT_IMPLEMENTED);
|
||||
}
|
||||
|
||||
$project = $dbForPlatform->getDocument('projects', $projectId);
|
||||
|
||||
if ($project->isEmpty()) {
|
||||
throw new Exception(Exception::PROJECT_NOT_FOUND, 'Project with the ID from state could not be found.');
|
||||
}
|
||||
|
||||
if (empty($giteaUserId) || empty($organization) || empty($accessToken)) {
|
||||
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Missing Gitea installation details');
|
||||
}
|
||||
|
||||
$projectInternalId = $project->getSequence();
|
||||
$teamId = $project->getAttribute('teamId', '');
|
||||
|
||||
$installation = $dbForPlatform->createDocument('installations', new Document([
|
||||
'$id' => ID::unique(),
|
||||
'$permissions' => [
|
||||
Permission::read(Role::team(ID::custom($teamId))),
|
||||
Permission::update(Role::team(ID::custom($teamId), 'owner')),
|
||||
Permission::update(Role::team(ID::custom($teamId), 'developer')),
|
||||
Permission::delete(Role::team(ID::custom($teamId), 'owner')),
|
||||
Permission::delete(Role::team(ID::custom($teamId), 'developer')),
|
||||
],
|
||||
'providerInstallationId' => $giteaUserId,
|
||||
'projectId' => $projectId,
|
||||
'projectInternalId' => $projectInternalId,
|
||||
'provider' => 'gitea',
|
||||
'organization' => $organization,
|
||||
'personal' => true,
|
||||
'personalAccessToken' => $accessToken,
|
||||
'personalRefreshToken' => '',
|
||||
'personalAccessTokenExpiry' => null,
|
||||
]));
|
||||
|
||||
$response->json([
|
||||
'installationId' => $installation->getId(),
|
||||
]);
|
||||
});
|
||||
|
||||
Http::shutdown()
|
||||
->groups(['mock'])
|
||||
->inject('response')
|
||||
|
||||
@@ -109,6 +109,7 @@ const APP_VCS_GITHUB_USERNAME = 'Appwrite';
|
||||
const APP_VCS_GITHUB_EMAIL = 'team@appwrite.io';
|
||||
const APP_VCS_GITHUB_URL = 'https://github.com/TeamAppwrite';
|
||||
const APP_VCS_GITEA_EMAIL = 'team@appwrite.io'; // Used to detect Appwrite's own commits
|
||||
const APP_VCS_GITLAB_EMAIL = 'team@appwrite.io'; // Used to detect Appwrite's own commits
|
||||
const APP_BRANDED_EMAIL_BASE_TEMPLATE = 'email-base-styled';
|
||||
|
||||
// Embeddings
|
||||
|
||||
@@ -203,6 +203,9 @@ services:
|
||||
- _APP_VCS_GITEA_CLIENT_ID
|
||||
- _APP_VCS_GITEA_CLIENT_SECRET
|
||||
- _APP_VCS_GITEA_WEBHOOK_SECRET
|
||||
- _APP_VCS_GITLAB_CLIENT_ID
|
||||
- _APP_VCS_GITLAB_CLIENT_SECRET
|
||||
- _APP_VCS_GITLAB_WEBHOOK_SECRET
|
||||
- _APP_VCS_WEBHOOK_URL
|
||||
- _APP_MIGRATIONS_FIREBASE_CLIENT_ID
|
||||
- _APP_MIGRATIONS_FIREBASE_CLIENT_SECRET
|
||||
@@ -494,6 +497,9 @@ services:
|
||||
- _APP_VCS_GITEA_CLIENT_ID
|
||||
- _APP_VCS_GITEA_CLIENT_SECRET
|
||||
- _APP_VCS_GITEA_WEBHOOK_SECRET
|
||||
- _APP_VCS_GITLAB_CLIENT_ID
|
||||
- _APP_VCS_GITLAB_CLIENT_SECRET
|
||||
- _APP_VCS_GITLAB_WEBHOOK_SECRET
|
||||
- _APP_VCS_WEBHOOK_URL
|
||||
- _APP_FUNCTIONS_TIMEOUT
|
||||
- _APP_SITES_TIMEOUT
|
||||
|
||||
@@ -7,6 +7,13 @@ use Appwrite\Auth\OAuth2;
|
||||
// Reference Material
|
||||
// https://docs.gitlab.com/ee/api/oauth2.html
|
||||
|
||||
/**
|
||||
* Shared with the "Sign in with GitLab" account-login OAuth2 provider, which
|
||||
* stores its secret as JSON ({"clientSecret": "...", "endpoint": "..."}) to
|
||||
* support self-hosted GitLab per-project. The VCS flow (see app/config/vcs.php)
|
||||
* only supports official gitlab.com, but still encodes to that same JSON
|
||||
* shape so getAppSecret()/getEndpoint() stay correct for both consumers.
|
||||
*/
|
||||
class Gitlab extends OAuth2
|
||||
{
|
||||
/**
|
||||
@@ -155,6 +162,52 @@ class Gitlab extends OAuth2
|
||||
return $user['name'] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $accessToken
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUserSlug(string $accessToken): string
|
||||
{
|
||||
$user = $this->getUser($accessToken);
|
||||
|
||||
return $user['username'] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @link https://docs.gitlab.com/ee/api/projects.html#create-project
|
||||
*
|
||||
* @param string $accessToken
|
||||
* @param string $repositoryName
|
||||
* @param bool $private
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function createRepository(string $accessToken, string $repositoryName, bool $private): array
|
||||
{
|
||||
$repository = $this->request('POST', $this->getEndpoint() . '/api/v4/projects', ['Authorization: Bearer ' . $accessToken, 'Content-Type: application/json'], \json_encode([
|
||||
'name' => $repositoryName,
|
||||
'visibility' => $private ? 'private' : 'public',
|
||||
]));
|
||||
|
||||
$repository = \json_decode($repository, true) ?? [];
|
||||
|
||||
// Normalize to the GitHub/Gitea field shape ProviderRepository expects.
|
||||
if (isset($repository['visibility'])) {
|
||||
$repository['private'] = $repository['visibility'] !== 'public';
|
||||
}
|
||||
|
||||
if (isset($repository['last_activity_at'])) {
|
||||
$repository['pushed_at'] = $repository['last_activity_at'];
|
||||
}
|
||||
|
||||
if (isset($repository['message']) && !\is_string($repository['message'])) {
|
||||
$repository['message'] = \json_encode($repository['message']);
|
||||
}
|
||||
|
||||
return $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $accessToken
|
||||
*
|
||||
@@ -185,9 +238,9 @@ class Gitlab extends OAuth2
|
||||
return $secret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extracts the Tenant Id from the JSON stored in appSecret. Defaults to 'common' as a fallback
|
||||
* Extracts the endpoint from the JSON stored in appSecret. Defaults to
|
||||
* gitlab.com as a fallback.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Appwrite\Platform\Modules\VCS\Http\Gitea\Events;
|
||||
|
||||
use Appwrite\Auth\OAuth2\Gitea as OAuth2Gitea;
|
||||
use Appwrite\Extend\Exception;
|
||||
use Appwrite\Platform\Action;
|
||||
use Appwrite\Platform\Modules\VCS\Http\GitHub\Deployment;
|
||||
@@ -17,7 +16,6 @@ use Utopia\Database\Query;
|
||||
use Utopia\Database\Validator\Authorization;
|
||||
use Utopia\Platform\Scope\HTTP;
|
||||
use Utopia\Span\Span;
|
||||
use Utopia\System\System;
|
||||
use Utopia\VCS\Adapter\Git;
|
||||
|
||||
class Create extends Action
|
||||
@@ -102,12 +100,8 @@ class Create extends Action
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the adapter for a repository's installation, refreshing its
|
||||
* OAuth2 token first. A null installation (not a Gitea installation) is
|
||||
* not an error -- the caller silently skips it. A token-refresh/adapter
|
||||
* failure, on the other hand, is pushed onto $errors so the caller can
|
||||
* surface it instead of dropping it silently -- Gitea's webhook delivery
|
||||
* gets a non-2xx response and the failure shows up in its redelivery log.
|
||||
* A refresh/adapter failure is pushed onto $errors instead of swallowed, so the
|
||||
* caller can surface a non-2xx response and Gitea logs it as a failed delivery.
|
||||
*/
|
||||
private function resolveAdapterForRepository(Document $repository, VcsFactory $vcsFactory, InstallationTokens $installationTokens, Database $dbForPlatform, Authorization $authorization, array &$errors): ?Git
|
||||
{
|
||||
@@ -118,14 +112,7 @@ class Create extends Action
|
||||
}
|
||||
|
||||
try {
|
||||
$oauth2 = new OAuth2Gitea(
|
||||
System::getEnv('_APP_VCS_GITEA_CLIENT_ID', ''),
|
||||
System::getEnv('_APP_VCS_GITEA_CLIENT_SECRET', ''),
|
||||
''
|
||||
);
|
||||
$oauth2->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', ''));
|
||||
|
||||
$installation = $installationTokens->refresh($installation, $dbForPlatform, $oauth2);
|
||||
$installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory);
|
||||
|
||||
return $vcsFactory->fromInstallation($installation);
|
||||
} catch (\Throwable $error) {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Platform\Modules\VCS\Http\Gitlab\Authorize;
|
||||
|
||||
use Appwrite\Auth\OAuth2;
|
||||
use Appwrite\Auth\OAuth2\Gitlab as OAuth2Gitlab;
|
||||
use Appwrite\Platform\Modules\VCS\Http\Authorize\Base;
|
||||
use Utopia\System\System;
|
||||
|
||||
class Get extends Base
|
||||
{
|
||||
public static function getName()
|
||||
{
|
||||
return 'getVCSGitlabAuthorize';
|
||||
}
|
||||
|
||||
public static function getProvider(): string
|
||||
{
|
||||
return 'gitlab';
|
||||
}
|
||||
|
||||
public static function getProviderName(): string
|
||||
{
|
||||
return 'GitLab';
|
||||
}
|
||||
|
||||
protected function createOAuth2(string $callback, array $state): OAuth2
|
||||
{
|
||||
// Auth\OAuth2\Gitlab reads the endpoint out of a JSON-encoded appSecret; no setEndpoint().
|
||||
return new OAuth2Gitlab(
|
||||
System::getEnv('_APP_VCS_GITLAB_CLIENT_ID', ''),
|
||||
\json_encode([
|
||||
'clientSecret' => System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''),
|
||||
'endpoint' => 'https://gitlab.com',
|
||||
]),
|
||||
$callback,
|
||||
$state,
|
||||
// api is required for webhook/merge-request-note writes; no finer-grained scope covers both.
|
||||
[
|
||||
'read_user',
|
||||
'api',
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Platform\Modules\VCS\Http\Gitlab\Callback;
|
||||
|
||||
use Appwrite\Auth\OAuth2;
|
||||
use Appwrite\Auth\OAuth2\Gitlab as OAuth2Gitlab;
|
||||
use Appwrite\Platform\Modules\VCS\Http\Callback\Base;
|
||||
use Utopia\System\System;
|
||||
|
||||
class Get extends Base
|
||||
{
|
||||
public static function getName()
|
||||
{
|
||||
return 'getVCSGitlabCallback';
|
||||
}
|
||||
|
||||
public static function getProvider(): string
|
||||
{
|
||||
return 'gitlab';
|
||||
}
|
||||
|
||||
public static function getProviderName(): string
|
||||
{
|
||||
return 'GitLab';
|
||||
}
|
||||
|
||||
protected function createOAuth2(string $callback): OAuth2
|
||||
{
|
||||
return new OAuth2Gitlab(
|
||||
System::getEnv('_APP_VCS_GITLAB_CLIENT_ID', ''),
|
||||
\json_encode([
|
||||
'clientSecret' => System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''),
|
||||
'endpoint' => 'https://gitlab.com',
|
||||
]),
|
||||
$callback
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,268 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Platform\Modules\VCS\Http\Gitlab\Events;
|
||||
|
||||
use Appwrite\Extend\Exception;
|
||||
use Appwrite\Platform\Action;
|
||||
use Appwrite\Platform\Modules\VCS\Http\GitHub\Deployment;
|
||||
use Appwrite\Utopia\Request;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Appwrite\Vcs\Factory as VcsFactory;
|
||||
use Appwrite\Vcs\InstallationTokens;
|
||||
use Appwrite\Vcs\RepositoryPullRequestCleanup;
|
||||
use Utopia\Console;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Database\Validator\Authorization;
|
||||
use Utopia\Platform\Scope\HTTP;
|
||||
use Utopia\Span\Span;
|
||||
use Utopia\VCS\Adapter\Git;
|
||||
|
||||
class Create extends Action
|
||||
{
|
||||
use HTTP;
|
||||
use Deployment;
|
||||
|
||||
public static function getName()
|
||||
{
|
||||
return 'createVCSGitlabEvent';
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this
|
||||
->setHttpMethod(Action::HTTP_REQUEST_METHOD_POST)
|
||||
->setHttpPath('/v1/vcs/gitlab/events')
|
||||
->desc('Create event')
|
||||
->groups(['api', 'vcs'])
|
||||
->label('scope', 'public')
|
||||
->inject('vcsFactory')
|
||||
->inject('vcsWebhookSecret')
|
||||
->inject('installationTokens')
|
||||
->inject('request')
|
||||
->inject('response')
|
||||
->inject('dbForPlatform')
|
||||
->inject('authorization')
|
||||
->inject('getProjectDB')
|
||||
->inject('deploymentsFactory')
|
||||
->inject('platform')
|
||||
->callback($this->action(...));
|
||||
}
|
||||
|
||||
public function action(
|
||||
VcsFactory $vcsFactory,
|
||||
callable $vcsWebhookSecret,
|
||||
InstallationTokens $installationTokens,
|
||||
Request $request,
|
||||
Response $response,
|
||||
Database $dbForPlatform,
|
||||
Authorization $authorization,
|
||||
callable $getProjectDB,
|
||||
callable $deploymentsFactory,
|
||||
array $platform
|
||||
) {
|
||||
$vcs = $vcsFactory->fromProvider('gitlab');
|
||||
|
||||
$event = $request->getHeaderLine($vcs->getEventHeaderName(), '');
|
||||
Span::add('vcs.gitlab.event.name', $event);
|
||||
|
||||
$payload = $request->getRawPayload();
|
||||
$signature = $request->getHeaderLine($vcs->getSignatureHeaderName(), '');
|
||||
$secretKey = $vcsWebhookSecret('gitlab');
|
||||
|
||||
$valid = !empty($secretKey) && $vcs->validateWebhookEvent($payload, $signature, $secretKey);
|
||||
Span::add('vcs.gitlab.event.signature.valid', $valid);
|
||||
|
||||
if (!$valid) {
|
||||
throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, 'Invalid webhook payload signature. Please make sure the webhook secret has same value in your GitLab repository settings and in the _APP_VCS_GITLAB_WEBHOOK_SECRET environment variable');
|
||||
}
|
||||
|
||||
$parsedPayload = $vcs->getEvent($event, $payload);
|
||||
|
||||
match ($event) {
|
||||
'Push Hook' => $this->handlePushEvent($parsedPayload, $vcsFactory, $installationTokens, $dbForPlatform, $authorization, $getProjectDB, $platform, $deploymentsFactory),
|
||||
'Merge Request Hook' => $this->handlePullRequestEvent($parsedPayload, $vcsFactory, $installationTokens, $dbForPlatform, $authorization, $getProjectDB, $platform, $deploymentsFactory),
|
||||
default => null,
|
||||
};
|
||||
|
||||
$response->json($parsedPayload);
|
||||
}
|
||||
|
||||
private function resolveGitlabInstallation(Document $repository, Database $dbForPlatform, Authorization $authorization): ?Document
|
||||
{
|
||||
$installation = $authorization->skip(fn () => $dbForPlatform->getDocument('installations', $repository->getAttribute('installationId', '')));
|
||||
|
||||
if ($installation->isEmpty() || $installation->getAttribute('provider', 'github') !== 'gitlab') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $installation;
|
||||
}
|
||||
|
||||
/**
|
||||
* A refresh/adapter failure is pushed onto $errors instead of swallowed, so the
|
||||
* caller can surface a non-2xx response and GitLab logs it as a failed delivery.
|
||||
*/
|
||||
private function resolveAdapterForRepository(Document $repository, VcsFactory $vcsFactory, InstallationTokens $installationTokens, Database $dbForPlatform, Authorization $authorization, array &$errors): ?Git
|
||||
{
|
||||
$installation = $this->resolveGitlabInstallation($repository, $dbForPlatform, $authorization);
|
||||
|
||||
if ($installation === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory);
|
||||
|
||||
return $vcsFactory->fromInstallation($installation);
|
||||
} catch (\Throwable $error) {
|
||||
$message = "Failed to resolve GitLab adapter for installation '{$installation->getId()}': " . $error->getMessage();
|
||||
Console::warning($message);
|
||||
Span::add("vcs.gitlab.event.installation.{$installation->getId()}.error", $message);
|
||||
$errors[] = $message;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private function handlePushEvent(
|
||||
array $parsedPayload,
|
||||
VcsFactory $vcsFactory,
|
||||
InstallationTokens $installationTokens,
|
||||
Database $dbForPlatform,
|
||||
Authorization $authorization,
|
||||
callable $getProjectDB,
|
||||
array $platform,
|
||||
callable $deploymentsFactory,
|
||||
) {
|
||||
$providerBranchDeleted = $parsedPayload['branchDeleted'] ?? false;
|
||||
$providerBranch = $parsedPayload['branch'] ?? '';
|
||||
$providerBranchUrl = $parsedPayload['branchUrl'] ?? '';
|
||||
$providerRepositoryId = $parsedPayload['repositoryId'] ?? '';
|
||||
$providerRepositoryName = $parsedPayload['repositoryName'] ?? '';
|
||||
$providerRepositoryUrl = $parsedPayload['repositoryUrl'] ?? '';
|
||||
$providerCommitHash = $parsedPayload['commitHash'] ?? '';
|
||||
$providerRepositoryOwner = $parsedPayload['owner'] ?? '';
|
||||
$providerCommitAuthorName = $parsedPayload['headCommitAuthorName'] ?? '';
|
||||
$providerCommitAuthorEmail = $parsedPayload['headCommitAuthorEmail'] ?? '';
|
||||
$providerCommitAuthorUrl = $parsedPayload['authorUrl'] ?? '';
|
||||
$providerCommitMessage = $parsedPayload['headCommitMessage'] ?? '';
|
||||
$providerCommitUrl = $parsedPayload['headCommitUrl'] ?? '';
|
||||
|
||||
Span::add('vcs.gitlab.event.repo.id', $providerRepositoryId);
|
||||
Span::add('vcs.gitlab.event.repo.name', $providerRepositoryName);
|
||||
Span::add('vcs.gitlab.event.branch', $providerBranch);
|
||||
|
||||
if ($providerCommitAuthorEmail === APP_VCS_GITLAB_EMAIL || $providerBranchDeleted) {
|
||||
return;
|
||||
}
|
||||
|
||||
$repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [
|
||||
Query::equal('providerRepositoryId', [$providerRepositoryId]),
|
||||
Query::limit(100),
|
||||
]));
|
||||
|
||||
$providerAffectedFiles = $parsedPayload['affectedFiles'] ?? [];
|
||||
|
||||
$errors = [];
|
||||
foreach ($repositories as $repository) {
|
||||
$adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $installationTokens, $dbForPlatform, $authorization, $errors);
|
||||
|
||||
if ($adapter === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$providerInstallationId = $repository->getAttribute('installationId', '');
|
||||
|
||||
$this->createGitDeployments($adapter, $providerInstallationId, [$repository], $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthorName, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, '', $providerAffectedFiles, false, $dbForPlatform, $authorization, $getProjectDB, $platform, $deploymentsFactory);
|
||||
}
|
||||
|
||||
if (!empty($errors)) {
|
||||
throw new Exception(Exception::GENERAL_UNKNOWN, \implode("\n", $errors));
|
||||
}
|
||||
}
|
||||
|
||||
private function handlePullRequestEvent(
|
||||
array $parsedPayload,
|
||||
VcsFactory $vcsFactory,
|
||||
InstallationTokens $installationTokens,
|
||||
Database $dbForPlatform,
|
||||
Authorization $authorization,
|
||||
callable $getProjectDB,
|
||||
array $platform,
|
||||
callable $deploymentsFactory,
|
||||
) {
|
||||
$action = $parsedPayload['action'] ?? '';
|
||||
|
||||
if ($action === 'opened' || $action === 'reopened' || $action === 'synchronize') {
|
||||
$providerBranch = $parsedPayload['branch'] ?? '';
|
||||
$providerBranchUrl = $parsedPayload['branchUrl'] ?? '';
|
||||
$providerRepositoryId = $parsedPayload['repositoryId'] ?? '';
|
||||
$providerRepositoryName = $parsedPayload['repositoryName'] ?? '';
|
||||
$providerRepositoryUrl = $parsedPayload['repositoryUrl'] ?? '';
|
||||
$providerPullRequestId = $parsedPayload['pullRequestNumber'] ?? '';
|
||||
$providerCommitHash = $parsedPayload['commitHash'] ?? '';
|
||||
$providerRepositoryOwner = $parsedPayload['owner'] ?? '';
|
||||
$external = $parsedPayload['external'] ?? true;
|
||||
$providerCommitUrl = $parsedPayload['headCommitUrl'] ?? '';
|
||||
$providerCommitAuthorUrl = $parsedPayload['authorUrl'] ?? '';
|
||||
|
||||
Span::add('vcs.gitlab.event.repo.id', $providerRepositoryId);
|
||||
Span::add('vcs.gitlab.event.repo.name', $providerRepositoryName);
|
||||
Span::add('vcs.gitlab.event.branch', $providerBranch);
|
||||
|
||||
// Ignore sync for non-external. We handle it in the push webhook.
|
||||
if (!$external && $action === 'synchronize') {
|
||||
return;
|
||||
}
|
||||
|
||||
$repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [
|
||||
Query::equal('providerRepositoryId', [$providerRepositoryId]),
|
||||
Query::orderDesc('$createdAt'),
|
||||
Query::limit(100),
|
||||
]));
|
||||
|
||||
$errors = [];
|
||||
foreach ($repositories as $repository) {
|
||||
$adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $installationTokens, $dbForPlatform, $authorization, $errors);
|
||||
|
||||
if ($adapter === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$providerInstallationId = $repository->getAttribute('installationId', '');
|
||||
|
||||
try {
|
||||
$commitDetails = $adapter->getCommit($providerRepositoryOwner, $providerRepositoryName, $providerCommitHash);
|
||||
} catch (\Throwable $e) {
|
||||
Console::warning("Failed to fetch commit '{$providerCommitHash}': " . $e->getMessage());
|
||||
$commitDetails = [];
|
||||
}
|
||||
$providerCommitAuthor = $commitDetails['commitAuthor'] ?? '';
|
||||
$providerCommitMessage = $commitDetails['commitMessage'] ?? '';
|
||||
|
||||
$prFiles = $adapter->getPullRequestFiles($providerRepositoryOwner, $providerRepositoryName, $providerPullRequestId);
|
||||
$providerAffectedFiles = [
|
||||
...array_column($prFiles, 'filename'),
|
||||
...array_filter(array_column($prFiles, 'previous_filename')),
|
||||
];
|
||||
|
||||
$this->createGitDeployments($adapter, $providerInstallationId, [$repository], $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthor, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, $providerPullRequestId, $providerAffectedFiles, $external, $dbForPlatform, $authorization, $getProjectDB, $platform, $deploymentsFactory);
|
||||
}
|
||||
|
||||
if (!empty($errors)) {
|
||||
throw new Exception(Exception::GENERAL_UNKNOWN, \implode("\n", $errors));
|
||||
}
|
||||
} elseif ($action === 'closed') {
|
||||
$providerRepositoryId = $parsedPayload['repositoryId'] ?? '';
|
||||
$providerPullRequestId = $parsedPayload['pullRequestNumber'] ?? '';
|
||||
$external = $parsedPayload['external'] ?? true;
|
||||
|
||||
if (!$external) {
|
||||
return;
|
||||
}
|
||||
|
||||
(new RepositoryPullRequestCleanup())->remove($dbForPlatform, $authorization, 'gitlab', $providerRepositoryId, $providerPullRequestId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ use Appwrite\SDK\Response as SDKResponse;
|
||||
use Appwrite\Utopia\Database\Validator\Queries\Branches;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Appwrite\Vcs\Factory as VcsFactory;
|
||||
use Appwrite\Vcs\InstallationTokens;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Exception\Query as QueryException;
|
||||
@@ -55,6 +56,7 @@ class XList extends Action
|
||||
->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true)
|
||||
->param('queries', [], new Branches(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit, offset, cursorAfter, and cursorBefore', true)
|
||||
->inject('vcsFactory')
|
||||
->inject('installationTokens')
|
||||
->inject('response')
|
||||
->inject('dbForPlatform')
|
||||
->callback($this->action(...));
|
||||
@@ -66,6 +68,7 @@ class XList extends Action
|
||||
string $search,
|
||||
array $queries,
|
||||
VcsFactory $vcsFactory,
|
||||
InstallationTokens $installationTokens,
|
||||
Response $response,
|
||||
Database $dbForPlatform
|
||||
) {
|
||||
@@ -81,6 +84,7 @@ class XList extends Action
|
||||
throw new Exception(Exception::INSTALLATION_NOT_FOUND);
|
||||
}
|
||||
|
||||
$installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory);
|
||||
$providerInstallationId = $installation->getAttribute('providerInstallationId');
|
||||
$vcs = $vcsFactory->fromInstallation($installation);
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ use Appwrite\SDK\Method;
|
||||
use Appwrite\SDK\Response as SDKResponse;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Appwrite\Vcs\Factory as VcsFactory;
|
||||
use Appwrite\Vcs\InstallationTokens;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Platform\Scope\HTTP;
|
||||
@@ -51,6 +52,7 @@ class Get extends Action
|
||||
->param('providerRootDirectory', '', new Text(256, 0), 'Path to get contents of nested directory', true)
|
||||
->param('providerReference', '', new Text(256, 0), 'Git reference (branch, tag, commit) to get contents from', true)
|
||||
->inject('vcsFactory')
|
||||
->inject('installationTokens')
|
||||
->inject('response')
|
||||
->inject('dbForPlatform')
|
||||
->callback($this->action(...));
|
||||
@@ -62,6 +64,7 @@ class Get extends Action
|
||||
string $providerRootDirectory,
|
||||
string $providerReference,
|
||||
VcsFactory $vcsFactory,
|
||||
InstallationTokens $installationTokens,
|
||||
Response $response,
|
||||
Database $dbForPlatform
|
||||
) {
|
||||
@@ -71,6 +74,7 @@ class Get extends Action
|
||||
throw new Exception(Exception::INSTALLATION_NOT_FOUND);
|
||||
}
|
||||
|
||||
$installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory);
|
||||
$providerInstallationId = $installation->getAttribute('providerInstallationId');
|
||||
$vcs = $vcsFactory->fromInstallation($installation);
|
||||
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
namespace Appwrite\Platform\Modules\VCS\Http\Installations\Repositories;
|
||||
|
||||
use Appwrite\Auth\OAuth2\Gitea as OAuth2Gitea;
|
||||
use Appwrite\Auth\OAuth2\Github as OAuth2Github;
|
||||
use Appwrite\Extend\Exception;
|
||||
use Appwrite\Platform\Action;
|
||||
use Appwrite\SDK\AuthType;
|
||||
@@ -16,7 +14,6 @@ use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Platform\Scope\HTTP;
|
||||
use Utopia\System\System;
|
||||
use Utopia\Validator\Boolean;
|
||||
use Utopia\Validator\Text;
|
||||
|
||||
@@ -81,7 +78,11 @@ class Create extends Action
|
||||
$provider = $installation->getAttribute('provider', 'github');
|
||||
|
||||
if ($installation->getAttribute('personal', false) === true) {
|
||||
$oauth2 = $this->createOAuth2($provider);
|
||||
$oauth2 = $vcsFactory->oauth2FromProvider($provider);
|
||||
|
||||
if (!\method_exists($oauth2, 'createRepository')) {
|
||||
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'VCS provider does not support repository creation: ' . $provider);
|
||||
}
|
||||
|
||||
$identity = null;
|
||||
if ($provider === 'github' && (empty($installation->getAttribute('personalAccessToken')) || empty($installation->getAttribute('personalRefreshToken')) || empty($installation->getAttribute('personalAccessTokenExpiry')))) {
|
||||
@@ -135,20 +136,4 @@ class Create extends Action
|
||||
|
||||
$response->dynamic(new Document($repository), Response::MODEL_PROVIDER_REPOSITORY);
|
||||
}
|
||||
|
||||
protected function createOAuth2(string $provider): OAuth2Github|OAuth2Gitea
|
||||
{
|
||||
if ($provider === 'github') {
|
||||
return new OAuth2Github(System::getEnv('_APP_VCS_GITHUB_CLIENT_ID', ''), System::getEnv('_APP_VCS_GITHUB_CLIENT_SECRET', ''), "");
|
||||
}
|
||||
|
||||
if ($provider === 'gitea') {
|
||||
$oauth2 = new OAuth2Gitea(System::getEnv('_APP_VCS_GITEA_CLIENT_ID', ''), System::getEnv('_APP_VCS_GITEA_CLIENT_SECRET', ''), "");
|
||||
$oauth2->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', ''));
|
||||
|
||||
return $oauth2;
|
||||
}
|
||||
|
||||
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Unsupported VCS provider: ' . $provider);
|
||||
}
|
||||
}
|
||||
+4
@@ -9,6 +9,7 @@ use Appwrite\SDK\Method;
|
||||
use Appwrite\SDK\Response as SDKResponse;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Appwrite\Vcs\Factory as VcsFactory;
|
||||
use Appwrite\Vcs\InstallationTokens;
|
||||
use Swoole\Coroutine\WaitGroup;
|
||||
use Utopia\Config\Adapters\Dotenv as ConfigDotenv;
|
||||
use Utopia\Config\Config;
|
||||
@@ -94,6 +95,7 @@ class Create extends Action
|
||||
->param('type', '', new WhiteList(['runtime', 'framework']), 'Detector type. Must be one of the following: runtime, framework', enum: new Enum(name: 'VCSDetectionType'))
|
||||
->param('providerRootDirectory', '', new Text(256, 0), 'Path to Root Directory', true)
|
||||
->inject('vcsFactory')
|
||||
->inject('installationTokens')
|
||||
->inject('response')
|
||||
->inject('dbForPlatform')
|
||||
->callback($this->action(...));
|
||||
@@ -105,6 +107,7 @@ class Create extends Action
|
||||
string $type,
|
||||
string $providerRootDirectory,
|
||||
VcsFactory $vcsFactory,
|
||||
InstallationTokens $installationTokens,
|
||||
Response $response,
|
||||
Database $dbForPlatform
|
||||
) {
|
||||
@@ -114,6 +117,7 @@ class Create extends Action
|
||||
throw new Exception(Exception::INSTALLATION_NOT_FOUND);
|
||||
}
|
||||
|
||||
$installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory);
|
||||
$providerInstallationId = $installation->getAttribute('providerInstallationId');
|
||||
$vcs = $vcsFactory->fromInstallation($installation);
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ use Appwrite\SDK\Method;
|
||||
use Appwrite\SDK\Response as SDKResponse;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Appwrite\Vcs\Factory as VcsFactory;
|
||||
use Appwrite\Vcs\InstallationTokens;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Platform\Scope\HTTP;
|
||||
@@ -49,6 +50,7 @@ class Get extends Action
|
||||
->param('installationId', '', new Text(256), 'Installation Id')
|
||||
->param('providerRepositoryId', '', new Text(256), 'Repository Id')
|
||||
->inject('vcsFactory')
|
||||
->inject('installationTokens')
|
||||
->inject('response')
|
||||
->inject('dbForPlatform')
|
||||
->callback($this->action(...));
|
||||
@@ -58,6 +60,7 @@ class Get extends Action
|
||||
string $installationId,
|
||||
string $providerRepositoryId,
|
||||
VcsFactory $vcsFactory,
|
||||
InstallationTokens $installationTokens,
|
||||
Response $response,
|
||||
Database $dbForPlatform
|
||||
) {
|
||||
@@ -67,6 +70,7 @@ class Get extends Action
|
||||
throw new Exception(Exception::INSTALLATION_NOT_FOUND);
|
||||
}
|
||||
|
||||
$installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory);
|
||||
$providerInstallationId = $installation->getAttribute('providerInstallationId');
|
||||
$vcs = $vcsFactory->fromInstallation($installation);
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ use Appwrite\SDK\Method;
|
||||
use Appwrite\SDK\Response as SDKResponse;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Appwrite\Vcs\Factory as VcsFactory;
|
||||
use Appwrite\Vcs\InstallationTokens;
|
||||
use Swoole\Coroutine\WaitGroup;
|
||||
use Utopia\Config\Adapters\Dotenv as ConfigDotenv;
|
||||
use Utopia\Config\Config;
|
||||
@@ -98,6 +99,7 @@ class XList extends Action
|
||||
->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true)
|
||||
->param('queries', [], new Queries([new Limit(), new Offset()]), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit and offset', true)
|
||||
->inject('vcsFactory')
|
||||
->inject('installationTokens')
|
||||
->inject('response')
|
||||
->inject('dbForPlatform')
|
||||
->callback($this->action(...));
|
||||
@@ -109,6 +111,7 @@ class XList extends Action
|
||||
string $search,
|
||||
array $queries,
|
||||
VcsFactory $vcsFactory,
|
||||
InstallationTokens $installationTokens,
|
||||
Response $response,
|
||||
Database $dbForPlatform
|
||||
) {
|
||||
@@ -122,6 +125,7 @@ class XList extends Action
|
||||
throw new Exception(Exception::INSTALLATION_NOT_FOUND);
|
||||
}
|
||||
|
||||
$installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory);
|
||||
$providerInstallationId = $installation->getAttribute('providerInstallationId');
|
||||
$vcs = $vcsFactory->fromInstallation($installation);
|
||||
|
||||
|
||||
@@ -9,6 +9,9 @@ use Appwrite\Platform\Modules\VCS\Http\GitHub\Authorize\External\Update as Updat
|
||||
use Appwrite\Platform\Modules\VCS\Http\GitHub\Authorize\Get as GetGitHubAuthorize;
|
||||
use Appwrite\Platform\Modules\VCS\Http\GitHub\Callback\Get as GetGitHubCallback;
|
||||
use Appwrite\Platform\Modules\VCS\Http\GitHub\Events\Create as CreateGitHubEvent;
|
||||
use Appwrite\Platform\Modules\VCS\Http\Gitlab\Authorize\Get as GetGitlabAuthorize;
|
||||
use Appwrite\Platform\Modules\VCS\Http\Gitlab\Callback\Get as GetGitlabCallback;
|
||||
use Appwrite\Platform\Modules\VCS\Http\Gitlab\Events\Create as CreateGitlabEvent;
|
||||
use Appwrite\Platform\Modules\VCS\Http\Installations\Delete as DeleteInstallation;
|
||||
use Appwrite\Platform\Modules\VCS\Http\Installations\Get as GetInstallation;
|
||||
use Appwrite\Platform\Modules\VCS\Http\Installations\Repositories\Branches\XList as ListRepositoryBranches;
|
||||
@@ -35,6 +38,10 @@ class Http extends Service
|
||||
$this->addAction(GetGiteaAuthorize::getName(), new GetGiteaAuthorize());
|
||||
$this->addAction(GetGiteaCallback::getName(), new GetGiteaCallback());
|
||||
|
||||
// GitLab Authorization & Callback
|
||||
$this->addAction(GetGitlabAuthorize::getName(), new GetGitlabAuthorize());
|
||||
$this->addAction(GetGitlabCallback::getName(), new GetGitlabCallback());
|
||||
|
||||
// Installations
|
||||
$this->addAction(GetInstallation::getName(), new GetInstallation());
|
||||
$this->addAction(ListInstallations::getName(), new ListInstallations());
|
||||
@@ -51,5 +58,6 @@ class Http extends Service
|
||||
// Events
|
||||
$this->addAction(CreateGitHubEvent::getName(), new CreateGitHubEvent());
|
||||
$this->addAction(CreateGiteaEvent::getName(), new CreateGiteaEvent());
|
||||
$this->addAction(CreateGitlabEvent::getName(), new CreateGitlabEvent());
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Appwrite\Vcs;
|
||||
|
||||
use Appwrite\Auth\OAuth2;
|
||||
use Appwrite\Extend\Exception;
|
||||
use Utopia\Cache\Cache;
|
||||
use Utopia\Config\Config;
|
||||
@@ -57,7 +58,7 @@ class Factory
|
||||
|
||||
$adapter = new ($this->registry[$key]['adapter'])($this->cache);
|
||||
|
||||
$endpoint = $this->getEnv($key, 'endpoint');
|
||||
$endpoint = $this->registry[$key]['endpoint'] ?? $this->getEnv($key, 'endpoint');
|
||||
if (!empty($endpoint) && \method_exists($adapter, 'setEndpoint')) {
|
||||
$adapter->setEndpoint(\rtrim($endpoint, '/'));
|
||||
}
|
||||
@@ -94,6 +95,27 @@ class Factory
|
||||
return $this->getEnv($key, 'webhookSecret');
|
||||
}
|
||||
|
||||
public function oauth2FromProvider(string $key): OAuth2
|
||||
{
|
||||
$builder = $this->registry[$key]['oauth2'] ?? null;
|
||||
|
||||
if (!\is_callable($builder)) {
|
||||
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Unsupported VCS provider: ' . $key);
|
||||
}
|
||||
|
||||
$clientId = $this->getEnv($key, 'clientId');
|
||||
$clientSecret = $this->getEnv($key, 'clientSecret');
|
||||
$endpoint = $this->registry[$key]['endpoint'] ?? $this->getEnv($key, 'endpoint');
|
||||
|
||||
$oauth2 = $builder($clientId, $clientSecret, $endpoint);
|
||||
|
||||
if (!$oauth2 instanceof OAuth2) {
|
||||
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'VCS provider "' . $key . '" oauth2 builder returned an invalid client');
|
||||
}
|
||||
|
||||
return $oauth2;
|
||||
}
|
||||
|
||||
protected function getEnv(string $key, string $name): string
|
||||
{
|
||||
$variable = $this->registry[$key]['variables'][$name]['envVariable'] ?? '';
|
||||
|
||||
@@ -10,6 +10,16 @@ use Utopia\Database\Document;
|
||||
|
||||
class InstallationTokens
|
||||
{
|
||||
/**
|
||||
* Refreshes an installation's token, resolving the OAuth2 client for its provider.
|
||||
*/
|
||||
public function refreshForInstallation(Document $installation, Database $dbForPlatform, Factory $vcsFactory): Document
|
||||
{
|
||||
$provider = $installation->getAttribute('provider', 'github');
|
||||
|
||||
return $this->refresh($installation, $dbForPlatform, $vcsFactory->oauth2FromProvider($provider));
|
||||
}
|
||||
|
||||
public function refresh(Document $installation, Database $dbForPlatform, OAuth2 $oauth2, ?Document $identity = null): Document
|
||||
{
|
||||
$accessToken = $installation->getAttribute('personalAccessToken');
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Vcs;
|
||||
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Database\Validator\Authorization;
|
||||
|
||||
/**
|
||||
* Untracks a closed/merged pull request from every repository connected to
|
||||
* it, scoped to a single provider (a GitHub and a GitLab repository can
|
||||
* share the same numeric providerRepositoryId, since each is scoped to its
|
||||
* own server).
|
||||
*/
|
||||
class RepositoryPullRequestCleanup
|
||||
{
|
||||
public function remove(
|
||||
Database $dbForPlatform,
|
||||
Authorization $authorization,
|
||||
string $provider,
|
||||
string $providerRepositoryId,
|
||||
int|string $providerPullRequestId,
|
||||
): void {
|
||||
$repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [
|
||||
Query::equal('providerRepositoryId', [$providerRepositoryId]),
|
||||
Query::orderDesc('$createdAt'),
|
||||
Query::limit(100),
|
||||
]));
|
||||
|
||||
foreach ($repositories as $repository) {
|
||||
$installation = $authorization->skip(fn () => $dbForPlatform->getDocument('installations', $repository->getAttribute('installationId', '')));
|
||||
|
||||
if ($installation->isEmpty() || $installation->getAttribute('provider', 'github') !== $provider) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$providerPullRequestIds = $repository->getAttribute('providerPullRequestIds', []);
|
||||
|
||||
if (\in_array($providerPullRequestId, $providerPullRequestIds)) {
|
||||
$providerPullRequestIds = \array_diff($providerPullRequestIds, [$providerPullRequestId]);
|
||||
$authorization->skip(fn () => $dbForPlatform->updateDocument('repositories', $repository->getId(), new Document(['providerPullRequestIds' => $providerPullRequestIds])));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Vcs;
|
||||
|
||||
use Appwrite\Auth\OAuth2\Github as OAuth2Github;
|
||||
use Appwrite\Extend\Exception;
|
||||
use Appwrite\Vcs\Factory;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@@ -13,12 +14,14 @@ use Utopia\Config\Config;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\VCS\Adapter\Git;
|
||||
use Utopia\VCS\Adapter\Git\GitHub;
|
||||
use Utopia\VCS\Adapter\Git\GitLab;
|
||||
|
||||
final class FactoryTest extends TestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
\putenv('_APP_VCS_TEST_TOKEN');
|
||||
\putenv('_APP_VCS_TEST_ENDPOINT');
|
||||
}
|
||||
|
||||
public function testRegistryEntries(): void
|
||||
@@ -28,6 +31,7 @@ final class FactoryTest extends TestCase
|
||||
|
||||
foreach ($registry as $key => $entry) {
|
||||
$this->assertTrue(\is_subclass_of($entry['adapter'], Git::class), "Adapter for '{$key}' must extend Git");
|
||||
$this->assertIsCallable($entry['oauth2'], "OAuth2 builder for '{$key}' must be callable");
|
||||
$this->assertNotEmpty($entry['variables'], "Variables missing for '{$key}'");
|
||||
foreach ($entry['variables'] as $name => $variable) {
|
||||
$this->assertStringStartsWith('_APP_VCS_', $variable['envVariable'] ?? '', "Env variable for '{$name}' missing or invalid for '{$key}'");
|
||||
@@ -35,6 +39,28 @@ final class FactoryTest extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function testOauth2FromProviderUnknownThrows(): void
|
||||
{
|
||||
$factory = new Factory($this->cache(), ['github' => $this->githubEntry()]);
|
||||
|
||||
$this->expectException(Exception::class);
|
||||
$factory->oauth2FromProvider('bitbucket');
|
||||
}
|
||||
|
||||
public function testOauth2FromProviderBuildsClient(): void
|
||||
{
|
||||
\putenv('_APP_VCS_GITHUB_CLIENT_ID=client-id');
|
||||
\putenv('_APP_VCS_GITHUB_CLIENT_SECRET=client-secret');
|
||||
|
||||
$factory = new Factory($this->cache(), ['github' => $this->githubEntry()]);
|
||||
$oauth2 = $factory->oauth2FromProvider('github');
|
||||
|
||||
$this->assertInstanceOf(OAuth2Github::class, $oauth2);
|
||||
|
||||
\putenv('_APP_VCS_GITHUB_CLIENT_ID');
|
||||
\putenv('_APP_VCS_GITHUB_CLIENT_SECRET');
|
||||
}
|
||||
|
||||
public function testFromProviderUnknownThrows(): void
|
||||
{
|
||||
$factory = new Factory($this->cache(), ['github' => $this->githubEntry()]);
|
||||
@@ -89,6 +115,33 @@ final class FactoryTest extends TestCase
|
||||
$this->assertSame(['test'], $factory->getProviders());
|
||||
}
|
||||
|
||||
public function testFromProviderAppliesFixedEndpoint(): void
|
||||
{
|
||||
$entry = [
|
||||
'adapter' => GitLab::class,
|
||||
'endpoint' => 'https://gitlab.com',
|
||||
'variables' => [],
|
||||
];
|
||||
$factory = new Factory($this->cache(), ['gitlab' => $entry]);
|
||||
|
||||
$adapter = $factory->fromProvider('gitlab');
|
||||
$this->assertSame('https://gitlab.com/owner/repo', $adapter->getRepositoryUrl('owner', 'repo'));
|
||||
}
|
||||
|
||||
public function testFromProviderFixedEndpointIgnoresEnv(): void
|
||||
{
|
||||
$entry = [
|
||||
'adapter' => GitLab::class,
|
||||
'endpoint' => 'https://gitlab.com',
|
||||
'variables' => [],
|
||||
];
|
||||
$factory = new Factory($this->cache(), ['gitlab' => $entry]);
|
||||
|
||||
\putenv('_APP_VCS_TEST_ENDPOINT=https://gitlab.example.com');
|
||||
$adapter = $factory->fromProvider('gitlab');
|
||||
$this->assertSame('https://gitlab.com/owner/repo', $adapter->getRepositoryUrl('owner', 'repo'));
|
||||
}
|
||||
|
||||
public function testGetWebhookSecret(): void
|
||||
{
|
||||
$factory = new Factory($this->cache(), ['github' => $this->githubEntry()]);
|
||||
@@ -112,6 +165,7 @@ final class FactoryTest extends TestCase
|
||||
{
|
||||
return [
|
||||
'adapter' => GitHub::class,
|
||||
'oauth2' => fn (string $clientId, string $clientSecret, string $endpoint) => new OAuth2Github($clientId, $clientSecret, ''),
|
||||
'variables' => [
|
||||
'appName' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_APP_NAME'],
|
||||
'privateKey' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_PRIVATE_KEY'],
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Vcs;
|
||||
|
||||
use Appwrite\Vcs\RepositoryPullRequestCleanup;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Validator\Authorization;
|
||||
|
||||
final class RepositoryPullRequestCleanupTest extends TestCase
|
||||
{
|
||||
public function testRemoveOnlyUpdatesGitlabRepositories(): void
|
||||
{
|
||||
// A GitHub repo and a GitLab repo can share the same numeric
|
||||
// providerRepositoryId since each is scoped to its own server.
|
||||
$gitlabRepository = new Document([
|
||||
'$id' => 'repo-gitlab',
|
||||
'installationId' => 'install-gitlab',
|
||||
'providerRepositoryId' => '5',
|
||||
'providerPullRequestIds' => [5],
|
||||
]);
|
||||
|
||||
$githubRepository = new Document([
|
||||
'$id' => 'repo-github',
|
||||
'installationId' => 'install-github',
|
||||
'providerRepositoryId' => '5',
|
||||
'providerPullRequestIds' => [5],
|
||||
]);
|
||||
|
||||
$gitlabInstallation = new Document(['$id' => 'install-gitlab', 'provider' => 'gitlab']);
|
||||
$githubInstallation = new Document(['$id' => 'install-github', 'provider' => 'github']);
|
||||
|
||||
$dbForPlatform = $this->createMock(Database::class);
|
||||
$authorization = $this->createStub(Authorization::class);
|
||||
|
||||
$dbForPlatform->method('find')->willReturn([$gitlabRepository, $githubRepository]);
|
||||
|
||||
$dbForPlatform->method('getDocument')
|
||||
->willReturnCallback(fn (string $collection, string $id): Document => match ($id) {
|
||||
'install-gitlab' => $gitlabInstallation,
|
||||
'install-github' => $githubInstallation,
|
||||
default => new Document(),
|
||||
});
|
||||
|
||||
$authorization->method('skip')->willReturnCallback(fn (callable $fn): mixed => $fn());
|
||||
|
||||
$dbForPlatform->expects($this->once())
|
||||
->method('updateDocument')
|
||||
->with('repositories', 'repo-gitlab', $this->callback(function (Document $doc) {
|
||||
$this->assertSame([], array_values($doc->getAttribute('providerPullRequestIds')));
|
||||
return true;
|
||||
}));
|
||||
|
||||
(new RepositoryPullRequestCleanup())->remove($dbForPlatform, $authorization, 'gitlab', '5', 5);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user