fix(catalog:register): support optional locations

Signed-off-by: Phil Kuang <pkuang@factset.com>
This commit is contained in:
Phil Kuang
2021-10-15 16:08:06 -04:00
parent 59e822b43c
commit b149e94290
3 changed files with 79 additions and 16 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend': patch
---
Allow `catalog:register` action to register optional locations
@@ -174,4 +174,42 @@ describe('catalog:register', () => {
'http://foo/var',
);
});
it('should ignore failures when dry running the location in the catalog if `optional` is set', async () => {
addLocation
.mockResolvedValueOnce({
entities: [],
})
.mockRejectedValueOnce(new Error('Not found'));
await action.handler({
...mockContext,
input: {
catalogInfoUrl: 'http://foo/var',
optional: true,
},
});
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(
'catalogInfoUrl',
'http://foo/var',
);
});
});
@@ -27,8 +27,8 @@ export function createCatalogRegisterAction(options: {
const { catalogClient, integrations } = options;
return createTemplateAction<
| { catalogInfoUrl: string }
| { repoContentsUrl: string; catalogInfoPath?: string }
| { catalogInfoUrl: string; optional?: boolean }
| { repoContentsUrl: string; catalogInfoPath?: string; optional?: boolean }
>({
id: 'catalog:register',
description:
@@ -46,6 +46,12 @@ export function createCatalogRegisterAction(options: {
'An absolute URL pointing to the catalog info file location',
type: 'string',
},
optional: {
title: 'Optional',
description:
'Permit the registered location to optionally exist. Default: false',
type: 'boolean',
},
},
},
{
@@ -64,6 +70,12 @@ export function createCatalogRegisterAction(options: {
'A relative path from the repo root pointing to the catalog info file, defaults to /catalog-info.yaml',
type: 'string',
},
optional: {
title: 'Optional',
description:
'Permit the registered location to optionally exist. Default: false',
type: 'boolean',
},
},
},
],
@@ -100,22 +112,30 @@ export function createCatalogRegisterAction(options: {
},
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 > 0) {
const { entities } = result;
const entity =
entities.find(e => !e.metadata.name.startsWith('generated-')) ??
entities[0];
ctx.output('entityRef', stringifyEntityRef(entity));
try {
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));
}
} catch (e) {
if (!input.optional) {
throw e;
}
}
ctx.output('catalogInfoUrl', catalogInfoUrl);
},
});