From 551233de1a6fbd3dab81953061218f0833d26ab5 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Tue, 7 Nov 2023 22:04:11 +0700 Subject: [PATCH 1/4] update LICENSE --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 7e4cce4..792c300 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 Lbil Technologies +Copyright (c) 2023 CSlant Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From b9accae557eabdf1448851a260bb37df72d81c1d Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Tue, 7 Nov 2023 22:04:46 +0700 Subject: [PATCH 2/4] fix: update exception for notification --- src/Exceptions/SendNotificationException.php | 4 ++-- src/Structures/Notification.php | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Exceptions/SendNotificationException.php b/src/Exceptions/SendNotificationException.php index db3f356..9e16740 100644 --- a/src/Exceptions/SendNotificationException.php +++ b/src/Exceptions/SendNotificationException.php @@ -4,8 +4,8 @@ final class SendNotificationException extends TelegramGitNotifierException { - public static function create(): self + public static function create(?string $exception = null): self { - return new self('Can\'t send notification'); + return new self('Can\'t send notification. ' . ($exception ?? '')); } } diff --git a/src/Structures/Notification.php b/src/Structures/Notification.php index 4f60e61..b1658cd 100644 --- a/src/Structures/Notification.php +++ b/src/Structures/Notification.php @@ -94,11 +94,10 @@ public function sendNotify(string $message = null, array $options = []): bool return true; } - throw SendNotificationException::create(); + $body = (string) $response->getBody(); + throw SendNotificationException::create($body); } catch (GuzzleException $e) { - error_log($e->getMessage()); + throw SendNotificationException::create($e->getMessage()); } - - return false; } } From 1e073b898c64370aff54ed0188b550b36692dd92 Mon Sep 17 00:00:00 2001 From: tanhongit Date: Tue, 7 Nov 2023 15:05:22 +0000 Subject: [PATCH 3/4] Fix styling --- src/Structures/Notification.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Structures/Notification.php b/src/Structures/Notification.php index b1658cd..6ab09d8 100644 --- a/src/Structures/Notification.php +++ b/src/Structures/Notification.php @@ -95,6 +95,7 @@ public function sendNotify(string $message = null, array $options = []): bool } $body = (string) $response->getBody(); + throw SendNotificationException::create($body); } catch (GuzzleException $e) { throw SendNotificationException::create($e->getMessage()); From f507dce796e7787c986cd9cd56ab0547b1f3f96d Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Tue, 7 Nov 2023 23:13:16 +0700 Subject: [PATCH 4/4] fix: update exception for config files --- src/Bot.php | 26 ++++++++++++++++++++++++-- src/Exceptions/ConfigFileException.php | 16 ++++++++++++++++ src/Interfaces/BotInterface.php | 7 +++++++ src/Interfaces/EventInterface.php | 9 ++++++++- src/Notifier.php | 18 +++++++++++++++--- src/Trait/EventTrait.php | 13 ++++++++++++- tests/ConfigTest.php | 14 ++++++++++++++ 7 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 src/Exceptions/ConfigFileException.php diff --git a/src/Bot.php b/src/Bot.php index 63eb059..1430503 100644 --- a/src/Bot.php +++ b/src/Bot.php @@ -3,6 +3,7 @@ namespace CSlant\TelegramGitNotifier; use CSlant\TelegramGitNotifier\Constants\EventConstant; +use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException; use CSlant\TelegramGitNotifier\Interfaces\BotInterface; use CSlant\TelegramGitNotifier\Interfaces\EventInterface; use CSlant\TelegramGitNotifier\Interfaces\SettingInterface; @@ -29,6 +30,17 @@ class Bot implements AppInterface, BotInterface, EventInterface, SettingInterfac public Setting $setting; + /** + * @param Telegram|null $telegram + * @param string|null $chatBotId + * @param Event|null $event + * @param string|null $platform + * @param string|null $platformFile + * @param Setting|null $setting + * @param string|null $settingFile + * + * @throws ConfigFileException + */ public function __construct( Telegram $telegram = null, ?string $chatBotId = null, @@ -38,12 +50,22 @@ public function __construct( Setting $setting = null, ?string $settingFile = null, ) { - $this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token')); - $this->setCurrentChatBotId($chatBotId); $this->event = $event ?? new Event(); $this->setPlatFormForEvent($platform, $platformFile); + $this->validatePlatformFile(); $this->setting = $setting ?? new Setting(); $this->updateSetting($settingFile); + $this->validateSettingFile(); + + $this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token')); + $this->setCurrentChatBotId($chatBotId); + } + + public function validateSettingFile(): void + { + if (empty($this->setting->getSettingFile())) { + throw ConfigFileException::settingFile($this->setting->getSettingFile()); + } } } diff --git a/src/Exceptions/ConfigFileException.php b/src/Exceptions/ConfigFileException.php new file mode 100644 index 0000000..ef7f731 --- /dev/null +++ b/src/Exceptions/ConfigFileException.php @@ -0,0 +1,16 @@ +telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token')); - $this->setCurrentChatBotId($chatBotId); - $this->event = $event ?? new Event(); $this->setPlatFormForEvent($platform, $platformFile); + $this->validatePlatformFile(); + + $this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token')); + $this->setCurrentChatBotId($chatBotId); $this->client = $client ?? new Client(); } diff --git a/src/Trait/EventTrait.php b/src/Trait/EventTrait.php index 049431c..b6aed21 100644 --- a/src/Trait/EventTrait.php +++ b/src/Trait/EventTrait.php @@ -3,13 +3,14 @@ namespace CSlant\TelegramGitNotifier\Trait; use CSlant\TelegramGitNotifier\Constants\EventConstant; +use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException; use Symfony\Component\HttpFoundation\Request; trait EventTrait { use ActionEventTrait; - public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, string $platformFile = null): void + public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, ?string $platformFile = null): void { /** @var array $platformFileDefaults */ $platformFileDefaults = config('telegram-git-notifier.data_file.platform'); @@ -31,4 +32,14 @@ public function handleEventFromRequest(Request $request): ?string return null; } + + public function validatePlatformFile(): void + { + if (empty($this->event->getEventConfig())) { + throw ConfigFileException::platformFile( + $this->event->platform, + $this->event->getPlatformFile() + ); + } + } } diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index ef343ca..3c14054 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -37,3 +37,17 @@ expect($this->bot->event->getEventConfig())->toBeArray(); expect($this->bot->event->getEventConfig())->toHaveKey('tag_push'); }); + +it('setting file is valid', function () { + $this->bot->updateSetting(); + $this->bot->validateSettingFile(); + + expect($this->bot->setting->getSettings())->toBeArray(); +}); + +it('platform file is valid', function () { + $this->bot->setPlatFormForEvent(); + $this->bot->validatePlatformFile(); + + expect($this->bot->event->getEventConfig())->toBeArray(); +});