From bd48e561001ae9a2335799ce5e7dac324b6efa9d Mon Sep 17 00:00:00 2001 From: marmionl Date: Tue, 31 Mar 2026 18:36:22 +0100 Subject: [PATCH] fix: bug fix for adding nested entity relations --- .changeset/soft-hands-look.md | 5 +++ app-config.yaml | 2 +- .../relations-backend/examples/group-1.yaml | 3 +- .../src/processor/processorConfig.test.ts | 32 +++++++++++++++++++ .../src/processor/processorConfig.ts | 3 +- 5 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 .changeset/soft-hands-look.md diff --git a/.changeset/soft-hands-look.md b/.changeset/soft-hands-look.md new file mode 100644 index 0000000..1bf5414 --- /dev/null +++ b/.changeset/soft-hands-look.md @@ -0,0 +1,5 @@ +--- +'@dweber019/backstage-plugin-relations-backend': minor +--- + +fix: nested relations for entities will now be generated diff --git a/app-config.yaml b/app-config.yaml index 302cf82..63f1b88 100644 --- a/app-config.yaml +++ b/app-config.yaml @@ -135,7 +135,7 @@ relationsProcessor: - sourceKind: group targetKinds: - user - attribute: productOwner + attribute: nestedContact.productOwner multi: false pairs: - incoming: productOwnerOf diff --git a/plugins/relations-backend/examples/group-1.yaml b/plugins/relations-backend/examples/group-1.yaml index 08b58f3..53753b9 100644 --- a/plugins/relations-backend/examples/group-1.yaml +++ b/plugins/relations-backend/examples/group-1.yaml @@ -8,4 +8,5 @@ spec: members: - john-doe - kevin-po - productOwner: user:default/kevin-po + nestedContact: + productOwner: user:default/kevin-po diff --git a/plugins/relations-backend/src/processor/processorConfig.test.ts b/plugins/relations-backend/src/processor/processorConfig.test.ts index 4777b3b..fc5390f 100644 --- a/plugins/relations-backend/src/processor/processorConfig.test.ts +++ b/plugins/relations-backend/src/processor/processorConfig.test.ts @@ -221,6 +221,38 @@ describe('processorConfig', () => { expect(relations).toHaveLength(2); }); + test('should return relation for nested attribute', async () => { + const config = new ConfigReader({ + relationsProcessor: { + relations: [ + { + sourceKind: 'System', + attribute: 'contacts.technologyOwner', + pairs: [ + { + incoming: 'technologyOwnerOf', + outgoing: 'technologyOwner', + }, + ], + }, + ], + }, + }); + const entity = { + kind: 'System', + spec: { + contacts: { + technologyOwner: 'user:default/john.doe', + }, + }, + } as unknown as Entity; + + const processorConfig = new ProcessorConfig(config); + const relations = processorConfig.getRelations(entity); + + expect(relations).toHaveLength(1); + }); + test('should throw error for config errors', async () => { const config = new ConfigReader({ relationsProcessor: { diff --git a/plugins/relations-backend/src/processor/processorConfig.ts b/plugins/relations-backend/src/processor/processorConfig.ts index 6bb71bc..a4f0ce1 100644 --- a/plugins/relations-backend/src/processor/processorConfig.ts +++ b/plugins/relations-backend/src/processor/processorConfig.ts @@ -1,6 +1,7 @@ import { Config } from '@backstage/config'; import relationV1alpha1Schema from './Relation.v1alpha1.schema.json'; import { Entity } from '@backstage/catalog-model'; +import { has } from 'lodash'; interface RelationConfig { sourceKind: string; @@ -25,7 +26,7 @@ export class ProcessorConfig { return this.relations.filter(relation => { return ( entity.kind.toLocaleLowerCase() === relation.sourceKind && - entity.spec?.hasOwnProperty(relation.attribute) && + has(entity.spec, relation.attribute) && (relation.sourceType === undefined || (entity.spec?.type && (entity.spec?.type as string).toLocaleLowerCase() ===