From b041aab8adea2e199564011cdb0f445a0a614479 Mon Sep 17 00:00:00 2001 From: Sebastian Qvarfordt Date: Tue, 13 Oct 2020 09:07:14 +0200 Subject: [PATCH] [TechDocs] Remove all git preparers and use a commonGit preparer instead (#2856) * Remove all git preparers and use a commonGit preparer instead * Added more tests * Updated default-app template --- packages/backend/src/plugins/techdocs.ts | 15 ++--- .../packages/backend/src/plugins/techdocs.ts | 7 +- .../src/techdocs/stages/prepare/azure.test.ts | 63 ------------------ .../src/techdocs/stages/prepare/azure.ts | 55 ---------------- .../{github.test.ts => commonGit.test.ts} | 36 ++++++++++- .../prepare/{gitlab.ts => commonGit.ts} | 9 +-- .../src/techdocs/stages/prepare/github.ts | 55 ---------------- .../techdocs/stages/prepare/gitlab.test.ts | 64 ------------------- .../src/techdocs/stages/prepare/index.ts | 4 +- 9 files changed, 46 insertions(+), 262 deletions(-) delete mode 100644 plugins/techdocs-backend/src/techdocs/stages/prepare/azure.test.ts delete mode 100644 plugins/techdocs-backend/src/techdocs/stages/prepare/azure.ts rename plugins/techdocs-backend/src/techdocs/stages/prepare/{github.test.ts => commonGit.test.ts} (59%) rename plugins/techdocs-backend/src/techdocs/stages/prepare/{gitlab.ts => commonGit.ts} (83%) delete mode 100644 plugins/techdocs-backend/src/techdocs/stages/prepare/github.ts delete mode 100644 plugins/techdocs-backend/src/techdocs/stages/prepare/gitlab.test.ts diff --git a/packages/backend/src/plugins/techdocs.ts b/packages/backend/src/plugins/techdocs.ts index 6e36c26f39..dea31c763b 100644 --- a/packages/backend/src/plugins/techdocs.ts +++ b/packages/backend/src/plugins/techdocs.ts @@ -21,9 +21,7 @@ import { Generators, LocalPublish, TechdocsGenerator, - GithubPreparer, - GitlabPreparer, - AzurePreparer, + CommonGitPreparer, } from '@backstage/plugin-techdocs-backend'; import { PluginEnvironment } from '../types'; import Docker from 'dockerode'; @@ -38,14 +36,13 @@ export default async function createPlugin({ generators.register('techdocs', techdocsGenerator); const preparers = new Preparers(); - const githubPreparer = new GithubPreparer(logger); - const gitlabPreparer = new GitlabPreparer(logger); - const azurePreparer = new AzurePreparer(logger); + const commonGitPreparer = new CommonGitPreparer(logger); + const directoryPreparer = new DirectoryPreparer(logger); preparers.register('dir', directoryPreparer); - preparers.register('github', githubPreparer); - preparers.register('gitlab', gitlabPreparer); - preparers.register('azure/api', azurePreparer); + preparers.register('github', commonGitPreparer); + preparers.register('gitlab', commonGitPreparer); + preparers.register('azure/api', commonGitPreparer); const publisher = new LocalPublish(logger); diff --git a/packages/create-app/templates/default-app/packages/backend/src/plugins/techdocs.ts b/packages/create-app/templates/default-app/packages/backend/src/plugins/techdocs.ts index b522992a75..5506228962 100644 --- a/packages/create-app/templates/default-app/packages/backend/src/plugins/techdocs.ts +++ b/packages/create-app/templates/default-app/packages/backend/src/plugins/techdocs.ts @@ -1,7 +1,7 @@ import { createRouter, DirectoryPreparer, - GithubPreparer, + CommonGitPreparer, Preparers, Generators, LocalPublish, @@ -22,10 +22,11 @@ export default async function createPlugin({ const preparers = new Preparers(); const directoryPreparer = new DirectoryPreparer(logger); - const githubPreparer = new GithubPreparer(logger); + const commonGitPreparer = new CommonGitPreparer(logger); preparers.register('dir', directoryPreparer); - preparers.register('github', githubPreparer); + preparers.register('github', commonGitPreparer); + preparers.register('gitlab', commonGitPreparer); const publisher = new LocalPublish(logger); diff --git a/plugins/techdocs-backend/src/techdocs/stages/prepare/azure.test.ts b/plugins/techdocs-backend/src/techdocs/stages/prepare/azure.test.ts deleted file mode 100644 index 39a3d5c7fa..0000000000 --- a/plugins/techdocs-backend/src/techdocs/stages/prepare/azure.test.ts +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2020 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 { getVoidLogger } from '@backstage/backend-common'; -import { AzurePreparer } from './azure'; -import { checkoutGitRepository } from '../../../helpers'; - -function normalizePath(path: string) { - return path - .replace(/^[a-z]:/i, '') - .split('\\') - .join('/'); -} - -jest.mock('../../../helpers', () => ({ - ...jest.requireActual<{}>('../../../helpers'), - checkoutGitRepository: jest.fn(() => '/tmp/backstage-repo/org/name/branch'), -})); - -const createMockEntity = (annotations = {}) => { - return { - apiVersion: 'version', - kind: 'TestKind', - metadata: { - name: 'test-component-name', - annotations: { - ...annotations, - }, - }, - }; -}; - -const logger = getVoidLogger(); - -describe('Azure DevOps preparer', () => { - it('should prepare temp docs path from Azure DevOps repo', async () => { - const preparer = new AzurePreparer(logger); - - const mockEntity = createMockEntity({ - 'backstage.io/techdocs-ref': - 'azure/api:https://dev.azure.com/backstage-org/backstage-project/_git/template-repo?path=%2Ftemplate.yaml', - }); - - const tempDocsPath = await preparer.prepare(mockEntity); - expect(checkoutGitRepository).toHaveBeenCalledTimes(1); - expect(normalizePath(tempDocsPath)).toEqual( - '/tmp/backstage-repo/org/name/branch/template.yaml', - ); - }); -}); diff --git a/plugins/techdocs-backend/src/techdocs/stages/prepare/azure.ts b/plugins/techdocs-backend/src/techdocs/stages/prepare/azure.ts deleted file mode 100644 index 18e6c3c755..0000000000 --- a/plugins/techdocs-backend/src/techdocs/stages/prepare/azure.ts +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2020 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 path from 'path'; -import { Entity } from '@backstage/catalog-model'; -import { InputError } from '@backstage/backend-common'; -import { PreparerBase } from './types'; -import parseGitUrl from 'git-url-parse'; -import { - parseReferenceAnnotation, - checkoutGitRepository, -} from '../../../helpers'; - -import { Logger } from 'winston'; - -export class AzurePreparer implements PreparerBase { - private readonly logger: Logger; - - constructor(logger: Logger) { - this.logger = logger; - } - - async prepare(entity: Entity): Promise { - const { type, target } = parseReferenceAnnotation( - 'backstage.io/techdocs-ref', - entity, - ); - - if (type !== 'azure/api') { - throw new InputError(`Wrong target type: ${type}, should be 'azure/api'`); - } - - try { - const repoPath = await checkoutGitRepository(target, this.logger); - const parsedGitLocation = parseGitUrl(target); - - return path.join(repoPath, parsedGitLocation.filepath); - } catch (error) { - this.logger.debug(`Repo checkout failed with error ${error.message}`); - throw error; - } - } -} diff --git a/plugins/techdocs-backend/src/techdocs/stages/prepare/github.test.ts b/plugins/techdocs-backend/src/techdocs/stages/prepare/commonGit.test.ts similarity index 59% rename from plugins/techdocs-backend/src/techdocs/stages/prepare/github.test.ts rename to plugins/techdocs-backend/src/techdocs/stages/prepare/commonGit.test.ts index a657cd1697..cec1110f97 100644 --- a/plugins/techdocs-backend/src/techdocs/stages/prepare/github.test.ts +++ b/plugins/techdocs-backend/src/techdocs/stages/prepare/commonGit.test.ts @@ -15,7 +15,7 @@ */ import { getVoidLogger } from '@backstage/backend-common'; -import { GithubPreparer } from './github'; +import { CommonGitPreparer } from './commonGit'; import { checkoutGitRepository } from '../../../helpers'; function normalizePath(path: string) { @@ -45,9 +45,9 @@ const createMockEntity = (annotations = {}) => { const logger = getVoidLogger(); -describe('github preparer', () => { +describe('commonGit preparer', () => { it('should prepare temp docs path from github repo', async () => { - const preparer = new GithubPreparer(logger); + const preparer = new CommonGitPreparer(logger); const mockEntity = createMockEntity({ 'backstage.io/techdocs-ref': @@ -60,4 +60,34 @@ describe('github preparer', () => { '/tmp/backstage-repo/org/name/branch/plugins/techdocs-backend/examples/documented-component', ); }); + + it('should prepare temp docs path from gitlab repo', async () => { + const preparer = new CommonGitPreparer(logger); + + const mockEntity = createMockEntity({ + 'backstage.io/techdocs-ref': + 'gitlab:https://gitlab.com/xesjkeee/go-logger/blob/master/catalog-info.yaml', + }); + + const tempDocsPath = await preparer.prepare(mockEntity); + expect(checkoutGitRepository).toHaveBeenCalledTimes(2); + expect(normalizePath(tempDocsPath)).toEqual( + '/tmp/backstage-repo/org/name/branch/catalog-info.yaml', + ); + }); + + it('should prepare temp docs path from azure repo', async () => { + const preparer = new CommonGitPreparer(logger); + + const mockEntity = createMockEntity({ + 'backstage.io/techdocs-ref': + 'azure/api:https://dev.azure.com/backstage-org/backstage-project/_git/template-repo?path=%2Ftemplate.yaml', + }); + + const tempDocsPath = await preparer.prepare(mockEntity); + expect(checkoutGitRepository).toHaveBeenCalledTimes(3); + expect(normalizePath(tempDocsPath)).toEqual( + '/tmp/backstage-repo/org/name/branch/template.yaml', + ); + }); }); diff --git a/plugins/techdocs-backend/src/techdocs/stages/prepare/gitlab.ts b/plugins/techdocs-backend/src/techdocs/stages/prepare/commonGit.ts similarity index 83% rename from plugins/techdocs-backend/src/techdocs/stages/prepare/gitlab.ts rename to plugins/techdocs-backend/src/techdocs/stages/prepare/commonGit.ts index 56f3793fbb..d9ba96a031 100644 --- a/plugins/techdocs-backend/src/techdocs/stages/prepare/gitlab.ts +++ b/plugins/techdocs-backend/src/techdocs/stages/prepare/commonGit.ts @@ -15,7 +15,6 @@ */ import path from 'path'; import { Entity } from '@backstage/catalog-model'; -import { InputError } from '@backstage/backend-common'; import { PreparerBase } from './types'; import parseGitUrl from 'git-url-parse'; import { @@ -25,7 +24,7 @@ import { import { Logger } from 'winston'; -export class GitlabPreparer implements PreparerBase { +export class CommonGitPreparer implements PreparerBase { private readonly logger: Logger; constructor(logger: Logger) { @@ -33,15 +32,11 @@ export class GitlabPreparer implements PreparerBase { } async prepare(entity: Entity): Promise { - const { type, target } = parseReferenceAnnotation( + const { target } = parseReferenceAnnotation( 'backstage.io/techdocs-ref', entity, ); - if (type !== 'gitlab') { - throw new InputError(`Wrong target type: ${type}, should be 'gitlab'`); - } - try { const repoPath = await checkoutGitRepository(target, this.logger); const parsedGitLocation = parseGitUrl(target); diff --git a/plugins/techdocs-backend/src/techdocs/stages/prepare/github.ts b/plugins/techdocs-backend/src/techdocs/stages/prepare/github.ts deleted file mode 100644 index a0f91a375a..0000000000 --- a/plugins/techdocs-backend/src/techdocs/stages/prepare/github.ts +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2020 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 path from 'path'; -import { Entity } from '@backstage/catalog-model'; -import { InputError } from '@backstage/backend-common'; -import { PreparerBase } from './types'; -import parseGitUrl from 'git-url-parse'; -import { - parseReferenceAnnotation, - checkoutGitRepository, -} from '../../../helpers'; - -import { Logger } from 'winston'; - -export class GithubPreparer implements PreparerBase { - private readonly logger: Logger; - - constructor(logger: Logger) { - this.logger = logger; - } - - async prepare(entity: Entity): Promise { - const { type, target } = parseReferenceAnnotation( - 'backstage.io/techdocs-ref', - entity, - ); - - if (type !== 'github') { - throw new InputError(`Wrong target type: ${type}, should be 'github'`); - } - - try { - const repoPath = await checkoutGitRepository(target, this.logger); - - const parsedGitLocation = parseGitUrl(target); - return path.join(repoPath, parsedGitLocation.filepath); - } catch (error) { - this.logger.debug(`Repo checkout failed with error ${error.message}`); - throw error; - } - } -} diff --git a/plugins/techdocs-backend/src/techdocs/stages/prepare/gitlab.test.ts b/plugins/techdocs-backend/src/techdocs/stages/prepare/gitlab.test.ts deleted file mode 100644 index 75cad50224..0000000000 --- a/plugins/techdocs-backend/src/techdocs/stages/prepare/gitlab.test.ts +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2020 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 { getVoidLogger } from '@backstage/backend-common'; -import { GitlabPreparer } from './gitlab'; -import { checkoutGitRepository } from '../../../helpers'; - -function normalizePath(path: string) { - return path - .replace(/^[a-z]:/i, '') - .split('\\') - .join('/'); -} - -jest.mock('../../../helpers', () => ({ - ...jest.requireActual<{}>('../../../helpers'), - checkoutGitRepository: jest.fn(() => '/tmp/backstage-repo/org/name/branch'), -})); - -const createMockEntity = (annotations = {}) => { - return { - apiVersion: 'version', - kind: 'TestKind', - metadata: { - name: 'test-component-name', - annotations: { - ...annotations, - }, - }, - }; -}; - -const logger = getVoidLogger(); - -describe('gitlab preparer', () => { - it('should prepare temp docs path from gitlab repo', async () => { - const preparer = new GitlabPreparer(logger); - - // TODO: fix url repo - const mockEntity = createMockEntity({ - 'backstage.io/techdocs-ref': - 'gitlab:https://gitlab.com/xesjkeee/go-logger/blob/master/catalog-info.yaml', - }); - - const tempDocsPath = await preparer.prepare(mockEntity); - expect(checkoutGitRepository).toHaveBeenCalledTimes(1); - expect(normalizePath(tempDocsPath)).toEqual( - '/tmp/backstage-repo/org/name/branch/catalog-info.yaml', - ); - }); -}); diff --git a/plugins/techdocs-backend/src/techdocs/stages/prepare/index.ts b/plugins/techdocs-backend/src/techdocs/stages/prepare/index.ts index 249854f7ee..46e1d84c69 100644 --- a/plugins/techdocs-backend/src/techdocs/stages/prepare/index.ts +++ b/plugins/techdocs-backend/src/techdocs/stages/prepare/index.ts @@ -14,8 +14,6 @@ * limitations under the License. */ export { DirectoryPreparer } from './dir'; -export { GithubPreparer } from './github'; -export { GitlabPreparer } from './gitlab'; -export { AzurePreparer } from './azure'; +export { CommonGitPreparer } from './commonGit'; export { Preparers } from './preparers'; export type { PreparerBuilder, PreparerBase } from './types';