/
slack-emoji-issues.js
79 lines (65 loc) · 2.3 KB
/
slack-emoji-issues.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
// Description:
// Uses the Slack Real Time Messaging API to file issues from emoji reactions
//
// Configuration:
// HUBOT_SLACK_GITHUB_ISSUES_CONFIG_PATH
// HUBOT_GITHUB_TOKEN
// HUBOT_SLACK_TOKEN
'use strict'
var path = require('path')
var slackGitHubIssues = require('..')
// Remove this block and close #15 when slackapi/hubot-slack#429 is done.
require('hubot-slack')
var CoffeeScriptCompatibleHubot = require('hubot')
var Hubot = require('hubot/es2015')
Hubot.Robot.prototype.react = CoffeeScriptCompatibleHubot.Robot.prototype.react
function configParams() {
var params = {updates: {}}
params.path = process.env.HUBOT_SLACK_GITHUB_ISSUES_CONFIG_PATH ||
path.join('config', 'slack-emoji-issues.json')
params.updates.slackApiToken = process.env.HUBOT_SLACK_TOKEN
params.updates.githubApiToken = process.env.HUBOT_GITHUB_TOKEN
return params
}
function slackDataStore(robot) {
// This may be undefined when running under test.
if (robot.adapter.client) {
return slackGitHubIssues.slackRtmDataStore(robot.adapter.client)
}
}
function fileIssue(robot, filer, response) {
// ReactionMessage (node_modules/hubot-slack/src/reaction-message.coffee) will
// trim the 'reaction_' prefix from 'reaction_added'. The slack-emoji-issues
// library requires we put it back.
response.message.type = 'reaction_' + response.message.type
filer.execute(response.message)
.then(function(issueUrl) {
response.reply('created: ' + issueUrl)
robot.emit(response.message.type, { err: null, created: issueUrl })
})
.catch(function(err) {
if (err) {
err = err.message || err
response.reply(err)
}
robot.emit(response.message.type, { err: err, created: null })
})
}
module.exports = function(robot) {
var logger = robot.logger,
filer,
listener = function(response) {
fileIssue(robot, filer, response)
}
try {
logger = slackGitHubIssues.logger(logger)
filer = slackGitHubIssues.singleInstanceReactionIssueFiler(
configParams(), slackDataStore(robot), logger)
listener.impl = filer
robot.react(listener)
logger.info(null, 'listening for reaction_added events')
} catch (err) {
logger.error(null, 'reaction_added listener registration failed:',
err instanceof Error ? err.message : err)
}
}