Skip to content

Commit

Permalink
feat(internal): repository cache revision (#8782)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Feb 20, 2021
1 parent 2c9a172 commit d2a7147
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 31 deletions.
18 changes: 3 additions & 15 deletions lib/util/cache/repository/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,16 @@ describe('lib/util/cache/repository', () => {
});
});
it('reads from cache and finalizes', async () => {
fs.readFile.mockResolvedValueOnce('{"repository":"abc/def"}' as any);
await repositoryCache.initialize({
...config,
repositoryCache: 'enabled',
});
await repositoryCache.finalize();
expect(fs.readFile.mock.calls).toHaveLength(1);
expect(fs.outputFile.mock.calls).toHaveLength(1);
});
it('migrates', async () => {
fs.readFile.mockResolvedValueOnce(
'{"repository":"abc/def","branches":[{"upgrades":[{"fromVersion":"1.0.0","toVersion":"1.0.1"}]}]}' as any
`{"repository":"abc/def","revision":${repositoryCache.CACHE_REVISION}}` as any
);
await repositoryCache.initialize({
...config,
repositoryCache: 'enabled',
});
expect(repositoryCache.getCache().branches[0].upgrades[0]).toEqual({
currentVersion: '1.0.0',
newVersion: '1.0.1',
});
await repositoryCache.finalize();
expect(fs.readFile.mock.calls).toHaveLength(1);
expect(fs.outputFile.mock.calls).toHaveLength(1);
});
it('gets', () => {
expect(repositoryCache.getCache()).toEqual({ scan: {} });
Expand Down
27 changes: 11 additions & 16 deletions lib/util/cache/repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { logger } from '../../../logger';
import { PackageFile } from '../../../manager/common';
import { RepoInitConfig } from '../../../workers/repository/init/common';

// Increment this whenever there could be incompatibilities between old and new cache structure
export const CACHE_REVISION = 1;

export interface BaseBranchCache {
sha: string; // branch commit sha
configHash: string; // object hash of config
Expand Down Expand Up @@ -38,6 +41,7 @@ export interface BranchCache {
export interface Cache {
branches?: BranchCache[];
repository?: string;
revision?: number;
init?: RepoInitConfig;
scan?: Record<string, BaseBranchCache>;
}
Expand All @@ -56,7 +60,11 @@ export function getCacheFileName(config: RenovateConfig): string {
}

function validate(config: RenovateConfig, input: any): Cache | null {
if (input?.repository === config.repository) {
if (
input &&
input.repository === config.repository &&
input.revision === CACHE_REVISION
) {
logger.debug('Repository cache is valid');
return input as Cache;
}
Expand All @@ -79,27 +87,14 @@ export async function initialize(config: RenovateConfig): Promise<void> {
} catch (err) {
logger.debug({ cacheFileName }, 'Repository cache not found');
}
cache = cache || Object.create({});
cache = cache || Object.create({ revision: CACHE_REVISION });
cache.repository = config.repository;
}

export function getCache(): Cache {
cache = cache || Object.create({});
cache = cache || Object.create({ revision: CACHE_REVISION });
delete cache.init;
cache.scan = cache.scan || Object.create({});
for (const branch of cache.branches || []) {
for (const upgrade of (branch.upgrades || []) as any) {
// migrate fromVersion to currentVersion
if (upgrade.fromVersion) {
upgrade.currentVersion = upgrade.fromVersion;
delete upgrade.fromVersion;
}
if (upgrade.toVersion) {
upgrade.newVersion = upgrade.toVersion;
delete upgrade.toVersion;
}
}
}
return cache;
}

Expand Down

0 comments on commit d2a7147

Please sign in to comment.