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

refactor: improve branch cache logic #17538

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
4176d6e
update interfaces
RahulGautamSingh Aug 29, 2022
5687ce9
implement new logic
RahulGautamSingh Aug 30, 2022
5c1f03c
new logic flow for branch cache
RahulGautamSingh Aug 31, 2022
4fd6135
Merge branch 'main' into test/conflict
RahulGautamSingh Aug 31, 2022
5f98200
refactor
RahulGautamSingh Aug 31, 2022
9ea5550
disapply partial<T>
RahulGautamSingh Aug 31, 2022
6c9bddf
Merge branch 'main' into test/conflict
RahulGautamSingh Aug 31, 2022
e75af3d
refactor: concise comment
RahulGautamSingh Aug 31, 2022
738ea12
test: add more tests
RahulGautamSingh Aug 31, 2022
89faad0
refactor: use baseBranchName
RahulGautamSingh Aug 31, 2022
cb3de5b
refactor: update comments
RahulGautamSingh Aug 31, 2022
d8a6668
fix : lint error
RahulGautamSingh Aug 31, 2022
3466ef5
Merge branch 'main' into test/conflict
RahulGautamSingh Aug 31, 2022
e31803e
test: cover gitConflict removal
RahulGautamSingh Sep 1, 2022
13aa84d
refactor: cache-conflict code
RahulGautamSingh Sep 1, 2022
fd835d3
use truthy checks instead of falsy
RahulGautamSingh Sep 1, 2022
3c6be15
Merge branch 'main' into test/conflict
RahulGautamSingh Sep 1, 2022
0f29ba4
test: add set-branch-sha.spec.ts
RahulGautamSingh Sep 1, 2022
4d72d3f
Update lib/util/git/conflicts-cache.ts
RahulGautamSingh Sep 2, 2022
436b53d
feat: update isBranchBehindBase cache-logic
RahulGautamSingh Sep 2, 2022
529b0aa
fix: lint issue
RahulGautamSingh Sep 2, 2022
2683125
fix coverage
RahulGautamSingh Sep 2, 2022
fd5a44f
Merge branch 'main' into test/conflict
RahulGautamSingh Sep 2, 2022
b1aad2c
remove only
RahulGautamSingh Sep 2, 2022
f5e341e
apply suggestions
RahulGautamSingh Sep 4, 2022
608d343
fix coverage
RahulGautamSingh Sep 4, 2022
4b7bf77
fix coverage
RahulGautamSingh Sep 4, 2022
91c490d
Merge branch 'renovatebot:main' into test/conflict
RahulGautamSingh Sep 5, 2022
19f182a
add test for parent-sha-cache
RahulGautamSingh Sep 5, 2022
f60c542
Merge branch 'test/conflict' of https://github.com/RahulGautamSingh/r…
RahulGautamSingh Sep 5, 2022
0ad539e
fix failing tests
RahulGautamSingh Sep 5, 2022
da9d9a0
add baseBranch arg to isBranchBehindBase()
RahulGautamSingh Sep 5, 2022
f4e68df
Merge branch 'main' into test/conflict
RahulGautamSingh Sep 6, 2022
599da96
Merge branch 'main' into test/conflict
RahulGautamSingh Sep 7, 2022
632419c
update logic as per linked issue
RahulGautamSingh Sep 7, 2022
79ba5a4
pass correct argument
RahulGautamSingh Sep 7, 2022
197dd7f
update tests to fix coverage and handle new logic
RahulGautamSingh Sep 7, 2022
66a88e8
Merge branch 'main' into test/conflict
RahulGautamSingh Sep 7, 2022
817cbc3
fix coverage issue
RahulGautamSingh Sep 7, 2022
5c7aef1
Merge branch 'test/conflict' of https://github.com/RahulGautamSingh/r…
RahulGautamSingh Sep 7, 2022
badcf61
Merge branch 'main' into test/conflict
RahulGautamSingh Sep 7, 2022
7164d52
remove code that creates branch in setX fns
RahulGautamSingh Sep 9, 2022
82330d9
refactor: comments
RahulGautamSingh Sep 9, 2022
90d25b2
test: update expected log
RahulGautamSingh Sep 9, 2022
b063881
refactor: convert to pure fns
RahulGautamSingh Sep 10, 2022
54b2d65
refactor: tests + modified-cache logic flow + log msgs
RahulGautamSingh Sep 10, 2022
039dfbe
Merge branch 'main' into test/conflict
RahulGautamSingh Sep 10, 2022
67ef0d0
reactor: create branch-cache regardless of repositoryCache value
RahulGautamSingh Sep 14, 2022
c98ce6a
Merge branch 'main' into test/conflict
RahulGautamSingh Sep 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/modules/manager/sbt/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,12 @@ export function extractPackageFile(
let parsedResult: Ctx | null = null;

try {
parsedResult = scala.query(content, query, {
vars: {},
deps: [],
registryUrls: [MAVEN_REPO],
});
parsedResult =
scala.query(content, query, {
vars: {},
deps: [],
registryUrls: [MAVEN_REPO],
}) ?? null;
} catch (err) /* istanbul ignore next */ {
logger.warn({ err }, 'Sbt parsing error');
}
Expand Down
10 changes: 7 additions & 3 deletions lib/util/cache/repository/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ export interface BranchUpgradeCache {

export interface BranchCache {
automerge: boolean;
baseBranchName: string;
baseBranchSha: string | null;
branchName: string;
isModified: boolean;
branchFingerprint?: string;
isBehindBaseBranch?: boolean;
isConflicted?: boolean;
isModified?: boolean;
parentSha?: string | null;
prNo: number | null;
sha: string | null;
parentSha: string | null;
upgrades: BranchUpgradeCache[];
branchFingerprint?: string;
}

export interface RepoCacheData {
Expand Down
98 changes: 80 additions & 18 deletions lib/util/git/behind-base-branch-cache.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { mocked, partial } from '../../../test/util';
import * as _repositoryCache from '../cache/repository';
import type { BranchCache, RepoCacheData } from '../cache/repository/types';
import { getCachedBehindBaseResult } from './behind-base-branch-cache';
import {
getCachedBehindBaseResult,
setCachedBehindBaseResult,
} from './behind-base-branch-cache';
import * as _git from '.';

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

describe('util/git/behind-base-branch-cache', () => {
let repoCache: RepoCacheData = {};
Expand All @@ -16,36 +22,92 @@ describe('util/git/behind-base-branch-cache', () => {

describe('getCachedBehindBaseResult', () => {
it('returns null if cache is not populated', () => {
expect(getCachedBehindBaseResult('foo', '111')).toBeNull();
expect(
getCachedBehindBaseResult('foo', 'SHA', 'base_foo', 'base_SHA')
).toBeNull();
});

it('returns null if branch not found', () => {
expect(getCachedBehindBaseResult('foo', '111')).toBeNull();
const branchCache = partial<BranchCache>({
branchName: 'not_foo',
isModified: false,
});
repoCache = { branches: [branchCache] };
expect(
getCachedBehindBaseResult('foo', 'SHA', 'base_foo', 'base_SHA')
).toBeNull();
});

it('returns null if cache is partially defined', () => {
const branchName = 'branchName';
it('returns null if isBehindBaseBranch is undefined', () => {
const branchCache = partial<BranchCache>({
branchName,
branchName: 'foo',
sha: 'SHA',
baseBranchName: 'base_foo',
isModified: false,
});
const repoCache: RepoCacheData = { branches: [branchCache] };
repoCache = { branches: [branchCache] };
repositoryCache.getCache.mockReturnValue(repoCache);
expect(getCachedBehindBaseResult(branchName, '111')).toBeNull();
expect(
getCachedBehindBaseResult('foo', 'SHA', 'base_foo', 'base_SHA')
).toBeNull();
});

it('returns true if target SHA has changed', () => {
repoCache.branches = [
{ branchName: 'foo', sha: 'aaa', parentSha: '222' } as BranchCache,
];
expect(getCachedBehindBaseResult('foo', '111')).toBeTrue();
it('returns value', () => {
const branchCache = partial<BranchCache>({
branchName: 'foo',
sha: 'SHA',
baseBranchSha: 'base_SHA',
baseBranchName: 'base_foo',
isBehindBaseBranch: false,
});
repoCache = { branches: [branchCache] };
repositoryCache.getCache.mockReturnValue(repoCache);
expect(
getCachedBehindBaseResult('foo', 'SHA', 'base_foo', 'base_SHA')
).toBeFalse();
});
});

describe('setCachedBehindBaseResult', () => {
it('returns if branch not found', () => {
git.getBranchCommit.mockReturnValueOnce('SHA');
setCachedBehindBaseResult('foo', false);
expect(repoCache).toMatchObject({});
});

it('returns false if target SHA has not changed', () => {
repoCache.branches = [
{ branchName: 'foo', sha: 'aaa', parentSha: '111' } as BranchCache,
];
expect(getCachedBehindBaseResult('foo', '111')).toBeFalse();
it('handles multiple branches', () => {
repoCache = {
branches: [
partial<BranchCache>({
branchName: 'foo',
isBehindBaseBranch: true,
baseBranchName: 'base_foo',
sha: 'SHA',
baseBranchSha: 'base_SHA',
}),
partial<BranchCache>({
branchName: 'bar',
isBehindBaseBranch: false,
}),
],
};
repositoryCache.getCache.mockReturnValue(repoCache);
setCachedBehindBaseResult('foo', false);
expect(repoCache).toMatchObject({
branches: [
{
branchName: 'foo',
isBehindBaseBranch: false,
baseBranchName: 'base_foo',
sha: 'SHA',
baseBranchSha: 'base_SHA',
},
{
branchName: 'bar',
isBehindBaseBranch: false,
},
],
});
});
});
});
36 changes: 28 additions & 8 deletions lib/util/git/behind-base-branch-cache.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
import { getCache } from '../cache/repository';

// Compare cached parent Sha of a branch to the fetched base-branch sha to determine whether the branch is behind the base
// Since cache is updated after each run, this will be sufficient to determine whether a branch is behind its parent.
export function getCachedBehindBaseResult(
branchName: string,
currentBaseBranchSha: string
branchSha: string,
baseBranchName: string,
baseBranchSha: string
): boolean | null {
const cache = getCache();
const { branches = [] } = cache;
const cachedBranch = branches?.find(
const branch = cache.branches?.find(
(branch) => branch.branchName === branchName
);

if (!cachedBranch?.parentSha) {
return null;
if (
branch &&
branch.baseBranchName === baseBranchName &&
branch.baseBranchSha === baseBranchSha &&
branch.sha === branchSha &&
branch.isBehindBaseBranch !== undefined
) {
return branch.isBehindBaseBranch;
}
return null;
}

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

if (!branch) {
return;
}

return currentBaseBranchSha !== cachedBranch.parentSha;
branch.isBehindBaseBranch = isBehind;
}
Loading