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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Settings;
|
||||
|
||||
use App\Actions\Git\GitProviderManager;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Settings\StoreUserGitConnectionRequest;
|
||||
use App\Http\Requests\Settings\UpdateUserGitConnectionRequest;
|
||||
use App\Models\UserGitConnection;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
use Throwable;
|
||||
|
||||
class GitConnectionController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly GitProviderManager $gitProviderManager,
|
||||
) {}
|
||||
|
||||
public function edit(Request $request): Response
|
||||
{
|
||||
return Inertia::render('settings/GitConnections', [
|
||||
'gitConnections' => $request->user()->gitConnections
|
||||
->map(fn (UserGitConnection $gitConnection): array => [
|
||||
'account_login' => $gitConnection->account_login,
|
||||
'account_name' => $gitConnection->account_name,
|
||||
'base_url' => $gitConnection->base_url,
|
||||
'created_at' => $gitConnection->created_at?->toISOString(),
|
||||
'id' => $gitConnection->id,
|
||||
'provider' => $gitConnection->provider->value,
|
||||
'provider_label' => $gitConnection->provider->label(),
|
||||
'provider_user_id' => $gitConnection->provider_user_id,
|
||||
'updated_at' => $gitConnection->updated_at?->toISOString(),
|
||||
'user_id' => $gitConnection->user_id,
|
||||
])
|
||||
->values(),
|
||||
'status' => $request->session()->get('status'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(StoreUserGitConnectionRequest $request): RedirectResponse
|
||||
{
|
||||
try {
|
||||
$gitProvider = $this->gitProviderManager->client($request->provider());
|
||||
$authenticatedUser = $gitProvider->getAuthenticatedUser(
|
||||
$request->baseUrl(),
|
||||
$request->apiToken(),
|
||||
);
|
||||
} catch (Throwable) {
|
||||
return back()
|
||||
->withErrors([
|
||||
'api_token' => 'KanbanGit could not authenticate against Gitea with this base URL and token.',
|
||||
]);
|
||||
}
|
||||
|
||||
UserGitConnection::query()->create([
|
||||
'account_login' => $authenticatedUser->login,
|
||||
'account_name' => $authenticatedUser->name,
|
||||
'api_token' => $request->apiToken(),
|
||||
'base_url' => $request->baseUrl(),
|
||||
'provider' => $request->provider(),
|
||||
'provider_user_id' => $authenticatedUser->providerUserId,
|
||||
'user_id' => $request->user()->id,
|
||||
]);
|
||||
|
||||
return to_route('git-connections.edit')
|
||||
->with('status', 'Git connection was added.');
|
||||
}
|
||||
|
||||
public function update(
|
||||
UpdateUserGitConnectionRequest $request,
|
||||
UserGitConnection $gitConnection,
|
||||
): RedirectResponse {
|
||||
$this->abortUnlessOwner($request, $gitConnection);
|
||||
|
||||
try {
|
||||
$gitProvider = $this->gitProviderManager->client($request->provider());
|
||||
$authenticatedUser = $gitProvider->getAuthenticatedUser(
|
||||
$request->baseUrl(),
|
||||
$request->apiToken(),
|
||||
);
|
||||
} catch (Throwable) {
|
||||
return back()
|
||||
->withErrors([
|
||||
'api_token' => 'KanbanGit could not authenticate against Gitea with this base URL and token.',
|
||||
]);
|
||||
}
|
||||
|
||||
$gitConnection->update([
|
||||
'account_login' => $authenticatedUser->login,
|
||||
'account_name' => $authenticatedUser->name,
|
||||
'api_token' => $request->apiToken(),
|
||||
'base_url' => $request->baseUrl(),
|
||||
'provider' => $request->provider(),
|
||||
'provider_user_id' => $authenticatedUser->providerUserId,
|
||||
]);
|
||||
|
||||
return to_route('git-connections.edit')
|
||||
->with('status', 'Git connection was updated.');
|
||||
}
|
||||
|
||||
public function destroy(Request $request, UserGitConnection $gitConnection): RedirectResponse
|
||||
{
|
||||
$this->abortUnlessOwner($request, $gitConnection);
|
||||
|
||||
$label = "{$gitConnection->account_login} @ {$gitConnection->base_url}";
|
||||
|
||||
$gitConnection->delete();
|
||||
|
||||
return to_route('git-connections.edit')
|
||||
->with('status', "{$label} was removed.");
|
||||
}
|
||||
|
||||
private function abortUnlessOwner(Request $request, UserGitConnection $gitConnection): void
|
||||
{
|
||||
abort_unless($gitConnection->user_id === $request->user()->id, 403);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Settings;
|
||||
|
||||
use App\Actions\Git\GitProviderKey;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class StoreUserGitConnectionRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user() !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'api_token' => ['required', 'string', 'max:2048'],
|
||||
'base_url' => [
|
||||
'required',
|
||||
'url:http,https',
|
||||
'max:255',
|
||||
Rule::unique('user_git_connections')
|
||||
->where(fn ($query) => $query
|
||||
->where('user_id', $this->user()->id)
|
||||
->where('provider', $this->input('provider'))),
|
||||
],
|
||||
'provider' => ['required', Rule::in(array_map(
|
||||
static fn (GitProviderKey $providerKey): string => $providerKey->value,
|
||||
GitProviderKey::cases(),
|
||||
))],
|
||||
];
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$this->merge([
|
||||
'base_url' => $this->normalizedBaseUrl(),
|
||||
'provider' => Str::lower(trim((string) $this->input('provider'))),
|
||||
]);
|
||||
}
|
||||
|
||||
public function apiToken(): string
|
||||
{
|
||||
return (string) $this->validated('api_token');
|
||||
}
|
||||
|
||||
public function baseUrl(): string
|
||||
{
|
||||
return (string) $this->validated('base_url');
|
||||
}
|
||||
|
||||
public function provider(): GitProviderKey
|
||||
{
|
||||
return GitProviderKey::from((string) $this->validated('provider'));
|
||||
}
|
||||
|
||||
private function normalizedBaseUrl(): string
|
||||
{
|
||||
$baseUrl = Str::of((string) $this->input('base_url'))
|
||||
->trim()
|
||||
->rtrim('/')
|
||||
->value();
|
||||
|
||||
return preg_replace('#/api/v1$#i', '', $baseUrl) ?? $baseUrl;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Settings;
|
||||
|
||||
use App\Actions\Git\GitProviderKey;
|
||||
use App\Models\UserGitConnection;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdateUserGitConnectionRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user() !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
/** @var UserGitConnection $gitConnection */
|
||||
$gitConnection = $this->route('gitConnection');
|
||||
|
||||
return [
|
||||
'api_token' => ['required', 'string', 'max:2048'],
|
||||
'base_url' => [
|
||||
'required',
|
||||
'url:http,https',
|
||||
'max:255',
|
||||
Rule::unique('user_git_connections')
|
||||
->ignore($gitConnection->id)
|
||||
->where(fn ($query) => $query
|
||||
->where('user_id', $this->user()->id)
|
||||
->where('provider', $this->input('provider'))),
|
||||
],
|
||||
'provider' => ['required', Rule::in(array_map(
|
||||
static fn (GitProviderKey $providerKey): string => $providerKey->value,
|
||||
GitProviderKey::cases(),
|
||||
))],
|
||||
];
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$this->merge([
|
||||
'base_url' => $this->normalizedBaseUrl(),
|
||||
'provider' => Str::lower(trim((string) $this->input('provider'))),
|
||||
]);
|
||||
}
|
||||
|
||||
public function apiToken(): string
|
||||
{
|
||||
return (string) $this->validated('api_token');
|
||||
}
|
||||
|
||||
public function baseUrl(): string
|
||||
{
|
||||
return (string) $this->validated('base_url');
|
||||
}
|
||||
|
||||
public function provider(): GitProviderKey
|
||||
{
|
||||
return GitProviderKey::from((string) $this->validated('provider'));
|
||||
}
|
||||
|
||||
private function normalizedBaseUrl(): string
|
||||
{
|
||||
$baseUrl = Str::of((string) $this->input('base_url'))
|
||||
->trim()
|
||||
->rtrim('/')
|
||||
->value();
|
||||
|
||||
return preg_replace('#/api/v1$#i', '', $baseUrl) ?? $baseUrl;
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Fortify\TwoFactorAuthenticatable;
|
||||
@@ -39,4 +40,11 @@ class User extends Authenticatable
|
||||
return $this->belongsToMany(Team::class)
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function gitConnections(): HasMany
|
||||
{
|
||||
return $this->hasMany(UserGitConnection::class)
|
||||
->orderBy('provider')
|
||||
->orderBy('base_url');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Actions\Git\GitProviderKey;
|
||||
use Database\Factories\UserGitConnectionFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
#[Fillable([
|
||||
'account_login',
|
||||
'account_name',
|
||||
'api_token',
|
||||
'base_url',
|
||||
'provider',
|
||||
'provider_user_id',
|
||||
'user_id',
|
||||
])]
|
||||
#[Hidden(['api_token'])]
|
||||
class UserGitConnection extends Model
|
||||
{
|
||||
/** @use HasFactory<UserGitConnectionFactory> */
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'api_token' => 'encrypted',
|
||||
'provider' => GitProviderKey::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user