Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
15a7497
wip: email
caspiano Jul 2, 2018
12e59d3
merge uplift branch
caspiano Jul 2, 2018
6ac5158
email container v0.0.1
caspiano Jul 2, 2018
f1d71c4
manifest
caspiano Jul 2, 2018
ec15565
glob attachments relative from root
caspiano Jul 2, 2018
ae39589
rename packages to containers
caspiano Jul 2, 2018
2f51ca0
wip: email and slack
caspiano Jul 2, 2018
da9a3a1
untracked files
caspiano Jul 2, 2018
2d03943
merge uplift
caspiano Jul 3, 2018
85afe14
email and slack drafted, now on to tests
caspiano Jul 3, 2018
09e4289
add tests for slack, minor changes to send(Email|Slack)
caspiano Jul 4, 2018
24de696
slack and email tests, currently erroring
caspiano Jul 4, 2018
fd4ac49
fix jest network error by setting nock CORS
caspiano Jul 4, 2018
de77c6b
slack tests pass, email tests fail due to mocking problems, fixed thr…
caspiano Jul 5, 2018
fd3446d
lint
sabrehagen Jul 10, 2018
4561f96
use common tsconfig
sabrehagen Jul 10, 2018
4615f7c
clean issues and add logger
sabrehagen Jul 10, 2018
ddaa401
good tsconfig and linting
sabrehagen Jul 10, 2018
4570df6
change to async
sabrehagen Jul 10, 2018
36801fd
promise to async
sabrehagen Jul 10, 2018
7a0b329
wip
caspiano Jul 10, 2018
a82bfd6
Merge branch 'email-and-slack' into merge-progress
sabrehagen Jul 10, 2018
00ed7dd
style
sabrehagen Jul 10, 2018
3af4fcc
fix jest logging, tests with mock files passing, style cleanup
sabrehagen Jul 11, 2018
f0e8c9c
dont export unused exportsss
sabrehagen Jul 11, 2018
66af4cd
one more test passing
sabrehagen Jul 11, 2018
e7fcf64
mock stat function
sabrehagen Jul 11, 2018
87b10e6
lock
sabrehagen Jul 11, 2018
34ba3ab
add build watch
sabrehagen Jul 12, 2018
472fdc0
add manifests
sabrehagen Jul 13, 2018
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules
yarn-error.log
build
dist
.coverage
/lerna-*
Binary file added .vs/slnx.sqlite
Binary file not shown.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
11 changes: 11 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,14 @@ The [`uid` and `gid`](http://www.linfo.org/uid.html) of the user the container i
### File Patterns

All parameters used for selecting files (e.g. [`attachments`](email/#attachments), `inputFiles`, `files`) are processed by [micromatch](https://github.com/micromatch/micromatch) which provides wildcard support for selecting multiple files e.g. `gear/*.stl`.

## Container Manifest

A container manifest describes a set of metadata for a container.

```yaml
image: your-image-title
author: Author S. Name
version: 1.0
schema: https://www.link-to-json-schema.com/schema.json
```
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ Uploads pipeline outputs to Stemn.

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md)
See [CONTRIBUTING.md](CONTRIBUTING.md)
6 changes: 6 additions & 0 deletions containers/cadquery/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"author": "stemn",
"name": "cadquery",
"version": "v1.0.0",
"schema": {}
}
7 changes: 5 additions & 2 deletions containers/cadquery/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
},
"homepage": "https://github.com/stemn/stemn-pipeline-containers#readme",
"devDependencies": {
"@types/nock": "^9.3.0",
"mock-fs": "^4.5.0",
"nock": "^9.4.1",
"prettier": "^1.13.7",
"version-bump-prompt": "^4.0.0"
},
"dependencies": {}
}
}
6 changes: 6 additions & 0 deletions containers/email/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"author": "stemn",
"name": "email",
"version": "v1.0.0",
"schema": {}
}
4 changes: 4 additions & 0 deletions containers/email/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
author: Caspian Baska
title: @stemn/email
version: 0.0.1
param-schema: https://raw.githubusercontent.com/stemn/stemn-pipeline-containers/master/packages/email/schema.json
42 changes: 31 additions & 11 deletions containers/email/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
"name": "@stemn/email",
"version": "1.0.0",
"description": "A container for emailing files and results of a Stemn pipeline.",
"main": "dist",
"scripts": {
"test": "jest",
"test:coverage": "yarn test --coverage",
"lint": "tslint --project tsconfig.json",
"build": "yarn build:project && yarn build:container",
"build": "yarn build:typescript && yarn build:container",
"build:container": "docker build -t stemn/email .",
"build:project": "tsc",
"build:typescript": "tsc",
"build:watch": "tsc --watch",
"release": "bump --tag --push",
"changelog:verify": "changelog-verify CHANGELOG.md",
"changelog:version": "version-changelog CHANGELOG.md && git add CHANGELOG.md",
Expand All @@ -20,20 +22,38 @@
"type": "git",
"url": "git+https://github.com/stemn/stemn-pipeline-containers.git"
},
"author": "Jackson Delahunt",
"author": "Caspian Baska",
"license": "MIT",
"bugs": {
"url": "https://github.com/stemn/stemn-pipeline-containers/issues"
},
"homepage": "https://github.com/stemn/stemn-pipeline-containers#readme",
"dependencies": {
"request": "^2.83.0",
"request-promise": "^4.2.2"
"@stemn/pipeline-logger": "^0.0.2",
"@types/axios": "^0.14.0",
"@types/micromatch": "^3.1.0",
"axios": "^0.18.0",
"bluebird": "^3.5.1",
"fs-extra": "^6.0.1",
"graceful-fs": "^4.1.11",
"klaw": "^2.1.1",
"klaw-sync": "^4.0.0",
"markdown-it": "^8.4.1",
"micromatch": "^3.1.10",
"request": "^2.83.0"
},
"devDependencies": {
"@types/node": "^9.4.7",
"@types/bluebird": "^3.5.21",
"@types/klaw": "^2.1.1",
"@types/klaw-sync": "^2.0.0",
"@types/markdown-it": "^0.0.4",
"@types/mock-fs": "^3.6.30",
"@types/nock": "^9.3.0",
"@types/node": "^10.5.1",
"graceful-fs": "^4.1.11",
"jest": "^23.2.0",
"mock-fs": "^4.5.0",
"nock": "^9.4.1",
"nodemon": "^1.14.12",
"typescript": "^2.7.2",
"prettier": "^1.13.7",
"ts-jest": "^23.0.0",
"typescript": "^2.9.2",
"version-bump-prompt": "^4.0.0"
}
}
25 changes: 25 additions & 0 deletions containers/email/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "http://json-schema.org/draft-06/schema#",
"properties": {
"attachments": {
"items": {
"type": "string"
},
"type": "array"
},
"body": {
"type": "markdown"
},
"subject": {
"type": "string"
},
"to": {
"items": {
"type": "string"
},
"type": "array"
}
},
"type": "object"
}

4 changes: 2 additions & 2 deletions containers/email/scripts/sendEmail.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { sendEmail } from "../src";
import { sendEmail } from '../src';

void sendEmail()
void sendEmail();
140 changes: 136 additions & 4 deletions containers/email/src/__tests__/email.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,137 @@
import { sendEmail } from "../index";
const mockFs = require('mock-fs');
import * as fs from 'fs-extra';
import * as Markdown from 'markdown-it';
import * as nock from 'nock';

it('works', () => {
expect(sendEmail).toBeInstanceOf(Function)
})
const { sendEmail } = require('../sendEmail');

// Mocked file objects
const mockedFiles: { [key: string]: string | Buffer } = {
'/pipeline/test.txt': 'foo bar baz',
'/pipeline/girtbysea.txt': 'bar',
'/pipeline/images/a.png': Buffer.from([8, 6, 7, 5, 3, 0, 9]),
};

jest.mock('fs-extra');
fs.stat.mockImplementation((filename: string) => mockedFiles[filename].length);

const markdown = Markdown({
html: true,
linkify: true,
typographer: true,
});

const emails = ['[email protected]'];
const emailContent = '# hey\n\n from the stemn pipeline';
const sendGridAuth = 'D3ADB33F';

beforeAll(() => {
Object.assign(process.env, {
STEMN_PIPELINE_PARAMS_TO: JSON.stringify(emails),
STEMN_PIPELINE_PARAMS_BODY: emailContent,
STEMN_SENDGRID_AUTH: sendGridAuth,
});

nock.disableNetConnect();
});

afterAll(() => {
nock.enableNetConnect();
});

beforeEach(() => {
mockFs(mockedFiles);
});

afterEach(() => {
mockFs.restore();
});

describe('sending an email with no attachments', () => {
let requestBody: any;
let response: any;

beforeAll(async () => {
nock('https://api.sendgrid.com')
.defaultReplyHeaders({ 'access-control-allow-origin': '*' })
.post('/v3/mail/send')
.reply(200, (uri: string, body: string) => {
requestBody = JSON.parse(body);
});

response = await sendEmail();
});

it('succeeds with 200', () => expect(response.status).toBe(200));
it('requests with correct auth header', () => expect(requestBody.headers.Authorization).toBe(`Bearer ${ sendGridAuth }`));
it('sends an email with no attachments', () => expect(requestBody.data.attachments).toEqual([]));
it('correctly renders markdown', () => {

const expected = [{
type: 'text/html',
value: markdown.render(emailContent),
}, {
type: 'text/plaintext',
value: emailContent,
}];

expect(requestBody.data.content).toEqual(expected);
});
});

describe('sending email with attachment', async () => {
let requestBody: any;

beforeAll(async () => {
Object.assign(process.env, {
STEMN_PIPELINE_PARAMS_ATTACHMENTS: JSON.stringify(['*.png']),
});

nock('https://api.sendgrid.com')
.defaultReplyHeaders({ 'access-control-allow-origin': '*' })
.post('/v3/mail/send')
.reply(200, (uri: string, body: string) => {
requestBody = JSON.parse(body);
});

return sendEmail();
});

afterAll(() => {
delete process.env.STEMN_PIPELINE_PARAMS_ATTACHMENTS;
});

it('correctly encodes attachments', () => {
const convertToExpected = (obj: any, { filename, content }: { filename: string; content: string }) => {
const env: any = {};
env[filename] = Buffer.from(content, 'base64').toString('ascii');
return { ...obj, ...env };
};

const attachments = requestBody.data.attachments.reduce(convertToExpected, {});
return expect(attachments).toMatchObject(mockedFiles);
});
});

describe('sending email with attachments that exceed limit', () => {
beforeAll(() => {
Object.assign(process.env, {
STEMN_PIPELINE_PARAMS_ATTACHMENTS: JSON.stringify(['*.txt']),
STEMN_MAX_ATTACHMENTS: 0,
});

nock('https://api.sendgrid.com')
.defaultReplyHeaders({ 'access-control-allow-origin': '*' })
.post('/v3/mail/send')
.reply(200);
});

afterAll(() => {
delete process.env.STEMN_PIPELINE_PARAMS_ATTACHMENTS;
delete process.env.STEMN_MAX_ATTACHMENTS;
});

it('fails when attachment limit exceeded', () => {
return expect(sendEmail).toThrow(/Attachment limit exceeded/);
});
});
22 changes: 1 addition & 21 deletions containers/email/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1 @@
const {
STEMN_PIPELINE_ID,
STEMN_PIPELINE_PARAMS_TO,
STEMN_PIPELINE_PARAMS_SUBJECT,
STEMN_PIPELINE_PARAMS_BODY,
STEMN_PIPELINE_PARAMS_ATTACHMENTS,
// STEMN_PIPELINE_ROOT,
// STEMN_PIPELINE_TMP,
STEMN_PIPELINE_TOKEN,
} = process.env;

export function sendEmail () {
console.log({ // tslint:disable-line
STEMN_PIPELINE_ID,
STEMN_PIPELINE_PARAMS_TO,
STEMN_PIPELINE_PARAMS_SUBJECT,
STEMN_PIPELINE_PARAMS_BODY,
STEMN_PIPELINE_PARAMS_ATTACHMENTS,
STEMN_PIPELINE_TOKEN,
});
};
export { sendEmail } from './sendEmail';
Loading