Add application API and OpenAPI docs
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Repositories;
|
||||
|
||||
use App\Actions\Git\Data\GitRepository;
|
||||
use App\Actions\Git\GitProviderManager;
|
||||
use App\Models\User;
|
||||
use App\Models\UserGitConnection;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class ResolveRepositorySelectionAttributes
|
||||
{
|
||||
public function __construct(
|
||||
private readonly GitProviderManager $gitProviderManager,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* base_url: string,
|
||||
* owner_name: string,
|
||||
* provider: mixed,
|
||||
* provider_repository_id: string,
|
||||
* repository_full_name: string,
|
||||
* repository_name: string,
|
||||
* repository_url: string
|
||||
* }
|
||||
*/
|
||||
public function handle(User $user, int $userGitConnectionId, string $providerRepositoryId): array
|
||||
{
|
||||
$userGitConnection = $this->ownedUserGitConnection($user, $userGitConnectionId);
|
||||
$repository = $this->repositoryFromConnection($userGitConnection, $providerRepositoryId);
|
||||
|
||||
if ($repository === null) {
|
||||
throw ValidationException::withMessages([
|
||||
'provider_repository_id' => 'The selected repository is not available through this Git connection.',
|
||||
]);
|
||||
}
|
||||
|
||||
return [
|
||||
'base_url' => $userGitConnection->base_url,
|
||||
'owner_name' => $repository->owner,
|
||||
'provider' => $userGitConnection->provider,
|
||||
'provider_repository_id' => $repository->providerRepositoryId,
|
||||
'repository_full_name' => $repository->fullName,
|
||||
'repository_name' => $repository->name,
|
||||
'repository_url' => $repository->htmlUrl,
|
||||
];
|
||||
}
|
||||
|
||||
private function ownedUserGitConnection(User $user, int $userGitConnectionId): UserGitConnection
|
||||
{
|
||||
$userGitConnection = UserGitConnection::query()->findOrFail($userGitConnectionId);
|
||||
|
||||
abort_unless($userGitConnection->user_id === $user->id, 403);
|
||||
|
||||
return $userGitConnection;
|
||||
}
|
||||
|
||||
private function repositoryFromConnection(
|
||||
UserGitConnection $userGitConnection,
|
||||
string $providerRepositoryId,
|
||||
): ?GitRepository {
|
||||
return collect(
|
||||
$this->gitProviderManager
|
||||
->client($userGitConnection->provider)
|
||||
->listRepositories($userGitConnection->base_url, $userGitConnection->api_token),
|
||||
)
|
||||
->first(fn (GitRepository $repository): bool => $repository->providerRepositoryId === $providerRepositoryId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user