From ef8c4ac1c1e9d8d4f8136a8538e020132499efd8 Mon Sep 17 00:00:00 2001 From: RahulGautamSingh <rahultesnik@gmail.com> Date: Fri, 21 Oct 2022 18:50:51 +0530 Subject: [PATCH] fix: parentSha logic (#18331) --- lib/util/cache/repository/types.ts | 2 +- lib/util/git/index.spec.ts | 17 ----------------- lib/util/git/index.ts | 26 ++------------------------ lib/util/git/parent-sha-cache.spec.ts | 19 ++++++++++++++++++- lib/util/git/parent-sha-cache.ts | 9 +++++++++ lib/workers/repository/cache.ts | 4 ++-- 6 files changed, 32 insertions(+), 45 deletions(-) diff --git a/lib/util/cache/repository/types.ts b/lib/util/cache/repository/types.ts index c08081a3e3..2512773be5 100644 --- a/lib/util/cache/repository/types.ts +++ b/lib/util/cache/repository/types.ts @@ -58,7 +58,7 @@ export interface BranchCache { /** * Parent commit of branch sha */ - parentSha: string | null; + parentSha?: string | null; /** * Pr nunber of PR created from this branch */ diff --git a/lib/util/git/index.spec.ts b/lib/util/git/index.spec.ts index 2bb96cad02..46bf23e451 100644 --- a/lib/util/git/index.spec.ts +++ b/lib/util/git/index.spec.ts @@ -315,23 +315,6 @@ describe('util/git/index', () => { }); }); - describe('getBranchParentSha(branchName)', () => { - it('should return sha if found', async () => { - const parentSha = await git.getBranchParentSha('renovate/future_branch'); - expect(parentSha).toHaveLength(40); - expect(parentSha).toEqual(git.getBranchCommit(defaultBranch)); - }); - - it('should return null if not found', async () => { - expect(await git.getBranchParentSha('not_found')).toBeNull(); - }); - - it('should return cached value', async () => { - parentShaCache.getCachedBranchParentShaResult.mockReturnValueOnce('111'); - expect(await git.getBranchParentSha('not_found')).toBe('111'); - }); - }); - describe('getBranchFiles(branchName)', () => { it('detects changed files compared to current base branch', async () => { const file: FileChange = { diff --git a/lib/util/git/index.ts b/lib/util/git/index.ts index df78425ec8..860e1be2fc 100644 --- a/lib/util/git/index.ts +++ b/lib/util/git/index.ts @@ -45,7 +45,7 @@ import { getCachedModifiedResult, setCachedModifiedResult, } from './modified-cache'; -import { getCachedBranchParentShaResult } from './parent-sha-cache'; +import { deleteCachedBranchParentShaResult } from './parent-sha-cache'; import { configSigningKey, writePrivateKey } from './private-key'; import type { CommitFilesConfig, @@ -490,29 +490,6 @@ export function getBranchCommit(branchName: string): CommitSha | null { return config.branchCommits[branchName] || null; } -// Return the parent commit SHA for a branch -export async function getBranchParentSha( - branchName: string -): Promise<CommitSha | null> { - const branchSha = getBranchCommit(branchName); - let parentSha = getCachedBranchParentShaResult(branchName, branchSha); - if (parentSha !== null) { - logger.debug( - `branch.getBranchParentSha(): using cached result "${parentSha}"` - ); - return parentSha; - } - - try { - // TODO: branchSha can be null (#7154) - parentSha = await git.revparse([`${branchSha!}^`]); - return parentSha; - } catch (err) { - logger.debug({ err }, 'Error getting branch parent sha'); - return null; - } -} - export async function getCommitMessages(): Promise<string[]> { await syncGit(); logger.debug('getCommitMessages'); @@ -686,6 +663,7 @@ export async function isBranchModified(branchName: string): Promise<boolean> { ); config.branchIsModified[branchName] = true; setCachedModifiedResult(branchName, true); + deleteCachedBranchParentShaResult(branchName); return true; } diff --git a/lib/util/git/parent-sha-cache.spec.ts b/lib/util/git/parent-sha-cache.spec.ts index 9e88085d83..6c7f1abfd7 100644 --- a/lib/util/git/parent-sha-cache.spec.ts +++ b/lib/util/git/parent-sha-cache.spec.ts @@ -1,7 +1,10 @@ import { mocked } from '../../../test/util'; import * as _repositoryCache from '../cache/repository'; import type { BranchCache, RepoCacheData } from '../cache/repository/types'; -import { getCachedBranchParentShaResult } from './parent-sha-cache'; +import { + deleteCachedBranchParentShaResult, + getCachedBranchParentShaResult, +} from './parent-sha-cache'; jest.mock('../cache/repository'); const repositoryCache = mocked(_repositoryCache); @@ -68,4 +71,18 @@ describe('util/git/parent-sha-cache', () => { expect(getCachedBranchParentShaResult('foo', '111')).toBe('000'); }); }); + + describe('deleteCachedBranchParentShaResult', () => { + it('returns null if cache is not populated', () => { + repoCache.branches = [ + { + branchName: 'foo', + sha: '111', + parentSha: 'sha', + } as BranchCache, + ]; + deleteCachedBranchParentShaResult('foo'); + expect(repoCache.branches[0].parentSha).toBeUndefined(); + }); + }); }); diff --git a/lib/util/git/parent-sha-cache.ts b/lib/util/git/parent-sha-cache.ts index bec5e8253e..f5842c3780 100644 --- a/lib/util/git/parent-sha-cache.ts +++ b/lib/util/git/parent-sha-cache.ts @@ -13,3 +13,12 @@ export function getCachedBranchParentShaResult( return null; } + +export function deleteCachedBranchParentShaResult(branchName: string): void { + const { branches } = getCache(); + const branch = branches?.find((branch) => branch.branchName === branchName); + + if (branch?.parentSha) { + delete branch.parentSha; + } +} diff --git a/lib/workers/repository/cache.ts b/lib/workers/repository/cache.ts index 2b76554c02..90ee99d101 100644 --- a/lib/workers/repository/cache.ts +++ b/lib/workers/repository/cache.ts @@ -9,10 +9,10 @@ import type { } from '../../util/cache/repository/types'; import { getBranchCommit, - getBranchParentSha, isBranchBehindBase, isBranchModified, } from '../../util/git'; +import { getCachedBranchParentShaResult } from '../../util/git/parent-sha-cache'; import type { BranchConfig, BranchUpgradeConfig } from '../types'; function generateBranchUpgradeCache( @@ -57,7 +57,7 @@ async function generateBranchCache( let isModified = false; let isBehindBase = false; if (sha) { - parentSha = await getBranchParentSha(branchName); + parentSha = getCachedBranchParentShaResult(branchName, sha); const branchPr = await platform.getBranchPr(branchName); if (branchPr) { prNo = branchPr.number; -- GitLab