2023-03-27 10:44:31 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2023-05-16 14:11:55 +02:00
|
|
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
2023-03-30 09:47:04 +02:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2023-03-29 12:27:02 +02:00
|
|
|
use Spatie\Activitylog\Models\Activity;
|
2023-05-04 22:29:14 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2023-05-16 14:11:55 +02:00
|
|
|
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;
|
2023-03-29 12:27:02 +02:00
|
|
|
|
2023-03-27 10:44:31 +02:00
|
|
|
class Application extends BaseModel
|
|
|
|
{
|
2023-03-30 11:09:39 +02:00
|
|
|
protected static function booted()
|
|
|
|
{
|
|
|
|
static::created(function ($application) {
|
|
|
|
ApplicationSetting::create([
|
|
|
|
'application_id' => $application->id,
|
|
|
|
]);
|
|
|
|
});
|
2023-04-26 10:02:19 +02:00
|
|
|
static::deleting(function ($application) {
|
|
|
|
$application->settings()->delete();
|
|
|
|
$application->persistentStorages()->delete();
|
|
|
|
});
|
2023-03-30 11:09:39 +02:00
|
|
|
}
|
2023-04-25 14:43:35 +02:00
|
|
|
protected $fillable = [
|
|
|
|
'name',
|
2023-05-09 14:42:10 +02:00
|
|
|
'project_id',
|
2023-04-25 14:43:35 +02:00
|
|
|
'description',
|
|
|
|
'git_repository',
|
|
|
|
'git_branch',
|
2023-05-10 13:05:32 +02:00
|
|
|
'git_full_url',
|
2023-04-25 14:43:35 +02:00
|
|
|
'build_pack',
|
|
|
|
'environment_id',
|
|
|
|
'destination_id',
|
|
|
|
'destination_type',
|
|
|
|
'source_id',
|
|
|
|
'source_type',
|
|
|
|
'ports_mappings',
|
|
|
|
'ports_exposes',
|
2023-04-26 13:01:09 +02:00
|
|
|
'publish_directory',
|
2023-05-10 13:05:32 +02:00
|
|
|
'private_key_id'
|
2023-04-25 14:43:35 +02:00
|
|
|
];
|
2023-05-16 14:11:55 +02:00
|
|
|
|
2023-04-26 13:01:09 +02:00
|
|
|
public function publishDirectory(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
|
|
|
set: fn ($value) => $value ? '/' . ltrim($value, '/') : null,
|
|
|
|
);
|
|
|
|
}
|
2023-05-10 12:22:27 +02:00
|
|
|
public function gitBranchLocation(): Attribute
|
2023-05-08 12:22:45 +02:00
|
|
|
{
|
|
|
|
return Attribute::make(
|
2023-05-10 11:06:54 +02:00
|
|
|
get: function () {
|
|
|
|
if (!is_null($this->source?->html_url) && !is_null($this->git_repository) && !is_null($this->git_branch)) {
|
2023-05-10 12:22:27 +02:00
|
|
|
return "{$this->source->html_url}/{$this->git_repository}/tree/{$this->git_branch}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
public function gitCommits(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
|
|
|
get: function () {
|
|
|
|
if (!is_null($this->source?->html_url) && !is_null($this->git_repository) && !is_null($this->git_branch)) {
|
|
|
|
return "{$this->source->html_url}/{$this->git_repository}/commits/{$this->git_branch}";
|
2023-05-10 11:06:54 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-08 12:22:45 +02:00
|
|
|
);
|
|
|
|
}
|
2023-04-26 13:01:09 +02:00
|
|
|
public function baseDirectory(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
|
|
|
set: fn ($value) => '/' . ltrim($value, '/'),
|
|
|
|
);
|
|
|
|
}
|
2023-04-26 14:29:33 +02:00
|
|
|
public function portsMappings(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
|
|
|
set: fn ($value) => $value === "" ? null : $value,
|
|
|
|
);
|
|
|
|
}
|
2023-04-24 13:25:02 +02:00
|
|
|
public function portsMappingsArray(): Attribute
|
2023-03-30 09:47:04 +02:00
|
|
|
{
|
|
|
|
return Attribute::make(
|
2023-04-24 13:25:02 +02:00
|
|
|
get: fn () =>
|
|
|
|
is_null($this->ports_mappings)
|
2023-03-30 09:47:04 +02:00
|
|
|
? []
|
2023-04-26 14:29:33 +02:00
|
|
|
: explode(',', $this->ports_mappings),
|
2023-03-30 09:47:04 +02:00
|
|
|
|
|
|
|
);
|
|
|
|
}
|
2023-04-24 13:25:02 +02:00
|
|
|
public function portsExposesArray(): Attribute
|
2023-03-30 09:47:04 +02:00
|
|
|
{
|
|
|
|
return Attribute::make(
|
2023-04-24 13:25:02 +02:00
|
|
|
get: fn () =>
|
|
|
|
is_null($this->ports_exposes)
|
2023-03-30 09:47:04 +02:00
|
|
|
? []
|
2023-04-24 13:25:02 +02:00
|
|
|
: explode(',', $this->ports_exposes)
|
2023-03-30 09:47:04 +02:00
|
|
|
);
|
|
|
|
}
|
2023-05-04 22:29:14 +02:00
|
|
|
public function environment_variables(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(EnvironmentVariable::class);
|
|
|
|
}
|
2023-05-05 14:48:40 +02:00
|
|
|
public function runtime_environment_variables(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(EnvironmentVariable::class)->where('key', 'not like', 'NIXPACKS_%');
|
|
|
|
}
|
2023-05-05 09:02:50 +02:00
|
|
|
public function build_environment_variables(): HasMany
|
|
|
|
{
|
2023-05-05 14:48:40 +02:00
|
|
|
return $this->hasMany(EnvironmentVariable::class)->where('is_build_time', true)->where('key', 'not like', 'NIXPACKS_%');
|
2023-05-05 09:02:50 +02:00
|
|
|
}
|
2023-05-05 10:51:58 +02:00
|
|
|
public function nixpacks_environment_variables(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(EnvironmentVariable::class)->where('key', 'like', 'NIXPACKS_%');
|
|
|
|
}
|
2023-05-10 13:05:32 +02:00
|
|
|
public function private_key()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(PrivateKey::class);
|
|
|
|
}
|
2023-04-26 14:29:33 +02:00
|
|
|
public function environment()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Environment::class);
|
|
|
|
}
|
2023-05-30 15:52:17 +02:00
|
|
|
public function previews()
|
|
|
|
{
|
|
|
|
return $this->hasMany(ApplicationPreview::class);
|
|
|
|
}
|
2023-04-26 14:29:33 +02:00
|
|
|
public function settings()
|
|
|
|
{
|
|
|
|
return $this->hasOne(ApplicationSetting::class);
|
|
|
|
}
|
|
|
|
public function destination()
|
|
|
|
{
|
|
|
|
return $this->morphTo();
|
|
|
|
}
|
|
|
|
public function source()
|
|
|
|
{
|
|
|
|
return $this->morphTo();
|
|
|
|
}
|
|
|
|
public function persistentStorages()
|
|
|
|
{
|
|
|
|
return $this->morphMany(LocalPersistentVolume::class, 'resource');
|
|
|
|
}
|
|
|
|
|
2023-05-31 14:24:20 +02:00
|
|
|
public function deployments(int $skip = 0, int $take = 10)
|
2023-03-29 12:52:22 +02:00
|
|
|
{
|
2023-05-31 14:24:20 +02:00
|
|
|
ray('Skip ' . $skip . ' Take ' . $take);
|
|
|
|
return ApplicationDeploymentQueue::where('application_id', $this->id)->orderBy('created_at', 'desc')->skip($skip)->take($take)->get();
|
2023-03-29 12:52:22 +02:00
|
|
|
}
|
2023-03-29 12:27:02 +02:00
|
|
|
public function get_deployment(string $deployment_uuid)
|
2023-03-28 15:47:37 +02:00
|
|
|
{
|
2023-05-03 08:51:03 +02:00
|
|
|
return Activity::where('subject_id', $this->id)->where('properties->type_uuid', '=', $deployment_uuid)->first();
|
2023-03-28 15:47:37 +02:00
|
|
|
}
|
2023-05-10 11:43:49 +02:00
|
|
|
public function isDeployable(): bool
|
|
|
|
{
|
2023-05-31 11:24:02 +02:00
|
|
|
if ($this->settings->is_auto_deploy_enabled) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
public function isPRDeployable(): bool
|
|
|
|
{
|
|
|
|
if ($this->settings->is_preview_deployments_enabled) {
|
2023-05-10 11:43:49 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-05-10 13:05:32 +02:00
|
|
|
public function deploymentType()
|
|
|
|
{
|
|
|
|
if (data_get($this, 'source')) {
|
|
|
|
return 'source';
|
|
|
|
}
|
|
|
|
if (data_get($this, 'private_key_id')) {
|
|
|
|
return 'deploy_key';
|
|
|
|
}
|
|
|
|
throw new \Exception('No deployment type found');
|
|
|
|
}
|
2023-03-27 10:44:31 +02:00
|
|
|
}
|