Created
May 23, 2014 02:01
-
-
Save kminiatures/df34f50e390b80f51a4c to your computer and use it in GitHub Desktop.
bitbucket pull-request POST hook to Slack chat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
create fook at bitbucket
directory tree