Skip to content
Draft
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
8 changes: 4 additions & 4 deletions apps/netlify/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
Netlify App
===
# Netlify App

## Local Development

The Netlify App is a React app (in `src`) plus two Contentful Functions (in `functions`) which handle
Netlify build hooks:

* `handleNetlifyBuild`: triggered by the `Netlify Build` app action, fires the build hook of one site.
* `appEventHandler`: fires the configured build hooks when Entries or Assets are published/unpublished.
- `handleNetlifyBuild`: triggered by the `Netlify Build` app action, fires the build hook of one site.
- `appEventHandler`: fires the configured build hooks when Entries or Assets are published/unpublished.

### Launching the app

Run:

```
npm run start
```
Expand Down
26 changes: 15 additions & 11 deletions apps/netlify/functions/handleNetlifyBuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ import { fireBuildHook, getBuildHookIdFromSiteName, validateParams } from './lib
*
* @param {Object} event - Contains the name of the site hosted in netlify.
* @param {Object} context - Provides access to the CMA client and other context information
* @returns {Object} The response from your App Action
* @returns {Object} `{ success: true }`, or `{ success: false, error }` when the build could not be fired
*/
export const handler = async (event, context) => {
validateParams(context.appInstallationParameters);
try {
validateParams(context.appInstallationParameters);

const buildHookId = getBuildHookIdFromSiteName(
event.body?.siteName,
context.appInstallationParameters
);
const buildHookId = getBuildHookIdFromSiteName(
event.body?.siteName,
context.appInstallationParameters
);

if (!buildHookId) {
throw new Error('Cannot find build hook');
}
if (!buildHookId) {
throw new Error('Cannot find build hook');
}

await fireBuildHook(buildHookId);
return { success: true };
await fireBuildHook(buildHookId);
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
};
19 changes: 11 additions & 8 deletions apps/netlify/functions/handlers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ describe('functions', () => {
expect(result).toEqual({ success: true });
});

it('fails when the site has no build hook', async () => {
await expect(buildHookHandler({ body: { siteName: 'unknown' } }, context)).rejects.toThrow(
'Cannot find build hook'
);
it('returns the error when the site has no build hook', async () => {
const result = await buildHookHandler({ body: { siteName: 'unknown' } }, context);

expect(result).toEqual({ success: false, error: 'Cannot find build hook' });
expect(fetch).not.toHaveBeenCalled();
});

it('fails when Netlify rejects the build hook', async () => {
it('returns the error when Netlify rejects the build hook', async () => {
fetch.mockResolvedValue({ ok: false, status: 404 });

await expect(buildHookHandler({ body: { siteName: 'my-site' } }, context)).rejects.toThrow(
'Netlify build hook responded with status 404'
);
const result = await buildHookHandler({ body: { siteName: 'my-site' } }, context);

expect(result).toEqual({
success: false,
error: 'Netlify build hook responded with status 404',
});
});
});

Expand Down
Loading
Loading