getAttribute('entrypoint', ''); $providerInstallationId = $installation->getAttribute('providerInstallationId', ''); $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); $owner = $github->getOwnerName($providerInstallationId); $providerRepositoryId = $function->getAttribute('providerRepositoryId', ''); try { $repositoryName = $github->getRepositoryName($providerRepositoryId) ?? ''; if (empty($repositoryName)) { throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } } catch (RepositoryNotFound $e) { throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } // TODO: Support tag and commit in future if ($referenceType === 'branch') { $providerBranch = empty($reference) ? $function->getAttribute('providerBranch', 'main') : $reference; } $authorUrl = "https://github.com/$owner"; $repositoryUrl = "https://github.com/$owner/$repositoryName"; $branchUrl = "https://github.com/$owner/$repositoryName/tree/$providerBranch"; $commitDetails = []; if ($template->isEmpty()) { try { $commitDetails = $github->getLatestCommit($owner, $repositoryName, $providerBranch); } catch (\Throwable $error) { Console::warning('Failed to get latest commit details'); Console::warning($error->getMessage()); Console::warning($error->getTraceAsString()); } } $deployment = $dbForProject->createDocument('deployments', new Document([ '$id' => $deploymentId, '$permissions' => [ Permission::read(Role::any()), Permission::update(Role::any()), Permission::delete(Role::any()), ], 'resourceId' => $function->getId(), 'resourceInternalId' => $function->getInternalId(), 'resourceType' => 'functions', 'entrypoint' => $entrypoint, 'commands' => $function->getAttribute('commands', ''), 'type' => 'vcs', 'installationId' => $installation->getId(), 'installationInternalId' => $installation->getInternalId(), 'providerRepositoryId' => $providerRepositoryId, 'repositoryId' => $function->getAttribute('repositoryId', ''), 'repositoryInternalId' => $function->getAttribute('repositoryInternalId', ''), 'providerBranchUrl' => $branchUrl, 'providerRepositoryName' => $repositoryName, 'providerRepositoryOwner' => $owner, 'providerRepositoryUrl' => $repositoryUrl, 'providerCommitHash' => $commitDetails['commitHash'] ?? '', 'providerCommitAuthorUrl' => $authorUrl, 'providerCommitAuthor' => $commitDetails['commitAuthor'] ?? '', 'providerCommitMessage' => $commitDetails['commitMessage'] ?? '', 'providerCommitUrl' => $commitDetails['commitUrl'] ?? '', 'providerBranch' => $providerBranch, 'providerRootDirectory' => $function->getAttribute('providerRootDirectory', ''), 'search' => implode(' ', [$deploymentId, $entrypoint]), 'activate' => $activate, ])); $queueForBuilds ->setType(BUILD_TYPE_DEPLOYMENT) ->setResource($function) ->setDeployment($deployment) ->setTemplate($template); return $deployment; } public function redeployVcsSite(Request $request, Document $site, Document $project, Document $installation, Database $dbForProject, Database $dbForPlatform, Build $queueForBuilds, Document $template, GitHub $github, bool $activate, string $referenceType = 'branch', string $reference = ''): Document { $deploymentId = ID::unique(); $providerInstallationId = $installation->getAttribute('providerInstallationId', ''); $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); $owner = $github->getOwnerName($providerInstallationId); $providerRepositoryId = $site->getAttribute('providerRepositoryId', ''); try { $repositoryName = $github->getRepositoryName($providerRepositoryId) ?? ''; if (empty($repositoryName)) { throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } } catch (RepositoryNotFound $e) { throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } // TODO: Support tag and commit in future if ($referenceType === 'branch') { $providerBranch = empty($reference) ? $site->getAttribute('providerBranch', 'main') : $reference; } $authorUrl = "https://github.com/$owner"; $repositoryUrl = "https://github.com/$owner/$repositoryName"; $branchUrl = "https://github.com/$owner/$repositoryName/tree/$providerBranch"; $commitDetails = []; if ($template->isEmpty()) { try { $commitDetails = $github->getLatestCommit($owner, $repositoryName, $providerBranch); } catch (\Throwable $error) { Console::warning('Failed to get latest commit details'); Console::warning($error->getMessage()); Console::warning($error->getTraceAsString()); } } $deployment = $dbForProject->createDocument('deployments', new Document([ '$id' => $deploymentId, '$permissions' => [ Permission::read(Role::any()), Permission::update(Role::any()), Permission::delete(Role::any()), ], 'resourceId' => $site->getId(), 'resourceInternalId' => $site->getInternalId(), 'resourceType' => 'sites', 'buildCommand' => $site->getAttribute('buildCommand', ''), 'installCommand' => $site->getAttribute('installCommand', ''), 'outputDirectory' => $site->getAttribute('outputDirectory', ''), 'type' => 'vcs', 'installationId' => $installation->getId(), 'installationInternalId' => $installation->getInternalId(), 'providerRepositoryId' => $providerRepositoryId, 'repositoryId' => $site->getAttribute('repositoryId', ''), 'repositoryInternalId' => $site->getAttribute('repositoryInternalId', ''), 'providerBranchUrl' => $branchUrl, 'providerRepositoryName' => $repositoryName, 'providerRepositoryOwner' => $owner, 'providerRepositoryUrl' => $repositoryUrl, 'providerCommitHash' => $commitDetails['commitHash'] ?? '', 'providerCommitAuthorUrl' => $authorUrl, 'providerCommitAuthor' => $commitDetails['commitAuthor'] ?? '', 'providerCommitMessage' => $commitDetails['commitMessage'] ?? '', 'providerCommitUrl' => $commitDetails['commitUrl'] ?? '', 'providerBranch' => $providerBranch, 'providerRootDirectory' => $site->getAttribute('providerRootDirectory', ''), 'search' => implode(' ', [$deploymentId]), 'activate' => $activate, ])); $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); $domain = ID::unique() . "." . $sitesDomain; $ruleId = md5($domain); Authorization::skip( fn () => $dbForPlatform->createDocument('rules', new Document([ '$id' => $ruleId, 'projectId' => $project->getId(), 'projectInternalId' => $project->getInternalId(), 'domain' => $domain, 'type' => 'deployment', 'deploymentId' => $deployment->getId(), 'deploymentInternalId' => $deployment->getInternalId(), 'deploymentResourceType' => 'site', 'deploymentResourceId' => $site->getId(), 'deploymentResourceInternalId' => $site->getInternalId(), 'deploymentVcsProviderBranch' => $providerBranch, 'deploymentUpdatePolicy' => '', 'status' => 'verified', 'certificateId' => '', 'search' => implode(' ', [$ruleId, $domain]), ])) ); $queueForBuilds ->setType(BUILD_TYPE_DEPLOYMENT) ->setResource($site) ->setDeployment($deployment) ->setTemplate($template); return $deployment; } protected function listRules(Document $project, array $queries, Database $database, callable $callback): void { $limit = 100; $cursor = null; do { $queries = \array_merge([ Query::limit($limit), Query::equal("projectInternalId", [$project->getInternalId()]) ], $queries); if ($cursor !== null) { $queries[] = Query::cursorAfter($cursor); } $results = $database->find('rules', $queries); $total = \count($results); if ($total > 0) { $cursor = $results[$total - 1]; } if ($total < $limit) { $cursor = null; } foreach ($results as $document) { if (is_callable($callback)) { $callback($document); } } } while (!\is_null($cursor)); } }