fix: Register plugin to prioritise Component kind for entityRef
Signed-off-by: Bert Huang <bert.huang@outlook.com>
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-scaffolder-backend': patch
|
||||
---
|
||||
|
||||
fix: Register plugin to prioritise Component kind for entityRef
|
||||
+107
-22
@@ -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),
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user