Skip to content

Commit

Permalink
Support reading the subject from a file. (#173)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
benjyw authored Aug 8, 2023
1 parent 6d8218d commit 3c0bbc5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
<p>Paragraph</p>
</body>
</html>
- subject: Plain body (Markdown)
- subject: file://testdata/subject.txt
convert_markdown: true
body: file://README.md
- subject: HTML body (Markdown)
Expand Down
22 changes: 11 additions & 11 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ const fs = require("fs")
const showdown = require("showdown")
const path = require("path")

function getBody(bodyOrFile, convertMarkdown) {
let body = bodyOrFile
function getText(textOrFile, convertMarkdown) {
let text = textOrFile

// Read body from file
if (bodyOrFile.startsWith("file://")) {
const file = bodyOrFile.replace("file://", "")
body = fs.readFileSync(file, "utf8")
// 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()
body = converter.makeHtml(body)
text = converter.makeHtml(text)
}

return body
return text
}

function getFrom(from, username) {
Expand Down Expand Up @@ -116,14 +116,14 @@ async function main() {
const info = await transport.sendMail({
from: getFrom(from, username),
to: to,
subject: subject,
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 ? getBody(body, false) : undefined,
html: htmlBody ? getBody(htmlBody, convertMarkdown) : undefined,
text: body ? getText(body, false) : undefined,
html: htmlBody ? getText(htmlBody, convertMarkdown) : undefined,
priority: priority ? priority : undefined,
attachments: attachments ? (await getAttachments(attachments)) : undefined,
})
Expand Down
1 change: 1 addition & 0 deletions testdata/subject.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Plain body (Markdown)

0 comments on commit 3c0bbc5

Please sign in to comment.