Skip to content

Commit b6653c2

Browse files
committed
feat: enhance action handling with identifier support
- src/context/directory/handlers/actions.ts: add includeIdentifiers parameter to mapToAction function - src/context/yaml/handlers/actions.ts: add includeIdentifiers parameter to mapActions function - src/tools/auth0/handlers/actions.ts: allow additional properties in action schema - test/context/directory/actions.test.js: add test for dumping actions with identifiers - test/context/yaml/actions.test.js: add test for dumping actions with identifiers
1 parent 409ef3b commit b6653c2

File tree

5 files changed

+125
-8
lines changed

5 files changed

+125
-8
lines changed

src/context/directory/handlers/actions.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ function mapActionCode(filePath, action) {
7373
return `./${constants.ACTIONS_DIRECTORY}/${actionName}/code.js`;
7474
}
7575

76-
function mapToAction(filePath, action): Partial<Action> {
76+
function mapToAction(filePath, action, includeIdentifiers: boolean): Partial<Action> {
7777
return {
78+
...(includeIdentifiers && action.id ? { id: action.id } : {}),
7879
name: action.name,
7980
code: mapActionCode(filePath, action),
8081
runtime: action.runtime,
@@ -105,12 +106,16 @@ async function dump(context: DirectoryContext): Promise<void> {
105106
// Create Actions folder
106107
const actionsFolder = path.join(context.filePath, constants.ACTIONS_DIRECTORY);
107108
fs.ensureDirSync(actionsFolder);
109+
const includeIdentifiers = Boolean(context.config.AUTH0_EXPORT_IDENTIFIERS);
108110
filteredActions.forEach((action) => {
109111
// Dump template metadata
110112
const name = sanitize(action.name);
111113
const actionFile = path.join(actionsFolder, `${name}.json`);
112114
log.info(`Writing ${actionFile}`);
113-
fs.writeFileSync(actionFile, JSON.stringify(mapToAction(context.filePath, action), null, 2));
115+
fs.writeFileSync(
116+
actionFile,
117+
JSON.stringify(mapToAction(context.filePath, action, includeIdentifiers), null, 2)
118+
);
114119
});
115120
}
116121

src/context/yaml/handlers/actions.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ import { Action, isMarketplaceAction } from '../../../tools/auth0/handlers/actio
1111

1212
type ParsedActions = ParsedAsset<'actions', Partial<Action>[]>;
1313

14-
type Secret = { name: string; value: string };
15-
1614
function parseCode(context: YAMLContext, code: string) {
1715
if (code) {
18-
//@ts-ignore TODO: understand why two arguments are passed when context.loadFile only accepts one
16+
// @ts-ignore TODO: understand why two arguments are passed when context.loadFile only accepts one
1917
return context.loadFile(code, constants.ACTIONS_DIRECTORY);
2018
}
2119
}
@@ -38,7 +36,7 @@ async function parse(context: YAMLContext): Promise<ParsedActions> {
3836

3937
function mapSecrets(secrets) {
4038
if (typeof secrets === 'string') {
41-
return secrets; //Enables keyword preservation to operate on action secrets
39+
return secrets; // Enables keyword preservation to operate on action secrets
4240
}
4341
if (secrets && secrets.length > 0) {
4442
return secrets.map((secret) => ({ name: secret.name, value: secret.value }));
@@ -80,11 +78,14 @@ async function dump(context: YAMLContext): Promise<ParsedActions> {
8078
return true;
8179
});
8280

81+
const includeIdentifiers = Boolean(context.config.AUTH0_EXPORT_IDENTIFIERS);
82+
8383
return {
8484
actions: filteredActions.map((action) => ({
85+
...(includeIdentifiers && action.id ? { id: action.id } : {}),
8586
name: action.name,
8687
deployed: !!action.deployed || !!action.all_changes_deployed,
87-
//@ts-ignore because Action resource needs to be typed more accurately
88+
// @ts-ignore because Action resource needs to be typed more accurately
8889
code: mapActionCode(context.basePath, action),
8990
runtime: action.runtime,
9091
dependencies: action.dependencies || [],

src/tools/auth0/handlers/actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const schema = {
4242
items: {
4343
type: 'object',
4444
required: ['name', 'supported_triggers', 'code'],
45-
additionalProperties: false,
45+
additionalProperties: true,
4646
properties: {
4747
code: { type: 'string', default: '' },
4848
runtime: { type: 'string' },

test/context/directory/actions.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,60 @@ describe('#directory context actions', () => {
223223
);
224224
});
225225

226+
it('should dump actions with identifiers when AUTH0_EXPORT_IDENTIFIERS is true', async () => {
227+
const actionName = 'action-with-id';
228+
const dir = path.join(testDataDir, 'directory', 'test3-with-id');
229+
cleanThenMkdir(dir);
230+
const context = new Context(
231+
{ AUTH0_INPUT_FILE: dir, AUTH0_EXPORT_IDENTIFIERS: true },
232+
mockMgmtClient()
233+
);
234+
const codeValidation =
235+
'/** @type {PostLoginAction} */ module.exports = async (event, context) => { console.log("test-action"); return {}; };';
236+
237+
context.assets.actions = [
238+
{
239+
id: 'act_123',
240+
name: actionName,
241+
code: codeValidation,
242+
runtime: 'node12',
243+
dependencies: [],
244+
secrets: [],
245+
supported_triggers: [
246+
{
247+
id: 'post-login',
248+
version: 'v1',
249+
},
250+
],
251+
deployed: true,
252+
status: 'built',
253+
},
254+
];
255+
256+
await handler.dump(context);
257+
258+
const actionsFolder = path.join(dir, constants.ACTIONS_DIRECTORY);
259+
expect(loadJSON(path.join(actionsFolder, 'action-with-id.json'))).to.deep.equal({
260+
id: 'act_123',
261+
name: actionName,
262+
code: './actions/action-with-id/code.js',
263+
runtime: 'node12',
264+
dependencies: [],
265+
secrets: [],
266+
supported_triggers: [
267+
{
268+
id: 'post-login',
269+
version: 'v1',
270+
},
271+
],
272+
deployed: true,
273+
status: 'built',
274+
});
275+
expect(fs.readFileSync(path.join(actionsFolder, actionName, 'code.js'), 'utf8')).to.deep.equal(
276+
codeValidation
277+
);
278+
});
279+
226280
it('should exclude marketplace actions during dump', async () => {
227281
const marketplaceAction = {
228282
id: 'D1AF7CCF-7ZAB-417F-81C0-533595A926D8',

test/context/yaml/actions.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,63 @@ describe('#YAML context actions', () => {
131131
);
132132
});
133133

134+
it('should dump actions with identifiers when AUTH0_EXPORT_IDENTIFIERS is true', async () => {
135+
const dir = path.join(testDataDir, 'yaml', 'actionsDumpWithId');
136+
cleanThenMkdir(dir);
137+
const context = new Context(
138+
{ AUTH0_INPUT_FILE: path.join(dir, 'tenant.yaml'), AUTH0_EXPORT_IDENTIFIERS: true },
139+
mockMgmtClient()
140+
);
141+
const codeValidation =
142+
'/** @type {PostLoginAction} */ module.exports = async (event, context) => { console.log("test-action"); return {}; };';
143+
144+
context.assets.actions = [
145+
{
146+
id: 'act_123',
147+
name: 'action-one',
148+
code: codeValidation,
149+
runtime: 'node12',
150+
status: 'built',
151+
dependencies: [],
152+
supported_triggers: [
153+
{
154+
id: 'post-login',
155+
version: 'v1',
156+
},
157+
],
158+
secrets: [],
159+
deployed: true,
160+
},
161+
];
162+
163+
const dumped = await handler.dump(context);
164+
expect(dumped).to.deep.equal({
165+
actions: [
166+
{
167+
id: 'act_123',
168+
name: 'action-one',
169+
code: './actions/action-one/code.js',
170+
runtime: 'node12',
171+
status: 'built',
172+
dependencies: [],
173+
supported_triggers: [
174+
{
175+
id: 'post-login',
176+
version: 'v1',
177+
},
178+
],
179+
secrets: [],
180+
deployed: true,
181+
},
182+
],
183+
});
184+
185+
const actionsFolder = path.join(dir, 'actions', 'action-one');
186+
expect(fs.readFileSync(path.join(actionsFolder, 'code.js'), 'utf8')).to.deep.equal(
187+
codeValidation
188+
);
189+
});
190+
134191
it('should exclude marketplace actions during dump', async () => {
135192
const dir = path.join(testDataDir, 'yaml', 'actionsDump');
136193
cleanThenMkdir(dir);

0 commit comments

Comments
 (0)