最終的にされる通知のメッセージイメージ
Name: Taro, Repository: WebhookSample, Branch: master
Comment: First Commit
1 added webhook.php: , 0 removed: , 0 modified: ,
http://www.your.gitbucket.server.com/gitbucket/username/repogitoryname/commit/xxxxxxxxxxxxxxxxxxx
Announcement
GitBucketのバージョンによって,Webhookで飛ぶJSONの中身が異なります.
最初の記事はv2.7の時に書いたものですが,V3.1.1にしたらうまく表示されませんでした.
一応下記のように書いたら,Webhookで飛んできたJSONをそのままSlackに投げるので,
目視で確認できます.(他にもっといい方法も有ると思いますが.)
<?php
$text = $_REQUEST['payload'];
if(isset($_GET['webhook'])) {
$webhook = $_GET['webhook'];
}
$post = array(
'text' => $text,
'username' => 'Incoming WebHooks'
);
$ch = curl_init($webhook);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array());
curl_setopt($ch, CURLOPT_POSTFIELDS, array('payload' => json_encode($post)));
curl_exec($ch);
curl_close($ch);
?>
以下本題です.
Webhook URLの取得
- 左上にあるチーム名横にある v をクリック
- Configure Integrations選択
- Incoming WebHooks を Add
- Channelを選択して "Add Incoming WebHooks Integration" 押す
ここまででWebhook URL取得できました.
https://hooks.slack.com/services/AAAAAA/BBBBBB/XXXXXX
という感じのもの.
このURL中に,Slackのチーム名,チャネル名などの情報が埋め込まれている感じです.
動作確認
Webhook URLを取得した画面にあるExampleの通りcURLコマンドを実行すると,
curl -X POST --data-urlencode 'payload={"channel": "#your_channel", "username": "webhookbot", "text": "This is posted to #your_channel and comes from a bot named webhookbot.", "icon_emoji": ":ghost:"}' https://hooks.slack.com/services/AAAAAA/BBBBBB/XXXXXX
選択したチャネルに
This is posted to #your_channel and comes from a bot named webhookbot.
とおばけアイコンと共にメッセージが表示されます.
ここで,チャンネル名を別のに変更すると,そのチャンネルにメッセージが飛んでしまいます(これではチャンネル毎にWebhook URLを取得する意味が無い…).なので,Slackのバグの可能性有り.
簡単にコマンドについて説明すると,usernameが発言者名,textが発現するメッセージ,icon_emojiがアイコンの画像の指定.
"icon_emoji": ":ghost:"
を書かないと,デフォルトのアイコンになります.
デフォルトのアイコンは,このWebhook URLのページの一番下で好きな画像をアップロードして変更できます.
usernameも記述しないと,アイコンと同様の箇所で設定したものが使用されます.
なので,以降username, icon_emoji, (先ほどのバグがあるので)channelは指定しないものとします.
Slackにコメントするサーバを立てる
Apacheなどで適当にサーバを立てます.
Ubuntuだと/var/www/htmlにwebhook.phpなど適当な名前をつけてファイルを作ります.
php初めて書いたのでもっときれいな書き方有ると思います…
<?php
if(isset($_REQUEST['payload'])) {
$payload = json_decode($_REQUEST['payload'], true);
$branch = str_replace('refs/heads/', '', $payload['ref']);
$text = 'Name: ' . $payload['pusher']['login'] . ', Repository: ' . $payload['repository']['name'] . ', Branch: ' . $branch . "\n";
foreach ($payload['commits'] as $commit) {
$text .= 'Comment: ' . $commit['message'];
$text .= count($commit['added']) . ' added: ';
foreach ($commit['added'] as $added) {
$text .= $added;
$text .= ', ';
}
if (count($commit['added']) == 0) {
$text .= ', ';
}
$text .= count($commit['removed']) . ' removed: ';
foreach ($commit['removed'] as $removed) {
$text .= $removed;
$text .= ', ';
}
if (count($commit['removed']) == 0) {
$text .= ', ';
}
$text .= count($commit['modified']) . ' modified: ';
foreach ($commit['modified'] as $modified) {
$text .= $modified;
$text .= ', ';
}
if (count($commit['modified']) == 0) {
$text .= ', ';
}
$text .= "\n";
$text .= $commit['html_url'] . "\n";
}
if(isset($_GET['webhook'])) {
$webhook = $_GET['webhook'];
}
$post = array(
'text' => $text,
);
$ch = curl_init($webhook);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array());
curl_setopt($ch, CURLOPT_POSTFIELDS, array('payload' => json_encode($post)));
curl_exec($ch);
curl_close($ch);
}
?>
GitBucket v2.7ではURLだった部分が,v3.1.1では html_url になりました.
他にもちょこちょこ変更あり.
webhook.phpに関する説明
GitBucketで何かイベントが発生すると,JSON形式でメッセージが飛びます.
このプログラムでは,そのメッセージをjson_decodeなどで必要な情報をだけ取り出して,Slackでbotが発言するメッセージに変換して,先ほどのcURLコマンドを実行して発言をしています.
GitBucketの設定
- GitBucket上の連携したいリポジトリのページに行きます.
- 右側のメニューのWikiの下のSettingをクリック.
- Service Hooksを選択.
- WebHook URLsに先ほど立てたサーバのURLを指定(例:http://127.0.0.1/webhook.php?webhook=https://hooks.slack.com/services/AAAAAA/BBBBBB/XXXXXX)
以上で完成です.
これで指定のリポジトリで何かイベントがあるとSlackにメッセージが行きます.
webhook.phpで生成されるメッセージは結構適当です.
適宜変更してください.