forked from vineetchoudhary/mailgun-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (81 loc) · 2.89 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
const core = require('@actions/core');
const fs = require('fs');
const _ = require('lodash');
async function run() {
try {
//fixed inputs
const apiKey = core.getInput('api-key');
const domain = core.getInput('domain');
const to = core.getInput('to');
if (apiKey === undefined || apiKey == '') {
throw new Error('Undefined Mailgun API key. Please add "api-key" input in your workflow file.');
}
if (domain === undefined || domain == '') {
throw new Error('Undefined domain. Please add "domain" input in your workflow file.');
}
if (to === undefined || to == '') {
throw new Error('Undefined email address of the recipient(s). Please add "to" input in your workflow file.')
}
//from
var from = core.getInput('from');
if (from === undefined || from == '') {
from = 'hello@' + domain;
}
//Get process env var
const {
GITHUB_EVENT_PATH,
GITHUB_ACTOR,
GITHUB_EVENT_NAME,
GITHUB_REPOSITORY
} = process.env;
const EVENT_PAYLOAD = JSON.parse(fs.readFileSync(GITHUB_EVENT_PATH, "utf8"));
const DEFAULT_MESSAGE = `@${GITHUB_ACTOR} (${GITHUB_EVENT_NAME}) at ${GITHUB_REPOSITORY}`;
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
const ReplaceMustaches = data => _.template(data)({ ...process.env, EVENT_PAYLOAD })
//subject
var subject = core.getInput('subject');
if (subject === undefined || subject == '') {
subject = DEFAULT_MESSAGE;
} else {
subject = ReplaceMustaches(subject);
}
//body
var body = core.getInput('body');
if (body === undefined || body == '') {
body = DEFAULT_MESSAGE;
} else {
body = ReplaceMustaches(body);
}
var mailgun = require('mailgun-js')({ apiKey: apiKey, domain: domain });
var MailComposer = require('nodemailer/lib/mail-composer');
var data = {
from: from,
to: to,
subject: subject,
html: body
};
var mail = new MailComposer(data);
mail.compile().build((err, message) => {
if (err) {
core.setFailed(err);
return;
}
var dataToSend = {
to: to,
message: message.toString('ascii')
};
mailgun.messages().sendMime(dataToSend, (sendError, body) => {
if (sendError) {
core.setFailed(sendError);
return;
} else {
core.setOutput('response', body.message);
return;
}
});
});
} catch (error) {
core.setFailed(error.message);
}
}
run()