-
Notifications
You must be signed in to change notification settings - Fork 1
/
feishuWebhook.js
132 lines (123 loc) · 3.94 KB
/
feishuWebhook.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const rp = require("request")
// forwardMessageFeishu()
exports.forwardMessageFeishu = function (req, res) {
// function forwardMessageFeishu(req, res) {
var url = req.query["url"]
if (url == "") {
res.status(400).send({ code: "Not find a webhook url in the query" })
return 1
}
var notificationTypeList = ["pull_request", "release"]
var actionTypeList = ["opened", "closed", "published"]
var githubEventType = req.headers["x-github-event"]
var payload = JSON.parse(req.body["payload"])
if (payload == null) {
console.log(req)
res.status(400).send({ code: "Not find a body in the request" })
// res.status(400).send(req.body)
return 1
}
console.log(payload.action)
console.log(actionTypeList.includes(payload.action))
if (!(actionTypeList.includes(payload.action))) {
console.log(req)
res.status(400).send({ code: "The Action-Type is not in notification list" })
return 1
}
let eventName = ""
let eventBody = ""
console.log(payload.sender.login)
let eventUrl = payload.repository != null ? payload.repository.html_url != null ? payload.repository.html_url : "" : ""
const senderName = payload.sender != null ? payload.sender.login != null ? payload.sender.login : "Github" : "Github"
switch (githubEventType) {
case "pull_request": {
const body = payload
eventName = [body.sender.login, "just", body.action, "a PR: ", body.repository.name + ":", body.pull_request.head.label.split(":")[1], "->", body.pull_request.base.label.split(":")[1]].join(" ")
eventBody = ["**" + body.pull_request.title + "**", body.pull_request.body].join("\n")
eventUrl = body.pull_request.html_url
break
}
case "release": {
const body = payload
console.log(payload.release)
eventName = [body.sender.login, "just", body.action, "a release: ", body.release.tag_name].join(" ")
eventBody = "版本更新:" + body.release.tag_name
eventUrl = body.release.html_url
break
}
default: {
eventName = "暂不支持该消息类型"
console.log("暂不支持该消息类型")
break
}
}
const options = {
method: "POST",
uri: url.toString(),
body: getMessageBody(eventName, eventBody, eventUrl, senderName),
json: true,
}
console.log(options)
try {
if (notificationTypeList.includes(githubEventType)) {
const result = rp(options)
res.status(200).send(result)
return result
} else {
res.status(200).send({ result: "Github EventType is unsupport" })
}
} catch (err) {
console.log("=============error=============")
console.log(err)
return { code: "some error happened, please see it in firebase console log" }
}
}
function getMessageBody(eventName, eventBody, eventUrl, senderName) {
var body = {
"msg_type": "interactive",
"card": {
"config": {
"wide_screen_mode": true,
},
"header": {
"title": {
"tag": "plain_text",
"content": eventName, // 标题内容
},
"template": "blue", // 标题主题颜色
},
"elements": [
{
"tag": "markdown",
"content": eventBody + "\n[**Look it on Github**]($urlVal)", // if u want @all, add this: <at id=all></at>
"href": {
"urlVal": {
"url": eventUrl,
"android_url": eventUrl,
"ios_url": eventUrl,
"pc_url": eventUrl,
},
},
},
{
"tag": "note",
"elements": [
{
"tag": "img",
"img_key": "img_v2_cc1a5248-b289-457f-87f9-1061a325ab5g",
"alt": {
"tag": "plain_text",
"content": "GitHub",
},
},
{
"tag": "lark_md",
"content": "[" + senderName + "](https://github.com/" + senderName + ")",
},
],
},
],
},
}
return body
}