2023-06-13 13:01:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
use App\Enums\ProcessStatus;
|
|
|
|
use App\Models\Application;
|
|
|
|
use App\Models\ApplicationPreview;
|
|
|
|
use Illuminate\Bus\Queueable;
|
2023-09-14 08:12:44 +00:00
|
|
|
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
|
2023-06-13 13:01:11 +00:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
2024-06-10 20:43:34 +00:00
|
|
|
class ApplicationPullRequestUpdateJob implements ShouldBeEncrypted, ShouldQueue
|
2023-06-13 13:01:11 +00:00
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
public string $build_logs_url;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-06-13 13:01:11 +00:00
|
|
|
public string $body;
|
|
|
|
|
|
|
|
public function __construct(
|
2024-01-26 17:46:50 +00:00
|
|
|
public Application $application,
|
|
|
|
public ApplicationPreview $preview,
|
|
|
|
public ProcessStatus $status,
|
|
|
|
public ?string $deployment_uuid = null
|
2024-06-19 06:59:46 +00:00
|
|
|
) {}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-06-13 13:01:11 +00:00
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
try {
|
2024-03-01 10:41:22 +00:00
|
|
|
if ($this->application->is_public_repository()) {
|
2024-06-06 10:50:38 +00:00
|
|
|
ray('Public repository. Skipping comment update.');
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-03-01 10:41:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-01-26 17:46:50 +00:00
|
|
|
if ($this->status === ProcessStatus::CLOSED) {
|
|
|
|
$this->delete_comment();
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-01-26 17:46:50 +00:00
|
|
|
return;
|
2024-06-10 20:43:34 +00:00
|
|
|
} elseif ($this->status === ProcessStatus::IN_PROGRESS) {
|
2023-06-13 13:01:11 +00:00
|
|
|
$this->body = "The preview deployment is in progress. 🟡\n\n";
|
2024-06-10 20:43:34 +00:00
|
|
|
} elseif ($this->status === ProcessStatus::FINISHED) {
|
2023-06-13 13:01:11 +00:00
|
|
|
$this->body = "The preview deployment is ready. 🟢\n\n";
|
|
|
|
if ($this->preview->fqdn) {
|
|
|
|
$this->body .= "[Open Preview]({$this->preview->fqdn}) | ";
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
} elseif ($this->status === ProcessStatus::ERROR) {
|
2023-06-13 13:01:11 +00:00
|
|
|
$this->body = "The preview deployment failed. 🔴\n\n";
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
$this->build_logs_url = base_url()."/project/{$this->application->environment->project->uuid}/{$this->application->environment->name}/application/{$this->application->uuid}/deployment/{$this->deployment_uuid}";
|
2024-01-26 17:46:50 +00:00
|
|
|
|
2024-06-10 20:43:34 +00:00
|
|
|
$this->body .= '[Open Build Logs]('.$this->build_logs_url.")\n\n\n";
|
|
|
|
$this->body .= 'Last updated at: '.now()->toDateTimeString().' CET';
|
2023-06-13 13:01:11 +00:00
|
|
|
|
|
|
|
ray('Updating comment', $this->body);
|
|
|
|
if ($this->preview->pull_request_issue_comment_id) {
|
|
|
|
$this->update_comment();
|
|
|
|
} else {
|
|
|
|
$this->create_comment();
|
|
|
|
}
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-06-13 13:01:11 +00:00
|
|
|
ray($e);
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-06-06 10:50:38 +00:00
|
|
|
return $e;
|
2023-06-13 13:01:11 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-06-13 13:01:11 +00:00
|
|
|
private function update_comment()
|
|
|
|
{
|
2023-10-06 11:46:42 +00:00
|
|
|
['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: [
|
2023-06-13 13:01:11 +00:00
|
|
|
'body' => $this->body,
|
|
|
|
], throwError: false);
|
|
|
|
if (data_get($data, 'message') === 'Not Found') {
|
|
|
|
ray('Comment not found. Creating new one.');
|
|
|
|
$this->create_comment();
|
|
|
|
}
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-06-13 13:01:11 +00:00
|
|
|
private function create_comment()
|
|
|
|
{
|
2024-01-26 17:46:50 +00:00
|
|
|
['data' => $data] = githubApi(source: $this->application->source, endpoint: "/repos/{$this->application->git_repository}/issues/{$this->preview->pull_request_id}/comments", method: 'post', data: [
|
2023-06-13 13:01:11 +00:00
|
|
|
'body' => $this->body,
|
|
|
|
]);
|
|
|
|
$this->preview->pull_request_issue_comment_id = $data['id'];
|
|
|
|
$this->preview->save();
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-01-26 17:46:50 +00:00
|
|
|
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');
|
|
|
|
}
|
2023-06-13 13:01:11 +00:00
|
|
|
}
|