Repository navigation
Expand file tree
/
Copy pathcommon.js
More file actions
145 lines (132 loc) · 4.55 KB
/
Copy pathcommon.js
File metadata and controls
145 lines (132 loc) · 4.55 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
const https = require('https');
const MAPPING_LIST = require('./mapping.json');
function getMentionList(body) {
const mentionList = [];
if (body == null || body === undefined) {
return mentionList;
}
Object.keys(MAPPING_LIST).forEach(key => {
if (body.indexOf(`@${key}`) >= 0) {
mentionList.push(`<@${MAPPING_LIST[key]}>`);
}
});
return mentionList;
}
function getPostData(message) {
return {
username: 'github2slack',
channel: process.env.CHANNEL_ID,
text: message.title,
token: process.env.API_TOKEN
};
}
function getPostOptions() {
return {
host: 'slack.com',
port: '443',
path: '/api/chat.postMessage',
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.API_TOKEN}`,
'Content-Type': 'application/json'
}
};
}
function post(message) {
return new Promise((resolve, _reject) => {
const data = getPostData(message);
const options = getPostOptions();
const req = https.request(options, res => {
res.setEncoding('utf8');
res.on('data', chunk => {
const result = JSON.parse(chunk);
if (result.ok) {
resolve(chunk);
} else {
resolve(new Error());
}
});
});
req.on('error', e => {
resolve(new Error(e));
});
req.write(JSON.stringify(data));
req.end();
});
}
function getMessageObject(event) {
const gitHubBody = JSON.parse(event.body);
const eventName = event.headers['X-GitHub-Event'];
const msgObj = {
title: null,
body: null
};
if (eventName === 'pull_request') {
const pullRequestLink = `[<${gitHubBody.pull_request.html_url}|${gitHubBody.pull_request.title}>]`;
const pullRequestUser = gitHubBody.pull_request.user.login;
if (gitHubBody.action === 'opened') {
msgObj.title = `${pullRequestUser} Pullrequest Opened ${pullRequestLink}`;
msgObj.body = gitHubBody.pull_request.body;
} else if (gitHubBody.action === 'closed') {
msgObj.title = `Pullrequest Closed ${pullRequestLink}`;
msgObj.body = '';
} else if (gitHubBody.action === 'review_requested') {
msgObj.title = `Review requested ${pullRequestLink}`;
msgObj.body = `@${gitHubBody.requested_reviewer.login}`;
}
} else if (eventName === 'issues') {
const issueLink = `[<${gitHubBody.issue.html_url}|${gitHubBody.issue.title}>]`;
if (gitHubBody.action === 'opened') {
msgObj.title = `Issue Opened ${issueLink}`;
msgObj.body = gitHubBody.issue.body;
} else if (gitHubBody.action === 'closed') {
msgObj.title = `Issue Closed ${issueLink}`;
msgObj.body = '';
}
} else if (eventName === 'issue_comment' && gitHubBody.action === 'created') {
msgObj.title = `${gitHubBody.comment.user.login} Comment on [<${gitHubBody.comment.html_url}|${gitHubBody.issue.title}>]`;
msgObj.body = gitHubBody.comment.body;
} else if (
eventName === 'pull_request_review' &&
gitHubBody.action === 'submitted'
) {
const pullRequestLink = `[<${gitHubBody.pull_request.html_url}|${gitHubBody.pull_request.title}>]`;
if (gitHubBody.review.state === 'approved') {
msgObj.title = `${gitHubBody.review.user.login} Pullrequest approval ${pullRequestLink}`;
msgObj.body = `@${gitHubBody.pull_request.user.login}`;
} else if (gitHubBody.review.state === 'changes_requested') {
msgObj.title = `${gitHubBody.review.user.login} Pullrequest change request ${pullRequestLink}`;
msgObj.body = '';
}
} else if (
eventName === 'pull_request_review_comment' &&
gitHubBody.action === 'created'
) {
msgObj.title = `${gitHubBody.comment.user.login} Review on [<${gitHubBody.comment.html_url}|${gitHubBody.pull_request.title}>]`;
msgObj.body = gitHubBody.comment.body;
} else if (
eventName === 'discussion' &&
gitHubBody.action === 'created'
) {
const discussionLink = `[<${gitHubBody.discussion.html_url}|${gitHubBody.discussion.title}>]`;
msgObj.title = `Discussion Created ${discussionLink}`;
msgObj.body = gitHubBody.discussion.body;
} else if (eventName === 'discussion_comment' && gitHubBody.action === 'created') {
msgObj.title = `${gitHubBody.comment.user.login} Comment on [<${gitHubBody.comment.html_url}|${gitHubBody.discussion.title}>]`;
msgObj.body = gitHubBody.comment.body;
} else {
return msgObj;
}
const mentionList = getMentionList(msgObj.body);
if (mentionList.length > 0) {
msgObj.title = `${mentionList.join(' ')} ${msgObj.title}`;
}
return msgObj;
}
module.exports = {
getMentionList,
getPostData,
getPostOptions,
post,
getMessageObject
};