121 lines
4.5 KiB
PHP
121 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Repositories;
|
|
|
|
use App\Actions\Git\Data\GitRepository;
|
|
use App\Actions\Git\GitProviderManager;
|
|
use App\Http\Resources\AvailableGitRepositorySourceResource;
|
|
use App\Http\Resources\ProjectResource;
|
|
use App\Http\Resources\RepositoryConnectionResource;
|
|
use App\Models\Project;
|
|
use App\Models\RepositoryConnection;
|
|
use App\Models\User;
|
|
use App\Models\UserGitConnection;
|
|
use Illuminate\Support\Collection;
|
|
use Throwable;
|
|
|
|
class BuildProjectRepositorySettingsPayload
|
|
{
|
|
public function __construct(
|
|
private readonly GitProviderManager $gitProviderManager,
|
|
) {}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function handle(Project $project, User $user, mixed $status = null): array
|
|
{
|
|
$repositoryConnections = $project->repositoryConnections()
|
|
->get()
|
|
->each(function (RepositoryConnection $repositoryConnection) use ($user): void {
|
|
$repositoryConnection->setAttribute(
|
|
'has_personal_access',
|
|
$this->hasPersonalAccess($user, $repositoryConnection),
|
|
);
|
|
});
|
|
|
|
return [
|
|
'availableRepositorySources' => AvailableGitRepositorySourceResource::collection(
|
|
$this->availableRepositorySources($user),
|
|
)->resolve(),
|
|
'project' => ProjectResource::make($project)->resolve(),
|
|
'repositoryConnections' => RepositoryConnectionResource::collection($repositoryConnections)->resolve(),
|
|
'status' => $status,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<int, array{
|
|
* account_login: string,
|
|
* account_name: string|null,
|
|
* base_url: string,
|
|
* error: string|null,
|
|
* provider: string,
|
|
* provider_label: string,
|
|
* repositories: array<int, array{
|
|
* full_name: string,
|
|
* owner_name: string,
|
|
* provider_repository_id: string,
|
|
* repository_name: string,
|
|
* repository_url: string,
|
|
* visibility_label: string
|
|
* }>,
|
|
* user_git_connection_id: int
|
|
* }>
|
|
*/
|
|
public function availableRepositorySources(User $user): array
|
|
{
|
|
/** @var Collection<int, UserGitConnection> $gitConnections */
|
|
$gitConnections = $user->gitConnections()
|
|
->get();
|
|
|
|
return $gitConnections
|
|
->map(function (UserGitConnection $gitConnection): array {
|
|
try {
|
|
$repositories = collect(
|
|
$this->gitProviderManager
|
|
->client($gitConnection->provider)
|
|
->listRepositories($gitConnection->base_url, $gitConnection->api_token),
|
|
)
|
|
->map(fn (GitRepository $repository): array => [
|
|
'full_name' => $repository->fullName,
|
|
'owner_name' => $repository->owner,
|
|
'provider_repository_id' => $repository->providerRepositoryId,
|
|
'repository_name' => $repository->name,
|
|
'repository_url' => $repository->htmlUrl,
|
|
'visibility_label' => $repository->isPrivate ? 'Private' : 'Public',
|
|
])
|
|
->sortBy('full_name')
|
|
->values()
|
|
->all();
|
|
|
|
$error = null;
|
|
} catch (Throwable) {
|
|
$repositories = [];
|
|
$error = sprintf('%s could not load repositories from this Git connection right now.', config('app.name'));
|
|
}
|
|
|
|
return [
|
|
'account_login' => $gitConnection->account_login,
|
|
'account_name' => $gitConnection->account_name,
|
|
'base_url' => $gitConnection->base_url,
|
|
'error' => $error,
|
|
'provider' => $gitConnection->provider->value,
|
|
'provider_label' => $gitConnection->provider->label(),
|
|
'repositories' => $repositories,
|
|
'user_git_connection_id' => $gitConnection->id,
|
|
];
|
|
})
|
|
->values()
|
|
->all();
|
|
}
|
|
|
|
public function hasPersonalAccess(User $user, RepositoryConnection $repositoryConnection): bool
|
|
{
|
|
return $user->gitConnections()
|
|
->where('provider', $repositoryConnection->provider)
|
|
->where('base_url', $repositoryConnection->base_url)
|
|
->exists();
|
|
}
|
|
}
|