diff --git a/web/Modules/Customer/App/Filament/Pages/CloneGitRepository.php b/web/Modules/Customer/App/Filament/Pages/CloneGitRepository.php index 99fd227..5fa6fe8 100644 --- a/web/Modules/Customer/App/Filament/Pages/CloneGitRepository.php +++ b/web/Modules/Customer/App/Filament/Pages/CloneGitRepository.php @@ -28,11 +28,7 @@ class CloneGitRepository extends Page protected static bool $shouldRegisterNavigation = false; - public $state = [ - 'url' => '', - 'name' => '', - 'git_ssh_key_id' => '', - ]; + public $state = []; public $repositoryDetails = []; public function getTitle(): string @@ -66,6 +62,8 @@ class CloneGitRepository extends Page if (isset($repoDetails['name'])) { $set('name', $repoDetails['owner'] .'/'. $repoDetails['name']); $this->repositoryDetails = $repoDetails; + + $set('dir', $repoDetails['name']); } }) ->placeholder('Enter the URL of the repository'), @@ -137,7 +135,6 @@ class CloneGitRepository extends Page ->label('Directory') ->columnSpanFull() ->required() - ->default('public_html/') ->placeholder('Enter the directory to clone the repository'), ]), @@ -164,7 +161,11 @@ class CloneGitRepository extends Page $newGitRepository->dir = $this->state['dir']; $newGitRepository->clone_from = $this->state['clone_from']; $newGitRepository->domain_id = $this->state['domain_id']; - $newGitRepository->git_ssh_key_id = (int) $this->state['git_ssh_key_id']; + + if (isset($this->state['git_ssh_key_id'])) { + $newGitRepository->git_ssh_key_id = $this->state['git_ssh_key_id']; + } + $newGitRepository->status = GitRepository::STATUS_PENDING; $newGitRepository->save(); diff --git a/web/Modules/Customer/App/Filament/Resources/GitRepositoryResource.php b/web/Modules/Customer/App/Filament/Resources/GitRepositoryResource.php index 9b5b1f3..e067256 100644 --- a/web/Modules/Customer/App/Filament/Resources/GitRepositoryResource.php +++ b/web/Modules/Customer/App/Filament/Resources/GitRepositoryResource.php @@ -79,14 +79,15 @@ class GitRepositoryResource extends Resource ->actions([ Tables\Actions\EditAction::make(), Tables\Actions\Action::make('pull') - // ->hidden(fn (GitRepository $record) => $record->status !== 'cloned') + ->hidden(fn (GitRepository $record) => $record->status !== 'cloned') ->icon('heroicon-o-arrow-down-tray') ->action(function (GitRepository $record) { $gitRepository = GitRepository::find($record->id); - $gitRepository->pull(); + $gitRepository->clone(); - }) + }), + Tables\Actions\DeleteAction::make(), ]) ->bulkActions([ // Tables\Actions\BulkActionGroup::make([ diff --git a/web/app/Models/GitRepository.php b/web/app/Models/GitRepository.php index 01eab17..8446547 100644 --- a/web/app/Models/GitRepository.php +++ b/web/app/Models/GitRepository.php @@ -3,6 +3,7 @@ namespace App\Models; use App\GitClient; +use App\ShellApi; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use function Psy\sh; @@ -36,7 +37,21 @@ class GitRepository extends Model 'domain_id', 'git_ssh_key_id', ]; + public static function boot() + { + parent::boot(); + static::created(function ($model) { + $model->clone(); + }); + + static::deleting(function ($model) { + $projectDir = $model->domain->domain_root . '/' . $model->dir; + ShellApi::safeDelete($projectDir,[ + $model->domain->domain_root . '/', + ]); + }); + } public function domain() { @@ -108,18 +123,20 @@ class GitRepository extends Model } $shellCommand = []; + $shellCommand[] = 'echo "Cloning started at $(date)"'; + $shellCommand[] = 'export HOME=/home/'.$findHostingSubscription->system_username; +// $shellCommand[] = 'cd '.$findDomain->domain_root; if ($gitSSHKey) { $cloneUrl = 'git@'.$gitSSHUrl['provider'].':'.$gitSSHUrl['owner'].'/'.$gitSSHUrl['name'].'.git'; $shellCommand[] = 'git -c core.sshCommand="ssh -i '.$privateKeyFile .'" clone '.$cloneUrl.' '.$projectDir . ' 2>&1'; } else { - $shellCommand[] = 'git clone '.$this->url.' '.$projectDir . ' 2>&1'; + $gitCloneCommand = 'git clone '.$this->url.' '.$projectDir . ' 2>&1'; + $shellCommand[] = 'su -m '.$findHostingSubscription->system_username.' -c "'.$gitCloneCommand.'"'; } $shellCommand[] = 'phyre-php /usr/local/phyre/web/artisan git-repository:mark-as-cloned '.$this->id; - $shellCommand[] = 'echo "Cloning completed at $(date)"'; - $shellContent = ''; foreach ($shellCommand as $command) { $shellContent .= $command . "\n";