feat: tags and tag deploy webhooks

This commit is contained in:
Andras Bacsai
2024-02-01 15:38:12 +01:00
parent 44efe0b5e1
commit 6312c0ba84
20 changed files with 394 additions and 98 deletions

View File

@@ -29,7 +29,7 @@ class Index extends Component
}
$this->project = $project;
$this->environment = $environment;
$this->applications = $environment->applications->sortBy('name');
$this->applications = $environment->applications->load(['tags'])->sortBy('name');
$this->applications = $this->applications->map(function ($application) {
if (data_get($application, 'environment.project.uuid')) {
$application->hrefLink = route('project.application.configuration', [

View File

@@ -0,0 +1,62 @@
<?php
namespace App\Livewire\Project\Shared;
use App\Models\Tag;
use Livewire\Component;
class Tags extends Component
{
public $resource = null;
public ?string $new_tag = null;
protected $listeners = [
'refresh' => '$refresh',
];
public function mount()
{
}
public function deleteTag($id, $name)
{
try {
$found_more_tags = Tag::where(['name' => $name, 'team_id' => currentTeam()->id])->first();
$this->resource->tags()->detach($id);
if ($found_more_tags->resources()->get()->count() == 0) {
$found_more_tags->delete();
}
$this->refresh();
} catch (\Exception $e) {
return handleError($e, $this);
}
}
public function refresh()
{
$this->resource->load(['tags']);
$this->new_tag = null;
}
public function submit()
{
try {
$this->validate([
'new_tag' => 'required|string|min:2'
]);
$tags = str($this->new_tag)->trim()->explode(' ');
foreach ($tags as $tag) {
$found = Tag::where(['name' => $tag, 'team_id' => currentTeam()->id])->first();
if (!$found) {
$found = Tag::create([
'name' => $tag,
'team_id' => currentTeam()->id
]);
}
$this->resource->tags()->syncWithoutDetaching($found->id);
}
$this->refresh();
} catch (\Exception $e) {
return handleError($e, $this);
}
}
public function render()
{
return view('livewire.project.shared.tags');
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Livewire\Tags;
use App\Models\Tag;
use Livewire\Component;
class Index extends Component
{
public $tags = [];
public function mount() {
$this->tags = Tag::where('team_id', currentTeam()->id)->get()->unique('name')->sortBy('name');
}
public function render()
{
return view('livewire.tags.index');
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Livewire\Tags;
use App\Models\Tag;
use Livewire\Component;
class Show extends Component
{
public Tag $tag;
public $resources;
public $webhook = null;
public function mount()
{
$tag = Tag::ownedByCurrentTeam()->where('name', request()->tag_name)->first();
if (!$tag) {
return redirect()->route('tags.index');
}
$this->webhook = generatTagDeployWebhook($tag->name);
$this->resources = $tag->resources()->get();
$this->tag = $tag;
}
public function render()
{
return view('livewire.tags.show');
}
}

View File

@@ -211,6 +211,10 @@ class Application extends BaseModel
: explode(',', $this->ports_exposes)
);
}
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
public function team()
{
return data_get($this, 'environment.project.team');

32
app/Models/Tag.php Normal file
View File

@@ -0,0 +1,32 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
class Tag extends BaseModel
{
protected $guarded = [];
public function name(): Attribute
{
return Attribute::make(
get: fn ($value) => strtolower($value),
set: fn ($value) => strtolower($value)
);
}
static public function ownedByCurrentTeam()
{
return Tag::whereTeamId(currentTeam()->id)->orderBy('name');
}
public function applications()
{
return $this->morphedByMany(Application::class, 'taggable');
}
public function resources() {
return $this->applications();
}
}