-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
97 lines (89 loc) · 2.64 KB
/
handler.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
"use strict";
require("dotenv").config();
const request = require("request");
const createPostWithPublication = require("./src/medium");
const successResponse = body => ({
statusCode: 200,
body: JSON.stringify(body)
});
const getIssueComments = commentsURL =>
new Promise((resolve, reject) => {
request(
{
url: commentsURL,
headers: {
"User-Agent": "kjj6198"
}
},
(err, res, body) => (err ? reject(err) : resolve(JSON.parse(body)))
);
});
const serializeComments = comments =>
comments.map(comment => comment.body).join("\n");
module.exports.uploadToMedium = (event, context, callback) => {
const githubEvent = event.headers["X-GitHub-Event"];
const body = JSON.parse(event.body);
console.log("Event: ", JSON.stringify(event));
if (githubEvent === "issues" && body.action === "closed") {
const commentsURL = body.issue.comments_url;
getIssueComments(commentsURL)
.then(serializeComments)
.then(serializedComments =>
createPostWithPublication(process.env.MEDIUM_PUBLISH_KEY)({
title: body.issue.title,
content: body.issue.body + "\n" + serializedComments
})
)
.then(body => {
callback(null, {
statusCode: 200,
body: JSON.stringify(body)
});
})
.catch(err => callback(null, { body: JSON.stringify(err) }));
} else {
callback(null, {
statusCode: 400,
body: JSON.stringify({
message: "bad request. Action not allow."
})
});
}
};
module.exports.toSlack = (event, context, callback) => {
const githubEvent = event.headers["X-GitHub-Event"];
const body = JSON.parse(event.body);
request.post(process.env.SLACK_WEBHOOK_URL, {
body: JSON.stringify({
username: body.issue.assignee && body.issue.assignee.login,
text: body.issue.title,
attachments: [
{
color: "good",
author_name: body.issue.assignee && body.issue.assignee.login,
author_link: body.issue.assignee.html_url,
author_icon: body.issue.assignee.avatar_url,
title: body.issue.title,
title_link: body.issue.html_url,
fields: [
{
title: "內容",
value:
},
{
title: "主講",
value: body.issue.assignee && body.issue.assignee.login,
short: true
}
],
footer: body.issue.labels.map(l => l.name).join(", "),
ts: Math.floor(new Date(body.issue.closed_at).getTime() / 1000)
}
]
})
});
callback(null, {
statusCode: 200,
body: "ok"
});
};