From 6ff15f9b53aa28a0a4f594a8c533434c0188cd31 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Mon, 28 Oct 2024 14:00:23 +0100 Subject: [PATCH 1/7] WIP: GitHub Comments for Sites --- app/controllers/api/vcs.php | 4 +- src/Appwrite/Vcs/Comment.php | 91 ++++++++++++++++++++++++++++++------ 2 files changed, 79 insertions(+), 16 deletions(-) diff --git a/app/controllers/api/vcs.php b/app/controllers/api/vcs.php index 9021c6c518..bd19eecace 100644 --- a/app/controllers/api/vcs.php +++ b/app/controllers/api/vcs.php @@ -117,11 +117,11 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId $comment->parseComment($github->getComment($owner, $repositoryName, $latestCommentId)); $comment->addBuild($project, $resource, $commentStatus, $deploymentId, $action); - $latestCommentId = \strval($github->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment())); + $latestCommentId = \strval($github->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment($resourceType))); } else { $comment = new Comment(); $comment->addBuild($project, $resource, $commentStatus, $deploymentId, $action); - $latestCommentId = \strval($github->createComment($owner, $repositoryName, $providerPullRequestId, $comment->generateComment())); + $latestCommentId = \strval($github->createComment($owner, $repositoryName, $providerPullRequestId, $comment->generateComment($resourceType))); if (!empty($latestCommentId)) { $teamId = $project->getAttribute('teamId', ''); diff --git a/src/Appwrite/Vcs/Comment.php b/src/Appwrite/Vcs/Comment.php index 18379f1099..55f433fff0 100644 --- a/src/Appwrite/Vcs/Comment.php +++ b/src/Appwrite/Vcs/Comment.php @@ -27,23 +27,28 @@ class Comment return \count($this->builds) === 0; } - public function addBuild(Document $project, Document $function, string $buildStatus, string $deploymentId, array $action): void + public function addBuild(Document $project, Document $resource, string $buildStatus, string $deploymentId, array $action): void { + var_dump("resource received in addBuild"); + var_dump($resource); // Unique index - $id = $project->getId() . '_' . $function->getId(); + $id = $project->getId() . '_' . $resource->getId(); $this->builds[$id] = [ 'projectName' => $project->getAttribute('name'), 'projectId' => $project->getId(), - 'functionName' => $function->getAttribute('name'), - 'functionId' => $function->getId(), + 'resourceName' => $resource->getAttribute('name'), + 'resourceId' => $resource->getId(), 'buildStatus' => $buildStatus, 'deploymentId' => $deploymentId, 'action' => $action, ]; + + var_dump("resource id"); + var_dump($resource->getId()); } - public function generateComment(): string + public function generateComment(string $resourceType): string { $json = \json_encode($this->builds); @@ -51,16 +56,27 @@ class Comment $projects = []; + $resource = match ($resourceType) { + 'function' => 'functions', + 'site' => 'sites', + }; + + $resourceId = match ($resourceType) { + 'function' => 'functionId', + 'site' => 'siteId', + }; + foreach ($this->builds as $id => $build) { if (!\array_key_exists($build['projectId'], $projects)) { $projects[$build['projectId']] = [ 'name' => $build['projectName'], - 'functions' => [] + 'functions' => [], + 'sites' => [] ]; } - $projects[$build['projectId']]['functions'][$build['functionId']] = [ - 'name' => $build['functionName'], + $projects[$build['projectId']][$resource][$build[$resourceId]] = [ + 'name' => $build['resourceName'], 'status' => $build['buildStatus'], 'deploymentId' => $build['deploymentId'], 'action' => $build['action'], @@ -68,14 +84,11 @@ class Comment } foreach ($projects as $projectId => $project) { - $text .= "**{$project['name']}** `{$projectId}`\n\n"; - $text .= "| Function | ID | Status | Action |\n"; - $text .= "| :- | :- | :- | :- |\n"; - $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') == 'disabled' ? 'http' : 'https'; $hostname = System::getEnv('_APP_DOMAIN'); foreach ($project['functions'] as $functionId => $function) { + var_dump("entered flow for function"); if ($function['status'] === 'waiting' || $function['status'] === 'processing' || $function['status'] === 'building') { $text .= "**Your function deployment is in progress. Please check back in a few minutes for the updated status.**\n\n"; } elseif ($function['status'] === 'ready') { @@ -93,6 +106,9 @@ class Comment $imagesUrl = $protocol . '://' . $hostname . '/images/vcs/'; $imageUrl = '' . $status . ''; + var_dump("image url"); + var_dump($imageUrl); + return $imageUrl; }; @@ -114,9 +130,56 @@ class Comment } $text .= "\n\n"; + + var_dump("project"); + var_dump($project); + foreach ($project['sites'] as $siteId => $site) { + var_dump("entered flow for site"); + var_dump($site); + if ($site['status'] === 'waiting' || $site['status'] === 'processing' || $site['status'] === 'building') { + $text .= "**Your site deployment is in progress. Please check back in a few minutes for the updated status.**\n\n"; + } elseif ($site['status'] === 'ready') { + $text .= "**Your site has been successfully deployed.**\n\n"; + } else { + $text .= "**Your site deployment has failed. Please check the logs for more details and retry.**\n\n"; + } + + $text .= "Project name: **{$project['name']}** \nProject ID: `{$projectId}`\n\n"; + $text .= "| Site | ID | Status | Preview link | Action |\n"; + $text .= "| :- | :- | :- | :- | :- |\n"; + + $generateImage = function (string $status) use ($protocol, $hostname) { + $extention = $status === 'building' ? 'gif' : 'png'; + $imagesUrl = $protocol . '://' . $hostname . '/console/images/vcs/'; + $imageUrl = '' . $status . ''; + + var_dump("image url"); + var_dump($imageUrl); + + return $imageUrl; + }; + + $status = match ($site['status']) { + 'waiting' => $generateImage('waiting') . ' Waiting to build', + 'processing' => $generateImage('processing') . ' Processing', + 'building' => $generateImage('building') . ' Building', + 'ready' => $generateImage('ready') . ' Ready', + 'failed' => $generateImage('failed') . ' Failed', + }; + + if ($site['action']['type'] === 'logs') { + $action = '[View Logs](' . $protocol . '://' . $hostname . '/console/project-' . $projectId . '/sites/site-' . $siteId . '/deployment-' . $site['deploymentId'] . ')'; + } else { + $action = '[Authorize](' . $site['action']['url'] . ')'; + } + + $text .= "| {$site['name']} | `{$siteId}` | {$status} | 'preview link' | {$action} |\n"; + } + + $text .= "\n\n"; } - $functionUrl = $protocol . '://' . $hostname . '/console/project-' . $projectId . '/functions/function-' . $functionId; - $text .= "Only deployments on the production branch are activated automatically. If you'd like to activate this deployment, navigate to [your deployments]($functionUrl). Learn more about Appwrite [Function deployments](https://appwrite.io/docs/functions).\n\n"; + $sitesUrl = $protocol . '://' . $hostname . '/console/project-' . $projectId . '/sites/site-' . $siteId; + $text .= "Only deployments on the production branch are activated automatically. If you'd like to activate this deployment, navigate to [your deployments]($sitesUrl). Learn more about Appwrite [Site deployments](https://appwrite.io/docs/functions).\n\n"; $tip = $this->tips[array_rand($this->tips)]; $text .= "> **💡 Did you know?** \n " . $tip . "\n\n"; From 31b9f2b4f9a2e20ad528146b18e057f80a744566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 28 Oct 2024 14:19:44 +0100 Subject: [PATCH 2/7] Fix comments --- app/controllers/api/vcs.php | 10 +- .../Modules/Functions/Workers/Builds.php | 8 +- src/Appwrite/Vcs/Comment.php | 165 ++++++++---------- 3 files changed, 83 insertions(+), 100 deletions(-) diff --git a/app/controllers/api/vcs.php b/app/controllers/api/vcs.php index bd19eecace..d987bec826 100644 --- a/app/controllers/api/vcs.php +++ b/app/controllers/api/vcs.php @@ -115,13 +115,13 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId $latestCommentId = $latestComment->getAttribute('providerCommentId', ''); $comment = new Comment(); $comment->parseComment($github->getComment($owner, $repositoryName, $latestCommentId)); - $comment->addBuild($project, $resource, $commentStatus, $deploymentId, $action); + $comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action); - $latestCommentId = \strval($github->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment($resourceType))); + $latestCommentId = \strval($github->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment())); } else { $comment = new Comment(); - $comment->addBuild($project, $resource, $commentStatus, $deploymentId, $action); - $latestCommentId = \strval($github->createComment($owner, $repositoryName, $providerPullRequestId, $comment->generateComment($resourceType))); + $comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action); + $latestCommentId = \strval($github->createComment($owner, $repositoryName, $providerPullRequestId, $comment->generateComment())); if (!empty($latestCommentId)) { $teamId = $project->getAttribute('teamId', ''); @@ -157,7 +157,7 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId $latestCommentId = $comment->getAttribute('providerCommentId', ''); $comment = new Comment(); $comment->parseComment($github->getComment($owner, $repositoryName, $latestCommentId)); - $comment->addBuild($project, $resource, $commentStatus, $deploymentId, $action); + $comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action); $latestCommentId = \strval($github->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment())); } diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 403941c893..11eacb5ec3 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -949,9 +949,15 @@ class Builds extends Action // Wrap in try/finally to ensure lock file gets deleted try { + $resourceType = match($resource->getCollection()) { + 'functions' => 'function', + 'sites' => 'site', + default => throw new \Exception('Invalid resource type') + }; + $comment = new Comment(); $comment->parseComment($github->getComment($owner, $repositoryName, $commentId)); - $comment->addBuild($project, $resource, $status, $deployment->getId(), ['type' => 'logs']); + $comment->addBuild($project, $resource, $resourceType, $status, $deployment->getId(), ['type' => 'logs']); $github->updateComment($owner, $repositoryName, $commentId, $comment->generateComment()); } finally { $dbForConsole->deleteDocument('vcsCommentLocks', $commentId); diff --git a/src/Appwrite/Vcs/Comment.php b/src/Appwrite/Vcs/Comment.php index 55f433fff0..f896fdefc9 100644 --- a/src/Appwrite/Vcs/Comment.php +++ b/src/Appwrite/Vcs/Comment.php @@ -27,10 +27,8 @@ class Comment return \count($this->builds) === 0; } - public function addBuild(Document $project, Document $resource, string $buildStatus, string $deploymentId, array $action): void + public function addBuild(Document $project, Document $resource, string $resourceType, string $buildStatus, string $deploymentId, array $action): void { - var_dump("resource received in addBuild"); - var_dump($resource); // Unique index $id = $project->getId() . '_' . $resource->getId(); @@ -39,16 +37,14 @@ class Comment 'projectId' => $project->getId(), 'resourceName' => $resource->getAttribute('name'), 'resourceId' => $resource->getId(), + 'resourceType' => $resourceType, 'buildStatus' => $buildStatus, 'deploymentId' => $deploymentId, 'action' => $action, ]; - - var_dump("resource id"); - var_dump($resource->getId()); } - public function generateComment(string $resourceType): string + public function generateComment(): string { $json = \json_encode($this->builds); @@ -56,130 +52,111 @@ class Comment $projects = []; - $resource = match ($resourceType) { - 'function' => 'functions', - 'site' => 'sites', - }; - - $resourceId = match ($resourceType) { - 'function' => 'functionId', - 'site' => 'siteId', - }; - foreach ($this->builds as $id => $build) { if (!\array_key_exists($build['projectId'], $projects)) { $projects[$build['projectId']] = [ 'name' => $build['projectName'], - 'functions' => [], - 'sites' => [] + 'function' => [], + 'site' => [] ]; } - $projects[$build['projectId']][$resource][$build[$resourceId]] = [ - 'name' => $build['resourceName'], - 'status' => $build['buildStatus'], - 'deploymentId' => $build['deploymentId'], - 'action' => $build['action'], - ]; + if($build['resourceType'] === 'site') { + $projects[$build['projectId']]['site'][$build['resourceId']] = [ + 'name' => $build['resourceName'], + 'status' => $build['buildStatus'], + 'deploymentId' => $build['deploymentId'], + 'action' => $build['action'], + 'previewUrl' => 'http://google.com', + 'previewQrCode' => 'https://cloud.appwrite.io/v1/avatars/qr?text=https://www.google.com/' + ]; + } else if($build['resourceType'] === 'function') { + $projects[$build['projectId']]['function'][$build['resourceId']] = [ + 'name' => $build['resourceName'], + 'status' => $build['buildStatus'], + 'deploymentId' => $build['deploymentId'], + 'action' => $build['action'], + ]; + } } foreach ($projects as $projectId => $project) { $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') == 'disabled' ? 'http' : 'https'; $hostname = System::getEnv('_APP_DOMAIN'); - foreach ($project['functions'] as $functionId => $function) { - var_dump("entered flow for function"); - if ($function['status'] === 'waiting' || $function['status'] === 'processing' || $function['status'] === 'building') { - $text .= "**Your function deployment is in progress. Please check back in a few minutes for the updated status.**\n\n"; - } elseif ($function['status'] === 'ready') { - $text .= "**Your function has been successfully deployed.**\n\n"; - } else { - $text .= "**Your function deployment has failed. Please check the logs for more details and retry.**\n\n"; - } + $text .= "Project name: **{$project['name']}** \nProject ID: `{$projectId}`\n\n"; + + if(\count($project['function']) > 0) { - $text .= "Project name: **{$project['name']}** \nProject ID: `{$projectId}`\n\n"; $text .= "| Function | ID | Status | Action |\n"; $text .= "| :- | :- | :- | :- |\n"; - $generateImage = function (string $status) use ($protocol, $hostname) { - $extention = $status === 'building' ? 'gif' : 'png'; - $imagesUrl = $protocol . '://' . $hostname . '/images/vcs/'; - $imageUrl = '' . $status . ''; + foreach ($project['function'] as $functionId => $function) { + $generateImage = function (string $status) use ($protocol, $hostname) { + $extention = $status === 'building' ? 'gif' : 'png'; + $imagesUrl = $protocol . '://' . $hostname . '/images/vcs/'; + $imageUrl = '' . $status . ''; + return $imageUrl; + }; - var_dump("image url"); - var_dump($imageUrl); + $status = match ($function['status']) { + 'waiting' => $generateImage('waiting') . ' Waiting to build', + 'processing' => $generateImage('processing') . ' Processing', + 'building' => $generateImage('building') . ' Building', + 'ready' => $generateImage('ready') . ' Ready', + 'failed' => $generateImage('failed') . ' Failed', + }; - return $imageUrl; - }; + if ($function['action']['type'] === 'logs') { + $action = '[View Logs](' . $protocol . '://' . $hostname . '/console/project-' . $projectId . '/functions/function-' . $functionId . '/deployment-' . $function['deploymentId'] . ')'; + } else { + $action = '[Authorize](' . $function['action']['url'] . ')'; + } - $status = match ($function['status']) { - 'waiting' => $generateImage('waiting') . ' Waiting to build', - 'processing' => $generateImage('processing') . ' Processing', - 'building' => $generateImage('building') . ' Building', - 'ready' => $generateImage('ready') . ' Ready', - 'failed' => $generateImage('failed') . ' Failed', - }; - - if ($function['action']['type'] === 'logs') { - $action = '[View Logs](' . $protocol . '://' . $hostname . '/console/project-' . $projectId . '/functions/function-' . $functionId . '/deployment-' . $function['deploymentId'] . ')'; - } else { - $action = '[Authorize](' . $function['action']['url'] . ')'; + $text .= "| {$function['name']} | `{$functionId}` | {$status} | {$action} |\n"; } - - $text .= "| {$function['name']} | `{$functionId}` | {$status} | {$action} |\n"; } $text .= "\n\n"; - var_dump("project"); - var_dump($project); - foreach ($project['sites'] as $siteId => $site) { - var_dump("entered flow for site"); - var_dump($site); - if ($site['status'] === 'waiting' || $site['status'] === 'processing' || $site['status'] === 'building') { - $text .= "**Your site deployment is in progress. Please check back in a few minutes for the updated status.**\n\n"; - } elseif ($site['status'] === 'ready') { - $text .= "**Your site has been successfully deployed.**\n\n"; - } else { - $text .= "**Your site deployment has failed. Please check the logs for more details and retry.**\n\n"; - } + if(\count($project['site']) > 0) { - $text .= "Project name: **{$project['name']}** \nProject ID: `{$projectId}`\n\n"; - $text .= "| Site | ID | Status | Preview link | Action |\n"; + $text .= "| Site | ID | Status | Previews | Action |\n"; $text .= "| :- | :- | :- | :- | :- |\n"; - $generateImage = function (string $status) use ($protocol, $hostname) { - $extention = $status === 'building' ? 'gif' : 'png'; - $imagesUrl = $protocol . '://' . $hostname . '/console/images/vcs/'; - $imageUrl = '' . $status . ''; + foreach ($project['site'] as $siteId => $site) { + $generateImage = function (string $status) use ($protocol, $hostname) { + $extention = $status === 'building' ? 'gif' : 'png'; + $imagesUrl = $protocol . '://' . $hostname . '/console/images/vcs/'; + $imageUrl = '' . $status . ''; - var_dump("image url"); - var_dump($imageUrl); + return $imageUrl; + }; - return $imageUrl; - }; + $status = match ($site['status']) { + 'waiting' => $generateImage('waiting') . ' Waiting to build', + 'processing' => $generateImage('processing') . ' Processing', + 'building' => $generateImage('building') . ' Building', + 'ready' => $generateImage('ready') . ' Ready', + 'failed' => $generateImage('failed') . ' Failed', + }; - $status = match ($site['status']) { - 'waiting' => $generateImage('waiting') . ' Waiting to build', - 'processing' => $generateImage('processing') . ' Processing', - 'building' => $generateImage('building') . ' Building', - 'ready' => $generateImage('ready') . ' Ready', - 'failed' => $generateImage('failed') . ' Failed', - }; + if ($site['action']['type'] === 'logs') { + $action = '[View Logs](' . $protocol . '://' . $hostname . '/console/project-' . $projectId . '/sites/site-' . $siteId . '/deployment-' . $site['deploymentId'] . ')'; + } else { + $action = '[Authorize](' . $site['action']['url'] . ')'; + } - if ($site['action']['type'] === 'logs') { - $action = '[View Logs](' . $protocol . '://' . $hostname . '/console/project-' . $projectId . '/sites/site-' . $siteId . '/deployment-' . $site['deploymentId'] . ')'; - } else { - $action = '[Authorize](' . $site['action']['url'] . ')'; + $previews = '[Preview URL](' . $site['previewUrl'] . ') | [QR Code](' . $site['previewQrCode'] . ')'; + + $text .= "| {$site['name']} | `{$siteId}` | {$status} | {$previews} | {$action} |\n"; } - - $text .= "| {$site['name']} | `{$siteId}` | {$status} | 'preview link' | {$action} |\n"; } $text .= "\n\n"; } - $sitesUrl = $protocol . '://' . $hostname . '/console/project-' . $projectId . '/sites/site-' . $siteId; - $text .= "Only deployments on the production branch are activated automatically. If you'd like to activate this deployment, navigate to [your deployments]($sitesUrl). Learn more about Appwrite [Site deployments](https://appwrite.io/docs/functions).\n\n"; + + $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"; $tip = $this->tips[array_rand($this->tips)]; $text .= "> **💡 Did you know?** \n " . $tip . "\n\n"; From 9b4de5d7bf791feae6d7616e4e8bcfd213abf0ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 28 Oct 2024 14:39:12 +0100 Subject: [PATCH 3/7] Fix comments --- .../Modules/Functions/Workers/Builds.php | 14 +++- src/Appwrite/Vcs/Comment.php | 83 ++++++++++--------- 2 files changed, 56 insertions(+), 41 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 11eacb5ec3..43e5bbe876 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -955,9 +955,21 @@ class Builds extends Action default => throw new \Exception('Invalid resource type') }; + $previewUrl = match($resource->getCollection()) { + 'functions' => '', + 'sites' => $deployment->getAttribute('domain', ''), + default => throw new \Exception('Invalid resource type') + }; + + $previweQrCode = match($resource->getCollection()) { + 'functions' => '', + 'sites' => 'https://cloud.appwrite.io/v1/avatars/qr?text=' . $previewUrl, + default => throw new \Exception('Invalid resource type') + }; + $comment = new Comment(); $comment->parseComment($github->getComment($owner, $repositoryName, $commentId)); - $comment->addBuild($project, $resource, $resourceType, $status, $deployment->getId(), ['type' => 'logs']); + $comment->addBuild($project, $resource, $resourceType, $status, $deployment->getId(), ['type' => 'logs'], $previewUrl, $previweQrCode); $github->updateComment($owner, $repositoryName, $commentId, $comment->generateComment()); } finally { $dbForConsole->deleteDocument('vcsCommentLocks', $commentId); diff --git a/src/Appwrite/Vcs/Comment.php b/src/Appwrite/Vcs/Comment.php index f896fdefc9..f363ad4aed 100644 --- a/src/Appwrite/Vcs/Comment.php +++ b/src/Appwrite/Vcs/Comment.php @@ -27,7 +27,7 @@ class Comment return \count($this->builds) === 0; } - public function addBuild(Document $project, Document $resource, string $resourceType, string $buildStatus, string $deploymentId, array $action): void + public function addBuild(Document $project, Document $resource, string $resourceType, string $buildStatus, string $deploymentId, array $action, string $previewUrl, string $previewQrCode): void { // Unique index $id = $project->getId() . '_' . $resource->getId(); @@ -41,6 +41,8 @@ class Comment 'buildStatus' => $buildStatus, 'deploymentId' => $deploymentId, 'action' => $action, + 'previewQrCode' => $previewQrCode, + 'previewUrl' => $previewUrl, ]; } @@ -67,8 +69,8 @@ class Comment 'status' => $build['buildStatus'], 'deploymentId' => $build['deploymentId'], 'action' => $build['action'], - 'previewUrl' => 'http://google.com', - 'previewQrCode' => 'https://cloud.appwrite.io/v1/avatars/qr?text=https://www.google.com/' + 'previewUrl' => $build['$previewUrl'], + 'previewQrCode' => $build['previewQrCode'] ]; } else if($build['resourceType'] === 'function') { $projects[$build['projectId']]['function'][$build['resourceId']] = [ @@ -86,6 +88,42 @@ class Comment $text .= "Project name: **{$project['name']}** \nProject ID: `{$projectId}`\n\n"; + if(\count($project['site']) > 0) { + + $text .= "| Site | ID | Status | Previews | Action |\n"; + $text .= "| :- | :- | :- | :- | :- |\n"; + + foreach ($project['site'] as $siteId => $site) { + $generateImage = function (string $status) use ($protocol, $hostname) { + $extention = $status === 'building' ? 'gif' : 'png'; + $imagesUrl = $protocol . '://' . $hostname . '/console/images/vcs/'; + $imageUrl = '' . $status . ''; + + return $imageUrl; + }; + + $status = match ($site['status']) { + 'waiting' => $generateImage('waiting') . ' Waiting to build', + 'processing' => $generateImage('processing') . ' Processing', + 'building' => $generateImage('building') . ' Building', + 'ready' => $generateImage('ready') . ' Ready', + 'failed' => $generateImage('failed') . ' Failed', + }; + + if ($site['action']['type'] === 'logs') { + $action = '[View Logs](' . $protocol . '://' . $hostname . '/console/project-' . $projectId . '/sites/site-' . $siteId . '/deployment-' . $site['deploymentId'] . ')'; + } else { + $action = '[Authorize](' . $site['action']['url'] . ')'; + } + + $previews = '[Preview URL](' . $site['previewUrl'] . ') [QR Code](' . $site['previewQrCode'] . ')'; + + $text .= "| {$site['name']} | `{$siteId}` | {$status} | {$previews} | {$action} |\n"; + } + + $text .= "\n\n"; + } + if(\count($project['function']) > 0) { $text .= "| Function | ID | Status | Action |\n"; @@ -115,45 +153,10 @@ class Comment $text .= "| {$function['name']} | `{$functionId}` | {$status} | {$action} |\n"; } + + $text .= "\n\n"; } - $text .= "\n\n"; - - if(\count($project['site']) > 0) { - - $text .= "| Site | ID | Status | Previews | Action |\n"; - $text .= "| :- | :- | :- | :- | :- |\n"; - - foreach ($project['site'] as $siteId => $site) { - $generateImage = function (string $status) use ($protocol, $hostname) { - $extention = $status === 'building' ? 'gif' : 'png'; - $imagesUrl = $protocol . '://' . $hostname . '/console/images/vcs/'; - $imageUrl = '' . $status . ''; - - return $imageUrl; - }; - - $status = match ($site['status']) { - 'waiting' => $generateImage('waiting') . ' Waiting to build', - 'processing' => $generateImage('processing') . ' Processing', - 'building' => $generateImage('building') . ' Building', - 'ready' => $generateImage('ready') . ' Ready', - 'failed' => $generateImage('failed') . ' Failed', - }; - - if ($site['action']['type'] === 'logs') { - $action = '[View Logs](' . $protocol . '://' . $hostname . '/console/project-' . $projectId . '/sites/site-' . $siteId . '/deployment-' . $site['deploymentId'] . ')'; - } else { - $action = '[Authorize](' . $site['action']['url'] . ')'; - } - - $previews = '[Preview URL](' . $site['previewUrl'] . ') | [QR Code](' . $site['previewQrCode'] . ')'; - - $text .= "| {$site['name']} | `{$siteId}` | {$status} | {$previews} | {$action} |\n"; - } - } - - $text .= "\n\n"; } $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"; From c20e580a9ecc19931be7b5e511417f996f34795d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 28 Oct 2024 14:45:18 +0100 Subject: [PATCH 4/7] Fix missing params in VCS comment flow --- app/controllers/api/vcs.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/vcs.php b/app/controllers/api/vcs.php index d987bec826..b7abd61716 100644 --- a/app/controllers/api/vcs.php +++ b/app/controllers/api/vcs.php @@ -113,14 +113,15 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId if ($latestComment !== false && !$latestComment->isEmpty()) { $latestCommentId = $latestComment->getAttribute('providerCommentId', ''); + $comment = new Comment(); $comment->parseComment($github->getComment($owner, $repositoryName, $latestCommentId)); - $comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action); + $comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action, '', ''); $latestCommentId = \strval($github->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment())); } else { $comment = new Comment(); - $comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action); + $comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action, '', ''); $latestCommentId = \strval($github->createComment($owner, $repositoryName, $providerPullRequestId, $comment->generateComment())); if (!empty($latestCommentId)) { @@ -157,7 +158,7 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId $latestCommentId = $comment->getAttribute('providerCommentId', ''); $comment = new Comment(); $comment->parseComment($github->getComment($owner, $repositoryName, $latestCommentId)); - $comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action); + $comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action, '', ''); $latestCommentId = \strval($github->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment())); } From 45dc9a39d9168b43ec47d7a10d1f0722429ff2d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 28 Oct 2024 14:53:06 +0100 Subject: [PATCH 5/7] Get preview URL --- .../Platform/Modules/Functions/Workers/Builds.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index bd33503e69..448153ae6a 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -22,6 +22,7 @@ use Utopia\Database\Exception\Conflict; use Utopia\Database\Exception\Restricted; use Utopia\Database\Exception\Structure; use Utopia\Database\Helpers\ID; +use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; use Utopia\Logger\Log; use Utopia\Platform\Action; @@ -982,9 +983,15 @@ class Builds extends Action default => throw new \Exception('Invalid resource type') }; + $rule = Authorization::skip(fn () => $dbForConsole->findOne('rules', [ + Query::equal("projectInternalId", [$project->getInternalId()]), + Query::equal("resourceType", ["deployment"]), + Query::equal("resourceInternalId", [$deployment->getInternalId()]) + ])); + $previewUrl = match($resource->getCollection()) { 'functions' => '', - 'sites' => $deployment->getAttribute('domain', ''), + 'sites' => $rule->getAttribute('domain', ''), default => throw new \Exception('Invalid resource type') }; From dc8b9478ee6a96143258e7a7bb17e73937dddd2c Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Mon, 28 Oct 2024 15:20:36 +0100 Subject: [PATCH 6/7] Fix builds --- app/controllers/api/vcs.php | 2 +- .../Modules/Functions/Workers/Builds.php | 46 +++++++++---------- src/Appwrite/Vcs/Comment.php | 10 ++-- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/app/controllers/api/vcs.php b/app/controllers/api/vcs.php index b7abd61716..32e12a4932 100644 --- a/app/controllers/api/vcs.php +++ b/app/controllers/api/vcs.php @@ -113,7 +113,7 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId if ($latestComment !== false && !$latestComment->isEmpty()) { $latestCommentId = $latestComment->getAttribute('providerCommentId', ''); - + $comment = new Comment(); $comment->parseComment($github->getComment($owner, $repositoryName, $latestCommentId)); $comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action, '', ''); diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 448153ae6a..72390a7668 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -691,28 +691,6 @@ class Builds extends Action $build = $dbForProject->updateDocument('builds', $buildId, $build); - if ($isVcsEnabled) { - $this->runGitAction('ready', $github, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForConsole); - } - - Console::success("Build id: $buildId created"); - - /** Set auto deploy */ - if ($deployment->getAttribute('activate') === true) { - $resource->setAttribute('deploymentInternalId', $deployment->getInternalId()); - $resource->setAttribute('live', true); - switch ($resource->getCollection()) { - case 'functions': - $resource->setAttribute('deployment', $deployment->getId()); - $resource = $dbForProject->updateDocument('functions', $resource->getId(), $resource); - break; - case 'sites': - $resource->setAttribute('deploymentId', $deployment->getId()); - $resource = $dbForProject->updateDocument('sites', $resource->getId(), $resource); - break; - } - } - // Preview deployments for sites if ($resource->getCollection() === 'sites') { $ruleId = ID::unique(); @@ -738,6 +716,28 @@ class Builds extends Action ); } + if ($isVcsEnabled) { + $this->runGitAction('ready', $github, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForConsole); + } + + Console::success("Build id: $buildId created"); + + /** Set auto deploy */ + if ($deployment->getAttribute('activate') === true) { + $resource->setAttribute('deploymentInternalId', $deployment->getInternalId()); + $resource->setAttribute('live', true); + switch ($resource->getCollection()) { + case 'functions': + $resource->setAttribute('deployment', $deployment->getId()); + $resource = $dbForProject->updateDocument('functions', $resource->getId(), $resource); + break; + case 'sites': + $resource->setAttribute('deploymentId', $deployment->getId()); + $resource = $dbForProject->updateDocument('sites', $resource->getId(), $resource); + break; + } + } + if ($dbForProject->getDocument('builds', $buildId)->getAttribute('status') === 'canceled') { Console::info('Build has been canceled'); return; @@ -991,7 +991,7 @@ class Builds extends Action $previewUrl = match($resource->getCollection()) { 'functions' => '', - 'sites' => $rule->getAttribute('domain', ''), + 'sites' => !empty($rule) ? $rule->getAttribute('domain', '') : '', default => throw new \Exception('Invalid resource type') }; diff --git a/src/Appwrite/Vcs/Comment.php b/src/Appwrite/Vcs/Comment.php index f363ad4aed..e1d423d772 100644 --- a/src/Appwrite/Vcs/Comment.php +++ b/src/Appwrite/Vcs/Comment.php @@ -63,16 +63,16 @@ class Comment ]; } - if($build['resourceType'] === 'site') { + if ($build['resourceType'] === 'site') { $projects[$build['projectId']]['site'][$build['resourceId']] = [ 'name' => $build['resourceName'], 'status' => $build['buildStatus'], 'deploymentId' => $build['deploymentId'], 'action' => $build['action'], - 'previewUrl' => $build['$previewUrl'], + 'previewUrl' => $build['previewUrl'], 'previewQrCode' => $build['previewQrCode'] ]; - } else if($build['resourceType'] === 'function') { + } elseif ($build['resourceType'] === 'function') { $projects[$build['projectId']]['function'][$build['resourceId']] = [ 'name' => $build['resourceName'], 'status' => $build['buildStatus'], @@ -88,7 +88,7 @@ class Comment $text .= "Project name: **{$project['name']}** \nProject ID: `{$projectId}`\n\n"; - if(\count($project['site']) > 0) { + if (\count($project['site']) > 0) { $text .= "| Site | ID | Status | Previews | Action |\n"; $text .= "| :- | :- | :- | :- | :- |\n"; @@ -124,7 +124,7 @@ class Comment $text .= "\n\n"; } - if(\count($project['function']) > 0) { + if (\count($project['function']) > 0) { $text .= "| Function | ID | Status | Action |\n"; $text .= "| :- | :- | :- | :- |\n"; From 54b07e6b99c8a661e1fd9558341b1bc5766bb598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 28 Oct 2024 15:24:34 +0100 Subject: [PATCH 7/7] Add protocol --- src/Appwrite/Platform/Modules/Functions/Workers/Builds.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 72390a7668..a528a14c02 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -989,9 +989,10 @@ class Builds extends Action Query::equal("resourceInternalId", [$deployment->getInternalId()]) ])); + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') == 'disabled' ? 'http' : 'https'; $previewUrl = match($resource->getCollection()) { 'functions' => '', - 'sites' => !empty($rule) ? $rule->getAttribute('domain', '') : '', + 'sites' => !empty($rule) ? ("{$protocol}://" . $rule->getAttribute('domain', '')) : '', default => throw new \Exception('Invalid resource type') };