Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 58 additions & 9 deletions app/Jobs/ApplicationPullRequestUpdateJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,20 @@ public function handle()
match ($this->status) {
ProcessStatus::QUEUED => $this->body = "The preview deployment for **{$serviceName}** is queued. ⏳\n\n",
ProcessStatus::IN_PROGRESS => $this->body = "The preview deployment for **{$serviceName}** is in progress. 🟡\n\n",
ProcessStatus::FINISHED => $this->body = "The preview deployment for **{$serviceName}** is ready. 🟢\n\n".($this->preview->fqdn ? "[Open Preview]({$this->preview->fqdn}) | " : ''),
ProcessStatus::FINISHED => $this->body = "The preview deployment for **{$serviceName}** is ready. 🟢\n\n",
ProcessStatus::ERROR => $this->body = "The preview deployment for **{$serviceName}** failed. 🔴\n\n",
ProcessStatus::KILLED => $this->body = "The preview deployment for **{$serviceName}** was killed. ⚫\n\n",
ProcessStatus::CANCELLED => $this->body = "The preview deployment for **{$serviceName}** was cancelled. 🚫\n\n",
ProcessStatus::CLOSED => '', // Already handled above, but included for completeness
};
$this->build_logs_url = base_url()."/project/{$this->application->environment->project->uuid}/environment/{$this->application->environment->uuid}/application/{$this->application->uuid}/deployment/{$this->deployment_uuid}";

$this->body .= '[Open Build Logs]('.$this->build_logs_url.")\n\n\n";
$this->body .= 'Last updated at: '.now()->toDateTimeString().' CET';
if ($this->status === ProcessStatus::FINISHED) {
$this->append_preview_urls_to_body();
}

$this->build_logs_url = $this->create_build_log_url();
$this->body .= '[Open Build Logs](' . $this->build_logs_url . ")\n\n\n";
$this->body .= 'Last updated at: ' . now()->toDateTimeString() . ' CET';
if ($this->preview->pull_request_issue_comment_id) {
$this->update_comment();
} else {
Expand All @@ -69,25 +73,70 @@ public function handle()

private function update_comment()
{
['data' => $data] = githubApi(source: $this->application->source, endpoint: "/repos/{$this->application->git_repository}/issues/comments/{$this->preview->pull_request_issue_comment_id}", method: 'patch', data: [
['data' => $data] = githubApi(
source: $this->application->source,
endpoint: "/repos/{$this->application->git_repository}/issues/comments/{$this->preview->pull_request_issue_comment_id}",
method: 'patch',
data: [
'body' => $this->body,
], throwError: false);
],
throwError: false
);
if (data_get($data, 'message') === 'Not Found') {
$this->create_comment();
}
}

private function create_comment()
{
['data' => $data] = githubApi(source: $this->application->source, endpoint: "/repos/{$this->application->git_repository}/issues/{$this->preview->pull_request_id}/comments", method: 'post', data: [
['data' => $data] = githubApi(
source: $this->application->source,
endpoint: "/repos/{$this->application->git_repository}/issues/{$this->preview->pull_request_id}/comments",
method: 'post',
data: [
'body' => $this->body,
]);
]
);
$this->preview->pull_request_issue_comment_id = $data['id'];
$this->preview->save();
}

private function delete_comment()
{
githubApi(source: $this->application->source, endpoint: "/repos/{$this->application->git_repository}/issues/comments/{$this->preview->pull_request_issue_comment_id}", method: 'delete');
githubApi(
source: $this->application->source,
endpoint: "/repos/{$this->application->git_repository}/issues/comments/{$this->preview->pull_request_issue_comment_id}",
method: 'delete'
);
}

private function create_build_log_url(): string
{
$baseUrl = base_url();
$projectId = $this->application->environment->project->uuid ?? 'unknown';
$environmentId = $this->application->environment->uuid ?? 'unknown';
$applicationId = $this->application->uuid ?? 'unknown';
$deploymentId = $this->deployment_uuid ?? 'unknown';

return "$baseUrl/project/$projectId/environment/$environmentId/application/$applicationId/deployment/$deploymentId";
}

private function append_preview_urls_to_body(): void
{
if ($this->application->build_pack === 'dockercompose' && $this->preview->docker_compose_domains) {
$dockerComposeDomains = json_decode($this->preview->docker_compose_domains ?? '', true);
foreach ($dockerComposeDomains as $service => $entry) {
if (($domain = $entry['domain'] ?? null) !== null) {
$serviceName = ucfirst($service);
$this->body .= "[Open $serviceName]($domain) | ";
}
}

return;
}

if ($this->preview->fqdn) {
$this->body .= "[Open Preview]({$this->preview->fqdn}) | ";
}
}
}