From 5bfc1993ce2de1d7ed8690944b757b1f0cc90aab Mon Sep 17 00:00:00 2001 From: vassilidev Date: Sun, 16 Apr 2023 17:27:03 +0200 Subject: [PATCH 01/14] feat: Create automatically a thread on discord when creating a repository. --- .../Discord/Channels/CreateChannel.php | 35 +++++++++++ .../Channels/CreateThreadFromRepository.php | 49 +++++++++++++++ app/Actions/Discord/DiscordAction.php | 15 +++++ .../Github/Repository/CreateWebhook.php | 15 ++--- .../CreateWebhookFromRepository.php | 35 +++++++++++ app/DTO/Discord/ChannelData.php | 23 +++++++ app/Enums/Discord/ChannelType.php | 22 +++++++ .../Channel/Requests/CreateGuildChannel.php | 60 +++++++++++++++++++ .../Discord/DiscordAPIConnector.php | 48 +++++++++++++++ .../Github/GithubApiConnector.php | 2 +- .../CreateThreadFromRepositoryJob.php | 31 ++++++++++ .../CreateWebhookFromRepositoryJob.php | 31 ++++++++++ app/Models/Channel.php | 22 +++++++ app/Models/Repository.php | 22 +++++++ app/Observers/RepositoryObserver.php | 25 +++----- config/github.php | 7 --- config/services.php | 12 ++++ ...023_04_16_141842_create_channels_table.php | 34 +++++++++++ 18 files changed, 456 insertions(+), 32 deletions(-) create mode 100644 app/Actions/Discord/Channels/CreateChannel.php create mode 100644 app/Actions/Discord/Channels/CreateThreadFromRepository.php create mode 100644 app/Actions/Discord/DiscordAction.php create mode 100644 app/Actions/Github/Repository/CreateWebhookFromRepository.php create mode 100644 app/DTO/Discord/ChannelData.php create mode 100644 app/Enums/Discord/ChannelType.php create mode 100644 app/Http/Integrations/Discord/Channel/Requests/CreateGuildChannel.php create mode 100644 app/Http/Integrations/Discord/DiscordAPIConnector.php create mode 100644 app/Jobs/Discord/Channels/CreateThreadFromRepositoryJob.php create mode 100644 app/Jobs/Github/Repository/CreateWebhookFromRepositoryJob.php create mode 100644 app/Models/Channel.php delete mode 100644 config/github.php create mode 100644 database/migrations/2023_04_16_141842_create_channels_table.php diff --git a/app/Actions/Discord/Channels/CreateChannel.php b/app/Actions/Discord/Channels/CreateChannel.php new file mode 100644 index 0000000..5a709d1 --- /dev/null +++ b/app/Actions/Discord/Channels/CreateChannel.php @@ -0,0 +1,35 @@ +connector->send($request); + } +} diff --git a/app/Actions/Discord/Channels/CreateThreadFromRepository.php b/app/Actions/Discord/Channels/CreateThreadFromRepository.php new file mode 100644 index 0000000..71c271d --- /dev/null +++ b/app/Actions/Discord/Channels/CreateThreadFromRepository.php @@ -0,0 +1,49 @@ +execute( + name: $this->generateChannelName($repository->full_name), + channelType: ChannelType::FORUM, + parentId: config('services.discord.channels.code_reviews'), + )->dtoOrFail(); + + return $repository->channels()->save( + new Channel([ + 'channel_id' => $threadData->channelId, + 'parent_id' => $threadData->parentId, + 'guild_id' => $threadData->guildId, + 'type' => $threadData->type, + 'name' => $threadData->name, + ]), + ); + } catch (Exception) { + return false; + } + } + + private function generateChannelName(string $name): string + { + return Str::slug( + title: Str::replace( + search: '/', + replace: '-', + subject: $name, + ), + ); + } +} diff --git a/app/Actions/Discord/DiscordAction.php b/app/Actions/Discord/DiscordAction.php new file mode 100644 index 0000000..9e3df67 --- /dev/null +++ b/app/Actions/Discord/DiscordAction.php @@ -0,0 +1,15 @@ +connector = new DiscordAPIConnector; + } +} diff --git a/app/Actions/Github/Repository/CreateWebhook.php b/app/Actions/Github/Repository/CreateWebhook.php index 4bdb700..273831f 100644 --- a/app/Actions/Github/Repository/CreateWebhook.php +++ b/app/Actions/Github/Repository/CreateWebhook.php @@ -2,9 +2,7 @@ namespace App\Actions\Github\Repository; -use App\Models\Webhook; use ReflectionException; -use App\Models\Repository; use Saloon\Contracts\Response; use App\Actions\Github\GithubAction; use Saloon\Exceptions\PendingRequestException; @@ -20,12 +18,15 @@ class CreateWebhook * @throws ReflectionException * @throws PendingRequestException */ - public function execute(Repository $repository): Response - { + public function execute( + string $username, + string $repository, + string $secret, + ): Response { $request = new CreateRepositoryWebhook( - username: $repository->username, - repository: $repository->repository, - secret: $repository->node_id + username: $username, + repository: $repository, + secret: $secret, ); return $this->connector->send($request); diff --git a/app/Actions/Github/Repository/CreateWebhookFromRepository.php b/app/Actions/Github/Repository/CreateWebhookFromRepository.php new file mode 100644 index 0000000..37acf04 --- /dev/null +++ b/app/Actions/Github/Repository/CreateWebhookFromRepository.php @@ -0,0 +1,35 @@ +execute( + username: $repository->username, + repository: $repository->repository, + secret: Hash::make($repository->node_id), + )->dtoOrFail(); + + return $repository->webhooks()->save( + new Webhook([ + 'title' => $repository->full_name . ' hook', + 'hook_id' => $webhookData->id, + ]), + ); + } catch (Exception) { + return false; + } + } +} diff --git a/app/DTO/Discord/ChannelData.php b/app/DTO/Discord/ChannelData.php new file mode 100644 index 0000000..ae517ac --- /dev/null +++ b/app/DTO/Discord/ChannelData.php @@ -0,0 +1,23 @@ + $this->name, + 'type' => $this->channelType->value, + 'parent_id' => $this->parentId, + ]; + } + + /** + * @param Response $response + * + * @return ChannelData + */ + public function createDtoFromResponse(Response $response): ChannelData + { + return ChannelData::from($response->collect()); + } +} diff --git a/app/Http/Integrations/Discord/DiscordAPIConnector.php b/app/Http/Integrations/Discord/DiscordAPIConnector.php new file mode 100644 index 0000000..d21f484 --- /dev/null +++ b/app/Http/Integrations/Discord/DiscordAPIConnector.php @@ -0,0 +1,48 @@ + 'application/json', + 'Content-Type' => 'application/json', + ]; + } + + /** + * @return TokenAuthenticator + */ + protected function defaultAuth(): TokenAuthenticator + { + return new TokenAuthenticator( + token: (string)config('services.discord.bot_token'), + prefix: 'Bot' + ); + } +} diff --git a/app/Http/Integrations/Github/GithubApiConnector.php b/app/Http/Integrations/Github/GithubApiConnector.php index 2ec9146..8cd2e9f 100644 --- a/app/Http/Integrations/Github/GithubApiConnector.php +++ b/app/Http/Integrations/Github/GithubApiConnector.php @@ -23,7 +23,7 @@ class GithubApiConnector extends Connector */ public function resolveBaseUrl(): string { - return (string) config('github.api.base_path'); + return (string) config('services.github.base_path'); } /** diff --git a/app/Jobs/Discord/Channels/CreateThreadFromRepositoryJob.php b/app/Jobs/Discord/Channels/CreateThreadFromRepositoryJob.php new file mode 100644 index 0000000..e88976a --- /dev/null +++ b/app/Jobs/Discord/Channels/CreateThreadFromRepositoryJob.php @@ -0,0 +1,31 @@ +execute($this->repository); + } +} diff --git a/app/Jobs/Github/Repository/CreateWebhookFromRepositoryJob.php b/app/Jobs/Github/Repository/CreateWebhookFromRepositoryJob.php new file mode 100644 index 0000000..069d896 --- /dev/null +++ b/app/Jobs/Github/Repository/CreateWebhookFromRepositoryJob.php @@ -0,0 +1,31 @@ +execute($this->repository); + } +} diff --git a/app/Models/Channel.php b/app/Models/Channel.php new file mode 100644 index 0000000..bfc95d7 --- /dev/null +++ b/app/Models/Channel.php @@ -0,0 +1,22 @@ +morphTo(); + } +} diff --git a/app/Models/Repository.php b/app/Models/Repository.php index 7084b05..a66b4cc 100644 --- a/app/Models/Repository.php +++ b/app/Models/Repository.php @@ -2,10 +2,13 @@ namespace App\Models; +use App\Enums\Discord\ChannelType; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\Casts\Attribute; +use Illuminate\Database\Eloquent\Relations\HasOne; +use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphMany; @@ -40,6 +43,25 @@ public function webhooks(): MorphMany return $this->morphMany(Webhook::class, 'webhookable'); } + /** + * @return MorphMany + */ + public function channels(): MorphMany + { + return $this->morphMany(Channel::class, 'channelable'); + } + + /** + * @return MorphOne + */ + public function threadChannel(): MorphOne + { + return $this->morphOne(Channel::class, 'channelable') + ->ofMany([], function (Builder $query) { + $query->whereType(ChannelType::FORUM); + }); + } + /** * @return Attribute */ diff --git a/app/Observers/RepositoryObserver.php b/app/Observers/RepositoryObserver.php index 25529e6..025af1f 100644 --- a/app/Observers/RepositoryObserver.php +++ b/app/Observers/RepositoryObserver.php @@ -3,11 +3,11 @@ namespace App\Observers; use Exception; -use App\Models\Webhook; use App\Models\Repository; -use App\Actions\Github\Repository\CreateWebhook; +use Illuminate\Support\Facades\Bus; +use App\Jobs\Discord\Channels\CreateThreadFromRepositoryJob; use App\Actions\Github\Repository\DeleteWebhookFromRepository; -use App\DTO\Github\Repository\Webhook\WebhookCreatedData; +use App\Jobs\Github\Repository\CreateWebhookFromRepositoryJob; class RepositoryObserver { @@ -16,21 +16,12 @@ class RepositoryObserver */ public function created(Repository $repository): void { - try { - /** @var WebhookCreatedData $webhookData */ - $webhookData = app(CreateWebhook::class) - ->execute($repository) - ->dtoOrFail(); + CreateWebhookFromRepositoryJob::dispatch($repository); - $repository->webhooks()->save( - new Webhook([ - 'title' => $repository->full_name . ' hook', - 'hook_id' => $webhookData->id, - ]) - ); - } catch (Exception $e) { - return; - } + Bus::chain([ + new CreateThreadFromRepositoryJob($repository), + //TODO: Add first message + ])->dispatch(); } /** diff --git a/config/github.php b/config/github.php deleted file mode 100644 index 9a105d1..0000000 --- a/config/github.php +++ /dev/null @@ -1,7 +0,0 @@ - [ - 'base_path' => env('GITHUB_API_BASEPATH', 'https://api.github.com/'), - ] -]; \ No newline at end of file diff --git a/config/services.php b/config/services.php index 5866815..0436fca 100644 --- a/config/services.php +++ b/config/services.php @@ -39,6 +39,18 @@ 'client_secret' => env('GITHUB_CLIENT_SECRET'), 'redirect' => env('GITHUB_REDIRECT'), 'scopes' => env('GITHUB_SCOPES', 'admin:repo_hook'), + 'base_path' => env('GITHUB_API_BASEPATH', 'https://api.github.com/'), + ], + + 'discord' => [ + 'app_id' => env('DISCORD_APP_ID'), + 'public_key' => env('DISCORD_PUBLIC_KEY'), + 'bot_token' => env('DISCORD_BOT_TOKEN'), + 'guild_id' => env('DISCORD_GUILD_ID'), + 'channels' => [ + 'code_reviews' => env('DISCORD_CODE_REVIEW_CHANNELS_ID'), + ], + 'base_path' => env('DISCORD_API_BASEPATH', 'https://discord.com/api/v10/') ], ]; diff --git a/database/migrations/2023_04_16_141842_create_channels_table.php b/database/migrations/2023_04_16_141842_create_channels_table.php new file mode 100644 index 0000000..cdc1fa7 --- /dev/null +++ b/database/migrations/2023_04_16_141842_create_channels_table.php @@ -0,0 +1,34 @@ +id(); + $table->string('channel_id'); + $table->string('parent_id')->nullable(); + $table->string('guild_id')->nullable(); + /** @see ChannelType */ + $table->integer('type'); + $table->string('name'); + $table->morphs('channelable'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('channels'); + } +}; From 783c25ba91fa6fc6fbef531a23df09f3cc9fe83b Mon Sep 17 00:00:00 2001 From: Mohammad Javad Ghasemy Date: Thu, 4 May 2023 18:04:29 +0330 Subject: [PATCH 02/14] #52 feat: add discord enviroments --- .env.example | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index 16190dd..6b79944 100644 --- a/.env.example +++ b/.env.example @@ -59,4 +59,10 @@ VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" GITHUB_CLIENT_ID= GITHUB_CLIENT_SECRET= -GITHUB_REDIRECT= \ No newline at end of file +GITHUB_REDIRECT= + +DISCORD_APP_ID= +DISCORD_PUBLIC_KEY= +DISCORD_BOT_TOKEN= +DISCORD_GUILD_ID= +DISCORD_CODE_REVIEW_CHANNELS_ID= From 0a778b19ed74276140d0f984ba81bb828afd8c8b Mon Sep 17 00:00:00 2001 From: Mohammad Javad Ghasemy Date: Thu, 8 Jun 2023 10:21:37 +0330 Subject: [PATCH 03/14] fix: CI --- .github/workflows/deploy.yml | 2 +- app/Console/Commands/Discord/TerminateDiscordCommand.php | 2 ++ deploy.sh | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 deploy.sh diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index e593d8a..2c803ec 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -21,4 +21,4 @@ jobs: run: | npm i -g @liara/cli@3 liara deploy --app="codereviewpals" --api-token="$LIARA_TOKEN" --detach - liara shell --app="codereviewpals" --api-token="$LIARA_TOKEN" --command="php artisan migrate --force && php artisan horizon:terminate --wait && php artisan discord:terminate" + liara shell --app="codereviewpals" --api-token="$LIARA_TOKEN" --command="bash deploy.sh" diff --git a/app/Console/Commands/Discord/TerminateDiscordCommand.php b/app/Console/Commands/Discord/TerminateDiscordCommand.php index bffa5cc..701b68d 100644 --- a/app/Console/Commands/Discord/TerminateDiscordCommand.php +++ b/app/Console/Commands/Discord/TerminateDiscordCommand.php @@ -34,6 +34,8 @@ public function handle(): void posix_strerror(posix_get_last_error()) . ')' ); + return; } + cache()->delete(config('cache.discord.process_id')); } } diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..59f800a --- /dev/null +++ b/deploy.sh @@ -0,0 +1,3 @@ +php artisan migrate --force +php artisan horizon:terminate --wait +php artisan discord:terminate From 2f45789374acdb493769768d9f5a6277566aea4b Mon Sep 17 00:00:00 2001 From: Mohammad Javad Ghasemy Date: Thu, 8 Jun 2023 10:27:46 +0330 Subject: [PATCH 04/14] fix: CI test without --detach --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 2c803ec..d01b2cf 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -20,5 +20,5 @@ jobs: LIARA_TOKEN: ${{ secrets.LIARA_API_TOKEN }} run: | npm i -g @liara/cli@3 - liara deploy --app="codereviewpals" --api-token="$LIARA_TOKEN" --detach + liara deploy --app="codereviewpals" --api-token="$LIARA_TOKEN" liara shell --app="codereviewpals" --api-token="$LIARA_TOKEN" --command="bash deploy.sh" From d10cc1ada9db8f2a6eb4b8b51b58391f1a5085a7 Mon Sep 17 00:00:00 2001 From: Mohammad Javad Ghasemy Date: Thu, 8 Jun 2023 10:38:39 +0330 Subject: [PATCH 05/14] Revert "fix: CI test without --detach" This reverts commit 2f45789374acdb493769768d9f5a6277566aea4b. --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index d01b2cf..2c803ec 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -20,5 +20,5 @@ jobs: LIARA_TOKEN: ${{ secrets.LIARA_API_TOKEN }} run: | npm i -g @liara/cli@3 - liara deploy --app="codereviewpals" --api-token="$LIARA_TOKEN" + liara deploy --app="codereviewpals" --api-token="$LIARA_TOKEN" --detach liara shell --app="codereviewpals" --api-token="$LIARA_TOKEN" --command="bash deploy.sh" From 11a4163c1952f5f3f1404e17a089eab9cea5c95c Mon Sep 17 00:00:00 2001 From: Mohammad Javad Ghasemy Date: Thu, 8 Jun 2023 11:46:17 +0330 Subject: [PATCH 06/14] chor: config log channels --- config/logging.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/logging.php b/config/logging.php index c44d276..389d873 100644 --- a/config/logging.php +++ b/config/logging.php @@ -54,7 +54,7 @@ 'channels' => [ 'stack' => [ 'driver' => 'stack', - 'channels' => ['single'], + 'channels' => ['single', 'stderr', 'errorlog', 'syslog'], 'ignore_exceptions' => false, ], From a160ce47bb461d1eee65dfee2eec723e7f7eb36a Mon Sep 17 00:00:00 2001 From: Mohammad Javad Ghasemy Date: Thu, 8 Jun 2023 11:56:49 +0330 Subject: [PATCH 07/14] fix: don't throw error. log me please --- app/Console/Commands/Discord/RunDiscordBotCommand.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/Console/Commands/Discord/RunDiscordBotCommand.php b/app/Console/Commands/Discord/RunDiscordBotCommand.php index 2ac7fb9..c8b8fc0 100644 --- a/app/Console/Commands/Discord/RunDiscordBotCommand.php +++ b/app/Console/Commands/Discord/RunDiscordBotCommand.php @@ -8,6 +8,7 @@ use Discord\WebSockets\Intents; use Illuminate\Console\Command; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Log; class RunDiscordBotCommand extends Command { @@ -52,7 +53,12 @@ public function messageHandler(Message $message, Discord $discord) // don't listen to messages without ! return; } - return $this->messageRouting($message, $discord); + try { + return $this->messageRouting($message, $discord); + } catch (\Throwable $th) { + Log::error($th->getMessage(), $th->getTrace()); + dump($th->getMessage()); + } } protected function messageRouting(Message $inputMessage, Discord $discord) From 8e3f652235e0540adeb2753118657cfc02da39e4 Mon Sep 17 00:00:00 2001 From: Mohammad Javad Ghasemy Date: Thu, 8 Jun 2023 12:11:32 +0330 Subject: [PATCH 08/14] #fix: maybe better error handling --- app/Actions/Discord/AddPullRequestCommandAction.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Actions/Discord/AddPullRequestCommandAction.php b/app/Actions/Discord/AddPullRequestCommandAction.php index 9c6fcd9..f3db140 100644 --- a/app/Actions/Discord/AddPullRequestCommandAction.php +++ b/app/Actions/Discord/AddPullRequestCommandAction.php @@ -17,9 +17,9 @@ public function __construct(protected PullRequestService $service) public function __invoke(Message $message, Discord $discord, array $matches) { - $url = $matches[1] ?? ''; - $user = $this->getUser($message->author); try { + $url = $matches[1] ?? ''; + $user = $this->getUser($message->author); $this->service->createFromUrl($url, $user); $message->reply( "Thank you for your interest ❤️ \n Your Pull Request submitted successfully." From 349388eaa8ceb65aab7f897834943f335e6ff7eb Mon Sep 17 00:00:00 2001 From: Mohammad Javad Ghasemy Date: Thu, 8 Jun 2023 12:12:51 +0330 Subject: [PATCH 09/14] feat: handle unvalid commands by bot --- app/Console/Commands/Discord/RunDiscordBotCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Console/Commands/Discord/RunDiscordBotCommand.php b/app/Console/Commands/Discord/RunDiscordBotCommand.php index c8b8fc0..b142956 100644 --- a/app/Console/Commands/Discord/RunDiscordBotCommand.php +++ b/app/Console/Commands/Discord/RunDiscordBotCommand.php @@ -70,6 +70,7 @@ protected function messageRouting(Message $inputMessage, Discord $discord) } continue; } + $message->reply('Your command is not valid.'); } private function storeProcess() From 6ba9adb7c700735485d1f9390850caac8d1e14c0 Mon Sep 17 00:00:00 2001 From: Mohammad Javad Ghasemy Date: Thu, 8 Jun 2023 12:21:32 +0330 Subject: [PATCH 10/14] fix: should be input message --- app/Console/Commands/Discord/RunDiscordBotCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Console/Commands/Discord/RunDiscordBotCommand.php b/app/Console/Commands/Discord/RunDiscordBotCommand.php index b142956..5e14fb1 100644 --- a/app/Console/Commands/Discord/RunDiscordBotCommand.php +++ b/app/Console/Commands/Discord/RunDiscordBotCommand.php @@ -70,7 +70,7 @@ protected function messageRouting(Message $inputMessage, Discord $discord) } continue; } - $message->reply('Your command is not valid.'); + $inputMessage->reply('Your command is not valid.'); } private function storeProcess() From 3fbbfb8d662c9b68c5ab75f9842ecdc5bf8337eb Mon Sep 17 00:00:00 2001 From: Mohammad Javad Ghasemy Date: Wed, 14 Jun 2023 12:15:04 +0330 Subject: [PATCH 11/14] fix: issue about PR and discord --- app/Actions/Discord/AddPullRequestCommandAction.php | 4 ++-- app/Console/Commands/Discord/RunDiscordBotCommand.php | 7 +------ app/Services/Github/PullRequestService.php | 4 +--- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/app/Actions/Discord/AddPullRequestCommandAction.php b/app/Actions/Discord/AddPullRequestCommandAction.php index f3db140..9c6fcd9 100644 --- a/app/Actions/Discord/AddPullRequestCommandAction.php +++ b/app/Actions/Discord/AddPullRequestCommandAction.php @@ -17,9 +17,9 @@ public function __construct(protected PullRequestService $service) public function __invoke(Message $message, Discord $discord, array $matches) { + $url = $matches[1] ?? ''; + $user = $this->getUser($message->author); try { - $url = $matches[1] ?? ''; - $user = $this->getUser($message->author); $this->service->createFromUrl($url, $user); $message->reply( "Thank you for your interest ❤️ \n Your Pull Request submitted successfully." diff --git a/app/Console/Commands/Discord/RunDiscordBotCommand.php b/app/Console/Commands/Discord/RunDiscordBotCommand.php index 5e14fb1..75d8669 100644 --- a/app/Console/Commands/Discord/RunDiscordBotCommand.php +++ b/app/Console/Commands/Discord/RunDiscordBotCommand.php @@ -53,12 +53,7 @@ public function messageHandler(Message $message, Discord $discord) // don't listen to messages without ! return; } - try { - return $this->messageRouting($message, $discord); - } catch (\Throwable $th) { - Log::error($th->getMessage(), $th->getTrace()); - dump($th->getMessage()); - } + return $this->messageRouting($message, $discord); } protected function messageRouting(Message $inputMessage, Discord $discord) diff --git a/app/Services/Github/PullRequestService.php b/app/Services/Github/PullRequestService.php index dea69d3..5fa811e 100644 --- a/app/Services/Github/PullRequestService.php +++ b/app/Services/Github/PullRequestService.php @@ -71,9 +71,7 @@ public function createFromUrl(string $url, User $user): ?PullRequest { $pullRequestData = $this->getDataFromUrl(url: $url); - if (!$pullRequestData instanceof PullRequestData) { - exit(); - } + throw_if(!$pullRequestData instanceof PullRequestData, 'invalid pull request'); return app(CreatePullRequest::class)->execute( pullRequestData: $pullRequestData, From a29a85b47348967765fec7c4c22f41f663b48ecc Mon Sep 17 00:00:00 2001 From: Mohammad Javad Ghasemy Date: Sat, 17 Jun 2023 11:45:50 +0330 Subject: [PATCH 12/14] refactor: to queue --- .../Discord/AddPullRequestCommandAction.php | 17 ++++------ app/Jobs/CreatePullRequestByUrlJob.php | 32 +++++++++++++++++++ 2 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 app/Jobs/CreatePullRequestByUrlJob.php diff --git a/app/Actions/Discord/AddPullRequestCommandAction.php b/app/Actions/Discord/AddPullRequestCommandAction.php index 9c6fcd9..080a3d1 100644 --- a/app/Actions/Discord/AddPullRequestCommandAction.php +++ b/app/Actions/Discord/AddPullRequestCommandAction.php @@ -2,6 +2,7 @@ namespace App\Actions\Discord; +use App\Jobs\CreatePullRequestByUrlJob; use App\Models\User; use App\Services\Github\PullRequestService; use Discord\Builders\MessageBuilder; @@ -19,16 +20,12 @@ public function __invoke(Message $message, Discord $discord, array $matches) { $url = $matches[1] ?? ''; $user = $this->getUser($message->author); - try { - $this->service->createFromUrl($url, $user); - $message->reply( - "Thank you for your interest ❤️ \n Your Pull Request submitted successfully." - ); - } catch (\Throwable $th) { - $message->reply( - 'error occurred on creating PR. please check your URL and if everything is ok contact with admins 😉' - ); - } + + CreatePullRequestByUrlJob::dispatch($url, $user); + + $message->reply( + "Thank you for your interest ❤️ \n Your Pull Request submitted successfully." + ); } public function getUser(DiscordUser $discordUser): User diff --git a/app/Jobs/CreatePullRequestByUrlJob.php b/app/Jobs/CreatePullRequestByUrlJob.php new file mode 100644 index 0000000..f60394d --- /dev/null +++ b/app/Jobs/CreatePullRequestByUrlJob.php @@ -0,0 +1,32 @@ +createFromUrl($this->url, $this->user); + } +} From a3b27a6340bad409495d3b4bc1e134037400fe9b Mon Sep 17 00:00:00 2001 From: Mohammad Javad Ghasemy Date: Sun, 18 Jun 2023 23:10:08 +0330 Subject: [PATCH 13/14] #52 feat: work in progress to send PR to discord --- ...GetOrCreateAChannelByPullRequestAction.php | 38 +++++++++++ .../Discord/Channels/CreateChannel.php | 2 +- .../Channels/CreateThreadFromRepository.php | 49 --------------- .../Discord/CreateThreadFromPullRequest.php | 39 ++++++++++++ .../CreateWebhookFromRepository.php | 16 ++--- app/Actions/Github/Webhook/FetchWebhook.php | 2 +- .../CreateRepositoryByPullRequest.php | 4 +- app/DTO/Discord/ForumThreadData.php | 23 +++++++ app/DTO/Discord/MessageObject.php | 18 ++++++ app/DTO/RepositoryData.php | 1 + .../Channel/Requests/CreateForumPost.php | 63 +++++++++++++++++++ .../CreateThreadFromPullRequestJob.php | 45 +++++++++++++ .../CreateThreadFromRepositoryJob.php | 31 --------- .../FetchAllPullRequestFromRepository.php | 2 +- app/Models/Channel.php | 17 ++--- app/Models/PullRequest.php | 8 ++- app/Models/Repository.php | 18 ++++-- app/Observers/PullRequestObserver.php | 5 +- app/Observers/RepositoryObserver.php | 7 --- config/services.php | 20 +++--- ...repository_name_on_pull_requests_table.php | 27 ++++++++ ...55908_add_topics_to_repositories_table.php | 27 ++++++++ ...nge_description_to_pull_requests_table.php | 27 ++++++++ .../2023_06_18_161328_create_jobs_table.php | 32 ++++++++++ ...2_remove_channelable_to_channels_table.php | 27 ++++++++ database/seeders/ChannelSeeder.php | 22 +++++++ 26 files changed, 445 insertions(+), 125 deletions(-) create mode 100644 app/Actions/Channel/GetOrCreateAChannelByPullRequestAction.php delete mode 100644 app/Actions/Discord/Channels/CreateThreadFromRepository.php create mode 100644 app/Actions/Discord/CreateThreadFromPullRequest.php create mode 100644 app/DTO/Discord/ForumThreadData.php create mode 100644 app/DTO/Discord/MessageObject.php create mode 100644 app/Http/Integrations/Discord/Channel/Requests/CreateForumPost.php create mode 100644 app/Jobs/Discord/Channels/CreateThreadFromPullRequestJob.php delete mode 100644 app/Jobs/Discord/Channels/CreateThreadFromRepositoryJob.php create mode 100644 database/migrations/2023_06_18_102733_change_repository_to_repository_name_on_pull_requests_table.php create mode 100644 database/migrations/2023_06_18_155908_add_topics_to_repositories_table.php create mode 100644 database/migrations/2023_06_18_160726_change_description_to_pull_requests_table.php create mode 100644 database/migrations/2023_06_18_161328_create_jobs_table.php create mode 100644 database/migrations/2023_06_18_163222_remove_channelable_to_channels_table.php create mode 100644 database/seeders/ChannelSeeder.php diff --git a/app/Actions/Channel/GetOrCreateAChannelByPullRequestAction.php b/app/Actions/Channel/GetOrCreateAChannelByPullRequestAction.php new file mode 100644 index 0000000..f8f4f7e --- /dev/null +++ b/app/Actions/Channel/GetOrCreateAChannelByPullRequestAction.php @@ -0,0 +1,38 @@ +repository->language; + + $channel = Channel::name($mainLang)->first(); + if ($channel instanceof Channel) { + return $channel; + } + $channelData = app(CreateChannel::class) + ->execute( + name: $mainLang, + channelType: ChannelType::FORUM, + parentId: config('services.discord.channels.code_reviews') + ) + ->dtoOrFail(); + + return Channel::create($channelData->toArray()); + } +} diff --git a/app/Actions/Discord/Channels/CreateChannel.php b/app/Actions/Discord/Channels/CreateChannel.php index 5a709d1..610dbc2 100644 --- a/app/Actions/Discord/Channels/CreateChannel.php +++ b/app/Actions/Discord/Channels/CreateChannel.php @@ -27,7 +27,7 @@ public function execute( $request = new CreateGuildChannel( name: $name, channelType: $channelType, - parentId: $parentId, + parentId: $parentId ); return $this->connector->send($request); diff --git a/app/Actions/Discord/Channels/CreateThreadFromRepository.php b/app/Actions/Discord/Channels/CreateThreadFromRepository.php deleted file mode 100644 index 71c271d..0000000 --- a/app/Actions/Discord/Channels/CreateThreadFromRepository.php +++ /dev/null @@ -1,49 +0,0 @@ -execute( - name: $this->generateChannelName($repository->full_name), - channelType: ChannelType::FORUM, - parentId: config('services.discord.channels.code_reviews'), - )->dtoOrFail(); - - return $repository->channels()->save( - new Channel([ - 'channel_id' => $threadData->channelId, - 'parent_id' => $threadData->parentId, - 'guild_id' => $threadData->guildId, - 'type' => $threadData->type, - 'name' => $threadData->name, - ]), - ); - } catch (Exception) { - return false; - } - } - - private function generateChannelName(string $name): string - { - return Str::slug( - title: Str::replace( - search: '/', - replace: '-', - subject: $name, - ), - ); - } -} diff --git a/app/Actions/Discord/CreateThreadFromPullRequest.php b/app/Actions/Discord/CreateThreadFromPullRequest.php new file mode 100644 index 0000000..690d221 --- /dev/null +++ b/app/Actions/Discord/CreateThreadFromPullRequest.php @@ -0,0 +1,39 @@ +repository; + $tags = $repository->topics; + $tags[] = $repository->language; + $message = $this->makeMessage($pullRequest); + $request = new CreateForumPost( + name: $pullRequest->title, + message: $message, + channelId: $channel->channel_id + ); + + return $this->connector->send($request); + } + + public function makeMessage(PullRequest $pullRequest): MessageObject + { + return new MessageObject('HELLO'); + } +} diff --git a/app/Actions/Github/Repository/CreateWebhookFromRepository.php b/app/Actions/Github/Repository/CreateWebhookFromRepository.php index 37acf04..e9108d0 100644 --- a/app/Actions/Github/Repository/CreateWebhookFromRepository.php +++ b/app/Actions/Github/Repository/CreateWebhookFromRepository.php @@ -16,17 +16,19 @@ class CreateWebhookFromRepository public function execute(Repository $repository): Model|bool { try { - $webhookData = app(CreateWebhook::class)->execute( - username: $repository->username, - repository: $repository->repository, - secret: Hash::make($repository->node_id), - )->dtoOrFail(); + $webhookData = app(CreateWebhook::class) + ->execute( + username: $repository->username, + repository: $repository->repository_name, + secret: Hash::make($repository->node_id) + ) + ->dtoOrFail(); return $repository->webhooks()->save( new Webhook([ - 'title' => $repository->full_name . ' hook', + 'title' => $repository->full_name . ' hook', 'hook_id' => $webhookData->id, - ]), + ]) ); } catch (Exception) { return false; diff --git a/app/Actions/Github/Webhook/FetchWebhook.php b/app/Actions/Github/Webhook/FetchWebhook.php index 68bb0c8..748ebf8 100644 --- a/app/Actions/Github/Webhook/FetchWebhook.php +++ b/app/Actions/Github/Webhook/FetchWebhook.php @@ -42,7 +42,7 @@ public function getUsernameAndRepository(Repository|PullRequest $model): array if ($model instanceof Repository) { return [ 'username' => $model->username, - 'repository' => $model->repository, + 'repository' => $model->repository_name, ]; } return app(PullRequestService::class)->getRegexMatch($model->html_url); diff --git a/app/Actions/Repository/CreateRepositoryByPullRequest.php b/app/Actions/Repository/CreateRepositoryByPullRequest.php index 1f7ad2d..eca3b7c 100644 --- a/app/Actions/Repository/CreateRepositoryByPullRequest.php +++ b/app/Actions/Repository/CreateRepositoryByPullRequest.php @@ -23,7 +23,9 @@ public function __construct(protected RepositoryService $repositoryService) */ public function execute(PullRequest $pullRequest): Repository { - $repository = $this->repositoryService->getRepositoryByFullName($pullRequest->repository); + $repository = $this->repositoryService->getRepositoryByFullName( + $pullRequest->repository_name + ); if (!is_null($repository)) { return $repository; } diff --git a/app/DTO/Discord/ForumThreadData.php b/app/DTO/Discord/ForumThreadData.php new file mode 100644 index 0000000..5452f05 --- /dev/null +++ b/app/DTO/Discord/ForumThreadData.php @@ -0,0 +1,23 @@ +channelId); + } + + protected function defaultBody(): array + { + return [ + 'auto_archive_duration' => config('services.discord.configs.auto_archive_time'), + 'name' => $this->name, + 'message' => $this->message, + 'applied_tags' => $this->tags, + ]; + } + + /** + * @param Response $response + * + * @return ChannelData + */ + public function createDtoFromResponse(Response $response): ForumThreadData + { + return ForumThreadData::from($response->collect()); + } +} diff --git a/app/Jobs/Discord/Channels/CreateThreadFromPullRequestJob.php b/app/Jobs/Discord/Channels/CreateThreadFromPullRequestJob.php new file mode 100644 index 0000000..2f54106 --- /dev/null +++ b/app/Jobs/Discord/Channels/CreateThreadFromPullRequestJob.php @@ -0,0 +1,45 @@ +execute($this->pullRequest); + + app(CreateThreadFromPullRequest::class)->execute($this->pullRequest, $channel); + } +} diff --git a/app/Jobs/Discord/Channels/CreateThreadFromRepositoryJob.php b/app/Jobs/Discord/Channels/CreateThreadFromRepositoryJob.php deleted file mode 100644 index e88976a..0000000 --- a/app/Jobs/Discord/Channels/CreateThreadFromRepositoryJob.php +++ /dev/null @@ -1,31 +0,0 @@ -execute($this->repository); - } -} diff --git a/app/Jobs/Github/Repository/FetchAllPullRequestFromRepository.php b/app/Jobs/Github/Repository/FetchAllPullRequestFromRepository.php index ca0e985..22a73f2 100644 --- a/app/Jobs/Github/Repository/FetchAllPullRequestFromRepository.php +++ b/app/Jobs/Github/Repository/FetchAllPullRequestFromRepository.php @@ -35,7 +35,7 @@ public function handle(): void $pullRequests = app(GetAllPullRequest::class) ->execute( username: $this->repository->username, - repository: $this->repository->repository + repository: $this->repository->repository_name ) ->dtoOrFail(); diff --git a/app/Models/Channel.php b/app/Models/Channel.php index bfc95d7..7021f98 100644 --- a/app/Models/Channel.php +++ b/app/Models/Channel.php @@ -2,21 +2,22 @@ namespace App\Models; +use App\Enums\Discord\ChannelType; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphTo; class Channel extends Model { - protected $fillable = [ - 'channel_id', - 'parent_id', - 'guild_id', - 'type', - 'name', + protected $fillable = ['channel_id', 'parent_id', 'guild_id', 'type', 'name']; + + protected $casts = [ + 'type' => ChannelType::class, ]; - public function channelable(): MorphTo + public function scopeName(Builder $query, string $name): Builder { - return $this->morphTo(); + return $query->where('name', $name); } } diff --git a/app/Models/PullRequest.php b/app/Models/PullRequest.php index 8227300..6bdc0e7 100644 --- a/app/Models/PullRequest.php +++ b/app/Models/PullRequest.php @@ -17,12 +17,13 @@ class PullRequest extends Model */ protected $fillable = [ 'node_id', - 'repository', + 'repository_name', 'title', 'html_url', 'status', 'description', 'user_id', + 'repository_id', ]; /** @@ -39,4 +40,9 @@ public function user(): BelongsTo { return $this->belongsTo(User::class); } + + public function repository(): BelongsTo + { + return $this->belongsTo(Repository::class); + } } diff --git a/app/Models/Repository.php b/app/Models/Repository.php index f92bdac..0f28998 100644 --- a/app/Models/Repository.php +++ b/app/Models/Repository.php @@ -29,12 +29,17 @@ class Repository extends Model 'description', 'language', 'html_url', + 'topics', + ]; + + protected $casts = [ + 'topics' => 'array', ]; /** * @var string[] */ - protected $appends = ['username', 'repository']; + protected $appends = ['username', 'repository_name']; /** * @return BelongsTo @@ -65,10 +70,11 @@ public function channels(): MorphMany */ public function threadChannel(): MorphOne { - return $this->morphOne(Channel::class, 'channelable') - ->ofMany([], function (Builder $query) { - $query->whereType(ChannelType::FORUM); - }); + return $this->morphOne(Channel::class, 'channelable')->ofMany([], function ( + Builder $query + ) { + $query->whereType(ChannelType::FORUM); + }); } /** @@ -82,7 +88,7 @@ public function username(): Attribute /** * @return Attribute */ - public function repository(): Attribute + public function repositoryName(): Attribute { return Attribute::make(get: fn() => explode('/', $this->full_name)[1]); } diff --git a/app/Observers/PullRequestObserver.php b/app/Observers/PullRequestObserver.php index d3aee37..f6bda1b 100644 --- a/app/Observers/PullRequestObserver.php +++ b/app/Observers/PullRequestObserver.php @@ -5,6 +5,7 @@ use App\Models\PullRequest; use App\Actions\PullRequest\GenerateRepositoryName; use App\Actions\Repository\CreateRepositoryByPullRequest; +use App\Jobs\Discord\Channels\CreateThreadFromPullRequestJob; class PullRequestObserver { @@ -13,10 +14,12 @@ class PullRequestObserver */ public function created(PullRequest $pullRequest): void { - $pullRequest->repository = app(GenerateRepositoryName::class)->execute($pullRequest); + $pullRequest->repository_name = app(GenerateRepositoryName::class)->execute($pullRequest); $repository = app(CreateRepositoryByPullRequest::class)->execute($pullRequest); $pullRequest->repository_id = $repository->id; $pullRequest->saveQuietly(); + + CreateThreadFromPullRequestJob::dispatch($pullRequest); } /** diff --git a/app/Observers/RepositoryObserver.php b/app/Observers/RepositoryObserver.php index 5c46677..e66ee65 100644 --- a/app/Observers/RepositoryObserver.php +++ b/app/Observers/RepositoryObserver.php @@ -4,8 +4,6 @@ use Exception; use App\Models\Repository; -use Illuminate\Support\Facades\Bus; -use App\Jobs\Discord\Channels\CreateThreadFromRepositoryJob; use App\Actions\Github\Repository\DeleteWebhookFromRepository; use App\Jobs\Github\Repository\CreateWebhookFromRepositoryJob; @@ -25,11 +23,6 @@ public function restoring(Repository $repository): bool public function created(Repository $repository): void { CreateWebhookFromRepositoryJob::dispatch($repository); - - Bus::chain([ - new CreateThreadFromRepositoryJob($repository), - //TODO: Add first message - ])->dispatch(); } /** diff --git a/config/services.php b/config/services.php index 0436fca..178f069 100644 --- a/config/services.php +++ b/config/services.php @@ -1,7 +1,6 @@ [ - 'client_id' => env('GITHUB_CLIENT_ID'), + 'client_id' => env('GITHUB_CLIENT_ID'), 'client_secret' => env('GITHUB_CLIENT_SECRET'), - 'redirect' => env('GITHUB_REDIRECT'), - 'scopes' => env('GITHUB_SCOPES', 'admin:repo_hook'), - 'base_path' => env('GITHUB_API_BASEPATH', 'https://api.github.com/'), + 'redirect' => env('GITHUB_REDIRECT'), + 'scopes' => env('GITHUB_SCOPES', 'admin:repo_hook'), + 'base_path' => env('GITHUB_API_BASEPATH', 'https://api.github.com/'), ], 'discord' => [ - 'app_id' => env('DISCORD_APP_ID'), + 'app_id' => env('DISCORD_APP_ID'), 'public_key' => env('DISCORD_PUBLIC_KEY'), - 'bot_token' => env('DISCORD_BOT_TOKEN'), - 'guild_id' => env('DISCORD_GUILD_ID'), - 'channels' => [ + 'bot_token' => env('DISCORD_BOT_TOKEN'), + 'guild_id' => env('DISCORD_GUILD_ID'), + 'channels' => [ 'code_reviews' => env('DISCORD_CODE_REVIEW_CHANNELS_ID'), ], - 'base_path' => env('DISCORD_API_BASEPATH', 'https://discord.com/api/v10/') + 'base_path' => env('DISCORD_API_BASEPATH', 'https://discord.com/api/v10/'), ], - ]; diff --git a/database/migrations/2023_06_18_102733_change_repository_to_repository_name_on_pull_requests_table.php b/database/migrations/2023_06_18_102733_change_repository_to_repository_name_on_pull_requests_table.php new file mode 100644 index 0000000..da912aa --- /dev/null +++ b/database/migrations/2023_06_18_102733_change_repository_to_repository_name_on_pull_requests_table.php @@ -0,0 +1,27 @@ +renameColumn('repository', 'repository_name')->change(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('pull_requests', function (Blueprint $table) { + $table->renameColumn('repository_name', 'repository')->change(); + }); + } +}; diff --git a/database/migrations/2023_06_18_155908_add_topics_to_repositories_table.php b/database/migrations/2023_06_18_155908_add_topics_to_repositories_table.php new file mode 100644 index 0000000..2cb42f3 --- /dev/null +++ b/database/migrations/2023_06_18_155908_add_topics_to_repositories_table.php @@ -0,0 +1,27 @@ +json('topics')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('repositories', function (Blueprint $table) { + $table->dropColumn('topics'); + }); + } +}; diff --git a/database/migrations/2023_06_18_160726_change_description_to_pull_requests_table.php b/database/migrations/2023_06_18_160726_change_description_to_pull_requests_table.php new file mode 100644 index 0000000..c27500d --- /dev/null +++ b/database/migrations/2023_06_18_160726_change_description_to_pull_requests_table.php @@ -0,0 +1,27 @@ +text('description')->change(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('pull_requests', function (Blueprint $table) { + $table->string('description')->change(); + }); + } +}; diff --git a/database/migrations/2023_06_18_161328_create_jobs_table.php b/database/migrations/2023_06_18_161328_create_jobs_table.php new file mode 100644 index 0000000..6098d9b --- /dev/null +++ b/database/migrations/2023_06_18_161328_create_jobs_table.php @@ -0,0 +1,32 @@ +bigIncrements('id'); + $table->string('queue')->index(); + $table->longText('payload'); + $table->unsignedTinyInteger('attempts'); + $table->unsignedInteger('reserved_at')->nullable(); + $table->unsignedInteger('available_at'); + $table->unsignedInteger('created_at'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('jobs'); + } +}; diff --git a/database/migrations/2023_06_18_163222_remove_channelable_to_channels_table.php b/database/migrations/2023_06_18_163222_remove_channelable_to_channels_table.php new file mode 100644 index 0000000..05d1b4e --- /dev/null +++ b/database/migrations/2023_06_18_163222_remove_channelable_to_channels_table.php @@ -0,0 +1,27 @@ +dropMorphs('channelable'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('channels', function (Blueprint $table) { + $table->morphs('channelable'); + }); + } +}; diff --git a/database/seeders/ChannelSeeder.php b/database/seeders/ChannelSeeder.php new file mode 100644 index 0000000..dc7a28f --- /dev/null +++ b/database/seeders/ChannelSeeder.php @@ -0,0 +1,22 @@ + Date: Sun, 18 Jun 2023 23:41:43 +0330 Subject: [PATCH 14/14] feat: horizon for production --- app/Console/Kernel.php | 1 + app/Providers/HorizonServiceProvider.php | 4 +--- composer.json | 2 +- composer.lock | 14 +++++++------- config/app.php | 1 + config/horizon.php | 8 +++----- 6 files changed, 14 insertions(+), 16 deletions(-) diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 713e7cb..45df8e5 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -15,6 +15,7 @@ protected function schedule(Schedule $schedule): void $schedule->command('github:fetch-pull-request')->hourly(); $schedule->command('github:disable-unavailable-webhooks')->daily(); $schedule->command('queue:flush --hours=24')->daily(); + $schedule->command('horizon:snapshot')->everyFiveMinutes(); } /** diff --git a/app/Providers/HorizonServiceProvider.php b/app/Providers/HorizonServiceProvider.php index 76c42e5..9d30bdf 100644 --- a/app/Providers/HorizonServiceProvider.php +++ b/app/Providers/HorizonServiceProvider.php @@ -30,9 +30,7 @@ public function boot(): void protected function gate(): void { Gate::define('viewHorizon', function ($user) { - return in_array($user->email, [ - // - ]); + return in_array($user->email, config('horizon.admins', [])); }); } } diff --git a/composer.json b/composer.json index 4999499..877464c 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "guzzlehttp/guzzle": "^7.2", "inertiajs/inertia-laravel": "^0.6.3", "laravel/framework": "^10.0", - "laravel/horizon": "^5.15", + "laravel/horizon": "^5.16", "laravel/sanctum": "^3.2", "laravel/socialite": "^5.6", "laravel/tinker": "^2.8", diff --git a/composer.lock b/composer.lock index f4ba6cf..e4d2d1f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e845e16fa7d4738fbc00528e735bf5a4", + "content-hash": "8dff9937ea21c4bc1a3dff592f985f45", "packages": [ { "name": "brick/math", @@ -1501,16 +1501,16 @@ }, { "name": "laravel/horizon", - "version": "v5.15.0", + "version": "v5.16.1", "source": { "type": "git", "url": "https://github.com/laravel/horizon.git", - "reference": "b49be302566365e0e4d517aac9995a8fe20b580e" + "reference": "ddd6a49063ba3bdfdf5f8ce885d27a8368ad03c4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/horizon/zipball/b49be302566365e0e4d517aac9995a8fe20b580e", - "reference": "b49be302566365e0e4d517aac9995a8fe20b580e", + "url": "https://api.github.com/repos/laravel/horizon/zipball/ddd6a49063ba3bdfdf5f8ce885d27a8368ad03c4", + "reference": "ddd6a49063ba3bdfdf5f8ce885d27a8368ad03c4", "shasum": "" }, "require": { @@ -1573,9 +1573,9 @@ ], "support": { "issues": "https://github.com/laravel/horizon/issues", - "source": "https://github.com/laravel/horizon/tree/v5.15.0" + "source": "https://github.com/laravel/horizon/tree/v5.16.1" }, - "time": "2023-03-07T17:45:21+00:00" + "time": "2023-05-29T15:03:42+00:00" }, { "name": "laravel/sanctum", diff --git a/config/app.php b/config/app.php index ef76a7e..e01fa28 100644 --- a/config/app.php +++ b/config/app.php @@ -193,6 +193,7 @@ App\Providers\AuthServiceProvider::class, // App\Providers\BroadcastServiceProvider::class, App\Providers\EventServiceProvider::class, + App\Providers\HorizonServiceProvider::class, App\Providers\RouteServiceProvider::class, ], diff --git a/config/horizon.php b/config/horizon.php index 5101f6f..2167f5c 100644 --- a/config/horizon.php +++ b/config/horizon.php @@ -3,7 +3,6 @@ use Illuminate\Support\Str; return [ - /* |-------------------------------------------------------------------------- | Horizon Domain @@ -54,10 +53,7 @@ | */ - 'prefix' => env( - 'HORIZON_PREFIX', - Str::slug(env('APP_NAME', 'laravel'), '_').'_horizon:' - ), + 'prefix' => env('HORIZON_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_') . '_horizon:'), /* |-------------------------------------------------------------------------- @@ -210,4 +206,6 @@ ], ], ], + + 'admins' => ['geeksesi@gmail.com'], ];