Skip to content

Commit a027e63

Browse files
authored
Merge latest changes from origin before rebuilding docs (#2117)
* Added missing tests and make sure we merge latest changes before building docs from a repo * Removed flaky test
1 parent 2e0ede6 commit a027e63

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2020 Spotify AB
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { getVoidLogger } from '@backstage/backend-common';
18+
import { GithubPreparer } from './github';
19+
import { checkoutGitRepository } from './helpers';
20+
21+
jest.mock('./helpers', () => ({
22+
...jest.requireActual<{}>('./helpers'),
23+
checkoutGitRepository: jest.fn(() => '/tmp/backstage-repo/org/name/branch'),
24+
}));
25+
26+
const createMockEntity = (annotations = {}) => {
27+
return {
28+
apiVersion: 'version',
29+
kind: 'TestKind',
30+
metadata: {
31+
name: 'test-component-name',
32+
annotations: {
33+
...annotations,
34+
},
35+
},
36+
};
37+
};
38+
39+
const logger = getVoidLogger();
40+
41+
describe('github preparer', () => {
42+
it('should prepare temp docs path from github repo', async () => {
43+
const preparer = new GithubPreparer(logger);
44+
45+
const mockEntity = createMockEntity({
46+
'backstage.io/techdocs-ref':
47+
'github:https://github.com/spotify/backstage/blob/master/plugins/techdocs-backend/examples/documented-component',
48+
});
49+
50+
const tempDocsPath = await preparer.prepare(mockEntity);
51+
expect(checkoutGitRepository).toHaveBeenCalledTimes(1);
52+
expect(tempDocsPath).toEqual(
53+
'/tmp/backstage-repo/org/name/branch/plugins/techdocs-backend/examples/documented-component',
54+
);
55+
});
56+
});

plugins/techdocs-backend/src/techdocs/stages/prepare/helpers.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { Entity } from '@backstage/catalog-model';
1717
import { InputError } from '@backstage/backend-common';
1818
import { RemoteProtocol } from './types';
1919
import parseGitUrl from 'git-url-parse';
20-
import { Clone } from 'nodegit';
20+
import { Clone, Repository } from 'nodegit';
2121
import fs from 'fs-extra';
2222
import os from 'os';
2323
import path from 'path';
@@ -78,8 +78,14 @@ export const checkoutGitRepository = async (
7878
);
7979

8080
if (fs.existsSync(repositoryTmpPath)) {
81+
const repository = await Repository.open(repositoryTmpPath);
82+
await repository.mergeBranches(
83+
parsedGitLocation.ref,
84+
`origin/${parsedGitLocation.ref}`,
85+
);
8186
return repositoryTmpPath;
8287
}
88+
8389
const repositoryCheckoutUrl = parsedGitLocation.toString('https');
8490

8591
fs.mkdirSync(repositoryTmpPath, { recursive: true });

0 commit comments

Comments
 (0)