Implement Gitea user connection foundation
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Git\Contracts;
|
||||
|
||||
use App\Actions\Git\Data\GitAuthenticatedUser;
|
||||
use App\Actions\Git\Data\GitRepository;
|
||||
use App\Actions\Git\GitProviderKey;
|
||||
|
||||
interface GitProviderClient
|
||||
{
|
||||
public function key(): GitProviderKey;
|
||||
|
||||
public function getAuthenticatedUser(string $baseUrl, string $token): GitAuthenticatedUser;
|
||||
|
||||
/**
|
||||
* @return array<int, GitRepository>
|
||||
*/
|
||||
public function listRepositories(string $baseUrl, string $token): array;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Git\Data;
|
||||
|
||||
/**
|
||||
* Provider-agnostic representation of the authenticated remote account.
|
||||
*/
|
||||
final readonly class GitAuthenticatedUser
|
||||
{
|
||||
public function __construct(
|
||||
public string $providerUserId,
|
||||
public string $login,
|
||||
public ?string $name,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Git\Data;
|
||||
|
||||
/**
|
||||
* Provider-agnostic representation of a repository visible to a user connection.
|
||||
*/
|
||||
final readonly class GitRepository
|
||||
{
|
||||
public function __construct(
|
||||
public string $providerRepositoryId,
|
||||
public string $owner,
|
||||
public string $name,
|
||||
public string $fullName,
|
||||
public string $htmlUrl,
|
||||
public bool $isPrivate,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Git;
|
||||
|
||||
enum GitProviderKey: string
|
||||
{
|
||||
case Gitea = 'gitea';
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::Gitea => 'Gitea',
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Git;
|
||||
|
||||
use App\Actions\Git\Contracts\GitProviderClient;
|
||||
use App\Actions\Git\Gitea\GiteaProviderClient;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class GitProviderManager
|
||||
{
|
||||
public function __construct(
|
||||
private readonly GiteaProviderClient $giteaProviderClient,
|
||||
) {}
|
||||
|
||||
public function client(GitProviderKey|string $provider): GitProviderClient
|
||||
{
|
||||
$providerKey = $provider instanceof GitProviderKey
|
||||
? $provider
|
||||
: GitProviderKey::from(Str::lower(trim($provider)));
|
||||
|
||||
return match ($providerKey) {
|
||||
GitProviderKey::Gitea => $this->giteaProviderClient,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Git\Gitea;
|
||||
|
||||
use App\Actions\Git\Contracts\GitProviderClient;
|
||||
use App\Actions\Git\Data\GitAuthenticatedUser;
|
||||
use App\Actions\Git\Data\GitRepository;
|
||||
use App\Actions\Git\GitProviderKey;
|
||||
use Illuminate\Http\Client\PendingRequest;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class GiteaProviderClient implements GitProviderClient
|
||||
{
|
||||
public function key(): GitProviderKey
|
||||
{
|
||||
return GitProviderKey::Gitea;
|
||||
}
|
||||
|
||||
public function getAuthenticatedUser(string $baseUrl, string $token): GitAuthenticatedUser
|
||||
{
|
||||
/** @var array{id: int|string, login: string, full_name?: string|null} $payload */
|
||||
$payload = $this->request($baseUrl, $token)
|
||||
->get('/user')
|
||||
->throw()
|
||||
->json();
|
||||
|
||||
return new GitAuthenticatedUser(
|
||||
providerUserId: (string) $payload['id'],
|
||||
login: $payload['login'],
|
||||
name: blank($payload['full_name'] ?? null) ? null : $payload['full_name'],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, GitRepository>
|
||||
*/
|
||||
public function listRepositories(string $baseUrl, string $token): array
|
||||
{
|
||||
$page = 1;
|
||||
$repositories = [];
|
||||
|
||||
do {
|
||||
/** @var array<int, array{
|
||||
* id: int|string,
|
||||
* name: string,
|
||||
* full_name: string,
|
||||
* html_url: string,
|
||||
* owner: array{login: string},
|
||||
* private: bool
|
||||
* }> $payload
|
||||
*/
|
||||
$payload = $this->request($baseUrl, $token)
|
||||
->get('/user/repos', [
|
||||
'limit' => 100,
|
||||
'page' => $page,
|
||||
])
|
||||
->throw()
|
||||
->json();
|
||||
|
||||
foreach ($payload as $repository) {
|
||||
$repositories[] = new GitRepository(
|
||||
providerRepositoryId: (string) $repository['id'],
|
||||
owner: $repository['owner']['login'],
|
||||
name: $repository['name'],
|
||||
fullName: $repository['full_name'],
|
||||
htmlUrl: $repository['html_url'],
|
||||
isPrivate: $repository['private'],
|
||||
);
|
||||
}
|
||||
|
||||
$page++;
|
||||
} while (count($payload) === 100);
|
||||
|
||||
return $repositories;
|
||||
}
|
||||
|
||||
private function request(string $baseUrl, string $token): PendingRequest
|
||||
{
|
||||
return Http::acceptJson()
|
||||
->baseUrl($this->apiBaseUrl($baseUrl))
|
||||
->connectTimeout(5)
|
||||
->timeout(10)
|
||||
->withHeader('Authorization', 'token '.$token);
|
||||
}
|
||||
|
||||
private function apiBaseUrl(string $baseUrl): string
|
||||
{
|
||||
$normalizedBaseUrl = Str::of(trim($baseUrl))
|
||||
->rtrim('/')
|
||||
->value();
|
||||
|
||||
if (Str::endsWith(Str::lower($normalizedBaseUrl), '/api/v1')) {
|
||||
return $normalizedBaseUrl;
|
||||
}
|
||||
|
||||
return Str::of($normalizedBaseUrl)
|
||||
->append('/api/v1')
|
||||
->value();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user