-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
137 lines (109 loc) · 4.02 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
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
const express = require('express');
const bodyParser = require('body-parser');
const { GithubClient } = require('./GithubClient');
const validateRequest = require('./validateRequest');
const createModalAction = require('./actions/createModal');
const createGithubIssue = require('./actions/createGithubIssue');
const createServer = (config) => {
const app = express();
const githubClient = GithubClient({
token: config.githubToken,
baseUrl: config.githubBaseUrl,
});
app.use(bodyParser.urlencoded({extended: false}));
app.get('/', (req, res) => res.send('Hello World!'));
app.get('/health', (req, res) => res.send('OK'));
app.post('/create-github-issue', async (req, res) => {
try {
const {isValid, reason} = validateRequest(req, config.slackSigningSecret);
if (!isValid) {
console.log(reason);
res.status(403);
return res.send();
}
const payloadParsed = JSON.parse(req.body.payload);
const {type} = payloadParsed;
if (type === 'message_action') {
await createModalAction(payloadParsed, config.slackApiToken, config.channelMap);
} else if (type === 'view_submission') {
await createGithubIssue(githubClient, payloadParsed);
} else {
res.status(500);
console.error(`Action "${type}" is not recognized`);
return res.send();
}
res.status(200);
return res.send();
} catch (error) {
console.error(error);
res.status(500);
return res.send('An error occurred, try again later')
}
});
app.post('/external-select', async (req, res) => {
try {
const payload = JSON.parse(req.body.payload);
const {action_id: actionId, value} = payload;
if (actionId !== 'repository') {
console.log(`Unrecognized external-select action "${actionId}"`);
res.status(500);
return res.send(`Unrecognized external-select action "${actionId}"`);
}
const result = await githubClient.findRepository({name: value});
const options = result.search.edges.map(item => {
return {
text: {
type: 'plain_text',
text: item.node.nameWithOwner,
},
value: item.node.id,
}
});
res.status(200);
return res.send({options});
} catch (error) {
console.error(error);
res.status(500);
return res.send('An error occurred, try again later')
}
});
app.post('/slash-command', async (req, res) => {
try {
const { isValid, reason } = validateRequest(req, config.slackSigningSecret);
if (!isValid) {
console.log(reason);
res.status(403);
return res.send();
}
const { body } = req;
const payload = {
trigger_id: body.trigger_id,
channel: {
name: body.channel_name,
id: body.channel_id,
},
team: {
domain: body.team_domain,
},
message: {
user: body.user_id,
},
title: {
text: body.text,
},
user: {
username: body.user_name,
}
};
await createModalAction(payload, config.slackApiToken, config.channelMap);
res.status(200);
return res.send();
} catch (error) {
console.error(error);
res.status(500);
return res.send('An error occurred, try again later')
}
});
return app;
}
module.exports = createServer;