-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
89 lines (71 loc) · 2.8 KB
/
index.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
const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
const FRONTEND_REPO_NAME = proces.env.FRONTEND_REPO_NAME;
const BACKEND_REPO_NAME = proces.env.BACKEND_REPO_NAME;
const dateFormat = require('dateformat');
const IncomingWebhook = require('@slack/client').IncomingWebhook;
const webhook = new IncomingWebhook(SLACK_WEBHOOK_URL);
module.exports.subscribe = (event, callback) => {
const build = JSON.parse(new Buffer(event.data, 'base64').toString());
if (build.status === 'SUCCESS' || build.status === 'FAILURE') {
const message = generateSlackMessage(build);
webhook.send(message, callback);
} else {
console.log(`build status ${build.status}`);
console.log(`build info ${JSON.stringify(build)}`);
}
};
const generateSlackMessage = (build) => {
let repoName = 'UNKOWN REPONAME';
if (build.source.repoSource.repoName === FRONTEND_REPO_NAME) repoName = "Frontend";
if (build.source.repoSource.repoName === BACKEND_REPO_NAME) repoName = "Backend";
let environment = 'UNKOWN BRANCH'
if (build.source.repoSource.branchName === 'develop') environment = 'development';
if (build.source.repoSource.branchName === 'master') environment = 'production';
let fields = []
/** time to deployment */
const startTime = dateFormat(build.startTime, "h:MM:ss TT");
const finishTime = dateFormat(build.finishTime, "h:MM:ss TT");
const timeDifference = timeDiff(build.startTime, build.finishTime);
let deployment_time_field = {
title: 'deployment time: ',
value: `${startTime} -> ${finishTime} (${timeDifference})`
}
fields.push(deployment_time_field)
/** build steps */
let deployment_step_fields = generateStepTimes(build.steps)
fields = fields.concat(deployment_step_fields)
let message = {
text: `\`${build.status}\` ${repoName} to ${environment} environment`,
mrkdwn: true,
attachments: [
{
title: `build id ${build.id}`,
title_link: build.logUrl,
fields: fields
}
]
};
return message
}
const generateStepTimes = (steps) => {
stepTimes = [];
steps.forEach((step) => {
if (step.status === 'SUCCESS') {
stepTimes.push({
title: `${step.id}`,
value: `\`${step.status}\` -> \`${timeDiff(step.timing.startTime, step.timing.endTime)}\``
})
} else {
stepTimes.push({
title: `${step.id}`,
value: `\`${step.status}\``
})
}
})
return stepTimes;
}
const timeDiff = (startTime, finishTime) => {
let diff = new Date(finishTime) - new Date(startTime);
if (diff > 60e3) return `${Math.floor(diff / 60e3)} minutes`
else return `${Math.floor(diff / 1e3)} seconds`
}