-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
116 lines (96 loc) · 3.59 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
const { WebClient } = require('@slack/web-api');
const github = require('@actions/github');
const core = require('@actions/core');
const POST_ACTION = 'post'
const UPDATE_ACTION = 'update'
const REPLY_ACTION = 'reply'
const REACT_ACTION = 'react'
const post = async () => {
// eslint-disable-next-line no-unused-vars
const {payload} = github.context
const channelId = core.getInput('channel-id');
const botToken = core.getInput('slack-bot-token');
// eslint-disable-next-line no-eval
const messages = eval(core.getInput('message'));
const client = new WebClient(botToken);
await client.chat.postMessage({ channel: channelId, text: messages })
}
const update = async () => {
// eslint-disable-next-line no-unused-vars
const {payload} = github.context
const channelId = core.getInput('channel-id');
const botToken = core.getInput('slack-bot-token');
// eslint-disable-next-line no-eval
const stringMatcher = eval(core.getInput('string-matcher'));
// eslint-disable-next-line no-eval
const messages = eval(core.getInput('message'));
const client = new WebClient(botToken);
const conversations = await client.conversations.history({ token: botToken, channel: channelId })
const message = conversations.messages.find((m)=> m.text.includes(stringMatcher))
if (message !== undefined){
await client.chat.update({ token: botToken, channel: channelId , ts: message.ts, text: messages})
} else {
await client.chat.postMessage({ channel: channelId, text: messages })
}
}
const reply = async () => {
// eslint-disable-next-line no-unused-vars
const {payload} = github.context
const channelId = core.getInput('channel-id');
const botToken = core.getInput('slack-bot-token');
// eslint-disable-next-line no-eval
const stringMatcher = eval(core.getInput('string-matcher'));
// eslint-disable-next-line no-eval
const messages = eval(core.getInput('message'));
const client = new WebClient(botToken);
const conversations = await client.conversations.history({ token: botToken, channel: channelId })
const message = conversations.messages.find((m)=> m.text.includes(stringMatcher))
if (message !== undefined){
await client.chat.postMessage({ token: botToken, channel: channelId , thread_ts: message.ts, text: messages})
} else {
core.setFailed('Message could not be found');
}
}
const react = async () => {
// eslint-disable-next-line no-unused-vars
const {payload} = github.context
const channelId = core.getInput('channel-id');
const botToken = core.getInput('slack-bot-token');
// eslint-disable-next-line no-eval
const stringMatcher = eval(core.getInput('string-matcher'));
// eslint-disable-next-line no-eval
const messages = core.getInput('message');
const client = new WebClient(botToken);
const conversations = await client.conversations.history({ token: botToken, channel: channelId })
const message = conversations.messages.find((m)=> m.text.includes(stringMatcher))
if (message !== undefined){
await client.reactions.add({ token: botToken, channel: channelId, name: messages })
} else {
core.setFailed('Message could not be found');
}
}
async function run() {
const action = core.getInput('action');
try {
switch(action) {
case POST_ACTION:
await post()
break;
case UPDATE_ACTION:
await update()
break;
case REPLY_ACTION:
await reply()
break;
case REACT_ACTION:
await react()
break;
default:
core.setFailed('Action does not exist');
break;
}
} catch (error){
core.setFailed(error.message);
}
}
run();