TechDocs: Deduplicate git clone for cache check

Signed-off-by: Himanshu Mishra <himanshu@orkohunter.net>
This commit is contained in:
Himanshu Mishra
2021-02-06 12:13:02 +01:00
parent f8a4a30fb0
commit df130db759
4 changed files with 5 additions and 16 deletions
+1 -8
View File
@@ -196,16 +196,9 @@ export const checkoutGitRepository = async (
};
export const getLastCommitTimestamp = async (
repositoryUrl: string,
config: Config,
repositoryLocation: string,
logger: Logger,
): Promise<number> => {
const repositoryLocation = await checkoutGitRepository(
repositoryUrl,
config,
logger,
);
const git = Git.fromAuth({ logger });
const sha = await git.resolveRef({ dir: repositoryLocation, ref: 'HEAD' });
const commit = await git.readCommit({ dir: repositoryLocation, sha });
@@ -29,6 +29,7 @@ function normalizePath(path: string) {
jest.mock('../../helpers', () => ({
...jest.requireActual<{}>('../../helpers'),
checkoutGitRepository: jest.fn(() => '/tmp/backstage-repo/org/name/branch'),
getLastCommitTimestamp: jest.fn(() => 12345678),
}));
const createMockEntity = (annotations = {}) => {
@@ -40,7 +40,7 @@ export class CommonGitPreparer implements PreparerBase {
options?: { etag?: string },
): Promise<PreparerResponse> {
this.logger.warn(
'You are using the legacy git preparer in TechDocs which will be removed in near future (30 days). ' +
'You are using the legacy git preparer in TechDocs which will be removed in near future (March 2021). ' +
'Migrate to URL reader by updating `backstage.io/techdocs-ref` annotation in `catalog-info.yaml` ' +
'to be prefixed with `url:`. Read the migration guide and benefits at https://github.com/backstage/backstage/issues/4409 ',
);
@@ -57,13 +57,8 @@ export class CommonGitPreparer implements PreparerBase {
this.config,
this.logger,
);
// Check if etag has changed for cache invalidation.
const etag = await getLastCommitTimestamp(
target,
this.config,
this.logger,
);
const etag = await getLastCommitTimestamp(repoPath, this.logger);
if (options?.etag === etag.toString()) {
throw new NotModifiedError();
}
@@ -70,7 +70,7 @@ export class DirectoryPreparer implements PreparerBase {
async prepare(entity: Entity): Promise<PreparerResponse> {
this.logger.warn(
'You are using the legacy dir preparer in TechDocs which will be removed in near future (30 days). ' +
'You are using the legacy dir preparer in TechDocs which will be removed in near future (March 2021). ' +
'Migrate to URL reader by updating `backstage.io/techdocs-ref` annotation in `catalog-info.yaml` ' +
'to be prefixed with `url:`. Read the migration guide and benefits at https://github.com/backstage/backstage/issues/4409 ',
);