diff --git a/app/init/resources.php b/app/init/resources.php index 8957b4f8ff..c08f823f9e 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -379,6 +379,7 @@ $container->set('servers', function () { $container->set('promiseAdapter', fn ($register) => $register->get('promiseAdapter'), ['register']); $container->set('vcsFactory', fn (Cache $cache) => new VcsFactory($cache), ['cache']); +$container->set('gitHub', fn (VcsFactory $vcsFactory) => $vcsFactory->fromProvider('github'), ['vcsFactory']); $container->set('installationTokens', fn () => new InstallationTokens(), []); $container->set('repositoryWebhooks', fn (VcsFactory $vcsFactory) => new RepositoryWebhooks($vcsFactory), ['vcsFactory']); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php index 5c20f10eb4..c1e3da5adf 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php @@ -53,8 +53,9 @@ trait Deployment ?Backend $deployments = null, ) { $errors = []; + $provider = $vcs->getName(); foreach ($repositories as $repository) { - $logBase = 'vcs.github.event.repo.unknown'; + $logBase = "vcs.{$provider}.event.repo.unknown"; try { $repositoryId = $repository->getId(); @@ -62,7 +63,7 @@ trait Deployment $resourceId = $repository->getAttribute('resourceId'); $resourceType = $repository->getAttribute('resourceType'); - $logBase = "vcs.github.event.repo.{$repositoryId}"; + $logBase = "vcs.{$provider}.event.repo.{$repositoryId}"; Span::add('project.id', $projectId); Span::add("{$logBase}.resource.id", $resourceId); Span::add("{$logBase}.resource.type", $resourceType); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php index 40820d93dc..10ec1bbcb6 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php @@ -102,7 +102,15 @@ class Create extends Action return $installation; } - private function resolveAdapterForRepository(Document $repository, VcsFactory $vcsFactory, InstallationTokens $installationTokens, Database $dbForPlatform, Authorization $authorization): ?Git + /** + * 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. + */ + private function resolveAdapterForRepository(Document $repository, VcsFactory $vcsFactory, InstallationTokens $installationTokens, Database $dbForPlatform, Authorization $authorization, array &$errors): ?Git { $installation = $this->resolveGiteaInstallation($repository, $dbForPlatform, $authorization); @@ -122,7 +130,10 @@ class Create extends Action return $vcsFactory->fromInstallation($installation); } catch (\Throwable $error) { - Console::warning("Failed to resolve Gitea adapter for installation '{$installation->getId()}': " . $error->getMessage()); + $message = "Failed to resolve Gitea adapter for installation '{$installation->getId()}': " . $error->getMessage(); + Console::warning($message); + Span::add("vcs.gitea.event.installation.{$installation->getId()}.error", $message); + $errors[] = $message; return null; } } @@ -166,8 +177,9 @@ class Create extends Action $providerAffectedFiles = $parsedPayload['affectedFiles'] ?? []; + $errors = []; foreach ($repositories as $repository) { - $adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $installationTokens, $dbForPlatform, $authorization); + $adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $installationTokens, $dbForPlatform, $authorization, $errors); if ($adapter === null) { continue; @@ -177,6 +189,10 @@ class Create extends Action $this->createGitDeployments($adapter, $providerInstallationId, [$repository], $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthorName, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, '', $providerAffectedFiles, false, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform); } + + if (!empty($errors)) { + throw new Exception(Exception::GENERAL_UNKNOWN, \implode("\n", $errors)); + } } private function handlePullRequestEvent( @@ -219,8 +235,9 @@ class Create extends Action Query::limit(100), ])); + $errors = []; foreach ($repositories as $repository) { - $adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $installationTokens, $dbForPlatform, $authorization); + $adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $installationTokens, $dbForPlatform, $authorization, $errors); if ($adapter === null) { continue; @@ -245,6 +262,10 @@ class Create extends Action $this->createGitDeployments($adapter, $providerInstallationId, [$repository], $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthor, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, $providerPullRequestId, $providerAffectedFiles, $external, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform); } + + if (!empty($errors)) { + throw new Exception(Exception::GENERAL_UNKNOWN, \implode("\n", $errors)); + } } elseif ($action === 'closed') { $providerRepositoryId = $parsedPayload['repositoryId'] ?? ''; $providerPullRequestId = $parsedPayload['pullRequestNumber'] ?? '';