mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-03 06:56:15 +00:00
#591 - source-controls
This commit is contained in:
@ -4,6 +4,7 @@
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\ServerProvider;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class EditServerProvider
|
||||
{
|
||||
@ -12,6 +13,8 @@ class EditServerProvider
|
||||
*/
|
||||
public function edit(ServerProvider $serverProvider, Project $project, array $input): ServerProvider
|
||||
{
|
||||
Validator::make($input, self::rules())->validate();
|
||||
|
||||
$serverProvider->profile = $input['name'];
|
||||
$serverProvider->project_id = isset($input['global']) && $input['global'] ? null : $project->id;
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\SourceControl;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
@ -17,10 +17,12 @@ class ConnectSourceControl
|
||||
*/
|
||||
public function connect(Project $project, array $input): SourceControl
|
||||
{
|
||||
Validator::make($input, self::rules($input))->validate();
|
||||
|
||||
$sourceControl = new SourceControl([
|
||||
'provider' => $input['provider'],
|
||||
'profile' => $input['name'],
|
||||
'url' => Arr::has($input, 'url') ? $input['url'] : null,
|
||||
'url' => isset($input['url']) && $input['url'] ? $input['url'] : null,
|
||||
'project_id' => isset($input['global']) && $input['global'] ? null : $project->id,
|
||||
]);
|
||||
|
||||
@ -28,8 +30,7 @@ public function connect(Project $project, array $input): SourceControl
|
||||
|
||||
if (! $sourceControl->provider()->connect()) {
|
||||
throw ValidationException::withMessages([
|
||||
'token' => __('Cannot connect to :provider or invalid token!', ['provider' => $sourceControl->provider]
|
||||
),
|
||||
'token' => __('Cannot connect to :provider or invalid token!', ['provider' => $sourceControl->provider]),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\SourceControl;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class EditSourceControl
|
||||
@ -15,46 +16,25 @@ class EditSourceControl
|
||||
*/
|
||||
public function edit(SourceControl $sourceControl, Project $project, array $input): SourceControl
|
||||
{
|
||||
Validator::make($input, self::rules())->validate();
|
||||
|
||||
$sourceControl->profile = $input['name'];
|
||||
$sourceControl->url = $input['url'] ?? null;
|
||||
$sourceControl->project_id = isset($input['global']) && $input['global'] ? null : $project->id;
|
||||
|
||||
$sourceControl->provider_data = $sourceControl->provider()->editData($input);
|
||||
|
||||
if (! $sourceControl->provider()->connect()) {
|
||||
throw ValidationException::withMessages([
|
||||
'token' => __('Cannot connect to :provider or invalid token!', ['provider' => $sourceControl->provider]),
|
||||
]);
|
||||
}
|
||||
|
||||
$sourceControl->save();
|
||||
|
||||
return $sourceControl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
* @return array<string, array<int, mixed>>
|
||||
*/
|
||||
public static function rules(SourceControl $sourceControl, array $input): array
|
||||
public static function rules(): array
|
||||
{
|
||||
$rules = [
|
||||
return [
|
||||
'name' => [
|
||||
'required',
|
||||
],
|
||||
];
|
||||
|
||||
return array_merge($rules, self::providerRules($sourceControl, $input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
* @return array<string, array<int, mixed>>
|
||||
*
|
||||
* @throws ValidationException
|
||||
*/
|
||||
private static function providerRules(SourceControl $sourceControl, array $input): array
|
||||
{
|
||||
return $sourceControl->provider()->editRules($input);
|
||||
}
|
||||
}
|
||||
|
@ -53,8 +53,6 @@ public function create(Request $request, Project $project): ServerProviderResour
|
||||
{
|
||||
$this->authorize('create', ServerProvider::class);
|
||||
|
||||
$this->validate($request, CreateServerProvider::rules($request->all()));
|
||||
|
||||
/** @var User $user */
|
||||
$user = auth()->user();
|
||||
$serverProvider = app(CreateServerProvider::class)->create($user, $project, $request->all());
|
||||
@ -85,8 +83,6 @@ public function update(Request $request, Project $project, ServerProvider $serve
|
||||
|
||||
$this->validateRoute($project, $serverProvider);
|
||||
|
||||
$this->validate($request, EditServerProvider::rules());
|
||||
|
||||
$serverProvider = app(EditServerProvider::class)->edit($serverProvider, $project, $request->all());
|
||||
|
||||
return new ServerProviderResource($serverProvider);
|
||||
|
@ -53,8 +53,6 @@ public function create(Request $request, Project $project): SourceControlResourc
|
||||
{
|
||||
$this->authorize('create', SourceControl::class);
|
||||
|
||||
$this->validate($request, ConnectSourceControl::rules($request->all()));
|
||||
|
||||
$sourceControl = app(ConnectSourceControl::class)->connect($project, $request->all());
|
||||
|
||||
return new SourceControlResource($sourceControl);
|
||||
@ -87,8 +85,6 @@ public function update(Request $request, Project $project, SourceControl $source
|
||||
|
||||
$this->validateRoute($project, $sourceControl);
|
||||
|
||||
$this->validate($request, EditSourceControl::rules($sourceControl, $request->all()));
|
||||
|
||||
$sourceControl = app(EditSourceControl::class)->edit($sourceControl, $project, $request->all());
|
||||
|
||||
return new SourceControlResource($sourceControl);
|
||||
|
73
app/Http/Controllers/SourceControlController.php
Normal file
73
app/Http/Controllers/SourceControlController.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\SourceControl\ConnectSourceControl;
|
||||
use App\Actions\SourceControl\DeleteSourceControl;
|
||||
use App\Actions\SourceControl\EditSourceControl;
|
||||
use App\Http\Resources\SourceControlResource;
|
||||
use App\Models\SourceControl;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
use Spatie\RouteAttributes\Attributes\Delete;
|
||||
use Spatie\RouteAttributes\Attributes\Get;
|
||||
use Spatie\RouteAttributes\Attributes\Middleware;
|
||||
use Spatie\RouteAttributes\Attributes\Patch;
|
||||
use Spatie\RouteAttributes\Attributes\Post;
|
||||
use Spatie\RouteAttributes\Attributes\Prefix;
|
||||
|
||||
#[Prefix('settings/source-controls')]
|
||||
#[Middleware(['auth'])]
|
||||
class SourceControlController extends Controller
|
||||
{
|
||||
#[Get('/', name: 'source-controls')]
|
||||
public function index(): Response
|
||||
{
|
||||
$this->authorize('viewAny', SourceControl::class);
|
||||
|
||||
return Inertia::render('source-controls/index', [
|
||||
'sourceControls' => SourceControlResource::collection(SourceControl::getByProjectId(user()->current_project_id)->simplePaginate(config('web.pagination_size'))),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Get('/json', name: 'source-controls.json')]
|
||||
public function json(): ResourceCollection
|
||||
{
|
||||
$this->authorize('viewAny', SourceControl::class);
|
||||
|
||||
return SourceControlResource::collection(SourceControl::getByProjectId(user()->current_project_id)->get());
|
||||
}
|
||||
|
||||
#[Post('/', name: 'source-controls.store')]
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
$this->authorize('create', SourceControl::class);
|
||||
|
||||
app(ConnectSourceControl::class)->connect(user()->currentProject, $request->all());
|
||||
|
||||
return back()->with('success', 'Source control created.');
|
||||
}
|
||||
|
||||
#[Patch('/{sourceControl}', name: 'source-controls.update')]
|
||||
public function update(Request $request, SourceControl $sourceControl): RedirectResponse
|
||||
{
|
||||
$this->authorize('update', $sourceControl);
|
||||
|
||||
app(EditSourceControl::class)->edit($sourceControl, user()->currentProject, $request->all());
|
||||
|
||||
return back()->with('success', 'Source control updated.');
|
||||
}
|
||||
|
||||
#[Delete('{sourceControl}', name: 'source-controls.destroy')]
|
||||
public function destroy(SourceControl $sourceControl): RedirectResponse
|
||||
{
|
||||
$this->authorize('delete', $sourceControl);
|
||||
|
||||
app(DeleteSourceControl::class)->delete($sourceControl);
|
||||
|
||||
return to_route('source-controls')->with('success', 'Source control deleted.');
|
||||
}
|
||||
}
|
@ -26,16 +26,6 @@ public function createData(array $input): array
|
||||
];
|
||||
}
|
||||
|
||||
public function editRules(array $input): array
|
||||
{
|
||||
return $this->createRules($input);
|
||||
}
|
||||
|
||||
public function editData(array $input): array
|
||||
{
|
||||
return $this->createData($input);
|
||||
}
|
||||
|
||||
public function data(): array
|
||||
{
|
||||
// support for older data
|
||||
|
@ -29,12 +29,15 @@ public function createRules(array $input): array
|
||||
public function connect(): bool
|
||||
{
|
||||
try {
|
||||
ds($this->getApiUrl());
|
||||
$res = Http::withToken($this->data()['token'])
|
||||
->get($this->getApiUrl().'/projects');
|
||||
->get($this->getApiUrl().'/version');
|
||||
} catch (Exception) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ds($res->status());
|
||||
|
||||
return $res->successful();
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\SourceControlProviders;
|
||||
|
||||
use App\Exceptions\FailedToDeployGitHook;
|
||||
use App\Exceptions\FailedToDeployGitKey;
|
||||
use App\Exceptions\FailedToDestroyGitHook;
|
||||
|
||||
@ -19,18 +20,6 @@ public function createRules(array $input): array;
|
||||
*/
|
||||
public function createData(array $input): array;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function editRules(array $input): array;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function editData(array $input): array;
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
@ -43,10 +32,10 @@ public function getRepo(string $repo): mixed;
|
||||
public function fullRepoUrl(string $repo, string $key): string;
|
||||
|
||||
/**
|
||||
* @param array<mixed> $events
|
||||
* @param array<int, mixed> $events
|
||||
* @return array<string, mixed>
|
||||
*
|
||||
* @throws \App\Exceptions\FailedToDeployGitHook
|
||||
* @throws FailedToDeployGitHook
|
||||
*/
|
||||
public function deployHook(string $repo, array $events, string $secret): array;
|
||||
|
||||
@ -56,7 +45,7 @@ public function deployHook(string $repo, array $events, string $secret): array;
|
||||
public function destroyHook(string $repo, string $hookId): void;
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
public function getLastCommit(string $repo, string $branch): ?array;
|
||||
|
||||
|
Reference in New Issue
Block a user