Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pristine in branchCache #18478

Merged
merged 18 commits into from Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/util/cache/repository/types.ts
Expand Up @@ -56,9 +56,9 @@ export interface BranchCache {
*/
isModified?: boolean;
/**
* Parent commit of branch sha
*
*/
parentSha?: string | null;
pristine?: boolean;
/**
* Pr nunber of PR created from this branch
*/
Expand Down
4 changes: 0 additions & 4 deletions lib/util/git/index.spec.ts
Expand Up @@ -11,21 +11,18 @@ import { newlineRegex, regEx } from '../regex';
import * as _behindBaseCache from './behind-base-branch-cache';
import * as _conflictsCache from './conflicts-cache';
import * as _modifiedCache from './modified-cache';
import * as _parentShaCache from './parent-sha-cache';
import type { FileChange } from './types';
import * as git from '.';
import { setNoVerify } from '.';

jest.mock('./conflicts-cache');
jest.mock('./behind-base-branch-cache');
jest.mock('./modified-cache');
jest.mock('./parent-sha-cache');
jest.mock('delay');
jest.mock('../cache/repository');
const behindBaseCache = mocked(_behindBaseCache);
const conflictsCache = mocked(_conflictsCache);
const modifiedCache = mocked(_modifiedCache);
const parentShaCache = mocked(_parentShaCache);
// Class is no longer exported
const SimpleGit = Git().constructor as { prototype: ReturnType<typeof Git> };

Expand Down Expand Up @@ -116,7 +113,6 @@ describe('util/git/index', () => {
// override some local git settings for better testing
const local = Git(tmpDir.path);
await local.addConfig('commit.gpgsign', 'false');
parentShaCache.getCachedBranchParentShaResult.mockReturnValue(null);
behindBaseCache.getCachedBehindBaseResult.mockReturnValue(null);
});

Expand Down
2 changes: 0 additions & 2 deletions lib/util/git/index.ts
Expand Up @@ -45,7 +45,6 @@ import {
getCachedModifiedResult,
setCachedModifiedResult,
} from './modified-cache';
import { deleteCachedBranchParentShaResult } from './parent-sha-cache';
import { configSigningKey, writePrivateKey } from './private-key';
import type {
CommitFilesConfig,
Expand Down Expand Up @@ -663,7 +662,6 @@ export async function isBranchModified(branchName: string): Promise<boolean> {
);
config.branchIsModified[branchName] = true;
setCachedModifiedResult(branchName, true);
deleteCachedBranchParentShaResult(branchName);
return true;
}

Expand Down
88 changes: 0 additions & 88 deletions lib/util/git/parent-sha-cache.spec.ts

This file was deleted.

24 changes: 0 additions & 24 deletions lib/util/git/parent-sha-cache.ts

This file was deleted.

37 changes: 37 additions & 0 deletions lib/util/git/pristine-cache.spec.ts
@@ -0,0 +1,37 @@
import { mocked, partial } from '../../../test/util';
import * as _repositoryCache from '../cache/repository';
import type { BranchCache, RepoCacheData } from '../cache/repository/types';
import { getCachedPristineResult } from './pristine-cache';

jest.mock('../cache/repository');
const repositoryCache = mocked(_repositoryCache);

describe('util/git/pristine-cache', () => {
let repoCache: RepoCacheData = {};

beforeEach(() => {
repoCache = {};
repositoryCache.getCache.mockReturnValue(repoCache);
});

describe('getCachedPristineResult', () => {
it('returns false if cache is not populated', () => {
expect(getCachedPristineResult('foo')).toBeFalse();
});

it('returns false if branch not found', () => {
repoCache.branches = [partial<BranchCache>({ branchName: 'not_foo' })];
expect(getCachedPristineResult('foo')).toBeFalse();
});

it('returns true', () => {
repoCache.branches = [
partial<BranchCache>({
branchName: 'foo',
pristine: true,
}),
];
expect(getCachedPristineResult('foo')).toBeTrue();
});
});
});
10 changes: 10 additions & 0 deletions lib/util/git/pristine-cache.ts
@@ -0,0 +1,10 @@
import { getCache } from '../cache/repository';

export function getCachedPristineResult(branchName: string): boolean {
const cache = getCache();
const branch = cache.branches?.find(
(branch) => branch.branchName === branchName
);

return branch?.pristine ?? false;
}
10 changes: 5 additions & 5 deletions lib/util/git/set-branch-commit.spec.ts
Expand Up @@ -16,7 +16,7 @@ describe('util/git/set-branch-commit', () => {
});

describe('setBranchCommit', () => {
it('sets new branch in cache if it doesn not exist', () => {
it('sets new branch in cache if it does not exist', () => {
git.getBranchCommit.mockReturnValueOnce('base_SHA');
setBranchNewCommit('branch_name', 'base_branch', 'SHA');
expect(logger.logger.debug).toHaveBeenCalledWith(
Expand All @@ -30,7 +30,7 @@ describe('util/git/set-branch-commit', () => {
baseBranchSha: 'base_SHA',
isBehindBase: false,
isModified: false,
parentSha: 'base_SHA',
pristine: true,
},
]);
});
Expand All @@ -44,8 +44,8 @@ describe('util/git/set-branch-commit', () => {
sha: 'SHA',
baseBranchSha: 'base_SHA',
isBehindBase: false,
isModified: false,
parentSha: 'base_SHA',
isModified: true,
pristine: false,
}),
],
};
Expand All @@ -60,7 +60,7 @@ describe('util/git/set-branch-commit', () => {
baseBranchSha: 'base_SHA',
isBehindBase: false,
isModified: false,
parentSha: 'base_SHA',
pristine: true,
},
]);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/util/git/set-branch-commit.ts
Expand Up @@ -32,5 +32,5 @@ export function setBranchNewCommit(
branch.baseBranchSha = baseBranchSha;
branch.isBehindBase = false;
branch.isModified = false;
branch.parentSha = baseBranchSha;
branch.pristine = true;
}
7 changes: 3 additions & 4 deletions lib/workers/repository/cache.ts
Expand Up @@ -12,7 +12,7 @@ import {
isBranchBehindBase,
isBranchModified,
} from '../../util/git';
import { getCachedBranchParentShaResult } from '../../util/git/parent-sha-cache';
import { getCachedPristineResult } from '../../util/git/pristine-cache';
import type { BranchConfig, BranchUpgradeConfig } from '../types';

function generateBranchUpgradeCache(
Expand Down Expand Up @@ -52,12 +52,11 @@ async function generateBranchCache(
try {
const sha = getBranchCommit(branchName) ?? null;
const baseBranchSha = getBranchCommit(baseBranch);
const pristine = getCachedPristineResult(branchName);
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
let prNo = null;
let parentSha = null;
let isModified = false;
let isBehindBase = false;
if (sha) {
parentSha = getCachedBranchParentShaResult(branchName, sha);
const branchPr = await platform.getBranchPr(branchName);
if (branchPr) {
prNo = branchPr.number;
Expand All @@ -78,7 +77,7 @@ async function generateBranchCache(
branchName,
isBehindBase,
isModified,
parentSha,
pristine,
prNo,
sha,
upgrades,
Expand Down
15 changes: 7 additions & 8 deletions lib/workers/repository/process/write.spec.ts
Expand Up @@ -294,7 +294,6 @@ describe('workers/repository/process/write', () => {
upgrades: [],
automerge: false,
prNo: null,
parentSha: null,
};

it('returns false if no cache', () => {
Expand Down Expand Up @@ -341,7 +340,7 @@ describe('workers/repository/process/write', () => {
});
});

it('when base branch name is different updates it and invalidates isModified value', () => {
it('when base branch name is different updates it and invalidates related cache', () => {
const repoCacheObj: RepoCacheData = {
branches: [
{
Expand All @@ -350,10 +349,10 @@ describe('workers/repository/process/write', () => {
sha: 'sha',
baseBranchSha: 'base_sha',
isModified: true,
pristine: false,
upgrades: [],
automerge: false,
prNo: null,
parentSha: null,
},
],
};
Expand All @@ -365,10 +364,10 @@ describe('workers/repository/process/write', () => {
sha: 'sha',
baseBranch: 'new_base_branch',
baseBranchSha: 'base_sha',
pristine: false,
upgrades: [],
automerge: false,
prNo: null,
parentSha: null,
});
});

Expand All @@ -381,10 +380,10 @@ describe('workers/repository/process/write', () => {
baseBranch: 'base_branch',
baseBranchSha: 'base_sha',
isBehindBase: true,
pristine: false,
upgrades: [],
automerge: false,
prNo: null,
parentSha: null,
},
],
};
Expand All @@ -397,9 +396,9 @@ describe('workers/repository/process/write', () => {
baseBranch: 'base_branch',
baseBranchSha: 'new_base_sha',
upgrades: [],
pristine: false,
automerge: false,
prNo: null,
parentSha: null,
});
});

Expand All @@ -413,11 +412,11 @@ describe('workers/repository/process/write', () => {
baseBranchSha: 'base_sha',
isBehindBase: true,
isModified: true,
pristine: true,
branchFingerprint: '123',
upgrades: [],
automerge: false,
prNo: null,
parentSha: null,
},
],
};
Expand All @@ -430,9 +429,9 @@ describe('workers/repository/process/write', () => {
baseBranch: 'base_branch',
baseBranchSha: 'base_sha',
upgrades: [],
pristine: false,
automerge: false,
prNo: null,
parentSha: null,
});
});
});
Expand Down