Skip to content

Commit

Permalink
Merge pull request #46 from mgkimsal/feature/filtered-user-name
Browse files Browse the repository at this point in the history
fix: treat username, database name and domain name the same
  • Loading branch information
mehrancodes authored Nov 25, 2023
2 parents 6bfe7f5 + e9067d7 commit d05ff08
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 115 deletions.
46 changes: 46 additions & 0 deletions app/Actions/FormattedBranchName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

/**
* This file is part of Veyoze CLI.
*
* (c) Mehran Rasulian <mehran.rasulian@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Actions;

use Illuminate\Support\Str;
use Lorisleiva\Actions\Concerns\AsAction;

class FormattedBranchName
{
use AsAction;

protected const STRING_LIMIT = 64;

protected const SLUGIFY_DICTIONARY = ['+' => '-', '_' => '-', '@' => '-'];

public function handle(string $branch, ?string $pattern): string
{
if (isset($pattern)) {
preg_match($pattern, $branch, $matches);
$branch = array_pop($matches);
}

$slugged = $this->slugifyIt($branch);

return Str::limit(
$slugged,
self::STRING_LIMIT
);
}

protected function slugifyIt(string $branch): string
{
return Str::slug($branch, '-', 'en', self::SLUGIFY_DICTIONARY);
}
}
15 changes: 2 additions & 13 deletions app/Actions/GenerateDomainName.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,16 @@

namespace App\Actions;

use Illuminate\Support\Str;
use Lorisleiva\Actions\Concerns\AsAction;

class GenerateDomainName
{
use AsAction;

public function handle(string $domain, string $branch, ?string $pattern): string
public function handle(string $domain, string $branch): string
{
return str($this->formatBranchName($branch, $pattern))
return str($branch)
->append('.', $domain)
->toString();
}

private function formatBranchName(string $branch, ?string $pattern): string
{
if (isset($pattern)) {
preg_match($pattern, $branch, $matches);
$branch = array_pop($matches);
}

return Str::slug($branch, '-', 'en', ['+' => '-', '_' => '-', '@' => '-']);
}
}
33 changes: 33 additions & 0 deletions app/Actions/GenerateStandardizedBranchName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/**
* This file is part of Veyoze CLI.
*
* (c) Mehran Rasulian <mehran.rasulian@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Actions;

use Illuminate\Support\Str;
use Lorisleiva\Actions\Concerns\AsAction;

class GenerateStandardizedBranchName
{
use AsAction;

public function handle(string $branch): string
{
$firstDigitsOfStringRemoved = preg_replace(
'/^\d+-?/',
'',
$branch
);

return Str::replace('-', '_', $firstDigitsOfStringRemoved);
}
}
44 changes: 21 additions & 23 deletions app/Services/Forge/ForgeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

namespace App\Services\Forge;

use App\Actions\FormattedBranchName;
use App\Actions\GenerateDomainName;
use Illuminate\Support\Str;
use App\Actions\GenerateStandardizedBranchName;
use Laravel\Forge\Forge;
use Laravel\Forge\Resources\Server;
use Laravel\Forge\Resources\Site;
Expand All @@ -41,11 +42,6 @@ class ForgeService
*/
public bool $siteNewlyMade = false;

/**
* Get the formatted domain based on subdomain pattern.
*/
private ?string $formattedDomain = null;

public function __construct(public ForgeSetting $setting, public Forge $forge)
{
$this->forge->setTimeout($this->setting->timeoutSeconds);
Expand All @@ -66,17 +62,27 @@ public function setDatabase(array $database): void
$this->database = $database;
}

public function getFormattedDomainName(): ?string
public function getFormattedBranchName(): string
{
if (is_null($this->formattedDomain)) {
$this->formattedDomain = GenerateDomainName::run(
$this->setting->domain,
$this->setting->branch,
$this->setting->subdomainPattern,
);
}
return FormattedBranchName::run(
$this->setting->branch,
$this->setting->subdomainPattern
);
}

public function getFormattedDomainName(): string
{
return GenerateDomainName::run(
$this->setting->domain,
$this->getFormattedBranchName()
);
}

return $this->formattedDomain;
public function getStandardizedBranchName(): string
{
return GenerateStandardizedBranchName::run(
$this->getFormattedBranchName()
);
}

public function createSite(string $serverId, array $payload): Site
Expand Down Expand Up @@ -105,12 +111,4 @@ public function markSiteAsNewlyMade(): void
{
$this->siteNewlyMade = true;
}

public function generateDatabaseName(): string
{
return Str::limit(
Str::slug($this->setting->branch, '_'),
64
);
}
}
2 changes: 1 addition & 1 deletion app/Services/Forge/Pipeline/CreateDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __invoke(ForgeService $service, Closure $next)
}

$dbPassword = Str::random(16);
$dbName = $service->generateDatabaseName();
$dbName = $service->getStandardizedBranchName();

if (! $this->databaseExists($service, $dbName)) {
$this->information('Creating database.');
Expand Down
3 changes: 1 addition & 2 deletions app/Services/Forge/Pipeline/OrCreateNewSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use App\Services\Forge\ForgeService;
use App\Traits\Outputifier;
use Closure;
use Illuminate\Support\Str;

class OrCreateNewSite
{
Expand Down Expand Up @@ -55,7 +54,7 @@ private function gatherSiteData(ForgeService $service): array
$this->information('---> Enabling site isolation.');

$data['isolated'] = true;
$data['username'] = Str::slug($service->setting->branch);
$data['username'] = $service->getStandardizedBranchName();
}

return $data;
Expand Down
2 changes: 1 addition & 1 deletion app/Services/Forge/Pipeline/RemoveDatabaseUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RemoveDatabaseUser
public function __invoke(ForgeService $service, Closure $next)
{
foreach ($service->forge->databaseUsers($service->setting->server) as $databaseUser) {
if ($databaseUser->name === $service->generateDatabaseName()) {
if ($databaseUser->name === $service->getStandardizedBranchName()) {
$this->information('Removing database with user.');

foreach ($databaseUser->databases as $database) {
Expand Down
90 changes: 90 additions & 0 deletions tests/Feature/Actions/FormattedBranchNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

use App\Actions\FormattedBranchName;

it('verify correct domain generation for various branch formats', function ($actual, $expected) {
expect(
FormattedBranchName::run(
$actual['branch'],
$actual['pattern'],
)
)
->toBe($expected);
})
->with([
[
'actual' => [
'branch' => 'user_notification+test',
'pattern' => null,
],
'expected' => 'user-notification-test',
],
[
'actual' => [
'branch' => 'feature/add-user-notification',
'pattern' => '/feature\/(.*)/i',
],
'expected' => 'add-user-notification',
],
[
'actual' => [
'branch' => 'feature/add-user-notification',
'pattern' => '/feature\/(.*)/i',
],
'expected' => 'add-user-notification',
],
[
'actual' => [
'branch' => 'feature/add@user-notification',
'pattern' => '/feature\/(.*)/i',
],
'expected' => 'add-user-notification',
],
[
'actual' => [
'branch' => 'feature/User-Notification',
'pattern' => '/feature\/(.*)/i',
],
'expected' => 'user-notification',
],
[
'actual' => [
'branch' => '360-add-user-notification',
'pattern' => '/\d+/i',
],
'expected' => '360',
],
[
'actual' => [
'branch' => 'vyz-145-add-user-notification',
'pattern' => '/vyz-\d+/i',
],
'expected' => 'vyz-145',
],
[
'actual' => [
'branch' => 'feature/user@notification!',
'pattern' => '/feature\/(.*)/i',
],
'expected' => 'user-notification',
],
]);

it('ensure graceful handling of invalid or problematic branch formats', function ($actual, $expected) {
expect(
FormattedBranchName::run(
$actual['branch'],
$actual['pattern'],
)
)
->toBe($expected);
})
->with([
[
'actual' => [
'branch' => 'feature/user-notification',
'pattern' => '/feature\/(.*)/i',
],
'expected' => 'user-notification',
],
]);
Loading

0 comments on commit d05ff08

Please sign in to comment.