From 44cfe6f735c9228606f5d3532710708f395ab6f5 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Wed, 1 Nov 2023 10:38:14 +0700 Subject: [PATCH 1/7] fix: handle to commit change php cs fixer --- .github/workflows/php-cs-fixer.yml | 7 ++++++- .github/workflows/setup_test.yml | 12 ++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/.github/workflows/php-cs-fixer.yml b/.github/workflows/php-cs-fixer.yml index 90d3b99..96e9a2d 100644 --- a/.github/workflows/php-cs-fixer.yml +++ b/.github/workflows/php-cs-fixer.yml @@ -8,7 +8,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: ref: ${{ github.head_ref }} @@ -16,3 +16,8 @@ jobs: uses: docker://oskarstark/php-cs-fixer-ga with: args: --config=.php-cs-fixer.dist.php --allow-risky=yes + + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: Fix styling diff --git a/.github/workflows/setup_test.yml b/.github/workflows/setup_test.yml index 726871d..9ad695e 100644 --- a/.github/workflows/setup_test.yml +++ b/.github/workflows/setup_test.yml @@ -1,13 +1,14 @@ -name: Setup and test +name: Setup & test on: [ push, pull_request ] jobs: tests: - name: Composer setup and tests - runs-on: ubuntu-latest + name: Composer P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.os }} + runs-on: ${{ matrix.os }} strategy: matrix: + os: [ ubuntu-latest ] php: [ '8.1', '8.2' ] laravel: [ 10.*, 9.* ] include: @@ -18,11 +19,14 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} + - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 + - name: Install dependencies run: | composer install --no-interaction --no-progress --no-suggest + - name: Run tests run: | composer validate --strict From b9175d36a65dc66cd067fda32e13a6ed73b3401d Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Wed, 1 Nov 2023 10:47:11 +0700 Subject: [PATCH 2/7] fix: add permission for auto commit on workflow --- .github/workflows/php-cs-fixer.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/php-cs-fixer.yml b/.github/workflows/php-cs-fixer.yml index 96e9a2d..802d168 100644 --- a/.github/workflows/php-cs-fixer.yml +++ b/.github/workflows/php-cs-fixer.yml @@ -2,6 +2,9 @@ name: Check & fix styling on: [push] +permissions: + contents: write + jobs: php-cs-fixer: runs-on: ubuntu-latest From 895f59444086a99ea46da63527a3c6185d0cf2f9 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Thu, 2 Nov 2023 10:14:15 +0700 Subject: [PATCH 3/7] add pest test package --- .gitignore | 1 + README.md | 2 +- composer.json | 13 +++++++++---- phpunit.xml.dist | 30 ++++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 phpunit.xml.dist diff --git a/.gitignore b/.gitignore index f09fb56..093d216 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ npm-debug.log /storage/fonts /storage/installing /storage/installed +/storage/json /log package-lock.json diff --git a/README.md b/README.md index 80fd7c3..8dd6de8 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ and manage customization through messages and buttons on Telegram.

![License](https://img.shields.io/github/license/lbiltech/telegram-git-notifier.svg?style=flat-square) -[![Latest Version](https://img.shields.io/github/release/lbiltech/telegram-git-notifier.svg?style=flat-square)](https://github.com/lbiltech/telegram-git-notifier/releases) +[![Latest Version on Packagist](https://img.shields.io/packagist/v/lbiltech/telegram-git-notifier.svg?style=flat-square)](https://packagist.org/lbiltech/telegram-git-notifier) [![Total Downloads](https://img.shields.io/packagist/dt/lbiltech/telegram-git-notifier.svg?style=flat-square)](https://packagist.org/packages/lbiltech/telegram-git-notifier) [![StyleCI](https://styleci.io/repos/683908657/shield)](https://styleci.io/repos/683908657) [![Quality Score](https://img.shields.io/scrutinizer/g/lbiltech/telegram-git-notifier.svg?style=flat-square)](https://scrutinizer-ci.com/g/lbiltech/telegram-git-notifier) diff --git a/composer.json b/composer.json index 4da0ec3..b7526b7 100644 --- a/composer.json +++ b/composer.json @@ -46,18 +46,23 @@ "vlucas/phpdotenv": "^5.5" }, "require-dev": { - "phpstan/phpstan": "^1.10.39", - "friendsofphp/php-cs-fixer": "^v3.37.1" + "friendsofphp/php-cs-fixer": "^v3.37.1", + "pestphp/pest": "^2.24", + "phpstan/phpstan": "^1.10.39" }, "scripts": { "analyse": "vendor/bin/phpstan", - "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes" + "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes", + "test": "vendor/bin/pest" }, "support": { "issues": "https://github.com/lbiltech/telegram-git-notifier/issues" }, "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true + } }, "minimum-stability": "dev", "prefer-stable": true diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..c426e8b --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,30 @@ + + + + + + + + + + + + tests + + + + + src/ + + + From 8ad43985d59afaa8ac4a3f72bd180d9f9ea7f09f Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Thu, 2 Nov 2023 10:32:21 +0700 Subject: [PATCH 4/7] fix: add script to get config files on composer command --- composer.json | 8 +++++++- install.sh | 17 +++++++++++++++++ storage/json/tgn/.gitkeep | 0 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 install.sh create mode 100644 storage/json/tgn/.gitkeep diff --git a/composer.json b/composer.json index b7526b7..fccc4e0 100644 --- a/composer.json +++ b/composer.json @@ -53,7 +53,13 @@ "scripts": { "analyse": "vendor/bin/phpstan", "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes", - "test": "vendor/bin/pest" + "test": "vendor/bin/pest", + "post-install-cmd": [ + "bash ./install.sh" + ], + "post-update-cmd": [ + "bash ./install.sh" + ] }, "support": { "issues": "https://github.com/lbiltech/telegram-git-notifier/issues" diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..9775fec --- /dev/null +++ b/install.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +json_files=( + "github-events.json" + "gitlab-events.json" + "tgn-settings.json" +) + +for file in "${json_files[@]}"; do + if [ ! -f "storage/json/tgn/$file" ]; then + cp "./config/jsons/$file" "storage/json/tgn/$file" + fi +done + +if [[ "$(uname -s -r)" == *"Linux"* && "$(cat /etc/os-release)" == *"Ubuntu"* ]]; then + chmod 777 storage/json/tgn/*.json +fi diff --git a/storage/json/tgn/.gitkeep b/storage/json/tgn/.gitkeep new file mode 100644 index 0000000..e69de29 From 22f22386b19243ebe3044edf8da053ef2ce3493b Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Thu, 2 Nov 2023 14:32:17 +0700 Subject: [PATCH 5/7] update bash install and config test --- install.sh | 7 +++++++ storage/json/tgn/.gitkeep | 0 tests/ConfigTest.php | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) delete mode 100644 storage/json/tgn/.gitkeep create mode 100644 tests/ConfigTest.php diff --git a/install.sh b/install.sh index 9775fec..16662a6 100644 --- a/install.sh +++ b/install.sh @@ -1,5 +1,9 @@ #!/bin/bash +echo "Setting up config files for Telegram Git Notifier..." + +mkdir -p storage/json/tgn + json_files=( "github-events.json" "gitlab-events.json" @@ -9,9 +13,12 @@ json_files=( for file in "${json_files[@]}"; do if [ ! -f "storage/json/tgn/$file" ]; then cp "./config/jsons/$file" "storage/json/tgn/$file" + echo "Created storage/json/tgn/$file" fi done if [[ "$(uname -s -r)" == *"Linux"* && "$(cat /etc/os-release)" == *"Ubuntu"* ]]; then chmod 777 storage/json/tgn/*.json fi + +echo "Telegram Git Notifier config files are ready!" diff --git a/storage/json/tgn/.gitkeep b/storage/json/tgn/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php new file mode 100644 index 0000000..38fa920 --- /dev/null +++ b/tests/ConfigTest.php @@ -0,0 +1,39 @@ +bot = new Bot(); +}); + +it('should return true', function () { + $this->assertTrue(true); +}); + +it('platform can be set for event with platform parameter', function () { + $this->bot->setPlatFormForEvent('gitlab'); + expect($this->bot->event->platform)->toBe('gitlab'); +}); + +it('platform can be set for event with null parameter', function () { + $this->bot->setPlatFormForEvent(); + expect($this->bot->event->platform)->toBe('github'); +}); + +it('platform can be set for event with platform file', function () { + $this->bot->setPlatFormForEvent('gitlab', 'storage/json/tgn/gitlab-events.json'); + expect($this->bot->event->platform)->toBe('gitlab'); + expect($this->bot->event->getPlatformFile())->toBe('storage/json/tgn/gitlab-events.json'); +}); + +it('can get json config for event - github', function () { + $this->bot->setPlatFormForEvent(); + expect($this->bot->event->getEventConfig())->toBeArray(); + expect($this->bot->event->getEventConfig())->toHaveKey('issue_comment'); +}); + +it('can get json config for event - gitlab', function () { + $this->bot->setPlatFormForEvent('gitlab', 'storage/json/tgn/gitlab-events.json'); + expect($this->bot->event->getEventConfig())->toBeArray(); + expect($this->bot->event->getEventConfig())->toHaveKey('tag_push'); +}); From d2982b1cc4f3ae1a202a9cfab8dbdf443483bba9 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Thu, 2 Nov 2023 15:51:48 +0700 Subject: [PATCH 6/7] add tests for validate access event and notifier --- .../Structures/NotificationInterface.php | 7 ++- src/Notifier.php | 4 +- src/Objects/Validator.php | 5 +- tests/NotifierTest.php | 25 ++++++++++ tests/TelegramGitNorifierTest.php | 0 tests/ValidatorTest.php | 48 +++++++++++++++++++ 6 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 tests/NotifierTest.php delete mode 100644 tests/TelegramGitNorifierTest.php create mode 100644 tests/ValidatorTest.php diff --git a/src/Interfaces/Structures/NotificationInterface.php b/src/Interfaces/Structures/NotificationInterface.php index 1e65546..96ca9b2 100644 --- a/src/Interfaces/Structures/NotificationInterface.php +++ b/src/Interfaces/Structures/NotificationInterface.php @@ -3,6 +3,7 @@ namespace LbilTech\TelegramGitNotifier\Interfaces\Structures; use LbilTech\TelegramGitNotifier\Exceptions\InvalidViewTemplateException; +use LbilTech\TelegramGitNotifier\Exceptions\MessageIsEmptyException; use LbilTech\TelegramGitNotifier\Exceptions\SendNotificationException; use Symfony\Component\HttpFoundation\Request; @@ -31,6 +32,7 @@ public function accessDenied( * * @return mixed|void * @throws InvalidViewTemplateException + * @throws MessageIsEmptyException * @see Notification::setPayload() */ public function setPayload(Request $request, string $event); @@ -60,7 +62,10 @@ public function getActionOfEvent(object $payload): string; /** * Convert chat and thread ids to array * Example: 1234567890;1234567890:thread1;1234567890:thread1,thread2 + * + * @param string|null $chatIds + * * @return array */ - public function parseNotifyChatIds(): array; + public function parseNotifyChatIds(?string $chatIds = null): array; } diff --git a/src/Notifier.php b/src/Notifier.php index c5cfa4b..7bb7490 100644 --- a/src/Notifier.php +++ b/src/Notifier.php @@ -42,11 +42,11 @@ public function __construct( $this->client = $client ?? new Client(); } - public function parseNotifyChatIds(): array + public function parseNotifyChatIds(?string $chatIds = null): array { $chatData = explode( NotificationConstant::CHAT_ID_PAIRS_SEPARATOR, - config('telegram-git-notifier.bot.notify_chat_ids') + $chatIds ?? config('telegram-git-notifier.bot.notify_chat_ids') ); $chatThreadMapping = []; diff --git a/src/Objects/Validator.php b/src/Objects/Validator.php index 6c2b4b8..5562f4c 100644 --- a/src/Objects/Validator.php +++ b/src/Objects/Validator.php @@ -50,10 +50,11 @@ public function isAccessEvent( $eventConfig = $eventConfig[$action]; } - if (!$eventConfig) { + if (is_array($eventConfig) || !$eventConfig) { + $eventConfig = false; error_log('\n Event config is not found \n'); } - return (bool)$eventConfig; + return $eventConfig; } } diff --git a/tests/NotifierTest.php b/tests/NotifierTest.php new file mode 100644 index 0000000..a61c320 --- /dev/null +++ b/tests/NotifierTest.php @@ -0,0 +1,25 @@ +nofitier = new Notifier(); +}); + +it('validates that the event files exist', function () { + $this->nofitier->setPlatFormForEvent('gitlab', 'storage/json/tgn/gitlab-events.json'); + expect($this->nofitier->event->getEventConfig())->toBeArray(); + expect($this->nofitier->event->getEventConfig())->toHaveKey('tag_push'); + + $this->nofitier->setPlatFormForEvent('github', 'storage/json/tgn/github-events.json'); + expect($this->nofitier->event->getEventConfig())->toBeArray(); + expect($this->nofitier->event->getEventConfig())->toHaveKey('issue_comment'); +}); + +it('can parse notification chat IDs', function () { + $chatThreadMapping = $this->nofitier->parseNotifyChatIds('-1201937489183;-1008168942527:46,2'); + expect($chatThreadMapping)->toBeArray(); + expect($chatThreadMapping)->toHaveKey('-1008168942527'); + expect($chatThreadMapping['-1008168942527'])->toBeArray(); + expect($chatThreadMapping['-1008168942527'])->toBe([0 => '46', 1 => '2']); +}); diff --git a/tests/TelegramGitNorifierTest.php b/tests/TelegramGitNorifierTest.php deleted file mode 100644 index e69de29..0000000 diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php new file mode 100644 index 0000000..2eed1e5 --- /dev/null +++ b/tests/ValidatorTest.php @@ -0,0 +1,48 @@ +bot = new Bot(); + $this->bot->updateSetting('storage/json/tgn/setting.json'); + $this->bot->setPlatFormForEvent(); + $this->validator = new Validator($this->bot->setting, $this->bot->event); +}); + +it('can validate the event that has no action to send a notification - GitHub', function () { + $result = $this->validator->isAccessEvent('github', 'push', (object)[]); + expect($result)->toBeTrue(); +}); + +it('can validate the event that has an action to send a notification - GitHub', function () { + $result = $this->validator->isAccessEvent('github', 'pull_request', (object)[ + 'action' => 'opened', + ]); + expect($result)->toBeTrue(); +}); + +it('can\'t validate the event that has an action but no payload to send a notification - GitHub', function () { + $result = $this->validator->isAccessEvent('github', 'pull_request', (object)[]); + expect($result)->toBeFalse(); +}); + +it('can validate the event that has no action to send a notification - gitlab', function () { + $this->bot->setPlatFormForEvent('gitlab'); + $result = $this->validator->isAccessEvent('gitlab', 'push', (object)[]); + expect($result)->toBeTrue(); +}); + +it('can validate the event that has an action to send a notification - gitlab', function () { + $this->bot->setPlatFormForEvent('gitlab'); + $result = $this->validator->isAccessEvent('gitlab', 'merge_request', (object)[ + 'action' => 'open', + ]); + expect($result)->toBeTrue(); +}); + +it('can\'t validate the event that has an action but no payload to send a notification - gitlab', function () { + $this->bot->setPlatFormForEvent('gitlab'); + $result = $this->validator->isAccessEvent('gitlab', 'merge_request', (object)[]); + expect($result)->toBeFalse(); +}); From aad90177a67d5c1a3189c15f1568c36149d329d5 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Thu, 2 Nov 2023 16:14:35 +0700 Subject: [PATCH 7/7] update workflows --- .github/workflows/phpstan.yml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index a17c22b..de07f19 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -14,8 +14,13 @@ jobs: with: php-version: '8.1' - - name: Install composer dependencies - uses: ramsey/composer-install@v2 + - 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 + run: | + composer analyse --error-format=github