てくてくあるく

WordPress の テーマ とか プラグイン に ついて 勉強しています

Bitbucket の Webhook から 受け取るデータの 中身とか
Mattermost の BOT の 作り方 とかはあるのですが
Pipelinesの 結果 を Mattermost で 受け取りたかったので 自作してみました

ちゃんと探したら あるのかもしれませんが
なかなか見つからなかったので 調べた結果の寄せ集めで 作ってみました

define( 'DEBUG', false );

// -----------------------------------------------

$time = time();

$json_encode = file_get_contents( 'php://input' );
$json_decode = json_decode( $json_encode );

if( DEBUG ) {
  file_put_contents( __DIR__ . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . $time . '-json.txt', $json_encode );
  file_put_contents( __DIR__ . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . $time . '-get.txt', json_encode( $_GET ) );
}

// -----------------------------------------------

$content = [];

if( $json_decode && $json_decode->commit_status ) {

  $content['username'] = 'Bitbucket';

  // ----------------------

  $attachments = [];

  if( $json_decode->commit_status->state == 'SUCCESSFUL' ) {
    $attachments['color'] = '#00c100';
  } else {
    $attachments['color'] = '#e40303';
  }

  $attachments['fields'][] = [
    'title' => ucfirst( $json_decode->commit_status->type ),
    'value' => $json_decode->commit_status->refname,
    'short' => true,
  ];

  $attachments['fields'][] = [
    'title' => 'Repository',
    'value' => $json_decode->commit_status->repository->full_name,
    'short' => true,
  ];

  $attachments['fields'][] = [
    'title' => 'Result',
    'value' => $json_decode->commit_status->state,
    'short' => true,
  ];

  $attachments['fields'][] = [
    'title' => 'Jobs',
    'value' => '[' . $json_decode->commit_status->name . '](' . $json_decode->commit_status->url . ')',
    'short' => true,
  ];

  $content['attachments'][] = $attachments;

  // ----------------------

}

if( DEBUG ) {
  file_put_contents( __DIR__ . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . $time . '-content.txt', json_encode( $content ) );
}

// -----------------------------------------------

$result = '';

if( $_GET['url'] && ! empty( $content ) ) {
  $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 );
  $result = curl_exec( $ch );
  curl_close( $ch );
}

if( DEBUG ) {
  file_put_contents( __DIR__ . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . $time . '-result.txt', $result );
}

2022/08/05 更新

Bitbucket の Webhook には 上記のPHP を 設置した URL に get値 で url に Mattermost の Incoming の URL を 付けて 設定します

Ex. (実際にはファイルは置かれていません)
https://tekuaru.jack-russell.jp/webhooks/bitbucket/?url=https://mattermost.jack-russell.jp/hooks/xxx-generatedkey-xxx

これで Bitbucket の Pipelines の 結果 を Webhook を 通して Mattermost で 受け取ることができます

たまに失敗してたりするので 結果が送られてくるのは 助かりますね

Related Article

GitHub や Bitbucket から 直接 WordPress の 更新 を 使って テーマ や プラグイン を アップデートする

詳細へ »

Vagrant で Mattermost を 立ち上げてみた

詳細へ »

Bitbucket の Pipelines で 運用出来る状態に なりました

詳細へ »