TechDocs: Fix building if you already have a corrupt git tmp folder (#2457)
* Fix building if you already have a corrupt git tmp folder * Logging the error we get before trying to recover
This commit is contained in:
committed by
GitHub
parent
9888418d41
commit
398d7eb91e
@@ -24,6 +24,7 @@ import defaultBranch from 'default-branch';
|
||||
import { Entity } from '@backstage/catalog-model';
|
||||
import { InputError } from '@backstage/backend-common';
|
||||
import { RemoteProtocol } from './techdocs/stages/prepare/types';
|
||||
import { Logger } from 'winston';
|
||||
|
||||
export type ParsedLocationAnnotation = {
|
||||
type: RemoteProtocol;
|
||||
@@ -110,6 +111,7 @@ export const getGitHubRepositoryTempFolder = async (
|
||||
|
||||
export const checkoutGithubRepository = async (
|
||||
repoUrl: string,
|
||||
logger: Logger,
|
||||
): Promise<string> => {
|
||||
const parsedGitLocation = parseGitUrl(repoUrl);
|
||||
const repositoryTmpPath = await getGitHubRepositoryTempFolder(repoUrl);
|
||||
@@ -119,14 +121,23 @@ export const checkoutGithubRepository = async (
|
||||
const token = process.env.GITHUB_PRIVATE_TOKEN || '';
|
||||
|
||||
if (fs.existsSync(repositoryTmpPath)) {
|
||||
const repository = await Repository.open(repositoryTmpPath);
|
||||
const currentBranchName = (await repository.getCurrentBranch()).shorthand();
|
||||
await repository.fetch('origin');
|
||||
await repository.mergeBranches(
|
||||
currentBranchName,
|
||||
`origin/${currentBranchName}`,
|
||||
);
|
||||
return repositoryTmpPath;
|
||||
try {
|
||||
const repository = await Repository.open(repositoryTmpPath);
|
||||
const currentBranchName = (
|
||||
await repository.getCurrentBranch()
|
||||
).shorthand();
|
||||
await repository.fetch('origin');
|
||||
await repository.mergeBranches(
|
||||
currentBranchName,
|
||||
`origin/${currentBranchName}`,
|
||||
);
|
||||
return repositoryTmpPath;
|
||||
} catch (e) {
|
||||
logger.info(
|
||||
`Found error "${e.message}" in cached repository "${repoUrl}" when getting latest changes. Removing cached repository.`,
|
||||
);
|
||||
fs.removeSync(repositoryTmpPath);
|
||||
}
|
||||
}
|
||||
|
||||
if (user && token) {
|
||||
@@ -143,8 +154,12 @@ export const checkoutGithubRepository = async (
|
||||
|
||||
export const getLastCommitTimestamp = async (
|
||||
repositoryUrl: string,
|
||||
logger: Logger,
|
||||
): Promise<number> => {
|
||||
const repositoryLocation = await checkoutGithubRepository(repositoryUrl);
|
||||
const repositoryLocation = await checkoutGithubRepository(
|
||||
repositoryUrl,
|
||||
logger,
|
||||
);
|
||||
|
||||
const repository = await Repository.open(repositoryLocation);
|
||||
const commit = await repository.getReferenceCommit('HEAD');
|
||||
|
||||
@@ -110,7 +110,7 @@ export class DocsBuilder {
|
||||
|
||||
// Should probably be broken out and handled per type later. Doing this for now since we only support github age checks
|
||||
if (type === 'github') {
|
||||
const lastCommit = await getLastCommitTimestamp(target);
|
||||
const lastCommit = await getLastCommitTimestamp(target, this.logger);
|
||||
const storageTimeStamp = buildMetadataStorage.getTimestamp();
|
||||
|
||||
// Check if documentation source is newer than what we have
|
||||
|
||||
@@ -43,7 +43,10 @@ export class DirectoryPreparer implements PreparerBase {
|
||||
switch (type) {
|
||||
case 'github': {
|
||||
const parsedGitLocation = parseGitUrl(target);
|
||||
const repoLocation = await checkoutGithubRepository(target);
|
||||
const repoLocation = await checkoutGithubRepository(
|
||||
target,
|
||||
this.logger,
|
||||
);
|
||||
|
||||
return path.dirname(
|
||||
path.join(repoLocation, parsedGitLocation.filepath),
|
||||
|
||||
@@ -43,7 +43,7 @@ export class GithubPreparer implements PreparerBase {
|
||||
}
|
||||
|
||||
try {
|
||||
const repoPath = await checkoutGithubRepository(target);
|
||||
const repoPath = await checkoutGithubRepository(target, this.logger);
|
||||
|
||||
const parsedGitLocation = parseGitUrl(target);
|
||||
return path.join(repoPath, parsedGitLocation.filepath);
|
||||
|
||||
Reference in New Issue
Block a user