制造一个github的PR BOT

简单来说就是利用Github 的Github Hook,选择在PR生成并指定Reviwer的时候进行事件呼出。
而利用GAS的api生成功能接受Hook发来的信息,再往slack的频道里发送消息。

Github webhooks:
Github/Setting/Webhooks/Add Webhook
选择类型为json,url为gas生成的app的url

GAS:
deploy as a web app
然后选择Anyone,even anonymous can access to the app.

GAS的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var CHANNEL = "YOUR_CHSNNEL_NAME";

function doPost(e) {
try {
recievePayload(e.postData.getDataAsString());
} catch (ex) {
notifyToSlack(CHANNEL, ex);
}
}

function recievePayload(json) {
var payload = JSON.parse(json);

if (payload.action === "opened") {
notifyToSlack(CHANNEL, review_request(payload))
}

if (payload.action === "submitted") {
notifyToSlack(CHANNEL, approve(payload))
}
}

function approve (payload) {
var message = "";
var reviwers = "";
payload.pull_request.requested_reviewers.forEach( i => {
reviwers += i.login + "さん、"
})

// PR Approvedされた時:
if (payload.review.state === "approved") {
message = convert_user(payload.pull_request.user.login) + "さん、\n下記のPR :lgtm2: をもらいました! \n"
+ "問題なければマージしてね \n\n"
+ "--------------------------------------------------\n "
+ "■PR TITLE: \n"
+ payload.pull_request.title + "\n\n"
+ "■PR URL: \n"
+ "\n" + payload.pull_request.html_url + "\n\n"
+ "--------------------------------------------------\n "
} else {
return;
}

return message;

}

function review_request(payload) {
var message = "";
var reviwers = "";

payload.pull_request.requested_reviewers.forEach( i => {
reviwers += convert_user(i.login) + "さん "
})

// PR新規作成された時:
if (payload.pull_request.state === "open" && reviwers !== "") {
message = reviwers + payload.pull_request.user.login + "からのPR依頼がきました! \n"
+ "手が空いてる時に、下記のPRのレビューをお願い致します〜 \n\n"
+ "--------------------------------------------------\n "
+ "■PR TITLE: \n"
+ payload.pull_request.title + "\n\n"
+ "■PR URL: \n"
+ "\n" + payload.pull_request.html_url + "\n\n"
+ "--------------------------------------------------\n "
} else {
return
//message = payload.pull_request.user.login + "下記のPRにReviewerを指定してください \n" + payload.pull_request.title + "\n" + payload.pull_request.html_url;
}

return message;

}

function convert_user(name) {
switch(name) {
case "<github-user-name>":
return "<slack-user-id>"
break;
default:
break;
}
}

function notifyToSlack(channel, message) {
var prop = PropertiesService.getScriptProperties().getProperties();
var slackApp = SlackApp.create('xoxp-xxxxxxxxxxxxxx');
slackApp.chatPostMessage(channel, message, { 
username: "PR_娘",
icon_emoji: ":musume:"
});
}