diff --git a/plugins/techdocs-backend/src/techdocs/stages/prepare/github.test.ts b/plugins/techdocs-backend/src/techdocs/stages/prepare/github.test.ts new file mode 100644 index 0000000000..0d29d9c7be --- /dev/null +++ b/plugins/techdocs-backend/src/techdocs/stages/prepare/github.test.ts @@ -0,0 +1,56 @@ +/* + * 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 { GithubPreparer } from './github'; +import { checkoutGitRepository } from './helpers'; + +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('github preparer', () => { + it('should prepare temp docs path from github repo', async () => { + const preparer = new GithubPreparer(logger); + + const mockEntity = createMockEntity({ + 'backstage.io/techdocs-ref': + 'github:https://github.com/spotify/backstage/blob/master/plugins/techdocs-backend/examples/documented-component', + }); + + const tempDocsPath = await preparer.prepare(mockEntity); + expect(checkoutGitRepository).toHaveBeenCalledTimes(1); + expect(tempDocsPath).toEqual( + '/tmp/backstage-repo/org/name/branch/plugins/techdocs-backend/examples/documented-component', + ); + }); +}); diff --git a/plugins/techdocs-backend/src/techdocs/stages/prepare/helpers.ts b/plugins/techdocs-backend/src/techdocs/stages/prepare/helpers.ts index 05030390a9..b2f04bbbbf 100644 --- a/plugins/techdocs-backend/src/techdocs/stages/prepare/helpers.ts +++ b/plugins/techdocs-backend/src/techdocs/stages/prepare/helpers.ts @@ -17,7 +17,7 @@ import { Entity } from '@backstage/catalog-model'; import { InputError } from '@backstage/backend-common'; import { RemoteProtocol } from './types'; import parseGitUrl from 'git-url-parse'; -import { Clone } from 'nodegit'; +import { Clone, Repository } from 'nodegit'; import fs from 'fs-extra'; import os from 'os'; import path from 'path'; @@ -78,8 +78,14 @@ export const checkoutGitRepository = async ( ); if (fs.existsSync(repositoryTmpPath)) { + const repository = await Repository.open(repositoryTmpPath); + await repository.mergeBranches( + parsedGitLocation.ref, + `origin/${parsedGitLocation.ref}`, + ); return repositoryTmpPath; } + const repositoryCheckoutUrl = parsedGitLocation.toString('https'); fs.mkdirSync(repositoryTmpPath, { recursive: true });