Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

feat: build trigger handler function #47

Merged
merged 7 commits into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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: 3 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ POSTMARK_API_KEY=
POSTMARK_STORE_OWNER_EMAIL=
POSTMARK_STORE_OWNER_NEW_ORDER_TEMPLATE_ID=
POSTMARK_NEW_ORDER_TEMPLATE_ID=
POSTMARK_ORDER_DISPATCHED_TEMPLATE_ID=
POSTMARK_ORDER_DISPATCHED_TEMPLATE_ID=
BUILD_HOOK=
BUILD_STAGE_TRIGGER=
72 changes: 72 additions & 0 deletions functions/deploy-site.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require('dotenv').config();

require('es6-promise').polyfill();
require('isomorphic-fetch');

const BUILD_HOOK = process.env.BUILD_HOOK;
const BUILD_STAGE_TRIGGER = process.env.STAGE_TRIGGER;

const BUILD_TRIGGERS = [
'createProduct',
'updateProduct',
'deleteProduct',
'createCategory',
'updateCategory',
'deleteCategory',
'createCollection',
'updateCollection',
'deleteCollection',
];

exports.handler = async event => {
if (!event.body)
return {
statusCode: 405,
message: 'You must provide a payload.',
};

const {
stageName,
info: { fieldName },
} = JSON.parse(event.body);

if (!req.body)
Copy link
Contributor

@ynnoj ynnoj Dec 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is req.body defined? Left over copy/pasta?

return {
statusCode: 422,
body: JSON.stringify({
message: 'No action required.',
}),
};

try {
if (
stageName === BUILD_STAGE_TRIGGER &&
BUILD_TRIGGERS.includes(fieldName)
) {
await fetch(BUILD_HOOK, {
method: 'post',
});

return {
statusCode: 200,
body: JSON.stringify({
message: 'Build successfully triggered.',
}),
};
}

return {
statusCode: 200,
body: JSON.stringify({
message: 'No build required.',
}),
};
} catch (err) {
console.log(err);

return {
statusCode: 500,
body: JSON.stringify(err),
};
}
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"apollo-server-lambda": "2.9.12",
"classnames": "^2.2.6",
"currency-formatter": "1.5.5",
"es6-promise": "4.2.8",
"gatsby": "2.17.0",
"gatsby-image": "^2.2.33",
"gatsby-plugin-postcss": "2.1.13",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4780,6 +4780,11 @@ es-to-primitive@^1.2.0:
is-date-object "^1.0.1"
is-symbol "^1.0.2"

es6-promise@4.2.8:
version "4.2.8"
resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.2.8.tgz#4eb21594c972bc40553d276e510539143db53e0a"
integrity sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w==

es6-promisify@^6.0.0:
version "6.0.2"
resolved "https://registry.yarnpkg.com/es6-promisify/-/es6-promisify-6.0.2.tgz#525c23725b8510f5f1f2feb5a1fbad93a93e29b4"
Expand Down