Now that we understand the basics of webhooks, let's go through the process of building out our own webhook-powered integration. In this tutorial, we'll create a repository webhook that will be responsible for listing out how popular our repository is, based on the number of issues it receives per day.
webhookの作成は、2ステップのプロセスです。 You'll first need to set up how you want your webhook to behave through GitHub Enterprise Server: what events should it listen to. その後、ペイロードを受信して管理するようにサーバーをセットアップします。
You can use the repository, organization, and app webhook REST APIs to create, update, delete, and ping webhooks. You can also use the REST API to change the configuration of the webhook. For example, you can modify the payload URL, content type, SSL verification, and secret. For more information, see:
Exposing localhost to the internet
For the purposes of this tutorial, we're going to use a local server to receive messages from GitHub. So, first of all, we need to expose our local development environment to the internet. We'll use ngrok to do this. ngrok is available, free of charge, for all major operating systems. For more information, see the ngrok download page.
After installing ngrok, you can expose your localhost by running ./ngrok http 4567 on the command line. 4567 is the port number on which our server will listen for messages. 以下のような行が表示されるはずです。
$ Forwarding http://7e9ea9dc.ngrok.io -> 127.0.0.1:4567
Make a note of the *.ngrok.io URL. We'll use it to set up our webhook.
Setting up a webhook
webhookは、Organizationもしくは特定のリポジトリにインストールできます。
webhookをセットアップするには、リポジトリもしくはOrganizationのsettings(設定)ページにアクセスしてください。 そこからWebhooksをクリックし、続いてAdd webhook(webhookの追加)をクリックしてください。
あるいは、Webhooks APIを通じてwebhookの構築と管理を行うこともできます。
webhookには、利用を開始する前にいくつかの設定オプションが必要です。 以下、それぞれの設定について見ていきます。
Payload URL
The payload URL is the URL of the server that will receive the webhook POST requests.
Since we're developing locally for our tutorial, we'll set it to the *.ngrok.io URL, followed by /payload. For example, http://7e9ea9dc.ngrok.io/payload.
Content type
webhookは、様々なコンテンツタイプを使って配信できます。
application/jsonコンテンツタイプは、JSONペイロードをPOSTリクエストのボディとして直接配信します。application/x-www-form-urlencodedコンテンツタイプは、JSONペイロードをpayloadと呼ばれるフォームのパラメータとして送信します。
要求に最適なものを選んでください。 このチュートリアルでは、デフォルトのコンテントタイプをapplication/jsonにしておけば問題ありません。
Secret
Setting a webhook secret allows you to ensure that POST requests sent to the payload URL are from GitHub Enterprise Server. When you set a secret, you'll receive the X-Hub-Signature and X-Hub-Signature-256 headers in the webhook POST request. For more information on how to use a secret with a signature header to secure your webhook payloads, see "Securing your webhooks."
SSL verification
If your "Payload URL" is a secure site (HTTPS), you will have the option to configure the SSL verification settings. If your "Payload URL" is not secure (HTTP), GitHub will not display this option. By default, GitHub verifies the SSL certificate of your website when delivering webhook payloads. SSL verification helps ensure that hook payloads are delivered to your URL endpoint securely. You have the option to disable SSL, but we recommend keeping Enable SSL verification selected.
Active
デフォルトでは、webhookの配信は「Active」です。 「Active」の選択を解除することで、webhookのペイロードの配信を無効化できます。
イベント
イベントは、webhookの中核です。 これらのwebhookは、リポジトリで特定のアクションが行われたときに動作し、それがサーバーのペイロードURLで受信され、処理が行われます。
webhookイベントと、それらのイベントがいつ動作するのかの完全なリストはwebhook APIリファレンスにあります。
Since our webhook is dealing with issues in a repository, we'll click Let me select individual events and then Issues. トリガーされたwebhookに対するIssueイベントを受信できるよう、必ずActiveを選択してください。 また、デフォルトオプションを使ってすべてのイベントを選択することもできます。
完了したら、Add webhook(webhookの追加)をクリックしてください。
Now that you've created the webhook, it's time to set up our local server to test the webhook. その方法はサーバーの設定を見てください。
Wildcard event
すべてのイベントに対してwebhookを設定するには、ワイルドカード(*)文字を使ってwebhookイベントを指定してください。 ワイルドカードイベントを追加すると、設定されたすべての既存のイベントはワイルドカードイベントで置き換えられ、サポートされるすべてのイベントについてペイロードが送信されます。 また、将来追加される可能性のある新しいイベントも自動的に受信されるようになります。

