2023-03-28 10:09:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2023-05-16 10:17:39 +00:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
|
|
|
2023-03-28 10:09:34 +00:00
|
|
|
class GithubApp extends BaseModel
|
|
|
|
{
|
2023-08-11 20:41:47 +00:00
|
|
|
protected $guarded = [];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-05-16 10:17:39 +00:00
|
|
|
protected $appends = ['type'];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-04-19 12:00:31 +00:00
|
|
|
protected $casts = [
|
|
|
|
'is_public' => 'boolean',
|
2024-06-10 20:43:34 +00:00
|
|
|
'type' => 'string',
|
2023-04-19 12:00:31 +00:00
|
|
|
];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-06-16 10:00:36 +00:00
|
|
|
protected $hidden = [
|
|
|
|
'client_secret',
|
|
|
|
'webhook_secret',
|
|
|
|
];
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2024-06-10 20:43:34 +00:00
|
|
|
public static function public()
|
2023-08-08 09:51:36 +00:00
|
|
|
{
|
2023-08-22 15:44:49 +00:00
|
|
|
return GithubApp::whereTeamId(currentTeam()->id)->whereisPublic(true)->whereNotNull('app_id')->get();
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 20:43:34 +00:00
|
|
|
public static function private()
|
2023-08-08 09:51:36 +00:00
|
|
|
{
|
2023-08-22 15:44:49 +00:00
|
|
|
return GithubApp::whereTeamId(currentTeam()->id)->whereisPublic(false)->whereNotNull('app_id')->get();
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
|
|
|
|
2023-05-10 09:02:59 +00: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) {
|
2024-06-10 20:43:34 +00:00
|
|
|
throw new \Exception('You cannot delete this GitHub App because it is in use by '.$applications_count.' application(s). Delete them first.');
|
2023-05-10 09:02:59 +00:00
|
|
|
}
|
2023-09-29 09:01:40 +00:00
|
|
|
$github_app->privateKey()->delete();
|
2023-05-10 09:02:59 +00:00
|
|
|
});
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-03-28 10:09:34 +00:00
|
|
|
public function applications()
|
|
|
|
{
|
|
|
|
return $this->morphMany(Application::class, 'source');
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-03-30 09:09:39 +00:00
|
|
|
public function privateKey()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(PrivateKey::class);
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-05-16 10:17:39 +00:00
|
|
|
public function type(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
|
|
|
get: function () {
|
|
|
|
if ($this->getMorphClass() === 'App\Models\GithubApp') {
|
|
|
|
return 'github';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2023-03-28 10:09:34 +00:00
|
|
|
}
|