diff --git a/app/Livewire/Project/Application/Source.php b/app/Livewire/Project/Application/Source.php index 426626e55..2d75d91f2 100644 --- a/app/Livewire/Project/Application/Source.php +++ b/app/Livewire/Project/Application/Source.php @@ -4,55 +4,92 @@ namespace App\Livewire\Project\Application; use App\Models\Application; use App\Models\PrivateKey; +use Livewire\Attributes\Locked; +use Livewire\Attributes\Rule; use Livewire\Component; class Source extends Component { - public $applicationId; - public Application $application; - public $private_keys; + #[Locked] + public $privateKeys; - protected $rules = [ - 'application.git_repository' => 'required', - 'application.git_branch' => 'required', - 'application.git_commit_sha' => 'nullable', - ]; + #[Rule(['nullable', 'string'])] + public ?string $privateKeyName = null; - protected $validationAttributes = [ - 'application.git_repository' => 'repository', - 'application.git_branch' => 'branch', - 'application.git_commit_sha' => 'commit sha', - ]; + #[Rule(['nullable', 'integer'])] + public ?int $privateKeyId = null; + + #[Rule(['required', 'string'])] + public string $gitRepository; + + #[Rule(['required', 'string'])] + public string $gitBranch; + + #[Rule(['nullable', 'string'])] + public ?string $gitCommitSha = null; public function mount() { - $this->get_private_keys(); + try { + $this->syncData(); + $this->getPrivateKeys(); + } catch (\Throwable $e) { + handleError($e, $this); + } } - private function get_private_keys() + public function syncData(bool $toModel = false) { - $this->private_keys = PrivateKey::whereTeamId(currentTeam()->id)->get()->reject(function ($key) { - return $key->id == $this->application->private_key_id; + if ($toModel) { + $this->validate(); + $this->application->update([ + 'git_repository' => $this->gitRepository, + 'git_branch' => $this->gitBranch, + 'git_commit_sha' => $this->gitCommitSha, + 'private_key_id' => $this->privateKeyId, + ]); + } else { + $this->gitRepository = $this->application->git_repository; + $this->gitBranch = $this->application->git_branch; + $this->gitCommitSha = $this->application->git_commit_sha; + $this->privateKeyId = $this->application->private_key_id; + $this->privateKeyName = data_get($this->application, 'private_key.name'); + } + } + + private function getPrivateKeys() + { + $this->privateKeys = PrivateKey::whereTeamId(currentTeam()->id)->get()->reject(function ($key) { + return $key->id == $this->privateKeyId; }); } - public function setPrivateKey(int $private_key_id) + public function setPrivateKey(int $privateKeyId) { - $this->application->private_key_id = $private_key_id; - $this->application->save(); - $this->application->refresh(); - $this->get_private_keys(); + try { + $this->privateKeyId = $privateKeyId; + $this->syncData(true); + $this->getPrivateKeys(); + $this->application->refresh(); + $this->privateKeyName = $this->application->private_key->name; + $this->dispatch('success', 'Private key updated!'); + } catch (\Throwable $e) { + return handleError($e, $this); + } } public function submit() { - $this->validate(); - if (! $this->application->git_commit_sha) { - $this->application->git_commit_sha = 'HEAD'; + try { + if (str($this->gitCommitSha)->isEmpty()) { + $this->gitCommitSha = 'HEAD'; + } + $this->syncData(true); + $this->dispatch('success', 'Application source updated!'); + } catch (\Throwable $e) { + return handleError($e, $this); } - $this->application->save(); - $this->dispatch('success', 'Application source updated!'); } } diff --git a/resources/views/livewire/project/application/source.blade.php b/resources/views/livewire/project/application/source.blade.php index 9a47566e5..4095c63e0 100644 --- a/resources/views/livewire/project/application/source.blade.php +++ b/resources/views/livewire/project/application/source.blade.php @@ -27,24 +27,22 @@
- - + +
- +
- @if (data_get($application, 'private_key_id')) + @if ($privateKeyId)

Deploy Key

Currently attached Private Key: {{ data_get($application, 'private_key.name') }} + class="dark:text-warning">{{ $privateKeyName }}

Select another Private Key

- @foreach ($private_keys as $key) + @foreach ($privateKeys as $key) {{ $key->name }} @endforeach