scaffolder-backend: fixed register action not returning an entity

Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Co-authored-by: blam <ben@blam.sh>
Co-authored-by: Johan Haals <johan.haals@gmail.com>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2021-10-05 11:26:08 +02:00
parent 3fee13e577
commit ca3086a7ad
3 changed files with 109 additions and 19 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend': patch
---
Fixed a bug where the `catalog:register` action would not return any entity when running towards recent versions of the catalog.
@@ -67,34 +67,107 @@ describe('catalog:register', () => {
});
it('should register location in catalog', async () => {
addLocation.mockResolvedValue({
entities: [
{
metadata: {
namespace: 'default',
name: 'test',
},
kind: 'Component',
} as Entity,
],
});
addLocation
.mockResolvedValueOnce({
entities: [],
})
.mockResolvedValueOnce({
entities: [
{
metadata: {
namespace: 'default',
name: 'test',
},
kind: 'Component',
} as Entity,
],
});
await action.handler({
...mockContext,
input: {
catalogInfoUrl: 'http://foo/var',
},
});
expect(addLocation).toBeCalledWith(
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',
'component:default/test',
);
expect(mockContext.output).toBeCalledWith(
'catalogInfoUrl',
'http://foo/var',
);
});
it('should register location in catalog and return the entity and not the generated location', async () => {
addLocation
.mockResolvedValueOnce({
entities: [],
})
.mockResolvedValueOnce({
entities: [
{
metadata: {
namespace: 'default',
name: 'generated-1238',
},
kind: 'Location',
} as Entity,
{
metadata: {
namespace: 'default',
name: 'test',
},
kind: 'Component',
} as Entity,
],
});
await action.handler({
...mockContext,
input: {
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',
);
expect(mockContext.output).toBeCalledWith(
'catalogInfoUrl',
@@ -17,7 +17,7 @@
import { InputError } from '@backstage/errors';
import { ScmIntegrations } from '@backstage/integration';
import { CatalogApi } from '@backstage/catalog-client';
import { getEntityName } from '@backstage/catalog-model';
import { stringifyEntityRef } from '@backstage/catalog-model';
import { createTemplateAction } from '../../createTemplateAction';
export function createCatalogRegisterAction(options: {
@@ -93,18 +93,30 @@ export function createCatalogRegisterAction(options: {
ctx.logger.info(`Registering ${catalogInfoUrl} in the catalog`);
const result = await catalogClient.addLocation(
await catalogClient.addLocation(
{
type: 'url',
target: catalogInfoUrl,
},
ctx.token ? { token: ctx.token } : {},
);
if (result.entities.length >= 1) {
const { kind, name, namespace } = getEntityName(result.entities[0]);
ctx.output('entityRef', `${kind}:${namespace}/${name}`);
ctx.output('catalogInfoUrl', catalogInfoUrl);
const result = await catalogClient.addLocation(
{
dryRun: true,
type: 'url',
target: catalogInfoUrl,
},
ctx.token ? { token: ctx.token } : {},
);
if (result.entities.length > 0) {
const { entities } = result;
const entity =
entities.find(e => !e.metadata.name.startsWith('generated-')) ??
entities[0];
ctx.output('entityRef', stringifyEntityRef(entity));
}
ctx.output('catalogInfoUrl', catalogInfoUrl);
},
});
}