forked from adrey/slack-file-upload-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (59 loc) · 2.03 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
const core = require('@actions/core');
const github = require('@actions/github');
var http = require('http');
var FormData = require('form-data');
var fs = require('fs');
function finishWithError(message) {
console.log(message);
core.setFailed(message);
}
function finish(result) {
core.setOutput("result", result);
}
function submit(form) {
return new Promise((resolve, reject) => {
var rawData = "";
form.submit("https://slack.com/api/files.upload", (err, res) => {
if(err) {
reject(err);
} else {
res.on("data", (chunk) => { rawData += chunk;});
res.on("end", () => {
var data = JSON.parse(rawData);
if(data.ok) {
finish(rawData);
resolve(data);
} else {
finishWithError(data.error);
reject(data.error);
}
});
}
});
});
}
async function run() {
try {
const token = core.getInput('token');
const path = core.getInput('path');
const channel = core.getInput('channel');
const filename = core.getInput('filename');
const filetype = core.getInput('filetype');
const initial_comment = core.getInput('initial_comment');
const thread_ts = core.getInput('thread_ts');
const title = core.getInput('title');
var form = new FormData();
form.append('token', token);
form.append('file', fs.createReadStream(path));
if(filename) form.append('filename', filename);
if(channel) form.append('channels', channel);
if(filetype) form.append('filetype', filetype);
if(initial_comment) form.append('initial_comment', initial_comment);
if(thread_ts) form.append('thread_ts', thread_ts);
if(title) form.append('title', title);
await submit(form);
} catch (error) {
finishWithError(error);
}
}
run()