mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-02 14:36:17 +00:00
source-controls (#193)
* edit source control * assign project after creation * global and project scoped source controls
This commit is contained in:
@ -14,7 +14,7 @@ public function create(User $user, array $input): Project
|
||||
$input['name'] = strtolower($input['name']);
|
||||
}
|
||||
|
||||
$this->validate($user, $input);
|
||||
$this->validate($input);
|
||||
|
||||
$project = new Project([
|
||||
'name' => $input['name'],
|
||||
@ -22,10 +22,12 @@ public function create(User $user, array $input): Project
|
||||
|
||||
$project->save();
|
||||
|
||||
$project->users()->attach($user);
|
||||
|
||||
return $project;
|
||||
}
|
||||
|
||||
private function validate(User $user, array $input): void
|
||||
private function validate(array $input): void
|
||||
{
|
||||
Validator::make($input, [
|
||||
'name' => [
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace App\Actions\SourceControl;
|
||||
|
||||
use App\Models\SourceControl;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
@ -10,7 +11,7 @@
|
||||
|
||||
class ConnectSourceControl
|
||||
{
|
||||
public function connect(array $input): void
|
||||
public function connect(User $user, array $input): void
|
||||
{
|
||||
$this->validate($input);
|
||||
|
||||
@ -18,6 +19,7 @@ public function connect(array $input): void
|
||||
'provider' => $input['provider'],
|
||||
'profile' => $input['name'],
|
||||
'url' => Arr::has($input, 'url') ? $input['url'] : null,
|
||||
'project_id' => isset($input['global']) && $input['global'] ? null : $user->current_project_id,
|
||||
]);
|
||||
|
||||
$this->validateProvider($sourceControl, $input);
|
||||
|
54
app/Actions/SourceControl/EditSourceControl.php
Normal file
54
app/Actions/SourceControl/EditSourceControl.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\SourceControl;
|
||||
|
||||
use App\Models\SourceControl;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class EditSourceControl
|
||||
{
|
||||
public function edit(SourceControl $sourceControl, User $user, array $input): void
|
||||
{
|
||||
$this->validate($input);
|
||||
|
||||
$sourceControl->profile = $input['name'];
|
||||
$sourceControl->url = isset($input['url']) ? $input['url'] : null;
|
||||
$sourceControl->project_id = isset($input['global']) && $input['global'] ? null : $user->current_project_id;
|
||||
|
||||
$this->validateProvider($sourceControl, $input);
|
||||
|
||||
$sourceControl->provider_data = $sourceControl->provider()->createData($input);
|
||||
|
||||
if (! $sourceControl->provider()->connect()) {
|
||||
throw ValidationException::withMessages([
|
||||
'token' => __('Cannot connect to :provider or invalid token!', ['provider' => $sourceControl->provider]
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
$sourceControl->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
private function validate(array $input): void
|
||||
{
|
||||
$rules = [
|
||||
'name' => [
|
||||
'required',
|
||||
],
|
||||
];
|
||||
Validator::make($input, $rules)->validate();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
private function validateProvider(SourceControl $sourceControl, array $input): void
|
||||
{
|
||||
Validator::make($input, $sourceControl->provider()->createRules($input))->validate();
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@
|
||||
|
||||
use App\Actions\SourceControl\ConnectSourceControl;
|
||||
use App\Actions\SourceControl\DeleteSourceControl;
|
||||
use App\Actions\SourceControl\EditSourceControl;
|
||||
use App\Facades\Toast;
|
||||
use App\Helpers\HtmxResponse;
|
||||
use App\Http\Controllers\Controller;
|
||||
@ -14,16 +15,23 @@
|
||||
|
||||
class SourceControlController extends Controller
|
||||
{
|
||||
public function index(): View
|
||||
public function index(Request $request): View
|
||||
{
|
||||
return view('settings.source-controls.index', [
|
||||
'sourceControls' => SourceControl::query()->orderByDesc('id')->get(),
|
||||
]);
|
||||
$data = [
|
||||
'sourceControls' => SourceControl::getByCurrentProject(),
|
||||
];
|
||||
|
||||
if ($request->has('edit')) {
|
||||
$data['editSourceControl'] = SourceControl::find($request->input('edit'));
|
||||
}
|
||||
|
||||
return view('settings.source-controls.index', $data);
|
||||
}
|
||||
|
||||
public function connect(Request $request): HtmxResponse
|
||||
{
|
||||
app(ConnectSourceControl::class)->connect(
|
||||
$request->user(),
|
||||
$request->input(),
|
||||
);
|
||||
|
||||
@ -32,6 +40,19 @@ public function connect(Request $request): HtmxResponse
|
||||
return htmx()->redirect(route('settings.source-controls'));
|
||||
}
|
||||
|
||||
public function update(SourceControl $sourceControl, Request $request): HtmxResponse
|
||||
{
|
||||
app(EditSourceControl::class)->edit(
|
||||
$sourceControl,
|
||||
$request->user(),
|
||||
$request->input(),
|
||||
);
|
||||
|
||||
Toast::success('Source control updated.');
|
||||
|
||||
return htmx()->redirect(route('settings.source-controls'));
|
||||
}
|
||||
|
||||
public function delete(SourceControl $sourceControl): RedirectResponse
|
||||
{
|
||||
try {
|
||||
|
@ -19,6 +19,7 @@
|
||||
* @property User $user
|
||||
* @property Collection<Server> $servers
|
||||
* @property Collection<NotificationChannel> $notificationChannels
|
||||
* @property Collection<SourceControl> $sourceControls
|
||||
*/
|
||||
class Project extends Model
|
||||
{
|
||||
@ -59,4 +60,9 @@ public function users(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'user_project')->withTimestamps();
|
||||
}
|
||||
|
||||
public function sourceControls(): HasMany
|
||||
{
|
||||
return $this->hasMany(SourceControl::class);
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,9 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\SourceControlProviders\SourceControlProvider;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
/**
|
||||
@ -12,6 +14,7 @@
|
||||
* @property ?string $profile
|
||||
* @property ?string $url
|
||||
* @property string $access_token
|
||||
* @property ?int $project_id
|
||||
*/
|
||||
class SourceControl extends AbstractModel
|
||||
{
|
||||
@ -23,11 +26,13 @@ class SourceControl extends AbstractModel
|
||||
'profile',
|
||||
'url',
|
||||
'access_token',
|
||||
'project_id',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'access_token' => 'encrypted',
|
||||
'provider_data' => 'encrypted:array',
|
||||
'project_id' => 'integer',
|
||||
];
|
||||
|
||||
public function provider(): SourceControlProvider
|
||||
@ -46,4 +51,16 @@ public function sites(): HasMany
|
||||
{
|
||||
return $this->hasMany(Site::class);
|
||||
}
|
||||
|
||||
public function project(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Project::class);
|
||||
}
|
||||
|
||||
public static function getByCurrentProject(): Collection
|
||||
{
|
||||
return self::query()
|
||||
->where('project_id', auth()->user()->current_project_id)
|
||||
->orWhereNull('project_id')->get();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user