Add backstage.io/template-source annotation via the catalog:write action

Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
This commit is contained in:
Eric Peterson
2024-03-11 12:55:08 +01:00
parent ea0e006e84
commit f34a9b178d
3 changed files with 50 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend': patch
---
The `catalog:write` action now automatically adds a `backstage.io/template-source` annotation, indicating which Scaffolder template was used to create the entity.
@@ -94,4 +94,34 @@ describe('catalog:write', () => {
yaml.stringify(entity),
);
});
it('should add backstage.io/source-template if provided', async () => {
const entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'n',
namespace: 'ns',
annotations: {},
},
spec: {},
};
await action.handler({
...mockContext,
templateInfo: { entityRef: 'template:default/test-skeleton' },
input: { entity },
});
const expectedEntity = JSON.parse(JSON.stringify(entity));
expectedEntity.metadata.annotations = {
'backstage.io/source-template': 'template:default/test-skeleton',
};
expect(fsMock.writeFile).toHaveBeenCalledTimes(1);
expect(fsMock.writeFile).toHaveBeenCalledWith(
resolvePath(mockContext.workspacePath, 'catalog-info.yaml'),
yaml.stringify(expectedEntity),
);
});
});
@@ -51,11 +51,25 @@ export function createCatalogWriteAction() {
async handler(ctx) {
ctx.logger.info(`Writing catalog-info.yaml`);
const { filePath, entity } = ctx.input;
const entityRef = ctx.templateInfo?.entityRef;
const path = filePath ?? 'catalog-info.yaml';
await fs.writeFile(
resolveSafeChildPath(ctx.workspacePath, path),
yaml.stringify(entity),
yaml.stringify({
...entity,
metadata: {
...entity.metadata,
...(entityRef
? {
annotations: {
...entity.metadata.annotations,
'backstage.io/source-template': entityRef,
},
}
: undefined),
},
}),
);
},
});