From c6c78b4acbe6c9b0918194dd4b35a9d9bd60d773 Mon Sep 17 00:00:00 2001 From: Heikki Hellgren Date: Mon, 20 Feb 2023 08:48:06 +0200 Subject: [PATCH] fix: catalog:fetch to throw error for null entity fixes #16447 Signed-off-by: Heikki Hellgren --- .changeset/eight-radios-bake.md | 5 +++++ .../actions/builtin/catalog/fetch.test.ts | 20 ++++++++++++++++++- .../actions/builtin/catalog/fetch.ts | 5 +++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 .changeset/eight-radios-bake.md diff --git a/.changeset/eight-radios-bake.md b/.changeset/eight-radios-bake.md new file mode 100644 index 0000000000..173739a625 --- /dev/null +++ b/.changeset/eight-radios-bake.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': patch +--- + +throw error from catalog:fetch scaffolder action when entity is null and optional is false diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/fetch.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/fetch.test.ts index f8bdf2bdcb..52c76c8769 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/fetch.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/fetch.test.ts @@ -90,7 +90,7 @@ describe('catalog:fetch', () => { expect(mockContext.output).toHaveBeenCalledWith('entity', null); }); - it('should throw error if entity not in catalog and optional is false', async () => { + it('should throw error if entity fetch fails from catalog and optional is false', async () => { getEntityByRef.mockImplementationOnce(() => { throw new Error('Not found'); }); @@ -109,4 +109,22 @@ describe('catalog:fetch', () => { }); expect(mockContext.output).not.toHaveBeenCalled(); }); + + it('should throw error if entity not in catalog and optional is false', async () => { + getEntityByRef.mockReturnValueOnce(null); + + await expect( + action.handler({ + ...mockContext, + input: { + entityRef: 'component:default/test', + }, + }), + ).rejects.toThrow('Entity component:default/test not found'); + + expect(getEntityByRef).toHaveBeenCalledWith('component:default/test', { + token: 'secret', + }); + expect(mockContext.output).not.toHaveBeenCalled(); + }); }); diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/fetch.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/fetch.ts index 913bf32cce..17e510ce64 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/fetch.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/fetch.ts @@ -93,6 +93,11 @@ export function createFetchCatalogEntityAction(options: { throw e; } } + + if (!entity && !optional) { + throw new Error(`Entity ${entityRef} not found`); + } + ctx.output('entity', entity ?? null); }, });