52 lines
1.5 KiB
PHP
52 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Applications;
|
|
|
|
use App\Models\Application;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Process;
|
|
use RuntimeException;
|
|
|
|
class VerifyRepositoryAccess
|
|
{
|
|
public function execute(Application $application): bool
|
|
{
|
|
if (! $application->deploy_key_private) {
|
|
throw new RuntimeException('Application does not have a deploy key.');
|
|
}
|
|
|
|
$directory = storage_path('app/private/operations/repository-access-'.$application->id.'-'.str()->random(8));
|
|
$keyPath = $directory.'/deploy_key';
|
|
|
|
File::ensureDirectoryExists($directory, 0700);
|
|
File::put($keyPath, $application->deploy_key_private);
|
|
File::chmod($keyPath, 0600);
|
|
|
|
try {
|
|
$result = Process::path($directory)
|
|
->env([
|
|
'GIT_SSH_COMMAND' => 'ssh -i '.$keyPath.' -o IdentitiesOnly=yes -o StrictHostKeyChecking=no',
|
|
])
|
|
->run([
|
|
'git',
|
|
'ls-remote',
|
|
'--heads',
|
|
$application->repository_url,
|
|
$application->default_branch,
|
|
]);
|
|
|
|
if ($result->successful()) {
|
|
$application->forceFill([
|
|
'deploy_key_installed_at' => now(),
|
|
])->save();
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
} finally {
|
|
rescue(fn () => File::deleteDirectory($directory), report: false);
|
|
}
|
|
}
|
|
}
|