coolify/app/Models/GithubApp.php

61 lines
1.7 KiB
PHP
Raw Normal View History

2023-03-28 12:09:34 +02:00
<?php
namespace App\Models;
2023-05-16 12:17:39 +02:00
use Illuminate\Database\Eloquent\Casts\Attribute;
2023-03-28 12:09:34 +02:00
class GithubApp extends BaseModel
{
2023-06-13 11:04:11 +02:00
protected $fillable = ['name', 'uuid', 'organization', 'api_url', 'html_url', 'custom_user', 'custom_port', 'team_id', 'client_secret', 'webhook_secret'];
2023-05-16 12:17:39 +02:00
protected $appends = ['type'];
2023-04-19 14:00:31 +02:00
protected $casts = [
'is_public' => 'boolean',
2023-05-16 12:17:39 +02:00
'type' => 'string'
2023-04-19 14:00:31 +02:00
];
2023-06-16 12:00:36 +02:00
protected $hidden = [
'client_secret',
'webhook_secret',
];
static public function public()
{
2023-08-11 17:31:53 +02:00
return GithubApp::whereTeamId(auth()->user()->currentTeam()->id)->whereisPublic(true)->whereNotNull('app_id')->get();
}
static public function private()
{
2023-08-11 17:31:53 +02:00
return GithubApp::whereTeamId(auth()->user()->currentTeam()->id)->whereisPublic(false)->whereNotNull('app_id')->get();
}
2023-05-10 11:02:59 +02:00
protected static function booted(): void
{
static::deleting(function (GithubApp $github_app) {
$applications_count = Application::where('source_id', $github_app->id)->count();
if ($applications_count > 0) {
throw new \Exception('You cannot delete this GitHub App because it is in use by ' . $applications_count . ' application(s). Delete them first.');
}
});
}
2023-03-28 12:09:34 +02:00
public function applications()
{
return $this->morphMany(Application::class, 'source');
}
public function privateKey()
{
return $this->belongsTo(PrivateKey::class);
}
2023-05-16 12:17:39 +02:00
public function type(): Attribute
{
return Attribute::make(
get: function () {
if ($this->getMorphClass() === 'App\Models\GithubApp') {
return 'github';
}
},
);
}
2023-03-28 12:09:34 +02:00
}