mirror of
https://github.com/vitodeploy/vito.git
synced 2025-04-19 18:01:37 +00:00
* Update UI to ask for URL for Gitlab as provider. * Create state for each source control in factory. * Save url for SourceControl when given. * Make Gitlab dynamically use correct URL (custom or default) * Update SourceControlsTest to check for custom url when given. * Style fixes. --------- Co-authored-by: Saeed Vaziry <61919774+saeedvaziry@users.noreply.github.com>
58 lines
1.5 KiB
PHP
58 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\SourceControl;
|
|
|
|
use App\Models\SourceControl;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class ConnectSourceControl
|
|
{
|
|
public function connect(array $input): void
|
|
{
|
|
$this->validate($input);
|
|
$sourceControl = new SourceControl([
|
|
'provider' => $input['provider'],
|
|
'profile' => $input['name'],
|
|
'access_token' => $input['token'],
|
|
'url' => Arr::has($input, 'url') ? $input['url'] : null,
|
|
]);
|
|
|
|
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 = [
|
|
'provider' => [
|
|
'required',
|
|
Rule::in(\App\Enums\SourceControl::getValues()),
|
|
],
|
|
'name' => [
|
|
'required',
|
|
],
|
|
'token' => [
|
|
'required',
|
|
],
|
|
'url' => [
|
|
'nullable',
|
|
'url:http,https',
|
|
'ends_with:/',
|
|
],
|
|
];
|
|
Validator::make($input, $rules)->validate();
|
|
}
|
|
}
|