Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: onboarding branch cache (#21768)
Co-authored-by: Rhys Arkins <rhys@arkins.net>
- Loading branch information
1 parent
a6dfa70
commit fc0fb19
Showing
7 changed files
with
222 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { | ||
RenovateConfig, | ||
git, | ||
mocked, | ||
partial, | ||
platform, | ||
} from '../../../../../test/util'; | ||
import { REPOSITORY_CLOSED_ONBOARDING } from '../../../../constants/error-messages'; | ||
import { logger } from '../../../../logger'; | ||
import type { Pr } from '../../../../modules/platform/types'; | ||
import * as _cache from '../../../../util/cache/repository'; | ||
import { isOnboarded } from './check'; | ||
|
||
jest.mock('../../../../util/cache/repository'); | ||
jest.mock('../../../../util/git'); | ||
|
||
const cache = mocked(_cache); | ||
|
||
describe('workers/repository/onboarding/branch/check', () => { | ||
const config = partial<RenovateConfig>({ | ||
requireConfig: 'required', | ||
suppressNotifications: [], | ||
}); | ||
|
||
it('skips normal onboarding check if onboardingCache is valid', async () => { | ||
cache.getCache.mockReturnValueOnce({ | ||
onboardingBranchCache: { | ||
onboardingBranch: 'configure/renovate', | ||
defaultBranchSha: 'default-sha', | ||
onboardingBranchSha: 'onboarding-sha', | ||
}, | ||
}); | ||
git.getBranchCommit | ||
.mockReturnValueOnce('default-sha') | ||
.mockReturnValueOnce('onboarding-sha'); | ||
const res = await isOnboarded(config); | ||
expect(res).toBeFalse(); | ||
expect(logger.debug).toHaveBeenCalledWith( | ||
'Onboarding cache is valid. Repo is not onboarded' | ||
); | ||
}); | ||
|
||
it('continues with normal logic if onboardingCache is invalid', async () => { | ||
cache.getCache.mockReturnValueOnce({ | ||
onboardingBranchCache: { | ||
onboardingBranch: 'configure/renovate', | ||
defaultBranchSha: 'default-sha', | ||
onboardingBranchSha: 'onboarding-sha', | ||
}, | ||
}); | ||
git.getFileList.mockResolvedValue([]); | ||
await isOnboarded(config); | ||
expect(logger.debug).not.toHaveBeenCalledWith( | ||
'Onboarding cache is valid. Repo is not onboarded' | ||
); | ||
}); | ||
|
||
it('continues with normal logic if closedPr exists', async () => { | ||
cache.getCache.mockReturnValue({}); | ||
platform.findPr.mockResolvedValue(partial<Pr>()); | ||
git.getFileList.mockResolvedValue([]); | ||
await expect(isOnboarded(config)).rejects.toThrow( | ||
REPOSITORY_CLOSED_ONBOARDING | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
lib/workers/repository/onboarding/branch/onboarding-branch-cache.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { mocked } from '../../../../../test/util'; | ||
import * as _cache from '../../../../util/cache/repository'; | ||
import type { RepoCacheData } from '../../../../util/cache/repository/types'; | ||
import { | ||
deleteOnboardingCache, | ||
setOnboardingCache, | ||
} from './onboarding-branch-cache'; | ||
|
||
jest.mock('../../../../util/cache/repository'); | ||
const cache = mocked(_cache); | ||
|
||
describe('workers/repository/onboarding/branch/onboarding-branch-cache', () => { | ||
it('sets new cache', () => { | ||
const dummyCache = {} satisfies RepoCacheData; | ||
cache.getCache.mockReturnValueOnce(dummyCache); | ||
setOnboardingCache('configure/renovate', 'default-sha', 'onboarding-sha'); | ||
expect(dummyCache).toEqual({ | ||
onboardingBranchCache: { | ||
onboardingBranch: 'configure/renovate', | ||
defaultBranchSha: 'default-sha', | ||
onboardingBranchSha: 'onboarding-sha', | ||
}, | ||
}); | ||
}); | ||
|
||
it('updates old cache', () => { | ||
const dummyCache = { | ||
onboardingBranchCache: { | ||
onboardingBranch: 'configure/renovate', | ||
defaultBranchSha: 'default-sha', | ||
onboardingBranchSha: 'onboarding-sha', | ||
}, | ||
} satisfies RepoCacheData; | ||
cache.getCache.mockReturnValueOnce(dummyCache); | ||
setOnboardingCache( | ||
'configure/renovate', | ||
'default-sha-1', | ||
'onboarding-sha-1' | ||
); | ||
expect(dummyCache).toEqual({ | ||
onboardingBranchCache: { | ||
onboardingBranch: 'configure/renovate', | ||
defaultBranchSha: 'default-sha-1', | ||
onboardingBranchSha: 'onboarding-sha-1', | ||
}, | ||
}); | ||
}); | ||
|
||
it('deletes cache', () => { | ||
const dummyCache = { | ||
onboardingBranchCache: { | ||
onboardingBranch: 'configure/renovate', | ||
defaultBranchSha: 'default-sha', | ||
onboardingBranchSha: 'onboarding-sha', | ||
}, | ||
} satisfies RepoCacheData; | ||
cache.getCache.mockReturnValueOnce(dummyCache); | ||
deleteOnboardingCache(); | ||
expect(dummyCache.onboardingBranchCache).toBeUndefined(); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
lib/workers/repository/onboarding/branch/onboarding-branch-cache.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { logger } from '../../../../logger'; | ||
import { getCache } from '../../../../util/cache/repository'; | ||
|
||
export function setOnboardingCache( | ||
onboardingBranch: string, | ||
defaultBranchSha: string, | ||
onboardingBranchSha: string | ||
): void { | ||
const cache = getCache(); | ||
const onboardingCache = { | ||
onboardingBranch, | ||
defaultBranchSha, | ||
onboardingBranchSha, | ||
}; | ||
if (cache.onboardingBranchCache) { | ||
logger.debug('Update Onboarding Cache'); | ||
} else { | ||
logger.debug('Create Onboarding Cache'); | ||
} | ||
cache.onboardingBranchCache = onboardingCache; | ||
} | ||
|
||
export function deleteOnboardingCache(): void { | ||
const cache = getCache(); | ||
|
||
if (cache?.onboardingBranchCache) { | ||
logger.debug('Delete Onboarding Cache'); | ||
delete cache.onboardingBranchCache; | ||
} | ||
} |