Skip to content

Commit

Permalink
Merge pull request #92 from mehrancodes/validate-subdomain-pattern
Browse files Browse the repository at this point in the history
Validate subdomain pattern
  • Loading branch information
mehrancodes authored Mar 26, 2024
2 parents eb8c56b + da84d1b commit 3f1aa0c
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 42 deletions.
28 changes: 28 additions & 0 deletions app/Rules/BranchNameRegex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\ValidationRule;

class BranchNameRegex implements DataAwareRule, ValidationRule
{
protected array $data = [];

public function validate(string $attribute, mixed $value, Closure $fail): void
{
$pattern = $this->data['subdomain_pattern'];

if (! empty($pattern) && ! preg_match($pattern, $value)) {
$fail('The subdomain regex pattern must be match with the branch name.');
}
}

public function setData(array $data): static
{
$this->data = $data;

return $this;
}
}
75 changes: 38 additions & 37 deletions app/Services/Forge/ForgeSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace App\Services\Forge;

use App\Rules\BranchNameRegex;
use App\Traits\Outputifier;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -187,50 +188,14 @@ class ForgeSetting
*/
public bool $inertiaSsrEnabled;

/**
* The validation rules.
*/
private array $validationRules = [
'token' => ['required'],
'server' => ['required'],
'domain' => ['required'],
'git_provider' => ['required'],
'repository' => ['required'],
'branch' => ['required'],
'project_type' => ['string'],
'php_version' => ['nullable', 'string'],
'subdomain_pattern' => ['nullable', 'string'],
'command' => ['nullable', 'string'],
'nginx_template' => ['nullable', 'int'],
'quick_deploy' => ['boolean'],
'site_isolation_required' => ['boolean'],
'job_scheduler_required' => ['boolean'],
'db_creation_required' => ['boolean'],
'db_name' => ['nullable', 'string'],
'auto_source_required' => ['boolean'],
'ssl_required' => ['boolean'],
'wait_on_ssl' => ['boolean'],
'wait_on_deploy' => ['boolean'],
'timeout_seconds' => ['required', 'int', 'min:0'],
'git_comment_enabled' => ['required', 'boolean'],
'git_issue_number' => ['exclude_if:git_comment_enabled,false', 'required', 'string'],
'git_token' => ['exclude_if:git_comment_enabled,false', 'required', 'string'],
'subdomain_name' => ['nullable', 'string', 'regex:/^[a-zA-Z0-9-_]+$/'],
'environment_url' => ['nullable', 'url'],
'slack_announcement_enabled' => ['required', 'boolean'],
'slack_bot_user_oauth_token' => ['exclude_if:slack_announcement_enabled,false', 'required', 'string'],
'slack_channel' => ['exclude_if:slack_announcement_enabled,false', 'required', 'string'],
'inertia_ssr_enabled' => ['required', 'boolean'],
];

public function __construct()
{
$this->init(config('forge'));
}

private function init(array $configurations): void
{
$validator = Validator::make($configurations, $this->validationRules);
$validator = $this->validate($configurations);

throw_if($validator->fails(), ValidationException::class, $validator->errors()->all());

Expand All @@ -241,4 +206,40 @@ private function init(array $configurations): void
}
}
}

protected function validate(array $configurations): \Illuminate\Validation\Validator
{
return Validator::make($configurations, [
'token' => ['required'],
'server' => ['required'],
'domain' => ['required'],
'git_provider' => ['required'],
'repository' => ['required'],
'branch' => ['required', new BranchNameRegex],
'project_type' => ['string'],
'php_version' => ['nullable', 'string'],
'subdomain_pattern' => ['nullable', 'string'],
'command' => ['nullable', 'string'],
'nginx_template' => ['nullable', 'int'],
'quick_deploy' => ['boolean'],
'site_isolation_required' => ['boolean'],
'job_scheduler_required' => ['boolean'],
'db_creation_required' => ['boolean'],
'db_name' => ['nullable', 'string'],
'auto_source_required' => ['boolean'],
'ssl_required' => ['boolean'],
'wait_on_ssl' => ['boolean'],
'wait_on_deploy' => ['boolean'],
'timeout_seconds' => ['required', 'int', 'min:0'],
'git_comment_enabled' => ['required', 'boolean'],
'git_issue_number' => ['exclude_if:git_comment_enabled,false', 'required', 'string'],
'git_token' => ['exclude_if:git_comment_enabled,false', 'required', 'string'],
'subdomain_name' => ['nullable', 'string', 'regex:/^[a-zA-Z0-9-_]+$/'],
'environment_url' => ['nullable', 'url'],
'slack_announcement_enabled' => ['required', 'boolean'],
'slack_bot_user_oauth_token' => ['exclude_if:slack_announcement_enabled,false', 'required', 'string'],
'slack_channel' => ['exclude_if:slack_announcement_enabled,false', 'required', 'string'],
'inertia_ssr_enabled' => ['required', 'boolean'],
]);
}
}
1 change: 0 additions & 1 deletion app/Services/Forge/Pipeline/CreateDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace App\Services\Forge\Pipeline;

use App\Actions\GenerateStandardizedBranchName;
use App\Services\Forge\ForgeService;
use App\Traits\Outputifier;
use Closure;
Expand Down
6 changes: 3 additions & 3 deletions app/Services/Forge/Pipeline/EnableInertiaSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ protected function addDaemonToStartInertiaSsr(ForgeService $service): void
$service->forge->createDaemon($service->server->id, [
'command' => 'php artisan inertia:start-ssr',
'user' => 'forge',
'directory' => $service->siteDirectory()
'directory' => $service->siteDirectory(),
]);
}

protected function addCommandToStopInertiaOnReDeploy(ForgeService $service): void
{
$script = $service->forge->siteDeploymentScript($service->server->id, $service->site->id);

if (!str_contains($script, $command = 'php artisan inertia:stop-ssr')) {
if (! str_contains($script, $command = 'php artisan inertia:stop-ssr')) {
$this->information('Including stop command for Inertia SSR in deploy script.');

$service->forge->updateSiteDeploymentScript(
$service->server->id,
$service->site->id,
$script . "\n\n$command"
$script."\n\n$command"
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion resources/lang/en/valiadtion.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
*/

'attributes' => [
'db_name' => 'FORGE_DB_NAME'
'db_name' => 'FORGE_DB_NAME',
],

];
36 changes: 36 additions & 0 deletions tests/Feature/Validations/BranchNameRegexTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use App\Rules\BranchNameRegex;
use Illuminate\Support\Facades\Validator;

test('it validates the subdomain pattern matches the branch name', function ($data, $failed) {
$validator = Validator::make($data, [
'branch' => ['required', new BranchNameRegex],
'subdomain_pattern' => ['string'],
]);

expect($validator->fails())->toBe($failed);
})
->with([
[
'actual' => [
'branch' => 'user-notification',
'subdomain_pattern' => '/feature\/(.+)/i',
],
'failed' => true,
],
[
'actual' => [
'branch' => 'feature/user-notification',
'subdomain_pattern' => '/feature\/(.+)/i',
],
'failed' => false,
],
[
'actual' => [
'branch' => 'feature/user-notification',
'subdomain_pattern' => '',
],
'failed' => false,
],
]);

0 comments on commit 3f1aa0c

Please sign in to comment.