diff --git a/.changeset/cold-steaks-flash.md b/.changeset/cold-steaks-flash.md new file mode 100644 index 0000000000..74cdcbdec2 --- /dev/null +++ b/.changeset/cold-steaks-flash.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': patch +--- + +fix: Register plugin to prioritise Component kind for entityRef diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/register.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/register.test.ts index e1f8823487..d257bced38 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/register.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/register.test.ts @@ -117,7 +117,7 @@ describe('catalog:register', () => { ); }); - it('should register location in catalog and return the entity and not the generated location', async () => { + it('should return entityRef with the Component entity and not the generated location', async () => { addLocation .mockResolvedValueOnce({ entities: [], @@ -131,6 +131,13 @@ describe('catalog:register', () => { }, kind: 'Location', } as Entity, + { + metadata: { + namespace: 'default', + name: 'test', + }, + kind: 'Api', + } as Entity, { metadata: { namespace: 'default', @@ -138,6 +145,13 @@ describe('catalog:register', () => { }, kind: 'Component', } as Entity, + { + metadata: { + namespace: 'default', + name: 'test', + }, + kind: 'Template', + } as Entity, ], }); await action.handler({ @@ -146,32 +160,103 @@ describe('catalog:register', () => { catalogInfoUrl: 'http://foo/var', }, }); - - expect(addLocation).toHaveBeenNthCalledWith( - 1, - { - type: 'url', - target: 'http://foo/var', - }, - {}, - ); - expect(addLocation).toHaveBeenNthCalledWith( - 2, - { - dryRun: true, - type: 'url', - target: 'http://foo/var', - }, - {}, - ); - expect(mockContext.output).toBeCalledWith( 'entityRef', 'component:default/test', ); + }); + + it('should return entityRef with the next non-generated entity if no Component kind can be found', async () => { + addLocation + .mockResolvedValueOnce({ + entities: [], + }) + .mockResolvedValueOnce({ + entities: [ + { + metadata: { + namespace: 'default', + name: 'generated-1238', + }, + kind: 'Location', + } as Entity, + { + metadata: { + namespace: 'default', + name: 'test', + }, + kind: 'Api', // should return this one + } as Entity, + { + metadata: { + namespace: 'default', + name: 'test', + }, + kind: 'Template', + } as Entity, + ], + }); + await action.handler({ + ...mockContext, + input: { + catalogInfoUrl: 'http://foo/var', + }, + }); + expect(mockContext.output).toBeCalledWith('entityRef', 'api:default/test'); + }); + + it('should return entityRef with the first entity if no non-generated entities can be found', async () => { + addLocation + .mockResolvedValueOnce({ + entities: [], + }) + .mockResolvedValueOnce({ + entities: [ + { + metadata: { + namespace: 'default', + name: 'generated-1238', + }, + kind: 'Location', + } as Entity, + { + metadata: { + namespace: 'default', + name: 'generated-1238', + }, + kind: 'Template', + } as Entity, + ], + }); + await action.handler({ + ...mockContext, + input: { + catalogInfoUrl: 'http://foo/var', + }, + }); expect(mockContext.output).toBeCalledWith( - 'catalogInfoUrl', - 'http://foo/var', + 'entityRef', + 'location:default/generated-1238', + ); + }); + + it('should not return entityRef if there are no entites', async () => { + addLocation + .mockResolvedValueOnce({ + entities: [], + }) + .mockResolvedValueOnce({ + entities: [], + }); + await action.handler({ + ...mockContext, + input: { + catalogInfoUrl: 'http://foo/var', + }, + }); + expect(mockContext.output).not.toBeCalledWith( + 'entityRef', + expect.any(String), ); }); diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/register.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/register.ts index 0c08131925..249939b0e6 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/register.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/catalog/register.ts @@ -125,9 +125,22 @@ export function createCatalogRegisterAction(options: { if (result.entities.length > 0) { const { entities } = result; - const entity = - entities.find(e => !e.metadata.name.startsWith('generated-')) ?? - entities[0]; + let entity: any; + // prioritise 'Component' type as it is the most central kind of entity + entity = entities.find( + (e: any) => + !e.metadata.name.startsWith('generated-') && + e.kind === 'Component', + ); + if (!entity) { + entity = entities.find( + (e: any) => !e.metadata.name.startsWith('generated-'), + ); + } + if (!entity) { + entity = entities[0]; + } + ctx.output('entityRef', stringifyEntityRef(entity)); } } catch (e) {