-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (78 loc) · 2.46 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
90
91
92
const jira = require('./lib/jira');
const messageParser = require('./lib/messageParser');
const messageFormatter = require('./lib/messageFormatter');
const slackUtils = require('./lib/slackUtils');
const configuration = {
jiraAPIHost: process.env.JIRA_API_HOST,
jiraHost: process.env.JIRA_HOST,
projectsKeys: process.env.JIRA_PROJECTS_KEYS.split(','),
jiraUser: process.env.JIRA_USER,
jiraPwd: process.env.JIRA_PWD,
},
jiraApi = jira(configuration),
parser = messageParser(configuration),
formatter = messageFormatter(configuration),
JIRA_KEY_MATCHER = /[A-Z0-9]*-\d+/i;
let bot;
const DEBUG = (message) => {
bot.logger.debug('JIRA-ISSUE-FETCHER: ' + message);
}
module.exports = (robot) => {
bot = robot;
DEBUG('Started!');
bot.hear(JIRA_KEY_MATCHER, (res) => {
if (slackUtils.isBotMessage(res.message)) {
return;
}
let message = {
room: res.message.user.room,
text: res.message.text,
needDetailedInfo: slackUtils.isDirectMessage(res.message.text, robot.name),
thread_ts: res.message.thread_ts,
};
getIssues(parser.extractIssueKeys(message))
.then(computeInfoLevel)
.then(buildResponseMessage)
.then(sendResponseMessage)
.catch((err) => handleError(err, message));
});
};
function getIssues(message) {
message.issues = [];
if (message.issueKeys.length) {
return jiraApi.getIssuesByKeys(message.issueKeys).then((issues) => {
message.issues = issues;
return message;
});
}
return Promise.resolve(message);
}
function computeInfoLevel(message) {
const messageChunks = message.text ? message.text.split(' ') : [];
message.needDetailedInfo |= messageChunks.length === message.issueKeys.length;
return message;
}
function buildResponseMessage(message) {
if (message.needDetailedInfo) {
message.response = formatter.buildDetailedMessages(message.issues);
} else {
message.response = formatter.buildMessages(message.issues);
}
return message;
}
function sendResponseMessage(message) {
if (message.response) {
postMessage(message.room, null, {
as_user: true,
attachments: message.response,
thread_ts: message.thread_ts,
});
}
}
function handleError(err, message) {
bot.logger.error('hubot-jira-issue-fetcher:', err.stack);
postMessage(message.room, formatter.buildErrorMessage(err.response.body), { as_user: true });
}
function postMessage(room, text, options) {
bot.adapter.client.web.chat.postMessage(room, text, options);
}