2023-04-19 10:42:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Project\Application;
|
2023-04-19 10:42:15 +00:00
|
|
|
|
|
|
|
|
use App\Models\Application;
|
2023-06-12 12:38:32 +00:00
|
|
|
use App\Models\PrivateKey;
|
2025-08-22 14:47:59 +00:00
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
2024-11-03 23:14:44 +00:00
|
|
|
use Livewire\Attributes\Locked;
|
2024-11-05 08:36:40 +00:00
|
|
|
use Livewire\Attributes\Validate;
|
2023-04-19 10:42:15 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
|
|
class Source extends Component
|
|
|
|
|
{
|
2025-08-22 14:47:59 +00:00
|
|
|
use AuthorizesRequests;
|
|
|
|
|
|
2023-04-19 10:42:15 +00:00
|
|
|
public Application $application;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-11-03 23:14:44 +00:00
|
|
|
#[Locked]
|
|
|
|
|
public $privateKeys;
|
|
|
|
|
|
2024-11-05 08:36:40 +00:00
|
|
|
#[Validate(['nullable', 'string'])]
|
2024-11-03 23:14:44 +00:00
|
|
|
public ?string $privateKeyName = null;
|
|
|
|
|
|
2024-11-05 08:36:40 +00:00
|
|
|
#[Validate(['nullable', 'integer'])]
|
2024-11-03 23:14:44 +00:00
|
|
|
public ?int $privateKeyId = null;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-11-05 08:36:40 +00:00
|
|
|
#[Validate(['required', 'string'])]
|
2024-11-03 23:14:44 +00:00
|
|
|
public string $gitRepository;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-11-05 08:36:40 +00:00
|
|
|
#[Validate(['required', 'string'])]
|
2024-11-03 23:14:44 +00:00
|
|
|
public string $gitBranch;
|
|
|
|
|
|
2026-03-10 21:22:48 +00:00
|
|
|
#[Validate(['nullable', 'string', 'regex:/^[a-zA-Z0-9][a-zA-Z0-9._\-\/]*$/'])]
|
2024-11-03 23:14:44 +00:00
|
|
|
public ?string $gitCommitSha = null;
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2025-04-11 16:22:42 +00:00
|
|
|
#[Locked]
|
|
|
|
|
public $sources;
|
|
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
public function mount()
|
|
|
|
|
{
|
2024-11-03 23:14:44 +00:00
|
|
|
try {
|
|
|
|
|
$this->syncData();
|
|
|
|
|
$this->getPrivateKeys();
|
2025-04-11 16:22:42 +00:00
|
|
|
$this->getSources();
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2024-11-03 23:14:44 +00:00
|
|
|
handleError($e, $this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 10:33:40 +00:00
|
|
|
public function updatedGitRepository()
|
|
|
|
|
{
|
|
|
|
|
$this->gitRepository = trim($this->gitRepository);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function updatedGitBranch()
|
|
|
|
|
{
|
|
|
|
|
$this->gitBranch = trim($this->gitBranch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function updatedGitCommitSha()
|
|
|
|
|
{
|
|
|
|
|
$this->gitCommitSha = trim($this->gitCommitSha);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-03 23:14:44 +00:00
|
|
|
public function syncData(bool $toModel = false)
|
|
|
|
|
{
|
|
|
|
|
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,
|
|
|
|
|
]);
|
2025-09-30 10:33:40 +00:00
|
|
|
// Refresh to get the trimmed values from the model
|
|
|
|
|
$this->application->refresh();
|
|
|
|
|
$this->syncData(false);
|
2024-11-03 23:14:44 +00:00
|
|
|
} 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');
|
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-03 23:14:44 +00:00
|
|
|
private function getPrivateKeys()
|
2023-06-12 12:38:32 +00:00
|
|
|
{
|
2024-11-03 23:14:44 +00:00
|
|
|
$this->privateKeys = PrivateKey::whereTeamId(currentTeam()->id)->get()->reject(function ($key) {
|
|
|
|
|
return $key->id == $this->privateKeyId;
|
2023-06-12 12:38:32 +00:00
|
|
|
});
|
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2025-04-11 16:22:42 +00:00
|
|
|
private function getSources()
|
|
|
|
|
{
|
|
|
|
|
// filter the current source out
|
|
|
|
|
$this->sources = currentTeam()->sources()->whereNotNull('app_id')->reject(function ($source) {
|
|
|
|
|
return $source->id === $this->application->source_id;
|
2025-04-11 18:25:25 +00:00
|
|
|
})->sortBy('name');
|
2025-04-11 16:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-03 23:14:44 +00:00
|
|
|
public function setPrivateKey(int $privateKeyId)
|
2023-06-12 12:38:32 +00:00
|
|
|
{
|
2024-11-03 23:14:44 +00:00
|
|
|
try {
|
2025-08-22 14:47:59 +00:00
|
|
|
$this->authorize('update', $this->application);
|
2024-11-03 23:14:44 +00:00
|
|
|
$this->privateKeyId = $privateKeyId;
|
|
|
|
|
$this->syncData(true);
|
|
|
|
|
$this->getPrivateKeys();
|
|
|
|
|
$this->application->refresh();
|
|
|
|
|
$this->privateKeyName = $this->application->private_key->name;
|
|
|
|
|
$this->dispatch('success', 'Private key updated!');
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2024-11-03 23:14:44 +00:00
|
|
|
return handleError($e, $this);
|
|
|
|
|
}
|
2023-06-12 12:38:32 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-05-10 10:22:27 +00:00
|
|
|
public function submit()
|
|
|
|
|
{
|
2025-08-22 14:47:59 +00:00
|
|
|
|
2024-11-03 23:14:44 +00:00
|
|
|
try {
|
2025-08-22 14:47:59 +00:00
|
|
|
$this->authorize('update', $this->application);
|
2024-11-03 23:14:44 +00:00
|
|
|
if (str($this->gitCommitSha)->isEmpty()) {
|
|
|
|
|
$this->gitCommitSha = 'HEAD';
|
|
|
|
|
}
|
|
|
|
|
$this->syncData(true);
|
|
|
|
|
$this->dispatch('success', 'Application source updated!');
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2024-11-03 23:14:44 +00:00
|
|
|
return handleError($e, $this);
|
2023-05-10 10:22:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-04-11 16:22:42 +00:00
|
|
|
|
|
|
|
|
public function changeSource($sourceId, $sourceType)
|
|
|
|
|
{
|
2025-08-22 14:47:59 +00:00
|
|
|
|
2025-04-11 16:22:42 +00:00
|
|
|
try {
|
2025-08-22 14:47:59 +00:00
|
|
|
$this->authorize('update', $this->application);
|
2025-04-11 16:22:42 +00:00
|
|
|
$this->application->update([
|
|
|
|
|
'source_id' => $sourceId,
|
|
|
|
|
'source_type' => $sourceType,
|
|
|
|
|
]);
|
2025-06-21 17:28:38 +00:00
|
|
|
|
|
|
|
|
['repository' => $customRepository] = $this->application->customRepository();
|
|
|
|
|
$repository = githubApi($this->application->source, "repos/{$customRepository}");
|
|
|
|
|
$data = data_get($repository, 'data');
|
|
|
|
|
$repository_project_id = data_get($data, 'id');
|
|
|
|
|
if (isset($repository_project_id)) {
|
|
|
|
|
if ($this->application->repository_project_id !== $repository_project_id) {
|
|
|
|
|
$this->application->repository_project_id = $repository_project_id;
|
|
|
|
|
$this->application->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-11 16:22:42 +00:00
|
|
|
$this->application->refresh();
|
2025-04-11 18:25:25 +00:00
|
|
|
$this->getSources();
|
2025-04-11 16:22:42 +00:00
|
|
|
$this->dispatch('success', 'Source updated!');
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return handleError($e, $this);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-19 10:42:15 +00:00
|
|
|
}
|