-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.js
74 lines (63 loc) · 2.59 KB
/
slack.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
'use strict';
const request = require('request');
const NUMBER_OF_APPROVALS_NEEDED = process.env.NUMBER_OF_APPROVALS_NEEDED || 2;
const githubToSlackHandles = process.env.GITHUB_TO_SLACK_HANDLE_MAP
? JSON.parse(process.env.GITHUB_TO_SLACK_HANDLE_MAP)
: null;
const getSlackHandle = (githubHandle) => {
if (!githubToSlackHandles) {
return githubHandle;
}
return typeof githubToSlackHandles[githubHandle] !== 'undefined'
? `@${githubToSlackHandles[githubHandle]}`
: githubHandle;
};
const shortPrMessage = ({number, user, url}) => `_<${url}|*#${number}* by ${getSlackHandle(user.login)}>_`;
const fullPrMessage = ({url, title, repo, status}) => {
const reviewsNeeded = Math.max(NUMBER_OF_APPROVALS_NEEDED - status, 0);
return `*<${url}|${title}>*
_in <${repo.html_url}|${repo.name}> · ${reviewsNeeded} review${reviewsNeeded === 1 ? '' : 's'} needed_`;
};
const attachment = (text, color) => ({
color,
mrkdwn_in: ['text'],
text,
});
const pushPrsToSlack = (
reviewablePullRequests,
approvedPullRequests,
rejectedPullRequests
) => {
const text = "Take a little :clock1: for your fellow engineers and show these reviews some :heart:\n"
+ (process.env.WEB_URL ? `_<${process.env.WEB_URL}|View in browser>_\n` : '');
const reviewablePrAttachments = reviewablePullRequests.length === 0
? [ attachment("You've reviewed all the things. Good job! :allthethings:", '#f1c40f') ]
: reviewablePullRequests.map(pr => attachment(fullPrMessage(pr), '#f1c40f'));
request({
url: process.env.SLACK_ENDPOINT,
method: 'POST',
json: {
channel: process.env.SLACK_CHANNEL || null,
text,
attachments: [
...reviewablePrAttachments,
approvedPullRequests.length === 0 ? null : attachment(
`:tada: ${approvedPullRequests.map(pr => shortPrMessage(pr)).join(" · ")}`,
'#2ecc71'
),
rejectedPullRequests.length === 0 ? null : attachment(
`:fire: ${rejectedPullRequests.map(pr => shortPrMessage(pr)).join(" · ")}`,
'#e74c3c'
),
].filter(x => x)
}
});
};
const pushProjectsToSlack = pullRequests => {
pushPrsToSlack(
pullRequests.slice().filter(pr => pr.status >= 0 && pr.status < NUMBER_OF_APPROVALS_NEEDED),
pullRequests.slice().filter(pr => pr.status >= NUMBER_OF_APPROVALS_NEEDED),
pullRequests.slice().filter(pr => pr.status < 0)
);
};
module.exports = { pushProjectsToSlack };