Skip to content

Commit

Permalink
refactor(webhook-custom-actions): allow to declare the activation of …
Browse files Browse the repository at this point in the history
…webhook custom actions (#670)
  • Loading branch information
Guillaume Gautreau authored Apr 18, 2023
1 parent 9ca5341 commit e90636e
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 12 deletions.
13 changes: 12 additions & 1 deletion packages/agent/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export default class Agent<S extends TSchema = TSchema> extends FrameworkMounter
throw new Error(`Can't load ${schemaPath}. Providing a schema is mandatory in production.`);
}
} else {
schema = await SchemaGenerator.buildSchema(dataSource);
schema = await SchemaGenerator.buildSchema(dataSource, this.buildSchemaFeatures());

const pretty = stringify(schema, { maxLength: 100 });
await writeFile(schemaPath, pretty, { encoding: 'utf-8' });
Expand All @@ -188,4 +188,15 @@ export default class Agent<S extends TSchema = TSchema> extends FrameworkMounter

this.options.logger('Info', message);
}

private buildSchemaFeatures(): Record<string, string> | null {
if (this.options.experimental?.webhookCustomActions) {
return {
// Versions correspond to the version of the feature
'webhook-custom-actions': '1.0.0',
};
}

return null;
}
}
3 changes: 3 additions & 0 deletions packages/agent/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export type AgentOptions = {
permissionsCacheDurationInSeconds?: number;
skipSchemaUpdate?: boolean;
forestAdminClient?: ForestAdminClient;
experimental?: {
webhookCustomActions?: boolean;
};
};
export type AgentOptionsWithDefaults = Readonly<Required<AgentOptions>>;

Expand Down
6 changes: 5 additions & 1 deletion packages/agent/src/utils/forest-schema/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { ForestSchema } from '@forestadmin/forestadmin-client';
import SchemaGeneratorCollection from './generator-collection';

export default class SchemaGenerator {
static async buildSchema(dataSource: DataSource): Promise<ForestSchema> {
static async buildSchema(
dataSource: DataSource,
features: Record<string, string> | null,
): Promise<ForestSchema> {
const { version } = require('../../../package.json'); // eslint-disable-line @typescript-eslint/no-var-requires,global-require,max-len

return {
Expand All @@ -16,6 +19,7 @@ export default class SchemaGenerator {
metadata: {
liana: 'forest-nodejs-agent',
liana_version: version,
liana_features: features,
stack: {
engine: 'nodejs',
engine_version: process.versions && process.versions.node,
Expand Down
9 changes: 9 additions & 0 deletions packages/agent/test/__data__/agent-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"collections": [],
"metadata": {
"liana": "forest-nodejs-agent",
"liana_version": "2.0.0",
"liana_features": null,
"stack": "agent-nodejs"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export default Factory.define<AgentOptionsWithDefaults>(() => ({
skipSchemaUpdate: false,
typingsMaxDepth: 5,
typingsPath: null,
experimental: {},
}));
43 changes: 35 additions & 8 deletions packages/agent/test/agent.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable max-classes-per-file */
/* eslint-disable @typescript-eslint/no-explicit-any */

import { readFile } from 'fs/promises';

import * as factories from './__factories__';
import Agent from '../src/agent';

Expand Down Expand Up @@ -98,6 +100,35 @@ describe('Agent', () => {
metadata: {
liana: 'forest-nodejs-agent',
liana_version: expect.stringMatching(/\d+\.\d+\.\d+.*/),
liana_features: null,
stack: expect.anything(),
},
});
});

test('that should upload the schema with experimental features', async () => {
const agent = new Agent({
...options,
experimental: {
webhookCustomActions: true,
},
});
await agent.start();

expect(mockSetupRoute).toHaveBeenCalledTimes(1);
expect(mockBootstrap).toHaveBeenCalledTimes(1);
expect(mockMakeRoutes).toHaveBeenCalledTimes(1);
expect(mockGetDataSource).toHaveBeenCalledTimes(1);
expect(mockUpdateTypesOnFileSystem).toHaveBeenCalledTimes(1);

expect(mockPostSchema).toHaveBeenCalledWith({
collections: [],
metadata: {
liana: 'forest-nodejs-agent',
liana_version: expect.stringMatching(/\d+\.\d+\.\d+.*/),
liana_features: {
'webhook-custom-actions': '1.0.0',
},
stack: expect.anything(),
},
});
Expand All @@ -109,6 +140,7 @@ describe('Agent', () => {
isProduction: true,
typingsPath: '/tmp/test_typings.ts',
forestAdminClient: factories.forestAdminClient.build({ postSchema: mockPostSchema }),
schemaPath: `${__dirname}/__data__/agent-schema.json`,
});

test('start should throw if the schema does not exists', async () => {
Expand All @@ -132,14 +164,9 @@ describe('Agent', () => {
expect(mockGetDataSource).toHaveBeenCalledTimes(1);
expect(mockUpdateTypesOnFileSystem).not.toHaveBeenCalled();

expect(mockPostSchema).toHaveBeenCalledWith({
collections: [],
metadata: {
liana: 'forest-nodejs-agent',
liana_version: expect.stringMatching(/\d+\.\d+\.\d+.*/),
stack: expect.anything(),
},
});
const schemaContent = JSON.parse(await readFile(options.schemaPath, 'utf8'));

expect(mockPostSchema).toHaveBeenCalledWith(schemaContent);
});

test('start should not update schema when specified', async () => {
Expand Down
29 changes: 27 additions & 2 deletions packages/agent/test/utils/forest-schema/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ describe('SchemaGenerator', () => {
const dataSource = factories.dataSource.buildWithCollection(
factories.collection.build({ name: 'books' }),
);
const schema = await SchemaGenerator.buildSchema(dataSource);
const schema = await SchemaGenerator.buildSchema(dataSource, null);

expect(schema).toStrictEqual({
collections: [expect.objectContaining({ name: 'books' })],
metadata: {
liana: 'forest-nodejs-agent',
liana_version: expect.any(String),
liana_features: null,
stack: {
engine: 'nodejs',
engine_version: expect.any(String),
Expand All @@ -28,8 +29,32 @@ describe('SchemaGenerator', () => {
factories.collection.build({ name: 'b' }),
factories.collection.build({ name: 'a' }),
]);
const schema = await SchemaGenerator.buildSchema(dataSource);
const schema = await SchemaGenerator.buildSchema(dataSource, null);

expect(schema.collections.map(c => c.name)).toStrictEqual(['a', 'b', 'B', 'ba']);
});

test('it should serialize features', async () => {
const dataSource = factories.dataSource.buildWithCollection(
factories.collection.build({ name: 'books' }),
);
const schema = await SchemaGenerator.buildSchema(dataSource, {
'webhook-custom-actions': '1.0.0',
});

expect(schema).toStrictEqual({
collections: [expect.objectContaining({ name: 'books' })],
metadata: {
liana: 'forest-nodejs-agent',
liana_version: expect.any(String),
liana_features: {
'webhook-custom-actions': '1.0.0',
},
stack: {
engine: 'nodejs',
engine_version: expect.any(String),
},
},
});
});
});
1 change: 1 addition & 0 deletions packages/forestadmin-client/src/schema/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type ForestSchema = {
metadata: {
liana: string;
liana_version: string;
liana_features: Record<string, string> | null;
stack: {
engine: string;
engine_version: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe('ForestAdminClientWithCache', () => {
metadata: {
liana: 'forest-nodejs-agent',
liana_version: '1.0.0',
liana_features: null,
stack: { engine: 'nodejs', engine_version: '16.0.0' },
},
});
Expand Down
3 changes: 3 additions & 0 deletions packages/forestadmin-client/test/schema/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('SchemaService', () => {
metadata: {
liana: 'forest-nodejs-agent',
liana_version: '1.0.0',
liana_features: null,
stack: { engine: 'nodejs', engine_version: '16.0.0' },
},
});
Expand All @@ -55,6 +56,7 @@ describe('SchemaService', () => {
meta: {
liana: 'forest-nodejs-agent',
liana_version: '1.0.0',
liana_features: null,
stack: { engine: 'nodejs', engine_version: '16.0.0' },
schemaFileHash: expect.any(String),
},
Expand All @@ -81,6 +83,7 @@ describe('SchemaService', () => {
metadata: {
liana: 'forest-nodejs-agent',
liana_version: '1.0.0',
liana_features: null,
stack: { engine: 'nodejs', engine_version: '16.0.0' },
},
});
Expand Down

0 comments on commit e90636e

Please sign in to comment.