generated from actions/container-action
-
Notifications
You must be signed in to change notification settings - Fork 188
/
main.js
169 lines (152 loc) · 5.87 KB
/
main.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const nodemailer = require("nodemailer")
const core = require("@actions/core")
const glob = require("@actions/glob")
const fs = require("fs")
const showdown = require("showdown")
const path = require("path")
function getText(textOrFile, convertMarkdown) {
let text = textOrFile
// Read text from file
if (textOrFile.startsWith("file://")) {
const file = textOrFile.replace("file://", "")
text = fs.readFileSync(file, "utf8")
}
// Convert Markdown to HTML
if (convertMarkdown) {
const converter = new showdown.Converter()
text = converter.makeHtml(text)
}
return text
}
function getFrom(from, username) {
if (from.match(/.+ <.+@.+>/)) {
return from
}
return `"${from}" <${username}>`
}
async function getAttachments(attachments) {
const globber = await glob.create(attachments.split(',').join('\n'))
const files = await globber.glob()
return files.map(f => ({ filename: path.basename(f), path: f, cid: f.replace(/^.*[\\\/]/, '')}))
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function main() {
try {
let serverAddress = core.getInput("server_address")
let serverPort = core.getInput("server_port")
let secure = core.getInput("secure")
let username = core.getInput("username")
let password = core.getInput("password")
if (!secure) {
secure = serverPort === "465" ? "true" : "false"
}
const connectionUrl = core.getInput("connection_url")
if (connectionUrl) {
const url = new URL(connectionUrl)
switch (url.protocol) {
default:
throw new Error(`Unsupported connection protocol '${url.protocol}'`)
case "smtp:":
serverPort = "25"
secure = "false"
break
case "smtp+starttls:":
serverPort = "465"
secure = "true"
break
}
if (url.hostname) {
serverAddress = url.hostname
}
if (url.port) {
serverPort = url.port
}
if (url.username) {
username = unescape(url.username)
}
if (url.password) {
password = unescape(url.password)
}
}
const subject = core.getInput("subject", { required: true })
const from = core.getInput("from", { required: true })
const to = core.getInput("to", { required: false })
const body = core.getInput("body", { required: false })
const htmlBody = core.getInput("html_body", { required: false })
const cc = core.getInput("cc", { required: false })
const bcc = core.getInput("bcc", { required: false })
const replyTo = core.getInput("reply_to", { required: false })
const inReplyTo = core.getInput("in_reply_to", { required: false })
const attachments = core.getInput("attachments", { required: false })
const convertMarkdown = core.getInput("convert_markdown", { required: false })
const ignoreCert = core.getInput("ignore_cert", { required: false })
const priority = core.getInput("priority", { required: false })
const nodemailerlog = core.getInput("nodemailerlog", { required: false })
const nodemailerdebug = core.getInput("nodemailerdebug", { required: false })
// if neither to, cc or bcc is provided, throw error
if (!to && !cc && !bcc) {
throw new Error("At least one of 'to', 'cc' or 'bcc' must be specified")
}
if (!serverAddress) {
throw new Error("Server address must be specified")
}
const transport = nodemailer.createTransport({
host: serverAddress,
auth: username && password ? {
user: username,
pass: password
} : undefined,
port: serverPort,
secure: secure === "true",
tls: ignoreCert == "true" ? {
rejectUnauthorized: false
} : undefined,
logger: nodemailerdebug == "true" ? true : nodemailerlog,
debug: nodemailerdebug,
})
var i = 1;
while (true) {
try {
const info = await transport.sendMail({
from: getFrom(from, username),
to: to,
subject: getText(subject, false),
cc: cc ? cc : undefined,
bcc: bcc ? bcc : undefined,
replyTo: replyTo ? replyTo : undefined,
inReplyTo: inReplyTo ? inReplyTo : undefined,
references: inReplyTo ? inReplyTo : undefined,
text: body ? getText(body, false) : undefined,
html: htmlBody ? getText(htmlBody, convertMarkdown) : undefined,
priority: priority ? priority : undefined,
attachments: attachments ? (await getAttachments(attachments)) : undefined,
});
break;
} catch (error) {
if (!error.message.includes("Try again later,")) {
core.setFailed(error.message)
break;
}
if (i > 10) {
core.setFailed(error.message)
break;
}
console.log("Received: " + error.message);
if (i < 2) {
console.log("Trying again in a minute...");
} else {
console.log("Trying again in " + i + " minutes...");
}
await sleep(i * 60000);
i++;
}
}
} catch (error) {
core.setFailed(error.message)
}
}
main()