oneduo / laravel-gitlab-webhook-client
处理传入Gitlab webhook请求的客户端
Requires
- php: ^8.1
- illuminate/contracts: ^10.0
- spatie/laravel-data: ^3.5
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.9
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- spatie/laravel-ray: ^1.26
README
这是一个小巧的客户端,允许你在Laravel应用程序中监听Gitlab webhook。你可以使用分发的事件和提供的数据在你的应用程序中执行操作。
支持的webhook类型
安装
您可以通过composer安装此包
composer require oneduo/laravel-gitlab-webhook-client
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="gitlab-webhook-client-config"
这是发布配置文件的内容
<?php declare(strict_types=1); return [ /* |-------------------------------------------------------------------------- | Webhook route |-------------------------------------------------------------------------- | | Here you may choose to enable the default webhook route provided by | the package. If you decide to disable this, you should manually register | the route in your application and implement the event dispatching logic | within your route. | | This registers the following route: POST /gitlab-webhook | | default: true */ 'route_enabled' => true, /* |-------------------------------------------------------------------------- | Secret Token Middleware |-------------------------------------------------------------------------- | | You may set the value of the secret token defined in Gitlab. | The package will validate all incoming requests against this given token, | and reject all unauthorized requests. | | Set the value to NULL to disable the middleware. | | default: null */ 'secret_token' => env('GITLAB_WEBHOOK_SECRET_TOKEN'), ];
使用方法
设置Gitlab
要开始,您必须首先在Gitlab项目中设置一个webhook。
您可以参考这里提供的官方文档 https://docs.gitlab.com/ee/user/project/integrations/webhooks.html。
设置webhook URL
默认情况下,当route_enabled
配置设置为true时,该包会自动注册一个路由来处理所有传入的webhook请求。
它注册为POST /gitlab-webhook
,您可以使用php artisan route:list
命令检查您的路由。
注意如果您希望实现自己的路由,请查看
WebhookController
以实现分发事件的类似逻辑。
当Gitlab向您的应用程序发送webhook请求时,该包将根据接收到的webhook类型分发事件。
例如,如果Gitlab发送一个合并请求
webhook,该包将分发一个MergeRequestEvent
事件。
您可以注册自己的监听器如下
class EventServiceProvider extends ServiceProvider { /** * The event to listener mappings for the application. * * @var array<class-string, array<int, class-string>> */ protected $listen = [ MergeRequestEvent::class => [ MergeRequestEventListener::class, ], ]; }
所有事件都进行了类型提示,以便方便地访问事件属性和数据
<?php declare(strict_types=1); namespace App; use App\Gitlab\Events\MergeRequestEvent; class MergeRequestEventListener { public function handle(MergeRequestEvent $event): void { logger()->info('New merge request event received'); logger()->info($event->mergeRequest->title); logger()->info('By: ' . $event->mergeRequest->last_commit->author->email); } }
注意您可以为每种事件类型使用单独的事件监听器,或者使用单个监听器来监听
WebhookEventContract
,这将捕获所有分发的事件。
注意请注意,一些属性和数据被认为是可为空的,您必须在这些值上实现必要的null检查。
去重
Gitlab不保证webhook的去重。您可以使用每个事件上的uuid
属性来处理应用程序中的去重。
安全性
每个事件都公开了Gitlab提供的头信息。您可以使用X-Gitlab-Token
头信息来验证请求的真实性,该请求与您在Gitlab项目设置中设置的密钥令牌相匹配。
测试
composer test
变更日志
有关最近更改的更多信息,请参阅CHANGELOG。
贡献
有关详细信息,请参阅CONTRIBUTING。
安全漏洞
有关如何报告安全漏洞的详细信息,请参阅我们的安全策略。
鸣谢
许可
MIT许可证(MIT)。有关更多信息,请参阅许可文件。