thecodingmachine / gitlab-hook-middleware
A PSR-15 middleware to manage Gitlab hooks.
Requires
- php: >=7.1
- psr/container: ^1
- psr/http-message: ^1
- psr/http-server-handler: ^1
- psr/http-server-middleware: ^1
- psr/log: ^1
- thecodingmachine/funky: ^1
- zendframework/zend-diactoros: ^1.8.5
Requires (Dev)
- maglnet/composer-require-checker: ^1.0
- mnapoli/simplex: ^0.5
- php-coveralls/php-coveralls: ^2
- phpstan/phpstan: ^0.10.3
- phpunit/phpunit: ^7.3.1
- squizlabs/php_codesniffer: ^3.3.1
- thecodingmachine/discovery: ^1
- thecodingmachine/phpstan-strict-rules: ^0.10.3
This package is auto-updated.
Last update: 2024-11-10 21:37:05 UTC
README
Gitlab hook PSR-15 middleware
This package is a PSR-15 Middleware to receive events sent by a Gitlab webhook and send them to a listener.
It's possible to use directly the hookReceiver to build the event object, but you could check the Gitlab authentification.
How does it work
The middleware checks the header named X-GITLAB-TOKEN and builds an event object. The object is dispatched to a listener that you must implement.
The middleware takes care of unserializing the payload and provides you with nice PHP objects instead of raw JSON data. It's possible to get the initial array payload with the getPayload() function.
Your listener will receive all events. If you want to listen on a specific event type, you must check the object type.
Event List:
Example
Listener implementation
<?php namespace Test; class Listener implements HookListenerInterface { /** * @param \TheCodingMachine\GitlabHook\EventInterface $event */ public function onEvent(EventInterface $event) { // Compute Push event if($event instanceof TheCodingMachine\GitlabHook\Model\Push::class) { // Display before echo $event->getBefore(); // Display the project name echo $event->getProject()->getName(); } // Compute MergeRequest event if($event instanceof TheCodingMachine\GitlabHook\Model\MergeRequest::class) { // Display target branch (this is in object_attributes) echo $event->getTargetBranch(); // Get initial payload var_dump($event->getPayload()); } } } ?>
Use without middleware
// Create your listener $listener = new Test\Listerner(); // Register your listener in the main HookReceiver instance $hookReceiver = new TheCodingMachine\GitlabHook\HookReceiver([$listener]); // Call handler function to execute check // $payload is array (json_decode) of data send by Gitlab webhook // $header is the result of HTTP_X_GITLAB_TOKEN header $hookReceiver->handle($payload, $header);
Use a middleware
// Create your listener $listener = new Test\Listerner(); // Register your listener in the main HookReceiver instance $hookReceiver = new TheCodingMachine\GitlabHook\HookReceiver([$listener]); // Create a PSR-3 logger $logger = new Psr\Log\NullLogger(); // Inject hookReceiver in Gitlab middleware // You must inject this middleware in the middleware pipe of your favorite framework. // See your framework documentation on how to do that. $middleware = new TheCodingMachine\GitlabHook\GitlabHookMiddleware($hookReceiver, 'secret', $logger);