appwrite/src/Appwrite/Vcs/Comment.php

223 lines
9.2 KiB
PHP
Raw Normal View History

<?php
namespace Appwrite\Vcs;
2024-03-06 17:34:21 +00:00
use Utopia\Database\Document;
2024-04-01 11:02:47 +00:00
use Utopia\System\System;
// TODO this class should be moved to a more appropriate place in the architecture
class Comment
{
// TODO: Add more tips
2023-08-22 08:16:17 +00:00
protected array $tips = [
'Appwrite has a Discord community with over 16 000 members.',
'You can use Avatars API to generate QR code for any text or URLs.',
'Cursor pagination performs better than offset pagination when loading further pages.',
2023-08-22 08:16:17 +00:00
];
protected string $statePrefix = '[appwrite]: #';
/**
* @var mixed[] $builds
*/
protected array $builds = [];
public function isEmpty(): bool
{
return \count($this->builds) === 0;
}
public function addBuild(Document $project, Document $resource, string $resourceType, string $buildStatus, string $deploymentId, array $action, string $previewUrl): void
{
// Unique index
2024-10-28 13:00:23 +00:00
$id = $project->getId() . '_' . $resource->getId();
$this->builds[$id] = [
'projectName' => $project->getAttribute('name'),
'projectId' => $project->getId(),
'region' => $project->getAttribute('region', 'default'),
2024-10-28 13:00:23 +00:00
'resourceName' => $resource->getAttribute('name'),
'resourceId' => $resource->getId(),
2024-10-28 13:19:44 +00:00
'resourceType' => $resourceType,
'buildStatus' => $buildStatus,
2023-08-09 09:52:34 +00:00
'deploymentId' => $deploymentId,
'action' => $action,
2024-10-28 13:39:12 +00:00
'previewUrl' => $previewUrl,
];
}
2024-10-28 13:19:44 +00:00
public function generateComment(): string
{
$json = \json_encode($this->builds);
$text = $this->statePrefix . \base64_encode($json) . "\n\n";
$projects = [];
foreach ($this->builds as $id => $build) {
if (!\array_key_exists($build['projectId'], $projects)) {
$projects[$build['projectId']] = [
'name' => $build['projectName'],
2024-10-28 13:19:44 +00:00
'function' => [],
'site' => []
];
}
2024-10-28 14:20:36 +00:00
if ($build['resourceType'] === 'site') {
2024-10-28 13:19:44 +00:00
$projects[$build['projectId']]['site'][$build['resourceId']] = [
'name' => $build['resourceName'],
'region' => $build['region'],
2024-10-28 13:19:44 +00:00
'status' => $build['buildStatus'],
'deploymentId' => $build['deploymentId'],
'action' => $build['action'],
2024-10-28 14:20:36 +00:00
'previewUrl' => $build['previewUrl'],
2024-10-28 13:19:44 +00:00
];
2024-10-28 14:20:36 +00:00
} elseif ($build['resourceType'] === 'function') {
2024-10-28 13:19:44 +00:00
$projects[$build['projectId']]['function'][$build['resourceId']] = [
'name' => $build['resourceName'],
'region' => $build['region'],
2024-10-28 13:19:44 +00:00
'status' => $build['buildStatus'],
'deploymentId' => $build['deploymentId'],
'action' => $build['action'],
];
}
}
$i = 0;
foreach ($projects as $projectId => $project) {
2024-04-01 11:02:47 +00:00
$protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') == 'disabled' ? 'http' : 'https';
$hostname = System::getEnv('_APP_DOMAIN');
2023-08-03 19:34:04 +00:00
$text .= "## {$project['name']}\n\n";
$text .= "Project ID: `{$projectId}`\n\n";
$isOpen = $i === 0;
2024-10-28 13:19:44 +00:00
2024-10-28 14:20:36 +00:00
if (\count($project['site']) > 0) {
$text .= "<details" . ($isOpen ? ' open' : '') . ">\n";
$text .= "<summary>Sites (" . \count($project['site']) . ")</summary>\n\n";
$text .= "<br>\n\n";
$text .= "| Site | Status | Logs | Preview | QR\n";
2024-10-28 13:39:12 +00:00
$text .= "| :- | :- | :- | :- | :- |\n";
2024-03-20 16:47:48 +00:00
2024-10-28 13:39:12 +00:00
foreach ($project['site'] as $siteId => $site) {
$extension = $site['status'] === 'building' ? 'gif' : 'png';
2024-10-28 13:39:12 +00:00
$pathLight = '/images/vcs/status-' . $site['status'] . '-light.' . $extension;
$pathDark = '/images/vcs/status-' . $site['status'] . '-dark.' . $extension;
2024-10-28 13:19:44 +00:00
2024-10-28 13:39:12 +00:00
$status = match ($site['status']) {
'waiting' => $this->generatImage($pathLight, $pathDark, 'Queued', 85) . ' _Queued_',
'processing' => $this->generatImage($pathLight, $pathDark, 'Processing', 85) . ' _Processing_',
'building' => $this->generatImage($pathLight, $pathDark, 'Building', 85) . ' _Building_',
'ready' => $this->generatImage($pathLight, $pathDark, 'Ready', 85) . ' _Ready_',
'failed' => $this->generatImage($pathLight, $pathDark, 'Failed', 85) . ' _Failed_',
2024-10-28 13:19:44 +00:00
};
2024-10-28 13:39:12 +00:00
if ($site['action']['type'] === 'logs') {
$action = '[View Logs](' . $protocol . '://' . $hostname . '/console/project-' . $site['region'] . '-' . $projectId . '/sites/site-' . $siteId . '/deployments/deployment-' . $site['deploymentId'] . ')';
2024-10-28 13:19:44 +00:00
} else {
2024-10-28 13:39:12 +00:00
$action = '[Authorize](' . $site['action']['url'] . ')';
2024-10-28 13:19:44 +00:00
}
$qrImagePathLight = '/images/vcs/qr-light.svg';
$qrImagePathDark = '/images/vcs/qr-dark.svg';
$consoleUrl = $protocol . '://' . $hostname . '/v1/avatars/qr?text=' . \urlencode($site['previewUrl']);
$qr = '[' . $this->generatImage($qrImagePathLight, $qrImagePathDark, 'QR Code', 28) . '](' . $consoleUrl . ')';
2024-10-28 13:39:12 +00:00
$preview = '[Preview URL](' . $site['previewUrl'] . ')';
$text .= "| &nbsp;**{$site['name']}**<br>`$siteId`";
$text .= "| {$status}";
$text .= "| {$action}";
$text .= "| {$preview}";
$text .= "| {$qr}";
$text .= "|\n";
2023-08-09 09:52:34 +00:00
}
$text .= "\n</details>\n\n";
2024-10-28 13:39:12 +00:00
}
2024-10-28 13:00:23 +00:00
2024-10-28 14:20:36 +00:00
if (\count($project['function']) > 0) {
$text .= "<details" . ($isOpen ? ' open' : '') . ">\n";
$text .= "<summary>Functions (" . \count($project['function']) . ")</summary>\n\n";
$text .= "<br>\n\n";
$text .= "| Function | ID | Status | Logs |\n";
2024-10-28 13:39:12 +00:00
$text .= "| :- | :- | :- | :- |\n";
2024-10-28 13:00:23 +00:00
2024-10-28 13:39:12 +00:00
foreach ($project['function'] as $functionId => $function) {
$extension = $function['status'] === 'building' ? 'gif' : 'png';
$pathLight = '/images/vcs/status-' . $function['status'] . '-light.' . $extension;
$pathDark = '/images/vcs/status-' . $function['status'] . '-dark.' . $extension;
2024-10-28 13:19:44 +00:00
$status = match ($function['status']) {
'waiting' => $this->generatImage($pathLight, $pathDark, 'Queued', 85) . ' _Queued_',
'processing' => $this->generatImage($pathLight, $pathDark, 'Processing', 85) . ' _Processing_',
'building' => $this->generatImage($pathLight, $pathDark, 'Building', 85) . ' _Building_',
'ready' => $this->generatImage($pathLight, $pathDark, 'Ready', 85) . ' _Ready_',
'failed' => $this->generatImage($pathLight, $pathDark, 'Failed', 85) . ' _Failed_',
2024-10-28 13:19:44 +00:00
};
2024-10-28 13:00:23 +00:00
2024-10-28 13:39:12 +00:00
if ($function['action']['type'] === 'logs') {
$action = '[View Logs](' . $protocol . '://' . $hostname . '/console/project-' . $function['region'] . '-' . $projectId . '/functions/function-' . $functionId . '/deployment-' . $function['deploymentId'] . ')';
2024-10-28 13:19:44 +00:00
} else {
2024-10-28 13:39:12 +00:00
$action = '[Authorize](' . $function['action']['url'] . ')';
2024-10-28 13:19:44 +00:00
}
$text .= "| &nbsp;**{$function['name']}**<br>`$functionId`";
$text .= "| {$status}";
$text .= "| {$action}";
$text .= "|\n";
2024-10-28 13:19:44 +00:00
}
2024-10-28 13:39:12 +00:00
$text .= "</details>\n\n";
2024-10-28 13:00:23 +00:00
}
$text .= "</details>\n\n";
2024-10-28 13:19:44 +00:00
$isLast = $i === \count($projects) - 1;
if (\count($projects) > 1 && $isLast) {
$text .= "---\n\n";
}
$i++;
}
2024-03-20 16:47:48 +00:00
2023-08-22 08:16:17 +00:00
$tip = $this->tips[array_rand($this->tips)];
$text .= "\n<br>\n\n> [!NOTE]\n> $tip\n\n";
2023-08-03 09:34:08 +00:00
return $text;
}
public function generatImage(string $pathLight, string $pathDark, string $alt, int $width): string
{
$protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') == 'disabled' ? 'http' : 'https';
$hostname = System::getEnv('_APP_DOMAIN');
$imageLight = $protocol . '://' . $hostname . $pathLight;
$imageDark = $protocol . '://' . $hostname . $pathDark;
$imageUrl = '<picture><source media="(prefers-color-scheme: dark)" srcset="' . $imageDark . '"><img alt="' . $alt . '" width="' . $width . '" align="center" src="' . $imageLight . '"></picture>';
return $imageUrl;
}
public function parseComment(string $comment): self
{
$state = \explode("\n", $comment)[0] ?? '';
$state = substr($state, strlen($this->statePrefix));
$json = \base64_decode($state);
$builds = \json_decode($json, true);
$this->builds = $builds;
return $this;
}
}