gnello / webhook-manager
使用webhook轻松地将一个或多个操作与特定存储库事件关联
1.4.0
2019-07-01 15:36 UTC
Requires
- php: >=7.0
- ext-json: *
Requires (Dev)
- phpunit/phpunit: *
- symfony/var-dumper: ^3.1
README
WebhookManager通过webhook轻松地将一个或多个操作与特定存储库事件关联。
支持的服务:Bitbucket、Github、TravisCI和任何自定义服务。
安装
强烈推荐使用composer安装WebhookManager
composer require gnello/webhook-manager
有关如何在本地机器上安装和使用Composer的更多信息,请参阅此处。
配置
在Bitbucket上
- 转到您存储库的设置
- 在“工作流”下点击“Webhooks”
- 点击“添加webhook”
- 输入您服务器上配置的WebhookManager的url(例如:https://mysite.com/webhooks)
- 设置触发器
- 保存!
在Github上
- 转到您存储库的设置
- 在“选项”下点击“Webhooks”
- 点击“添加webhook”
- 输入您服务器上配置的WebhookManager的url(例如:https://mysite.com/webhooks)
- 将内容类型设置为
application/json
- 设置事件
- 保存!
在TravisCI上
在您的.travis.yml
文件中添加以下内容
notifications:
webhooks: url of WebhookManager configured on your server (es. https://mysite.com/webhooks)
在自定义服务上
这取决于您!
使用方法
WebhookManager的使用非常简单
Bitbucket
require '../vendor/autoload.php'; use \Gnello\WebhookManager\App; use \Gnello\WebhookManager\Services\BitbucketService; $webhookManager = new App(); //Action on build passed $webhookManager->add([BitbucketService::BUILD_STATUS_CREATED, BitbucketService::BUILD_STATUS_UPDATED], function(BitbucketService $service) { $payload = $service->getPayload(); if ($payload['commit_status']['state'] == 'SUCCESSFUL') { //do some stuff } }); $webhookManager->listen();
Github
require '../vendor/autoload.php'; use \Gnello\WebhookManager\App; use \Gnello\WebhookManager\Services\GithubService; $webhookManager = new App(['service' => GithubService::class]); //Action on push event $webhookManager->add(GithubService::PUSH, function(GithubService $service) { $payload = $service->getPayload(); //do some stuff }); $webhookManager->listen();
TravisCI
require '../vendor/autoload.php'; use \Gnello\WebhookManager\App; use \Gnello\WebhookManager\Services\TravisCIService; $webhookManager = new App(['service' => TravisCIService::class]); //Action on build passed $webhookManager->add(TravisCIService::PUSH, function(TravisCIService $service) { $payload = $service->getPayload(); if ($payload['state'] === 'passed') { //do some stuff } }); $webhookManager->listen();
自定义服务
要使用自定义服务,您应该创建一个实现\Gnello\WebhookManager\Services\ServiceInterface
接口的类,然后将其在WebhookManager上注册。在WebhookManager选项中,您应该指定您想要使用自定义服务。
require '../vendor/autoload.php'; use \Gnello\WebhookManager\App; $webhookManager = new App(['service' => \YourCustomService::class]); //Action on custom event $webhookManager->add('custom_event', function(\YourCustomService $service) { $payload = $service->getPayload(); //do some stuff }); $webhookManager->add('another_event', function(\YourCustomService $service) { //do some stuff }); $webhookManager->listen();
选项
- Bitbucket是默认服务,但您可以按以下方式更改它
//github $webhookManager = new \Gnello\WebhookManager\App([ 'service' => \Gnello\WebhookManager\Services\GithubService::class ]); //travis ci $webhookManager = new \Gnello\WebhookManager\App([ 'service' => \Gnello\WebhookManager\Services\TravisCIService::class ]); //custom service $webhookManager = new \Gnello\WebhookManager\App([ 'service' => \Gnello\WebhookManager\Services\YourCustomService::class ]);
- Bitbucket和Github服务的json_decode被设置为将返回的对象转换为关联数组。您可以按这种方式更改此行为
$webhookManager = new \Gnello\WebhookManager\App([ 'json_decode_assoc' => false ]);