This repository was archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (50 loc) · 1.65 KB
/
Copy pathindex.js
File metadata and controls
60 lines (50 loc) · 1.65 KB
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
const core = require('@actions/core');
const axios = require('axios');
const { getEmbedsFromBody } = require('./parser.js');
require('dotenv').config();
const { GITHUB_EVENT_NAME, GITHUB_EVENT_PATH } = process.env;
function sendWebhook(event, webhook, embeds) {
if (embeds.length == 0)
return true; // no embeds to send
axios({
method: 'post',
url: webhook,
data: {
avatar_url: event.organization.avatar_url,
username: event.organization.login,
embeds,
}
}).catch(error => {
core.setFailed(error.message);
}).then(res => {
core.info('Command successfully ran!');
});
}
async function run() {
const publishWebhook = core.getInput('discordPublishWebhook');
const previewWebhook = core.getInput('discordPreviewWebhook');
const authorisedUsers = core.getInput('authorisedUsers').split(',');
if (GITHUB_EVENT_NAME != 'issue_comment')
return;
let event;
try {
event = require(GITHUB_EVENT_PATH);
} catch (error) {
return core.setFailed('Invalid event file: \'' + GITHUB_EVENT_PATH + '\'');
}
if (typeof event.pull_request !== 'undefined')
return core.setFailed('Not a PR');
if (!authorisedUsers.includes(event.comment.user.login))
return core.setFailed('Unauthorised!');
if (event.comment.body.startsWith(';preview'))
sendWebhook(event, previewWebhook, getEmbedsFromBody(event.issue.body));
else if (event.comment.body.startsWith(';publish'))
sendWebhook(event, publishWebhook, getEmbedsFromBody(event.issue.body));
else {
core.setFailed('Not a command');
}
}
run().catch(error => {
core.setFailed('Event failed: ' + error.message);
console.error(error);
});