Skip to content

Commit 3c0bbc5

Browse files
authored
Support reading the subject from a file. (#173)
Allows dynamic subjects to be generated at action runtime (e.g., "Foobar version 1.2.3 is released", where 1.2.3 is not known at action config time). Leverages the existing file:// support for body, but renames getBody to getText, in keeping with this expanded role.
1 parent 6d8218d commit 3c0bbc5

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
<p>Paragraph</p>
3131
</body>
3232
</html>
33-
- subject: Plain body (Markdown)
33+
- subject: file://testdata/subject.txt
3434
convert_markdown: true
3535
body: file://README.md
3636
- subject: HTML body (Markdown)

main.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ const fs = require("fs")
55
const showdown = require("showdown")
66
const path = require("path")
77

8-
function getBody(bodyOrFile, convertMarkdown) {
9-
let body = bodyOrFile
8+
function getText(textOrFile, convertMarkdown) {
9+
let text = textOrFile
1010

11-
// Read body from file
12-
if (bodyOrFile.startsWith("file://")) {
13-
const file = bodyOrFile.replace("file://", "")
14-
body = fs.readFileSync(file, "utf8")
11+
// Read text from file
12+
if (textOrFile.startsWith("file://")) {
13+
const file = textOrFile.replace("file://", "")
14+
text = fs.readFileSync(file, "utf8")
1515
}
1616

1717
// Convert Markdown to HTML
1818
if (convertMarkdown) {
1919
const converter = new showdown.Converter()
20-
body = converter.makeHtml(body)
20+
text = converter.makeHtml(text)
2121
}
2222

23-
return body
23+
return text
2424
}
2525

2626
function getFrom(from, username) {
@@ -116,14 +116,14 @@ async function main() {
116116
const info = await transport.sendMail({
117117
from: getFrom(from, username),
118118
to: to,
119-
subject: subject,
119+
subject: getText(subject, false),
120120
cc: cc ? cc : undefined,
121121
bcc: bcc ? bcc : undefined,
122122
replyTo: replyTo ? replyTo : undefined,
123123
inReplyTo: inReplyTo ? inReplyTo : undefined,
124124
references: inReplyTo ? inReplyTo : undefined,
125-
text: body ? getBody(body, false) : undefined,
126-
html: htmlBody ? getBody(htmlBody, convertMarkdown) : undefined,
125+
text: body ? getText(body, false) : undefined,
126+
html: htmlBody ? getText(htmlBody, convertMarkdown) : undefined,
127127
priority: priority ? priority : undefined,
128128
attachments: attachments ? (await getAttachments(attachments)) : undefined,
129129
})

testdata/subject.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Plain body (Markdown)

0 commit comments

Comments
 (0)