2023-05-28 11:39:48 +00:00
< ? php
namespace Appwrite\Vcs ;
use Utopia\Database\Document ;
2023-08-03 19:34:04 +00:00
use Utopia\App ;
2023-05-28 11:39:48 +00:00
class Comment
{
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 $function , string $buildStatus , string $deploymentId ) : void
{
// Unique index
$id = $project -> getId () . '_' . $function -> getId ();
$this -> builds [ $id ] = [
'projectName' => $project -> getAttribute ( 'name' ),
'projectId' => $project -> getId (),
'functionName' => $function -> getAttribute ( 'name' ),
'functionId' => $function -> getId (),
'buildStatus' => $buildStatus ,
'deploymentId' => $deploymentId
];
}
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' ],
'functions' => []
];
}
$projects [ $build [ 'projectId' ]][ 'functions' ][ $build [ 'functionId' ]] = [
'name' => $build [ 'functionName' ],
'status' => $build [ 'buildStatus' ],
'deploymentId' => $build [ 'deploymentId' ]
];
}
2023-08-03 19:34:04 +00:00
//TODO: Update link to documentation
$text .= " **Your function has automatically been deployed.** Learn more about Appwrite Function Deployments in our [documentation](https://appwrite.io/docs/functions). \n \n " ;
2023-05-28 11:39:48 +00:00
foreach ( $projects as $projectId => $project ) {
2023-08-03 09:34:08 +00:00
$text .= " ** { $project [ 'name' ] } ** ` { $projectId } ` \n \n " ;
2023-08-05 20:40:53 +00:00
$text .= " | Function | ID | Status | Action | \n " ;
2023-08-03 09:34:08 +00:00
$text .= " | :- | :- | :- | :- | \n " ;
2023-05-28 11:39:48 +00:00
2023-08-03 19:34:04 +00:00
$protocol = App :: getEnv ( '_APP_OPTIONS_FORCE_HTTPS' ) == 'disabled' ? 'http' : 'https' ;
$hostname = App :: getEnv ( '_APP_DOMAIN' );
2023-05-28 11:39:48 +00:00
foreach ( $project [ 'functions' ] as $functionId => $function ) {
$status = match ( $function [ 'status' ]) {
2023-08-03 19:34:04 +00:00
'waiting' => '<img src="' . $protocol . '://' . $hostname . '/state-waiting.png" alt="Waiting" height="25" align="center"> Waiting to build' ,
'processing' => '<img src="' . $protocol . '://' . $hostname . '/animation-building.gif" alt="Processing" height="29" align="center"> Processing' ,
'building' => '<img src="' . $protocol . '://' . $hostname . '/animation-building.gif" alt="Building" height="29" align="center"> Building' ,
2023-08-04 05:59:23 +00:00
'ready' => '<img src="' . $protocol . '://' . $hostname . '/state-success.png" alt="Ready" height="25" align="center"> Ready' ,
'failed' => '<img src="' . $protocol . '://' . $hostname . '/state-failed.png" alt="Failed" height="25" align="center"> Failed' ,
2023-05-28 11:39:48 +00:00
};
2023-08-03 19:34:04 +00:00
//TODO: Update names of images
2023-05-28 11:39:48 +00:00
2023-08-05 20:40:53 +00:00
//TODO: Change View logs to Authorize for external contributors
$logs = '[View Logs](' . $protocol . '://' . $hostname . '/console/project-' . $projectId . '/functions/function-' . $functionId . '/deployment-' . $function [ 'deploymentId' ] . ')' ;
2023-05-28 11:39:48 +00:00
2023-08-03 09:34:08 +00:00
$text .= " | { $function [ 'name' ] } | ` { $functionId } ` | { $status } | { $logs } | \n " ;
2023-05-28 11:39:48 +00:00
}
2023-08-03 19:34:04 +00:00
$text .= " \n \n " ;
2023-05-28 11:39:48 +00:00
}
2023-08-03 19:34:04 +00:00
//TODO: Update did you know section
$text .= " > **💡 Did you know?** \n Appwrite has a discord community with XX members. [Come join us!](https://appwrite.io/discord) \n \n " ;
2023-08-03 09:34:08 +00:00
2023-05-28 11:39:48 +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 ;
}
}