Create a gist now

Instantly share code, notes, and snippets.

Embed
プルリクエストのラベル変更時にSlack通知するGoogleAppsScriptサンプル
function doPost(e) {
var url = 'https://hooks.slack.com/services/***************'; //slack webhook url
var channel = '***************'; //投稿先チャンネル名
var userName = 'github'; //BOTの名前
var json = e.postData.getDataAsString();
var payload = JSON.parse(json);
if (isSendAction(payload)) {
sendSlack(url, userName, channel, sendText(payload))
}
}
function sendText(payload) {
var labelName = payload.label.name;
var pullRequestUrl = payload.pull_request.html_url;
var text = pullRequestUrl + '\nに「' + labelName + '」がつきました\nレビューお願いします!';
return text;
}
function isSendAction(payload) {
// ラベルをつける以外のアクションは通知しない
if (payload.action !== 'labeled') {
return false;
}
// 通知対象のラベルの名前
if (payload.label.name === 'レビュー待ち' || payload.label.name === '再レビュー待ち' ) {
return true;
}
return false;
}
function sendSlack(url, userName, channel, text) {
UrlFetchApp.fetch(url, {
method: 'post',
payload: {
payload: JSON.stringify({
username : userName,
channe : channel,
icon_emoji : ':github:',
attachments: [
{
text: text,
mrkdwn_in: ["text"],
}
],
link_names: 1,
}),
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment