diff --git a/plugins/tech-insights-backend/src/service/fact/factRetrievers/entityFactRetriever.test.ts b/plugins/tech-insights-backend/src/service/fact/factRetrievers/entityFactRetriever.test.ts index c7c8ecf77a..fe4cd18645 100644 --- a/plugins/tech-insights-backend/src/service/fact/factRetrievers/entityFactRetriever.test.ts +++ b/plugins/tech-insights-backend/src/service/fact/factRetrievers/entityFactRetriever.test.ts @@ -15,7 +15,7 @@ */ import { entityFactRetriever } from './entityFactRetriever'; -import { Entity } from '@backstage/catalog-model'; +import { Entity, RELATION_OWNED_BY } from '@backstage/catalog-model'; import { PluginEndpointDiscovery, getVoidLogger, @@ -49,7 +49,7 @@ const defaultEntityListResponse: CatalogListResponse = { }, relations: [ { - type: 'ownedBy', + type: RELATION_OWNED_BY, target: { name: 'team-a', kind: 'group', namespace: 'default' }, }, ], @@ -63,6 +63,19 @@ const defaultEntityListResponse: CatalogListResponse = { spec: { type: 'service', lifecycle: 'test', + owner: '', + }, + }, + { + apiVersion: 'backstage.io/v1beta1', + metadata: { + name: 'service-with-user-owner', + }, + kind: 'Component', + spec: { + type: 'service', + lifecycle: 'test', + owner: 'user:my-user', }, }, ], @@ -108,4 +121,42 @@ describe('entityFactRetriever', () => { }); }); }); + describe('hasSpecWithGroupOwner', () => { + describe('where the entity has an group as owner in the spec', () => { + it('returns true for hasSpecWithGroupOwner', async () => { + const facts = await entityFactRetriever.handler(handlerContext); + expect( + facts.find(it => it.entity.name === 'service-with-owner'), + ).toMatchObject({ + facts: { + hasSpecWithGroupOwner: true, + }, + }); + }); + }); + describe('where the entity has a user as owner in the spec', () => { + it('returns false for hasSpecWithGroupOwner', async () => { + const facts = await entityFactRetriever.handler(handlerContext); + expect( + facts.find(it => it.entity.name === 'service-with-user-owner'), + ).toMatchObject({ + facts: { + hasSpecWithGroupOwner: false, + }, + }); + }); + }); + describe('where the entity does not have an owner in the spec', () => { + it('returns false for hasSpecWithGroupOwner', async () => { + const facts = await entityFactRetriever.handler(handlerContext); + expect( + facts.find(it => it.entity.name === 'service-without-owner'), + ).toMatchObject({ + facts: { + hasSpecWithGroupOwner: false, + }, + }); + }); + }); + }); }); diff --git a/plugins/tech-insights-backend/src/service/fact/factRetrievers/entityFactRetriever.ts b/plugins/tech-insights-backend/src/service/fact/factRetrievers/entityFactRetriever.ts index 8bc670b873..1488cf71ca 100644 --- a/plugins/tech-insights-backend/src/service/fact/factRetrievers/entityFactRetriever.ts +++ b/plugins/tech-insights-backend/src/service/fact/factRetrievers/entityFactRetriever.ts @@ -16,7 +16,7 @@ import { FactRetrieverContext } from '@backstage/plugin-tech-insights-node'; import { CatalogClient } from '@backstage/catalog-client'; -import { RELATION_OWNED_BY } from '@backstage/catalog-model'; +import { Entity, RELATION_OWNED_BY } from '@backstage/catalog-model'; const applicableEntityKinds = [ 'component', @@ -64,7 +64,7 @@ export const entityFactRetriever = { filter: entityKindFilter, }); - return entities.items.map(it => { + return entities.items.map((it: Entity) => { return { entity: { namespace: it.metadata.namespace!!, @@ -73,7 +73,9 @@ export const entityFactRetriever = { }, facts: { hasSpecWithOwner: Boolean(it.spec?.owner), - hasSpecWithGroupOwner: Boolean(it.spec?.owner && it.spec?.owner), + hasSpecWithGroupOwner: Boolean( + it.spec?.owner && (it.spec?.owner as string).startsWith('group:'), + ), hasOwnedByRelation: Boolean( it.relations && it.relations.find(({ type }) => type === RELATION_OWNED_BY),