Files
coolify/app/Traits/IncludesDeploymentCommitDetails.php
T
22a8c09dd6 Next (#12202)
Co-authored-by: Aditya Tripathi <aditya@climactic.co>
Co-authored-by: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: rosslh <ross@rosshill.ca>
Co-authored-by: Abdulfetah Suudi <suudiabdulfetah@gmail.com>
Co-authored-by: Dharmveer <51308779+dharmveer97@users.noreply.github.com>
Co-authored-by: Maxwell <136101+mxswd@users.noreply.github.com>
2026-10-19 10:07:23 +02:00

80 lines
2.9 KiB
PHP

<?php
namespace App\Traits;
use App\Models\Application;
use App\Models\ApplicationDeploymentQueue;
use App\Models\ApplicationPreview;
use Illuminate\Support\Str;
trait IncludesDeploymentCommitDetails
{
public ?string $commit_sha = null;
public ?string $commit_message = null;
public ?string $commit_url = null;
public ?string $commit_branch = null;
/** Store plain strings from the job's in-memory deployment, so no query runs here. */
protected function resolveDeploymentCommitDetails(Application $application, ?ApplicationDeploymentQueue $deployment, ?ApplicationPreview $preview = null): void
{
if (! $deployment) {
return;
}
$commit = trim((string) $deployment->commit);
if (preg_match('/^[0-9a-f]{7,64}$/i', $commit) === 1) {
$this->commit_sha = $commit;
$this->commit_url = $application->gitCommitLink($commit);
}
$message = trim(mb_scrub((string) $deployment->commitMessage(), 'UTF-8'));
if ($message !== '') {
$this->commit_message = Str::limit($message, 1000);
}
if (is_null($preview) && filled($application->git_branch)) {
$this->commit_branch = $application->git_branch;
}
}
/** @return array{sha: string|null, short_sha: string|null, url: string|null, branch: string|null, message: string|null}|null */
protected function deploymentCommitDetailsForMail(?object $notifiable): ?array
{
$enabled = (bool) data_get($notifiable, 'emailNotificationSettings.deployment_commit_details_email_notifications', false);
if (! $enabled || (is_null($this->commit_sha) && is_null($this->commit_message))) {
return null;
}
return [
'sha' => $this->commit_sha,
'short_sha' => $this->commit_sha ? substr($this->commit_sha, 0, 7) : null,
'url' => $this->commit_url,
'branch' => $this->commit_branch ? self::escapeMailMarkdown($this->commit_branch) : null,
'message' => $this->commit_message ? self::formatCommitMessageForMail($this->commit_message) : null,
];
}
/** Escape Markdown in user-controlled text; Blade escapes the HTML. */
private static function escapeMailMarkdown(string $text): string
{
return preg_replace('/([\\\\`*_{}\[\]()#+\-.!|~=])/u', '\\\\$1', $text);
}
private static function formatCommitMessageForMail(string $message): string
{
$paragraphs = preg_split('/\R\s*\R/u', str_replace("\r\n", "\n", $message));
return collect($paragraphs)
->map(fn (string $paragraph): string => collect(preg_split('/\R/u', $paragraph))
->map(fn (string $line): string => self::escapeMailMarkdown(trim($line)))
->filter(fn (string $line): bool => $line !== '')
->implode("\\\n"))
->filter(fn (string $paragraph): bool => $paragraph !== '')
->implode("\n\n");
}
}