Skip to content

Commit

Permalink
Merge pull request #4 from phpx-foundation/forge-support
Browse files Browse the repository at this point in the history
Automatically sync domains with forge
  • Loading branch information
inxilpro authored Nov 14, 2024
2 parents c64733b + 74e6ffa commit f30828a
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: PHPUnit

on:
push:
branches:
- main
pull_request:

jobs:
Expand Down
13 changes: 9 additions & 4 deletions app/Actions/ConfigureGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function handle(
string $timezone = 'America/New_York', // One true timezone
?string $bsky_url = null,
?string $meetup_url = null,
?string $og_asset = null,
): Group {
$group = Group::updateOrCreate([
'domain' => $domain,
Expand All @@ -32,14 +33,15 @@ public function handle(
'region' => $region,
'description' => $description,
'timezone' => $timezone,
'bsky_url' => $bsky_url
?: null,
'meetup_url' => $meetup_url
?: null,
'bsky_url' => $bsky_url ?: null,
'meetup_url' => $meetup_url ?: null,
'og_asset' => $og_asset ?: null,
]);

Cache::clear();

SyncDomainsWithForge::run();

app()->instance("group:{$domain}", $group);

return $group;
Expand Down Expand Up @@ -71,13 +73,15 @@ public function asCommand(Command $command): int
$timezone = suggest('Timezone', DateTimeZone::listIdentifiers(), default: str($existing->timezone), required: true);
$bsky_url = text('Is there a Bluesky URL?', default: str($existing->bsky_url));
$meetup_url = text('Is there a Meetup URL?', default: str($existing->meetup_url));
$og_asset = text('What is the open graph image name?', default: str($existing->og_asset));

table(['Option', 'Value'], [
['Name', $name],
['Description', $description],
['Timezone', $timezone],
['Bluesky', $bsky_url],
['Meetup', $meetup_url],
['Open Graph Image', $og_asset],
]);

if (confirm('Is this correct?')) {
Expand All @@ -89,6 +93,7 @@ public function asCommand(Command $command): int
timezone: $timezone,
bsky_url: $bsky_url,
meetup_url: $meetup_url,
og_asset: $og_asset,
);

$command->info($group->wasRecentlyCreated
Expand Down
85 changes: 85 additions & 0 deletions app/Actions/SyncDomainsWithForge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace App\Actions;

use App\Actions\Concerns\FetchesModelsForCommands;
use App\Models\ExternalGroup;
use App\Models\Group;
use App\Models\GroupMembership;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Laravel\Forge\Facades\Forge;
use Laravel\Forge\Resources\Site;
use Lorisleiva\Actions\Concerns\AsAction;
use Throwable;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\error;
use function Laravel\Prompts\table;

class SyncDomainsWithForge
{
use AsAction;

protected ?Collection $aliases = null;

protected ?Site $site = null;

public function handle(): Site
{
return Forge::addSiteAliases(
serverId: config('services.forge.server'),
siteId: config('services.forge.site'),
aliases: $this->aliases()->values()->toArray(),
);
}

public function getCommandSignature(): string
{
return 'forge:sync-domains {--force}';
}

public function asCommand(Command $command): int
{
$changes = $this->aliases()->diff($this->site()->aliases);

table(
headers: ['Domain', 'Status'],
rows: $this->aliases()->map(fn($alias) => [
$alias,
$changes->contains($alias) ? 'Will be added' : 'Exists',
]),
);

if ($command->option('force') || confirm('Save these changes?')) {
try {
$site = $this->handle();
} catch (Throwable $exception) {
error($exception->getMessage());
return 1;
}

info('New aliases: '.implode(', ', $site->aliases));

return 0;
}

return 1;
}

protected function site(): Site
{
return $this->site ??= Forge::site(
serverId: config('services.forge.server'),
siteId: config('services.forge.site'),
);
}

protected function aliases(): Collection
{
return $this->aliases ??= Group::query()->pluck('domain')
->merge($this->site()->aliases)
->unique()
->sort();
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"require": {
"php": "^8.2",
"glhd/bits": "^0.4.1",
"laravel/forge-sdk": "^3.19",
"laravel/framework": "^11.0",
"laravel/prompts": "^0.1.21",
"laravel/tinker": "^2.9",
Expand Down
76 changes: 75 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,11 @@
'channel' => env('SLACK_BOT_USER_DEFAULT_CHANNEL'),
],
],

'forge' => [
'token' => env('FORGE_TOKEN'),
'server' => env('FORGE_SERVER_ID'),
'site' => env('FORGE_SITE_ID'),
],

];
54 changes: 27 additions & 27 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory>tests/Feature</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>app</directory>
</include>
</source>
<php>
<server name="APP_ENV" value="testing"/>
<server name="APP_ENV" value="testing"/>
<server name="APP_MAINTENANCE_DRIVER" value="file"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_STORE" value="array"/>
<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>
<server name="MAIL_MAILER" value="array"/>
<server name="PULSE_ENABLED" value="false"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="TELESCOPE_ENABLED" value="false"/>
</php>
<testsuites>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory>tests/Feature</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>app</directory>
</include>
</source>
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_KEY" value="base64:tfsezwCu4ZRixRLA/+yL/qoouX++Q3lPAPOAbtnBCG8="/>
<env name="APP_MAINTENANCE_DRIVER" value="file"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_STORE" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MAIL_MAILER" value="array"/>
<env name="PULSE_ENABLED" value="false"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
</php>
</phpunit>
6 changes: 6 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ public function seedDefaultGroups(): void
$this->afterApplicationCreated(fn() => $this->seed(GroupSeeder::class));
}
}

#[Before]
public function disableVite(): void
{
$this->afterApplicationCreated(fn() => $this->withoutVite());
}
}
Empty file added tests/Unit/.gitkeep
Empty file.

0 comments on commit f30828a

Please sign in to comment.