Merge pull request #6068 from angeliski/add-action-write-catalog

feat:created a action to write a catalog-info.yml file
This commit is contained in:
Fredrik Adelöw
2021-06-24 11:52:00 +02:00
committed by GitHub
7 changed files with 139 additions and 1 deletions
+3
View File
@@ -124,6 +124,9 @@ export function createCatalogRegisterAction(options: {
integrations: ScmIntegrations;
}): TemplateAction<any>;
// @public (undocumented)
export function createCatalogWriteAction(): TemplateAction<any>;
// @public
export function createDebugLogAction(): TemplateAction<any>;
@@ -15,3 +15,4 @@
*/
export { createCatalogRegisterAction } from './register';
export { createCatalogWriteAction } from './write';
@@ -0,0 +1,71 @@
/*
* Copyright 2021 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import fs from 'fs-extra';
jest.mock('fs-extra');
const fsMock = fs as jest.Mocked<typeof fs>;
import { PassThrough } from 'stream';
import os from 'os';
import { getVoidLogger } from '@backstage/backend-common';
import { ORIGIN_LOCATION_ANNOTATION } from '@backstage/catalog-model';
import { createCatalogWriteAction } from './write';
import { resolve as resolvePath } from 'path';
import * as yaml from 'yaml';
describe('catalog:write', () => {
const action = createCatalogWriteAction();
const mockContext = {
workspacePath: os.tmpdir(),
logger: getVoidLogger(),
logStream: new PassThrough(),
output: jest.fn(),
createTemporaryDirectory: jest.fn(),
};
beforeEach(() => {
jest.resetAllMocks();
});
it('should write the catalog-info.yml in the workspace', async () => {
const entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'n',
namespace: 'ns',
annotations: {
[ORIGIN_LOCATION_ANNOTATION]: 'url:https://example.com',
},
},
spec: {},
};
await action.handler({
...mockContext,
input: {
entity,
},
});
expect(fsMock.writeFile).toHaveBeenCalledTimes(1);
expect(fsMock.writeFile).toHaveBeenCalledWith(
resolvePath(mockContext.workspacePath, 'catalog-info.yaml'),
yaml.stringify(entity),
);
});
});
@@ -0,0 +1,49 @@
/*
* Copyright 2021 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import fs from 'fs-extra';
import { createTemplateAction } from '../../createTemplateAction';
import * as yaml from 'yaml';
import { Entity } from '@backstage/catalog-model';
export function createCatalogWriteAction() {
return createTemplateAction<{ name?: string; entity: Entity }>({
id: 'catalog:write',
description: 'Writes the catalog-info.yaml for your template',
schema: {
input: {
type: 'object',
properties: {
entity: {
title: 'Entity info to write catalog-info.yaml',
description:
'You can provide the same values used in the Entity schema.',
type: 'object',
},
},
},
},
async handler(ctx) {
ctx.logStream.write(`Writing catalog-info.yaml`);
const { entity } = ctx.input;
await fs.writeFile(
`${ctx.workspacePath}/catalog-info.yaml`,
yaml.stringify(entity),
);
},
});
}
@@ -18,7 +18,10 @@ import { UrlReader } from '@backstage/backend-common';
import { CatalogApi } from '@backstage/catalog-client';
import { ScmIntegrations } from '@backstage/integration';
import { TemplaterBuilder } from '../../stages';
import { createCatalogRegisterAction } from './catalog';
import {
createCatalogRegisterAction,
createCatalogWriteAction,
} from './catalog';
import { createDebugLogAction } from './debug';
import { createFetchCookiecutterAction, createFetchPlainAction } from './fetch';
import {
@@ -64,5 +67,6 @@ export const createBuiltinActions = (options: {
}),
createDebugLogAction(),
createCatalogRegisterAction({ catalogClient, integrations }),
createCatalogWriteAction(),
];
};
@@ -48,6 +48,11 @@ export class TaskWorker {
return JSON.stringify(parseRepoUrl(repoUrl));
});
this.handlebars.registerHelper('projectSlug', repoUrl => {
const { owner, repo } = parseRepoUrl(repoUrl);
return `${owner}/${repo}`;
});
this.handlebars.registerHelper('json', obj => JSON.stringify(obj));
this.handlebars.registerHelper('not', value => !isTruthy(value));