Created
March 26, 2018 10:49
-
-
Save masayokoo/371fe93c026a5beedef9af426d97485d to your computer and use it in GitHub Desktop.
プルリクエストのラベル変更時にSlack通知するGoogleAppsScriptサンプル
This file contains hidden or 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 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