Last active
May 20, 2019 07:54
-
-
Save kaibadash/c35c9aff270011aafa8f777dc7414f74 to your computer and use it in GitHub Desktop.
GitHubReview.gs
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
// GitHubからWebHookを受け、releaseタグを作成する。 | |
// GitHub webhookで以下を受取るように設定すること | |
// - Pull request reviews | |
// - Pull request review comments | |
// | |
// dependencies: | |
// SlackApp https://script.google.com/macros/library/versions/d/1on93YOYfSmV92R5q59NpKmsyWIQD8qnoLYk-gkQBI92C58SPyA2x1-bq | |
var CHANNEL = "your_review_channel"; | |
function doPost(e) { | |
try { | |
recievePayload(e.postData.getDataAsString()); | |
} catch (ex) { | |
notifyToSlack(CHANNEL, ex); | |
} | |
} | |
function recievePayload(json) { | |
var payload = JSON.parse(json); | |
var reviewee; | |
if (payload["pull_request"]["review"]) { | |
reviewee = payload["review"]["user"]["login"]; | |
} | |
if (!reviewee) { | |
reviewee = payload["pull_request"]["user"]["login"]; | |
} | |
var reviewer = payload["sender"]["login"]; | |
var pullRequest = payload["pull_request"]["html_url"]; | |
var action = "なにか"; | |
var reaction = "よしなにして" | |
var message = ""; | |
// FIXME: Class化して抽象化したい… | |
switch (payload["action"]) { | |
case "submitted": // approved or request changes | |
if (payload["review"]["state"] == "changes_requested") { | |
action = "`修正もしくは回答を依頼` :request_changes: "; | |
reaction = "対応して"; | |
message = payload["review"]["body"]; | |
break; | |
} | |
if (payload["review"]["state"] == "approved") { | |
action = "`承認` :lgtm: "; | |
reaction = "問題なければマージして"; | |
message = payload["review"]["body"]; | |
break; | |
} | |
return; // nop | |
case "closed": | |
return; // nop | |
case "review_requested": | |
reviewee = payload["pull_request"]["requested_reviewers"][0]["login"]; | |
action = "`レビューを依頼` "; | |
reaction = "レビューして"; | |
break; | |
default: | |
return; // nop | |
break; | |
} | |
notifyToSlack(CHANNEL, | |
reviewee + "さんへ。\n" + reviewer + "さんが " + action + " したよ。" + reaction + "ね!\n" + | |
pullRequest + "\n" + message); | |
} | |
function notifyToSlack(channel, message) { | |
var prop = PropertiesService.getScriptProperties().getProperties(); | |
var slackApp = SlackApp.create(prop.SLACK_TOKEN); | |
slackApp.chatPostMessage(channel, message, { username: "lily", icon_emoji: ":lily_bot:"}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment