-
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add phpstan & update send notification handle #19
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
565c186
feat: add phpstan to analyze
tanhongit 6a094e5
feat: add phpstan webhook
tanhongit ae982f5
fix: update dependency
tanhongit 7f587b7
feat: send notification
tanhongit c8a85da
Fix styling
tanhongit 32fb02e
Merge pull request #2 from tanhongit/main
tanhongit d8b2bd9
fix: view namespace for package
tanhongit 593c3cd
update phpstan for multi version
tanhongit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: PHPStan | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
phpstan: | ||
runs-on: ${{ matrix.os }} | ||
name: PHPStan - P${{ matrix.php }} | ||
|
||
strategy: | ||
matrix: | ||
os: [ ubuntu-latest ] | ||
php: [ '8.1', '8.2', '8.3' ] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Setup PHP | ||
uses: shivammathur/[email protected] | ||
with: | ||
php-version: ${{ matrix.php }} | ||
|
||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Install dependencies | ||
run: | | ||
composer install --no-interaction --no-progress --no-suggest | ||
|
||
- name: Run PHPStan | ||
run: | | ||
composer analyse --error-format=github |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,11 +22,6 @@ | |
"name": "Tan Nguyen", | ||
"email": "[email protected]", | ||
"homepage": "https://tanhongit.com", | ||
"role": "Lead" | ||
}, | ||
{ | ||
"name": "Xuan Thinh", | ||
"email": "[email protected]", | ||
"role": "Developer" | ||
} | ||
], | ||
|
@@ -42,13 +37,20 @@ | |
}, | ||
"require": { | ||
"php": "^8.1", | ||
"cslant/telegram-git-notifier": "^v1.3.2" | ||
"cslant/telegram-git-notifier": "^v1.3" | ||
}, | ||
"require-dev": { | ||
"friendsofphp/php-cs-fixer": "^v3.37.1", | ||
"pestphp/pest": "^2.24" | ||
"friendsofphp/php-cs-fixer": "^v3.37", | ||
"nunomaduro/collision": "^7.10", | ||
"nunomaduro/larastan": "^2.6", | ||
"orchestra/testbench": "^8.14", | ||
"pestphp/pest": "^2.24", | ||
"phpstan/extension-installer": "^1.3", | ||
"phpstan/phpstan-deprecation-rules": "^1.1", | ||
"phpstan/phpstan-phpunit": "^1.3" | ||
}, | ||
"scripts": { | ||
"analyse": "vendor/bin/phpstan analyse", | ||
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes", | ||
"post-install-cmd": [ | ||
"bash vendor/cslant/telegram-git-notifier/install.sh" | ||
|
@@ -70,7 +72,8 @@ | |
"config": { | ||
"sort-packages": true, | ||
"allow-plugins": { | ||
"pestphp/pest-plugin": true | ||
"pestphp/pest-plugin": true, | ||
"phpstan/extension-installer": true | ||
} | ||
}, | ||
"minimum-stability": "dev", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
parameters: | ||
excludePaths: | ||
- src/Http/Actions/WebhookAction.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
includes: | ||
- phpstan-baseline.neon | ||
|
||
parameters: | ||
level: 9 | ||
paths: | ||
- src | ||
- routes | ||
- config | ||
tmpDir: build/phpstan | ||
checkOctaneCompatibility: true | ||
checkModelProperties: true | ||
checkMissingIterableValueType: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace CSlant\LaravelTelegramGitNotifier\Http\Actions; | ||
|
||
use CSlant\LaravelTelegramGitNotifier\Services\NotificationService; | ||
use CSlant\TelegramGitNotifier\Bot; | ||
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException; | ||
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException; | ||
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException; | ||
use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException; | ||
use CSlant\TelegramGitNotifier\Notifier; | ||
use GuzzleHttp\Client; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Telegram; | ||
|
||
class IndexAction | ||
{ | ||
protected Client $client; | ||
|
||
protected Bot $bot; | ||
|
||
protected Notifier $notifier; | ||
|
||
protected Request $request; | ||
|
||
/** | ||
* @throws ConfigFileException | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->client = new Client(); | ||
|
||
$telegram = new Telegram(config('telegram-git-notifier.bot.token')); | ||
$this->bot = new Bot($telegram); | ||
$this->notifier = new Notifier(); | ||
} | ||
|
||
/** | ||
* Handle telegram git notifier app. | ||
* | ||
* @return void | ||
* | ||
* @throws InvalidViewTemplateException | ||
* @throws MessageIsEmptyException | ||
* @throws SendNotificationException | ||
*/ | ||
public function index(): void | ||
{ | ||
$sendNotification = new NotificationService( | ||
$this->notifier, | ||
$this->bot->setting | ||
); | ||
$sendNotification->handle(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace CSlant\LaravelTelegramGitNotifier\Services; | ||
|
||
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException; | ||
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException; | ||
use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException; | ||
use CSlant\TelegramGitNotifier\Models\Setting; | ||
use CSlant\TelegramGitNotifier\Notifier; | ||
use CSlant\TelegramGitNotifier\Objects\Validator; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class NotificationService | ||
{ | ||
protected Request $request; | ||
|
||
protected array $chatIds = []; | ||
|
||
protected Notifier $notifier; | ||
|
||
protected Setting $setting; | ||
|
||
public function __construct( | ||
Notifier $notifier, | ||
Setting $setting, | ||
) { | ||
$this->request = Request::createFromGlobals(); | ||
$this->notifier = $notifier; | ||
$this->chatIds = $this->notifier->parseNotifyChatIds(); | ||
|
||
$this->setting = $setting; | ||
} | ||
|
||
/** | ||
* Handle to send notification from webhook event to telegram. | ||
* | ||
* @return void | ||
* | ||
* @throws InvalidViewTemplateException | ||
* @throws SendNotificationException | ||
* @throws MessageIsEmptyException | ||
*/ | ||
public function handle(): void | ||
{ | ||
$eventName = $this->notifier->handleEventFromRequest($this->request); | ||
if (!empty($eventName)) { | ||
$this->sendNotification($eventName); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $event | ||
* @return void | ||
* | ||
* @throws InvalidViewTemplateException | ||
* @throws SendNotificationException | ||
* @throws MessageIsEmptyException | ||
*/ | ||
private function sendNotification(string $event): void | ||
{ | ||
if (!$this->validateAccessEvent($event)) { | ||
return; | ||
} | ||
|
||
foreach ($this->chatIds as $chatId => $thread) { | ||
if (empty($chatId)) { | ||
continue; | ||
} | ||
|
||
if (empty($thread)) { | ||
$this->notifier->sendNotify(null, ['chat_id' => $chatId]); | ||
|
||
continue; | ||
} | ||
|
||
foreach ($thread as $threadId) { | ||
$this->notifier->sendNotify(null, [ | ||
'chat_id' => $chatId, 'message_thread_id' => $threadId, | ||
]); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Validate access event. | ||
* | ||
* @param string $event | ||
* @return bool | ||
* | ||
* @throws InvalidViewTemplateException|MessageIsEmptyException | ||
*/ | ||
private function validateAccessEvent(string $event): bool | ||
{ | ||
$payload = $this->notifier->setPayload($this->request, $event); | ||
$validator = new Validator($this->setting, $this->notifier->event); | ||
|
||
if (empty($payload) || !is_object($payload) | ||
|| !$validator->isAccessEvent( | ||
$this->notifier->event->platform, | ||
$event, | ||
$payload | ||
) | ||
) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add the namespace for view() and sync with core