-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
61 lines (47 loc) · 1.42 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
const core = require('@actions/core');
const { WebClient } = require('@slack/web-api');
const get = require('lodash/get');
const setWith = require('lodash/setWith');
const token = process.env.SLACK_BOT_TOKEN;
const web = new WebClient(token);
async function getUserData() {
try {
const email = core.getInput('email') || '';
if (email.length <= 0) {
throw new Error('The email value is required to run this action.');
}
const response = await web.users.lookupByEmail({ email });
if (!response.ok) {
throw new Error('The Slack API thrown an error.');
}
let fields = core.getInput('fields') || '';
if (fields.length <= 0) {
throw new Error('The fields value is required to run this action.');
}
let rawUserOutput = {};
fields = fields.split(',');
fields.map((field, index) => setWith(
rawUserOutput,
fields[index],
get(response.user, fields[index], 'invalid_field')
));
Object.entries(rawUserOutput).map((user) => {
let outputKey = user[0];
let outputValue = user[1];
if (typeof user[1] !== 'object') {
core.setOutput(outputKey, outputValue);
return;
}
for (let nestedObjKey in user[1]) {
outputKey = `${user[0]}_${nestedObjKey}`;
outputValue = user[1][nestedObjKey];
core.setOutput(outputKey, outputValue);
}
});
core.info('Success');
core.setOutput('user', rawUserOutput);
} catch (error) {
core.setFailed(error.message);
}
}
getUserData();