以前作成したコード

Bitbucket の Webhook から Mattermost へ 投稿する
https://tekuaru.jack-russell.jp/2020/06/05/2477/

を 元に GitHub から Mattermost へ Workflow runs の 結果 を 投稿するプログラムを書きました

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->workflow_run ) {

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

  $content['icon_url'] = 'https://avatars.githubusercontent.com/u/0';

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

  $attachments = [];

  if( $json_decode->workflow_run->conclusion == 'success' ) {
    $attachments['color'] = '#00c100';
  } else {
    $attachments['color'] = '#e40303';
  }

  $attachments['author_name'] = $json_decode->workflow_run->actor->login;
  $attachments['author_icon'] = $json_decode->workflow_run->actor->avatar_url;

  $attachments['fields'][] = [
    'title' => 'Repository',
    'value' => '[' . $json_decode->workflow_run->head_repository->full_name . '](' . $json_decode->workflow_run->head_repository->html_url . ')',
    'short' => false,
  ];

  $attachments['fields'][] = [
    'title' => 'Branch',
    'value' => $json_decode->workflow_run->head_branch,
    'short' => true,
  ];

  $attachments['fields'][] = [
    'title' => 'Commit',
    'value' => $json_decode->workflow_run->head_commit->message,
    'short' => true,
  ];

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

  $attachments['fields'][] = [
    'title' => 'Jobs',
    'value' => '[' . $json_decode->workflow_run->id . '](' . $json_decode->workflow_run->html_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 ) && ! empty( $json_decode->workflow_run->conclusion ) ) {
  $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 );
}

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

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

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

Related Article

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

詳細へ »

バージョン管理 してる?? ってか 何使ってる??

詳細へ »

Vagrant で Mattermost を 立ち上げてみた

詳細へ »