Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Email body

### `attachment`

Filename of the attachment
Filename of the attachment or directory

## Outputs

Expand All @@ -40,5 +40,5 @@ with:
to: 'example@example.com'
subject: 'building main'
body: 'This is a notification from GitHub actions.'
attachment: 'artifacts.zip'
attachment: 'artifacts.zip' # 'somedir/somesubdir'
```
49 changes: 42 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const core = require('@actions/core');
const mime = require('mime');
const { email } = require('@cinotify/js');
const {readFileSync} = require('fs');
const { readFileSync, lstatSync, readdirSync, statSync } = require('fs');
const { join, relative, basename } = require('path');

try {
const to = core.getInput('to');
Expand All @@ -11,13 +12,28 @@ try {

const attachmentPath = core.getInput('attachment');
if (attachmentPath) {
const file = readFileSync(attachmentPath);
const attachment = {
filename: attachmentPath,
type: mime.getType(attachmentPath),
content: file.toString('base64')
const isDirectory = lstatSync(attachmentPath).isDirectory();
if (isDirectory) {
const files = getFilesFromDirectory(attachmentPath);

files.forEach(filePath => {
const fileContent = readFileSync(filePath);
const attachment = {
filename: basename(relative(attachmentPath, filePath)),
type: mime.getType(filePath),
content: fileContent.toString('base64')
}
payload.attachments.push(attachment);
});
} else {
const file = readFileSync(attachmentPath);
const attachment = {
filename: basename(attachmentPath),
type: mime.getType(attachmentPath),
content: file.toString('base64')
}
payload.attachments = [attachment];
}
payload.attachments = [attachment];
}

email(payload).then(() => {
Expand All @@ -28,3 +44,22 @@ try {
} catch (error) {
core.setFailed(error.message);
}


function getFilesFromDirectory(dir) {
const files = readdirSync(dir);
let fileList = [];

files.forEach(file => {
const filePath = join(dir, file);
const stats = statSync(filePath);

if (stats.isDirectory()) {
fileList = fileList.concat(getFilesFromDirectory(filePath));
} else {
fileList.push(filePath);
}
});

return fileList;
}
56 changes: 38 additions & 18 deletions node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions node_modules/@actions/core/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions node_modules/@actions/core/lib/core.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion node_modules/@actions/core/lib/oidc-utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion node_modules/@actions/core/lib/oidc-utils.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions node_modules/@actions/core/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion node_modules/@actions/http-client/lib/auth.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions node_modules/@actions/http-client/lib/index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading