Skip to content

Instantly share code, notes, and snippets.

@masayokoo
Created March 26, 2018 10:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masayokoo/371fe93c026a5beedef9af426d97485d to your computer and use it in GitHub Desktop.
Save masayokoo/371fe93c026a5beedef9af426d97485d to your computer and use it in GitHub Desktop.
プルリクエストのラベル変更時に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