Skip to content

Instantly share code, notes, and snippets.

@kminiatures
Created May 23, 2014 02:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kminiatures/df34f50e390b80f51a4c to your computer and use it in GitHub Desktop.
Save kminiatures/df34f50e390b80f51a4c to your computer and use it in GitHub Desktop.
bitbucket pull-request POST hook to Slack chat
<?
function hash_value($obj, $keys){
if(empty($keys)) return $obj;
$key = array_shift($keys);
if(isset($obj[$key])){
return hash_value($obj[$key], $keys);
}else{
return '';
}
}
function logg($file, $str){
file_put_contents($file, "\n-----------------------------\n", FILE_APPEND);
file_put_contents($file, $str, FILE_APPEND);
}
# https://api.bitbucket.org/2.0/repositories/hoge/fuga/pullrequests/1156/comments/1945
# to
# https://bitbucket.org/hoge/fuga/pull-request/1156/diff#comment-1945
#
# https://api.bitbucket.org/2.0/repositories/hoge/fuga/commit/8xxx8819e5f7
# to
# https://bitbucket.org/kobayashi_at_hoge/fuga/commits/8xxx8819e5f7
function api_link_to_html_link($src){
$src = str_replace(
array('api.', '/1.0/repositories', '/2.0/repositories', 'pullrequests', '/comments/', '/commit/'),
array('', '', '', 'pull-request', '/diff#comment-', '/commits/'),
$src);
return $src;
}
function create_msg($desc, $obj){
$user = hash_value($obj, array('user', 'display_name'));
if(!$user){
$user = hash_value($obj, array('author', 'display_name'));
}
$link = hash_value($obj, array('links', 'html', 'href'));
$link = str_replace('api.bitbucket', 'bitbucket', $link);
if(!$link){
$link = api_link_to_html_link(hash_value($obj, array('links', 'self', 'href')));
}
$title = hash_value($obj, array('title'));
$repository_name = hash_value($obj, array("destination",'repository','full_name'));
$branch_name = hash_value($obj, array("destination",'branch','name'));
$comment = hash_value($obj, array('content', 'raw'));
if($repository_name) $repository_name = "リポジトリ:$repository_name";
if($branch_name) $branch_name = "ブランチ:$branch_name";
if($user) $user .= "によって";
if($comment) $comment = "\n「{$comment}」\n";
return "$user $desc$comment\n$repository_name $branch_name\n$title $link";
}
$json_string = file_get_contents('php://input');
$obj = json_decode($json_string, true);
$file = "/tmp/receive_hook.txt";
file_put_contents($file, $json_string);
foreach(array(
'pullrequest_approve' => '承認されました',
'pullrequest_comment_created' => 'コメントが追加されました',
'pullrequest_comment_deleted' => 'コメントが削除されました',
'pullrequest_comment_updated' => 'コメントが更新されました',
'pullrequest_created' => 'プルリクエストができました',
'pullrequest_merged' => 'プルリクエストがマージされました',
'pullrequest_declined' => 'プルリクエストが辞退されました',
'pullrequest_unapprove' => '承認が取り消されました',
'pullrequest_updated' => 'プルリクエストが更新されました',
) as $key => $desc){
if(isset($obj[$key])){
$text = create_msg($desc, $obj[$key]);
break;
}
}
if(!isset($text)) $text = "Hello";
logg($file, $text);
$params = http_build_query(array(
'token' => trim(file_get_contents('api_key')),
'channel' => trim(file_get_contents('channel')),
'text' => $text,
'username' => 'Bitbucket-Slack-Bot',
));
$url = "https://slack.com/api/chat.postMessage?$params";
logg($file, $url);
$result = `wget -O responce.txt '$url' 2>&1`;
logg($file, $result);
@1stevengrant
Copy link

found this by Googling, how is it used?

@kminiatures
Copy link
Author

create fook at bitbucket

http://gyazo.com/b5be84464778bf417f70491e7b48ba0f.png

directory tree

$ tree
.
├── api_key   # slack api key
├── channel   # slack post channel
└── index.php # bitbucket hook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment