Bitbucket の Webhook から Mattermost へ 投稿する
Bitbucket の Webhook から 受け取るデータの 中身とか
Mattermost の BOT の 作り方 とかはあるのですが
Pipelinesの 結果 を Mattermost で 受け取りたかったので 自作してみました
ちゃんと探したら あるのかもしれませんが
なかなか見つからなかったので 調べた結果の寄せ集めで 作ってみました
$json_encode = file_get_contents( 'php://input' ); $json_decode = json_decode( $json_encode ); if( $json_decode ) { $content = []; $content['text'] = '[' . $json_decode->commit_status->repository->full_name . ']' . ' ' . $json_decode->commit_status->type . ' : ' . $json_decode->commit_status->refname . PHP_EOL . 'Build status updated to ' . $json_decode->commit_status->state . PHP_EOL . '[' . $json_decode->commit_status->name . '](' . $json_decode->commit_status->url . ')'; // ------------------------------------------------------------------------------------------------- $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $_GET['url'] ); curl_setopt( $ch, CURLOPT_POST, true ); curl_setopt( $ch, CURLOPT_POSTFIELDS, 'payload=' . urlencode( json_encode( $content ) ) ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_exec( $ch ); curl_close( $ch ); }
Bitbucket の Webhook には 上記のPHP を 設置した URL に get値 で url に Mattermost の Incoming の URL を 付けて 設定します
Ex. (実際にはファイルは置かれていません)
https://tekuaru.jack-russell.jp/bitbucket/mattermost.php?url=https://mattermost.jack-russell.jp/hooks/xxx-generatedkey-xxx
これで Bitbucket の Pipelines の 結果 を Webhook を 通して Mattermost で 受け取ることができます
たまに失敗してたりするので 結果が送られてくるのは 助かりますね