appwrite/src/Appwrite/Vcs/Comment.php

183 lines
7.9 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
{
2023-08-22 08:16:17 +00:00
protected array $tips = [
'Appwrite has a Discord community with over 16 000 members. [Come join us!](https://appwrite.io/discord)',
'You can use [Avatars API](https://appwrite.io/docs/client/avatars?sdk=web-default#avatarsGetQR) to generate QR code for any text or URLs',
'[Cursor pagination](https://appwrite.io/docs/pagination#cursor-pagination) performs better than offset pagination when loading further pages',
];
protected string $statePrefix = '[appwrite]: #';
/**
* @var mixed[] $builds
*/
protected array $builds = [];
public function isEmpty(): bool
{
return \count($this->builds) === 0;
}
2024-10-28 13:39:12 +00:00
public function addBuild(Document $project, Document $resource, string $resourceType, string $buildStatus, string $deploymentId, array $action, string $previewUrl, string $previewQrCode): 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(),
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
'previewQrCode' => $previewQrCode,
'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'],
'status' => $build['buildStatus'],
'deploymentId' => $build['deploymentId'],
'action' => $build['action'],
2024-10-28 14:20:36 +00:00
'previewUrl' => $build['previewUrl'],
2024-10-28 13:39:12 +00:00
'previewQrCode' => $build['previewQrCode']
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'],
'status' => $build['buildStatus'],
'deploymentId' => $build['deploymentId'],
'action' => $build['action'],
];
}
}
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
2024-10-28 13:19:44 +00:00
$text .= "Project name: **{$project['name']}** \nProject ID: `{$projectId}`\n\n";
2024-10-28 14:20:36 +00:00
if (\count($project['site']) > 0) {
2024-10-28 13:39:12 +00:00
$text .= "| Site | ID | Status | Previews | Action |\n";
$text .= "| :- | :- | :- | :- | :- |\n";
2024-03-20 16:47:48 +00:00
2024-10-28 13:39:12 +00:00
foreach ($project['site'] as $siteId => $site) {
2024-10-28 13:19:44 +00:00
$generateImage = function (string $status) use ($protocol, $hostname) {
$extention = $status === 'building' ? 'gif' : 'png';
2024-10-28 13:39:12 +00:00
$imagesUrl = $protocol . '://' . $hostname . '/console/images/vcs/';
2024-10-28 13:19:44 +00:00
$imageUrl = '<picture><source media="(prefers-color-scheme: dark)" srcset="' . $imagesUrl . 'status-' . $status . '-dark.' . $extention . '"><img alt="' . $status . '" height="25" align="center" src="' . $imagesUrl . 'status-' . $status . '-light.' . $extention . '"></picture>';
2024-10-28 13:39:12 +00:00
2024-10-28 13:19:44 +00:00
return $imageUrl;
};
2024-10-28 13:39:12 +00:00
$status = match ($site['status']) {
2024-10-28 13:19:44 +00:00
'waiting' => $generateImage('waiting') . ' Waiting to build',
'processing' => $generateImage('processing') . ' Processing',
'building' => $generateImage('building') . ' Building',
'ready' => $generateImage('ready') . ' Ready',
'failed' => $generateImage('failed') . ' Failed',
};
2024-10-28 13:39:12 +00:00
if ($site['action']['type'] === 'logs') {
2025-03-13 12:28:05 +00:00
$action = '[View Logs](' . $protocol . '://' . $hostname . '/console/project-' . $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
}
2024-10-28 13:39:12 +00:00
$previews = '[Preview URL](' . $site['previewUrl'] . ') [QR Code](' . $site['previewQrCode'] . ')';
$text .= "| {$site['name']} | `{$siteId}` | {$status} | {$previews} | {$action} |\n";
2023-08-09 09:52:34 +00:00
}
2024-10-28 13:39:12 +00:00
$text .= "\n\n";
}
2024-10-28 13:00:23 +00:00
2024-10-28 14:20:36 +00:00
if (\count($project['function']) > 0) {
2024-10-28 13:00:23 +00:00
2024-10-28 13:39:12 +00:00
$text .= "| Function | ID | Status | Action |\n";
$text .= "| :- | :- | :- | :- |\n";
2024-10-28 13:00:23 +00:00
2024-10-28 13:39:12 +00:00
foreach ($project['function'] as $functionId => $function) {
2024-10-28 13:19:44 +00:00
$generateImage = function (string $status) use ($protocol, $hostname) {
$extention = $status === 'building' ? 'gif' : 'png';
2025-03-13 12:28:05 +00:00
$imagesUrl = $protocol . '://' . $hostname . '/console/images/vcs/';
2024-10-28 13:19:44 +00:00
$imageUrl = '<picture><source media="(prefers-color-scheme: dark)" srcset="' . $imagesUrl . 'status-' . $status . '-dark.' . $extention . '"><img alt="' . $status . '" height="25" align="center" src="' . $imagesUrl . 'status-' . $status . '-light.' . $extention . '"></picture>';
return $imageUrl;
};
2024-10-28 13:39:12 +00:00
$status = match ($function['status']) {
2024-10-28 13:19:44 +00:00
'waiting' => $generateImage('waiting') . ' Waiting to build',
'processing' => $generateImage('processing') . ' Processing',
'building' => $generateImage('building') . ' Building',
'ready' => $generateImage('ready') . ' Ready',
'failed' => $generateImage('failed') . ' Failed',
};
2024-10-28 13:00:23 +00:00
2024-10-28 13:39:12 +00:00
if ($function['action']['type'] === 'logs') {
2025-03-13 13:28:56 +00:00
$action = '[View Logs](' . $protocol . '://' . $hostname . '/console/project-' . $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
}
2024-10-28 13:39:12 +00:00
$text .= "| {$function['name']} | `{$functionId}` | {$status} | {$action} |\n";
2024-10-28 13:19:44 +00:00
}
2024-10-28 13:39:12 +00:00
$text .= "\n\n";
2024-10-28 13:00:23 +00:00
}
}
2024-10-28 13:19:44 +00:00
$text .= "Only deployments on the production branch are activated automatically. Learn more about Appwrite [Functions](https://appwrite.io/docs/functions) and [Sites](https://appwrite.io/docs/sites).\n\n";
2024-03-20 16:47:48 +00:00
2023-08-22 08:16:17 +00:00
$tip = $this->tips[array_rand($this->tips)];
$text .= "> **💡 Did you know?** \n " . $tip . "\n\n";
2023-08-03 09:34:08 +00:00
return $text;
}
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;
}
}