From 19e38d1780236d6963c9f7020ec26622c8f69d19 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Mon, 18 Oct 2021 02:57:15 +0100 Subject: [PATCH 01/14] add resources --- resources/views/mail/webhook.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 resources/views/mail/webhook.php diff --git a/resources/views/mail/webhook.php b/resources/views/mail/webhook.php new file mode 100644 index 0000000..e54566d --- /dev/null +++ b/resources/views/mail/webhook.php @@ -0,0 +1,13 @@ + + + + Webhook Event + + + +
+

Webhook {{$type}} Event

+

There is push event to repo

+
+ + From ab0962bb9016f5e893b85820ba6a50a21789215b Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Mon, 18 Oct 2021 02:57:39 +0100 Subject: [PATCH 02/14] add routes --- routes/web.php | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100755 routes/web.php diff --git a/routes/web.php b/routes/web.php new file mode 100755 index 0000000..9a22028 --- /dev/null +++ b/routes/web.php @@ -0,0 +1,11 @@ + Date: Mon, 18 Oct 2021 02:58:09 +0100 Subject: [PATCH 03/14] add events --- src/Events/NoPayload.php | 36 ++++++++++++++++++++++++++++ src/Events/Webhook.php | 51 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/Events/NoPayload.php create mode 100644 src/Events/Webhook.php diff --git a/src/Events/NoPayload.php b/src/Events/NoPayload.php new file mode 100644 index 0000000..ec078fb --- /dev/null +++ b/src/Events/NoPayload.php @@ -0,0 +1,36 @@ +payload = $payload; + $this->type = $type; + } + + /** + * Get the channels the event should broadcast on. + * + * @return \Illuminate\Broadcasting\Channel|array + */ + public function broadcastOn() + { + return new PrivateChannel('channel-name'); + } +} From 08da9407bbab1cce7ac799a5d5186e91b3cb5cc6 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Mon, 18 Oct 2021 02:58:34 +0100 Subject: [PATCH 04/14] add controllers --- src/Http/Controllers/GithookController.php | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 src/Http/Controllers/GithookController.php diff --git a/src/Http/Controllers/GithookController.php b/src/Http/Controllers/GithookController.php new file mode 100755 index 0000000..753bb41 --- /dev/null +++ b/src/Http/Controllers/GithookController.php @@ -0,0 +1,49 @@ +has('payload')) { + NoPayload::dispatch($request->all()); + info('No payload received'); + return; + } + + // get payload + $payload = json_decode($request->payload); + $event = $request->header('X-GitHub-Event'); + + event(new Webhook($payload, $event)); + } + + /** + * Check authentication + * + * @param [type] $payload + * @return void + */ + protected function authCheck() + { + $github_hash = request()->header('X-Hub-Signature'); + $secret = config('githook.secret'); + $payload = request()->getContent(); + + $local_hash = 'sha1=' . hash_hmac('sha1', $payload, $secret, false); + + return hash_equals($github_hash, $local_hash); + } +} \ No newline at end of file From 0cc5e684019888235014922d2473942af7b53111 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Mon, 18 Oct 2021 02:58:55 +0100 Subject: [PATCH 05/14] add mail --- src/Mail/Githook.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/Mail/Githook.php diff --git a/src/Mail/Githook.php b/src/Mail/Githook.php new file mode 100644 index 0000000..385582a --- /dev/null +++ b/src/Mail/Githook.php @@ -0,0 +1,41 @@ +payload = $payload; + $this->type = $type; + } + + /** + * Build the message. + * + * @return $this + */ + public function build() + { + return $this + ->view('githook::mail.webhook') + ->with('payload', $this->payload) + ->with('type', ucwords(str_replace('_', ' ', $this->type))); + } +} From a6006663e6340261d7cf7599370d845992e6d97a Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Mon, 18 Oct 2021 02:59:24 +0100 Subject: [PATCH 06/14] add providers --- src/Providers/EventServiceProvider.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/Providers/EventServiceProvider.php diff --git a/src/Providers/EventServiceProvider.php b/src/Providers/EventServiceProvider.php new file mode 100644 index 0000000..d29eeee --- /dev/null +++ b/src/Providers/EventServiceProvider.php @@ -0,0 +1,26 @@ + [ + // UpdatePostTitle::class, + // ] + ]; + + /** + * Register any events for your application. + * + * @return void + */ + public function boot() + { + parent::boot(); + } +} From 6434eb418ba0eecf11147977d35845ffaa437e37 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Mon, 18 Oct 2021 03:01:01 +0100 Subject: [PATCH 07/14] add providers --- composer.json | 3 +- config/config.php | 27 ++++++++++++++--- src/GithookServiceProvider.php | 55 ++++++++++++++++------------------ 3 files changed, 50 insertions(+), 35 deletions(-) diff --git a/composer.json b/composer.json index f49aec2..262d7a3 100644 --- a/composer.json +++ b/composer.json @@ -16,8 +16,7 @@ } ], "require": { - "php": "^7.4|^8.0", - "illuminate/support": "^8.0" + "php": "^7.0|^8.0" }, "require-dev": { "orchestra/testbench": "^6.0", diff --git a/config/config.php b/config/config.php index 48d0ee9..04637cb 100644 --- a/config/config.php +++ b/config/config.php @@ -1,8 +1,27 @@ env('GITHOOK_SECRET', null), + + + /* + |-------------------------------------------------------------------------- + | Github WebHook URL + |-------------------------------------------------------------------------- + | + | Github webhook URL. + | + */ + + 'url' => env('GITHOOK_URL', 'githook'), +]; diff --git a/src/GithookServiceProvider.php b/src/GithookServiceProvider.php index e2e1dd1..ad28e84 100644 --- a/src/GithookServiceProvider.php +++ b/src/GithookServiceProvider.php @@ -3,6 +3,8 @@ namespace Digitlimit\Githook; use Illuminate\Support\ServiceProvider; +use Digitlimit\Githook\Http\Controllers\GithookController; +use Digitlimit\Githook\Providers\EventServiceProvider; class GithookServiceProvider extends ServiceProvider { @@ -11,36 +13,25 @@ class GithookServiceProvider extends ServiceProvider */ public function boot() { - /* - * Optional methods to load your package assets - */ - // $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'githook'); - // $this->loadViewsFrom(__DIR__.'/../resources/views', 'githook'); - // $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); - // $this->loadRoutesFrom(__DIR__.'/routes.php'); + $this->loadViewsFrom(__DIR__.'/../resources/views', 'githook'); + $this->loadRoutesFrom(__DIR__.'/routes/web.php'); if ($this->app->runningInConsole()) { - $this->publishes([ - __DIR__.'/../config/config.php' => config_path('githook.php'), - ], 'config'); + + $this->publishes( + [ + __DIR__.'/../config/config.php' => config_path('githook.php'), + ], + 'config' + ); // Publishing the views. - /*$this->publishes([ - __DIR__.'/../resources/views' => resource_path('views/vendor/githook'), - ], 'views');*/ - - // Publishing assets. - /*$this->publishes([ - __DIR__.'/../resources/assets' => public_path('vendor/githook'), - ], 'assets');*/ - - // Publishing the translation files. - /*$this->publishes([ - __DIR__.'/../resources/lang' => resource_path('lang/vendor/githook'), - ], 'lang');*/ - - // Registering package commands. - // $this->commands([]); + $this->publishes( + [ + __DIR__.'/../resources/views' => resource_path('views/vendor/githook'), + ], + 'views' + ); } } @@ -49,12 +40,18 @@ public function boot() */ public function register() { + $this->app->register(EventServiceProvider::class); + $this->app->make(GithookController::class); + // Automatically apply the package configuration $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'githook'); // Register the main class to use with the facade - $this->app->singleton('githook', function () { - return new Githook; - }); + $this->app->singleton( + 'githook', + function () { + return new Githook; + } + ); } } From 4413a9de0eca3e037ff3f9adebe66eee641c4ec8 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Mon, 18 Oct 2021 03:23:06 +0100 Subject: [PATCH 08/14] update route --- src/GithookServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GithookServiceProvider.php b/src/GithookServiceProvider.php index ad28e84..354d5b8 100644 --- a/src/GithookServiceProvider.php +++ b/src/GithookServiceProvider.php @@ -14,7 +14,7 @@ class GithookServiceProvider extends ServiceProvider public function boot() { $this->loadViewsFrom(__DIR__.'/../resources/views', 'githook'); - $this->loadRoutesFrom(__DIR__.'/routes/web.php'); + $this->loadRoutesFrom(__DIR__.'/../routes/web.php'); if ($this->app->runningInConsole()) { From d1c66da8f9e2b2cc684c3baadd0a88304bffb244 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Mon, 18 Oct 2021 03:50:36 +0100 Subject: [PATCH 09/14] update mailable --- src/Mail/Githook.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Mail/Githook.php b/src/Mail/Githook.php index 385582a..118b638 100644 --- a/src/Mail/Githook.php +++ b/src/Mail/Githook.php @@ -23,7 +23,7 @@ class Githook extends Mailable public function __construct(object $payload, string $type) { $this->payload = $payload; - $this->type = $type; + $this->type = ucwords(str_replace('_', ' ', $this->type)); } /** @@ -33,9 +33,6 @@ public function __construct(object $payload, string $type) */ public function build() { - return $this - ->view('githook::mail.webhook') - ->with('payload', $this->payload) - ->with('type', ucwords(str_replace('_', ' ', $this->type))); + return $this->view('githook::mail.webhook'); } } From ae9a8391cf9008d245a11eb149d1f0ca3ddc6bf8 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Mon, 18 Oct 2021 03:58:17 +0100 Subject: [PATCH 10/14] add mailable blade view --- resources/views/mail/{webhook.php => webhook.blade.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename resources/views/mail/{webhook.php => webhook.blade.php} (100%) diff --git a/resources/views/mail/webhook.php b/resources/views/mail/webhook.blade.php similarity index 100% rename from resources/views/mail/webhook.php rename to resources/views/mail/webhook.blade.php From c63786f9fdebbfdd8fc87c37cd4c54f252e9be2b Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Mon, 18 Oct 2021 03:59:51 +0100 Subject: [PATCH 11/14] add mailable blade view --- resources/views/mail/{webhook.php => webhook.blade.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename resources/views/mail/{webhook.php => webhook.blade.php} (100%) diff --git a/resources/views/mail/webhook.php b/resources/views/mail/webhook.blade.php similarity index 100% rename from resources/views/mail/webhook.php rename to resources/views/mail/webhook.blade.php From 80934e69bc402b5e56f40d172831033fea2f04fc Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Mon, 18 Oct 2021 04:19:08 +0100 Subject: [PATCH 12/14] update mailable and readme --- README.md | 16 +++++++++------- src/Mail/Githook.php | 4 ++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b1c896a..a7adf90 100644 --- a/README.md +++ b/README.md @@ -4,14 +4,20 @@ [![Total Downloads](https://img.shields.io/packagist/dt/digitlimit/githook.svg?style=flat-square)](https://packagist.org/packages/digitlimit/githook) ![GitHub Actions](https://github.com/digitlimit/githook/actions/workflows/main.yml/badge.svg) -This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what PSRs you support to avoid any confusion with users and contributors. +This package is a simple Laravel Github Webhook helper for handling webhook events. ## Installation You can install the package via composer: ```bash -composer require digitlimit/githook +composer require digitlimit/githook "dev-develop" +``` + +Then publish config and mailable + +```bash +php artisan vendor:publish --provider="Digitlimit\Githook\GithookServiceProvider" ``` ## Usage @@ -45,8 +51,4 @@ If you discover any security related issues, please email frankemeks77@yahoo.com ## License -The MIT License (MIT). Please see [License File](LICENSE.md) for more information. - -## Laravel Package Boilerplate - -This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com). +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. \ No newline at end of file diff --git a/src/Mail/Githook.php b/src/Mail/Githook.php index 118b638..ff0bf85 100644 --- a/src/Mail/Githook.php +++ b/src/Mail/Githook.php @@ -11,9 +11,9 @@ class Githook extends Mailable { use Queueable, SerializesModels; - protected $payload; + public $payload; - protected $type; + public $type; /** * Create a new message instance. From 040ce7d2b7fde5f6fd2764098345990f7247cdf5 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Mon, 18 Oct 2021 04:35:44 +0100 Subject: [PATCH 13/14] update mailable and readme --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a7adf90..8fea420 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Very short description of the package +# Github [![Latest Version on Packagist](https://img.shields.io/packagist/v/digitlimit/githook.svg?style=flat-square)](https://packagist.org/packages/digitlimit/githook) [![Total Downloads](https://img.shields.io/packagist/dt/digitlimit/githook.svg?style=flat-square)](https://packagist.org/packages/digitlimit/githook) @@ -22,6 +22,14 @@ php artisan vendor:publish --provider="Digitlimit\Githook\GithookServiceProvider ## Usage +1. Add + +1. Setup Github Webhook +https://github.com/digitlimit/kcc-admin/settings/hooks + +![image](https://user-images.githubusercontent.com/2041419/137665069-f330f1e5-3907-4e59-a6b3-79c95be40ba0.png) + + ```php // Usage description here ``` From 6f7394a62ac8095f55e4de3a5278c547236c5b05 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Mon, 18 Oct 2021 13:51:45 +0100 Subject: [PATCH 14/14] update routes --- config/config.php | 3 +-- routes/web.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/config/config.php b/config/config.php index 04637cb..c1cda1c 100644 --- a/config/config.php +++ b/config/config.php @@ -12,7 +12,6 @@ */ 'secret' => env('GITHOOK_SECRET', null), - /* |-------------------------------------------------------------------------- @@ -23,5 +22,5 @@ | */ - 'url' => env('GITHOOK_URL', 'githook'), + 'url' => env('GITHOOK_URL', '/githook'), ]; diff --git a/routes/web.php b/routes/web.php index 9a22028..4d1af1b 100755 --- a/routes/web.php +++ b/routes/web.php @@ -8,4 +8,4 @@ |-------------------------------------------------------------------------- */ - Route::post('/githook', GithookController::class); \ No newline at end of file + Route::post(config('githook.url'), GithookController::class); \ No newline at end of file