Skip to content

Commit

Permalink
scaffolder-backend: fixed register action not returning an entity
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
4 people committed Oct 5, 2021
1 parent 3fee13e commit ca3086a
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-geckos-protect.md
@@ -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.
Expand Up @@ -67,25 +67,98 @@ 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).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',
'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).toBeCalledWith(

expect(addLocation).toHaveBeenNthCalledWith(
1,
{
type: 'url',
target: 'http://foo/var',
},
{},
);
expect(addLocation).toHaveBeenNthCalledWith(
2,
{
dryRun: true,
type: 'url',
target: 'http://foo/var',
},
Expand All @@ -94,7 +167,7 @@ describe('catalog:register', () => {

expect(mockContext.output).toBeCalledWith(
'entityRef',
'Component:default/test',
'component:default/test',
);
expect(mockContext.output).toBeCalledWith(
'catalogInfoUrl',
Expand Down
Expand Up @@ -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: {
Expand Down Expand Up @@ -93,18 +93,30 @@ export function createCatalogRegisterAction(options: {

ctx.logger.info(`Registering ${catalogInfoUrl} in the catalog`);

await catalogClient.addLocation(
{
type: 'url',
target: catalogInfoUrl,
},
ctx.token ? { token: ctx.token } : {},
);
const result = await catalogClient.addLocation(
{
dryRun: true,
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);

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);
},
});
}

0 comments on commit ca3086a

Please sign in to comment.