0
0
Fork 0
mirror of https://github.com/renovatebot/renovate.git synced 2025-01-11 13:48:55 +00:00

refactor: strongly type commit SHAs ()

Co-authored-by: Rhys Arkins <rhys@arkins.net>
This commit is contained in:
Tom Vervoort 2023-11-22 07:30:19 +01:00 committed by GitHub
parent 4f8e802c81
commit c82b9afb36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 197 additions and 124 deletions

View file

@ -7,6 +7,7 @@ import {
REPOSITORY_NOT_FOUND,
} from '../../../constants/error-messages';
import type * as _git from '../../../util/git';
import type { LongCommitSha } from '../../../util/git/types';
import type { Platform } from '../types';
jest.mock('timers/promises');
@ -215,7 +216,7 @@ describe('modules/platform/bitbucket-server/index', () => {
git.branchExists.mockReturnValue(true);
git.isBranchBehindBase.mockResolvedValue(false);
git.getBranchCommit.mockReturnValue(
'0d9c7726c3d628b7e28af234595cfd20febdbf8e',
'0d9c7726c3d628b7e28af234595cfd20febdbf8e' as LongCommitSha,
);
const endpoint =
scenarioName === 'endpoint with path'

View file

@ -1,5 +1,5 @@
import { git, partial } from '../../../test/util';
import type { CommitFilesConfig } from '../../util/git/types';
import type { CommitFilesConfig, LongCommitSha } from '../../util/git/types';
import { DefaultGitScm } from './default-scm';
jest.mock('../../util/git');
@ -14,7 +14,7 @@ describe('modules/platform/default-scm', () => {
});
it('delegate commitAndPush to util/git', async () => {
git.commitFiles.mockResolvedValueOnce('sha');
git.commitFiles.mockResolvedValueOnce('sha' as LongCommitSha);
await defaultGitScm.commitAndPush(partial<CommitFilesConfig>());
expect(git.commitFiles).toHaveBeenCalledTimes(1);
});
@ -26,7 +26,7 @@ describe('modules/platform/default-scm', () => {
});
it('delegate getBranchCommit to util/git', async () => {
git.getBranchCommit.mockReturnValueOnce('sha');
git.getBranchCommit.mockReturnValueOnce('sha' as LongCommitSha);
await defaultGitScm.getBranchCommit('branchName');
expect(git.getBranchCommit).toHaveBeenCalledTimes(1);
});
@ -56,7 +56,7 @@ describe('modules/platform/default-scm', () => {
});
it('delegate checkoutBranch to util/git', async () => {
git.checkoutBranch.mockResolvedValueOnce('');
git.checkoutBranch.mockResolvedValueOnce('sha' as LongCommitSha);
await defaultGitScm.checkoutBranch('branchName');
expect(git.checkoutBranch).toHaveBeenCalledTimes(1);
});

View file

@ -1,4 +1,5 @@
import * as httpMock from '../../../../test/http-mock';
import type { LongCommitSha } from '../../../util/git/types';
import { setBaseUrl } from '../../../util/http/gitea';
import { toBase64 } from '../../../util/string';
import {
@ -49,7 +50,8 @@ describe('modules/platform/gitea/gitea-helper', () => {
const giteaApiHost = 'https://gitea.renovatebot.com/';
const baseUrl = `${giteaApiHost}api/v1`;
const mockCommitHash = '0d9c7726c3d628b7e28af234595cfd20febdbf8e';
const mockCommitHash =
'0d9c7726c3d628b7e28af234595cfd20febdbf8e' as LongCommitSha;
const mockUser: User = {
id: 1,

View file

@ -18,6 +18,7 @@ import {
import type { logger as _logger } from '../../../logger';
import type { BranchStatus, PrState } from '../../../types';
import type * as _git from '../../../util/git';
import type { LongCommitSha } from '../../../util/git/types';
import { setBaseUrl } from '../../../util/http/gitea';
import type { PlatformResult } from '../types';
import type {
@ -50,7 +51,8 @@ describe('modules/platform/gitea/index', () => {
let gitvcs: jest.Mocked<typeof _git>;
let hostRules: typeof import('../../../util/host-rules');
const mockCommitHash = '0d9c7726c3d628b7e28af234595cfd20febdbf8e';
const mockCommitHash =
'0d9c7726c3d628b7e28af234595cfd20febdbf8e' as LongCommitSha;
const mockUser: User = {
id: 1,
@ -93,7 +95,7 @@ describe('modules/platform/gitea/index', () => {
base: { ref: 'some-base-branch' },
head: {
label: 'some-head-branch',
sha: 'some-head-sha',
sha: 'some-head-sha' as LongCommitSha,
repo: partial<Repo>({ full_name: mockRepo.full_name }),
},
}),
@ -109,7 +111,7 @@ describe('modules/platform/gitea/index', () => {
base: { ref: 'other-base-branch' },
head: {
label: 'other-head-branch',
sha: 'other-head-sha',
sha: 'other-head-sha' as LongCommitSha,
repo: partial<Repo>({ full_name: mockRepo.full_name }),
},
}),
@ -125,7 +127,7 @@ describe('modules/platform/gitea/index', () => {
base: { ref: 'draft-base-branch' },
head: {
label: 'draft-head-branch',
sha: 'draft-head-sha',
sha: 'draft-head-sha' as LongCommitSha,
repo: partial<Repo>({ full_name: mockRepo.full_name }),
},
}),
@ -797,7 +799,7 @@ describe('modules/platform/gitea/index', () => {
base: { ref: 'third-party-base-branch' },
head: {
label: 'other-head-branch',
sha: 'other-head-sha',
sha: 'other-head-sha' as LongCommitSha,
repo: partial<Repo>({ full_name: mockRepo.full_name }),
},
user: { username: 'not-renovate' },

View file

@ -1,3 +1,5 @@
import type { LongCommitSha } from '../../../util/git/types';
export interface PrReviewersParams {
reviewers?: string[];
team_reviewers?: string[];
@ -28,7 +30,7 @@ export interface PR {
};
head?: {
label: string;
sha: string;
sha: LongCommitSha;
repo?: Repo;
};
assignee?: {

View file

@ -12,6 +12,7 @@ import {
} from '../../../constants/error-messages';
import * as repository from '../../../util/cache/repository';
import * as _git from '../../../util/git';
import type { LongCommitSha } from '../../../util/git/types';
import * as _hostRules from '../../../util/host-rules';
import { setBaseUrl } from '../../../util/http/github';
import { toBase64 } from '../../../util/string';
@ -40,7 +41,7 @@ describe('modules/platform/github/index', () => {
git.isBranchBehindBase.mockResolvedValue(true);
git.getBranchCommit.mockReturnValue(
'0d9c7726c3d628b7e28af234595cfd20febdbf8e',
'0d9c7726c3d628b7e28af234595cfd20febdbf8e' as LongCommitSha,
);
hostRules.find.mockReturnValue({
token: '123test',
@ -888,7 +889,11 @@ describe('modules/platform/github/index', () => {
const pr1: GhRestPr = {
number: 1,
head: { ref: 'branch-1', sha: '111', repo: { full_name: 'some/repo' } },
head: {
ref: 'branch-1',
sha: '111' as LongCommitSha,
repo: { full_name: 'some/repo' },
},
base: { repo: { pushed_at: '' }, ref: 'repo/fork_branch' },
state: 'open',
title: 'PR #1',
@ -901,7 +906,11 @@ describe('modules/platform/github/index', () => {
const pr2: GhRestPr = {
...pr1,
number: 2,
head: { ref: 'branch-2', sha: '222', repo: { full_name: 'some/repo' } },
head: {
ref: 'branch-2',
sha: '222' as LongCommitSha,
repo: { full_name: 'some/repo' },
},
state: 'open',
title: 'PR #2',
updated_at: t2,
@ -910,7 +919,11 @@ describe('modules/platform/github/index', () => {
const pr3: GhRestPr = {
...pr1,
number: 3,
head: { ref: 'branch-3', sha: '333', repo: { full_name: 'some/repo' } },
head: {
ref: 'branch-3',
sha: '333' as LongCommitSha,
repo: { full_name: 'some/repo' },
},
state: 'open',
title: 'PR #3',
updated_at: t3,
@ -3520,12 +3533,14 @@ describe('modules/platform/github/index', () => {
beforeEach(() => {
git.prepareCommit.mockImplementation(({ files }) =>
Promise.resolve({
parentCommitSha: '1234567',
commitSha: '7654321',
parentCommitSha: '1234567' as LongCommitSha,
commitSha: '7654321' as LongCommitSha,
files,
}),
);
git.fetchBranch.mockImplementation(() => Promise.resolve('0abcdef'));
git.fetchBranch.mockImplementation(() =>
Promise.resolve('0abcdef' as LongCommitSha),
);
});
it('returns null if pre-commit phase has failed', async () => {

View file

@ -828,7 +828,10 @@ export async function findPr({
const REOPEN_THRESHOLD_MILLIS = 1000 * 60 * 60 * 24 * 7;
async function ensureBranchSha(branchName: string, sha: string): Promise<void> {
async function ensureBranchSha(
branchName: string,
sha: LongCommitSha,
): Promise<void> {
const repository = config.repository!;
try {
const commitUrl = `/repos/${repository}/git/commits/${sha}`;
@ -1918,7 +1921,7 @@ async function pushFiles(
`/repos/${config.repository}/git/commits`,
{ body: { message, tree: treeSha, parents: [parentCommitSha] } },
);
const remoteCommitSha = commitRes.body.sha;
const remoteCommitSha = commitRes.body.sha as LongCommitSha;
await ensureBranchSha(branchName, remoteCommitSha);
return remoteCommitSha;
} catch (err) {

View file

@ -1,5 +1,5 @@
import { git, mocked } from '../../../../test/util';
import type { CommitFilesConfig } from '../../../util/git/types';
import type { CommitFilesConfig, LongCommitSha } from '../../../util/git/types';
import { GithubScm } from './scm';
import * as _github from '.';
@ -8,7 +8,7 @@ const github = mocked(_github);
describe('modules/platform/github/scm', () => {
beforeEach(() => {
jest.spyOn(git, 'commitFiles').mockResolvedValue('sha');
jest.spyOn(git, 'commitFiles').mockResolvedValue('sha' as LongCommitSha);
});
const githubScm = new GithubScm();

View file

@ -1,3 +1,4 @@
import type { LongCommitSha } from '../../../util/git/types';
import type { Pr, PrBodyStruct } from '../types';
// https://developer.github.com/v3/repos/statuses
@ -33,7 +34,7 @@ export interface GhRestRepo {
export interface GhRestPr {
head: {
ref: string;
sha: string;
sha: LongCommitSha;
repo: {
full_name: string;
pushed_at?: string;

View file

@ -15,6 +15,7 @@ import {
import type { logger as _logger } from '../../../logger';
import type { BranchStatus } from '../../../types';
import type * as _git from '../../../util/git';
import type { LongCommitSha } from '../../../util/git/types';
import type * as _hostRules from '../../../util/host-rules';
import { toBase64 } from '../../../util/string';
@ -43,7 +44,7 @@ describe('modules/platform/gitlab/index', () => {
git.branchExists.mockReturnValue(true);
git.isBranchBehindBase.mockResolvedValue(true);
git.getBranchCommit.mockReturnValue(
'0d9c7726c3d628b7e28af234595cfd20febdbf8e',
'0d9c7726c3d628b7e28af234595cfd20febdbf8e' as LongCommitSha,
);
hostRules.find.mockReturnValue({
token: '123test',

View file

@ -1,3 +1,4 @@
import type { LongCommitSha } from '../../../util/git/types';
import type { Pr } from '../types';
export interface GitlabIssue {
@ -31,7 +32,7 @@ export interface GitLabMergeRequest {
assignees?: GitLabUser[];
reviewers?: GitLabUser[];
labels: string[];
sha: string;
sha: LongCommitSha;
head_pipeline?: {
status: string;
};

View file

@ -18,13 +18,15 @@ export class LocalFs implements PlatformScm {
branchExists(branchName: string): Promise<boolean> {
return Promise.resolve(true);
}
getBranchCommit(branchName: string): Promise<string | null> {
getBranchCommit(branchName: string): Promise<LongCommitSha | null> {
return Promise.resolve(null);
}
deleteBranch(branchName: string): Promise<void> {
return Promise.resolve();
}
commitAndPush(commitConfig: CommitFilesConfig): Promise<string | null> {
commitAndPush(
commitConfig: CommitFilesConfig,
): Promise<LongCommitSha | null> {
return Promise.resolve(null);
}
@ -46,7 +48,8 @@ export class LocalFs implements PlatformScm {
}
checkoutBranch(branchName: string): Promise<LongCommitSha> {
return Promise.resolve('');
// We don't care about the commit sha in local mode
return Promise.resolve('' as LongCommitSha);
}
mergeAndPush(branchName: string): Promise<void> {

View file

@ -77,7 +77,7 @@ export interface Pr {
labels?: string[];
number: number;
reviewers?: string[];
sha?: string;
sha?: LongCommitSha;
sourceRepo?: string;
state: string;
targetBranch?: string;

View file

@ -5,6 +5,7 @@ import {
getCachedBehindBaseResult,
setCachedBehindBaseResult,
} from './behind-base-branch-cache';
import type { LongCommitSha } from './types';
jest.mock('../cache/repository');
const repositoryCache = mocked(_repositoryCache);
@ -22,9 +23,9 @@ describe('util/git/behind-base-branch-cache', () => {
expect(
getCachedBehindBaseResult(
'branch',
'branch_sha',
'branch_sha' as LongCommitSha,
'base_branch',
'base_branch_sha',
'base_branch_sha' as LongCommitSha,
),
).toBeNull();
});
@ -45,9 +46,9 @@ describe('util/git/behind-base-branch-cache', () => {
expect(
getCachedBehindBaseResult(
'branch',
'branch_sha',
'branch_sha' as LongCommitSha,
'base_branch',
'base_branch_sha',
'base_branch_sha' as LongCommitSha,
),
).toBeNull();
});
@ -68,9 +69,9 @@ describe('util/git/behind-base-branch-cache', () => {
expect(
getCachedBehindBaseResult(
'branch',
'branch_sha',
'branch_sha' as LongCommitSha,
'base_branch',
'base_branch_sha',
'base_branch_sha' as LongCommitSha,
),
).toBeNull();
});
@ -91,9 +92,9 @@ describe('util/git/behind-base-branch-cache', () => {
expect(
getCachedBehindBaseResult(
'branch',
'branch_sha',
'branch_sha' as LongCommitSha,
'base_branch',
'base_branch_sha',
'base_branch_sha' as LongCommitSha,
),
).toBeNull();
});
@ -113,9 +114,9 @@ describe('util/git/behind-base-branch-cache', () => {
expect(
getCachedBehindBaseResult(
'branch',
'branch_sha',
'branch_sha' as LongCommitSha,
'base_branch',
'base_branch_sha',
'base_branch_sha' as LongCommitSha,
),
).toBeNull();
});
@ -136,9 +137,9 @@ describe('util/git/behind-base-branch-cache', () => {
expect(
getCachedBehindBaseResult(
'branch',
'branch_sha',
'branch_sha' as LongCommitSha,
'base_branch',
'base_branch_sha',
'base_branch_sha' as LongCommitSha,
),
).toBeNull();
});
@ -159,9 +160,9 @@ describe('util/git/behind-base-branch-cache', () => {
expect(
getCachedBehindBaseResult(
'branch',
'branch_sha',
'branch_sha' as LongCommitSha,
'base_branch',
'base_branch_sha',
'base_branch_sha' as LongCommitSha,
),
).toBeNull();
});
@ -182,9 +183,9 @@ describe('util/git/behind-base-branch-cache', () => {
expect(
getCachedBehindBaseResult(
'branch',
'branch_sha',
'branch_sha' as LongCommitSha,
'base_branch',
'base_branch_sha',
'base_branch_sha' as LongCommitSha,
),
).toBeTrue();
});

View file

@ -1,11 +1,12 @@
import { logger } from '../../logger';
import { getCache } from '../cache/repository';
import type { LongCommitSha } from './types';
export function getCachedBehindBaseResult(
branchName: string,
branchSha: string | null,
branchSha: LongCommitSha | null,
baseBranch: string,
baseBranchSha: string | null,
baseBranchSha: LongCommitSha | null,
): boolean | null {
const cache = getCache();
const branch = cache.branches?.find(

View file

@ -207,7 +207,8 @@ async function fetchBranchCommits(): Promise<void> {
.filter(Boolean)
.map((line) => line.trim().split(regEx(/\s+/)))
.forEach(([sha, ref]) => {
config.branchCommits[ref.replace('refs/heads/', '')] = sha;
config.branchCommits[ref.replace('refs/heads/', '')] =
sha as LongCommitSha;
});
} catch (err) /* istanbul ignore next */ {
const errChecked = checkForPlatformFailure(err);
@ -250,7 +251,7 @@ async function resetToBranch(branchName: string): Promise<void> {
}
// istanbul ignore next
export async function resetToCommit(commit: string): Promise<void> {
export async function resetToCommit(commit: LongCommitSha): Promise<void> {
logger.debug(`resetToCommit(${commit})`);
await git.raw(['reset', '--hard', commit]);
}
@ -442,7 +443,9 @@ export async function syncGit(): Promise<void> {
logger.debug({ durationMs }, 'git clone completed');
}
try {
config.currentBranchSha = (await git.raw(['rev-parse', 'HEAD'])).trim();
config.currentBranchSha = (
await git.raw(['rev-parse', 'HEAD'])
).trim() as LongCommitSha;
} catch (err) /* istanbul ignore next */ {
if (err.message?.includes('fatal: not a git repository')) {
throw new Error(REPOSITORY_CHANGED);
@ -519,7 +522,9 @@ export async function checkoutBranch(
try {
await gitRetry(() => git.checkout(['-f', branchName, '--']));
config.currentBranch = branchName;
config.currentBranchSha = (await git.raw(['rev-parse', 'HEAD'])).trim();
config.currentBranchSha = (
await git.raw(['rev-parse', 'HEAD'])
).trim() as LongCommitSha;
const latestCommitDate = (await git.log({ n: 1 }))?.latest?.date;
if (latestCommitDate) {
logger.debug({ branchName, latestCommitDate }, 'latest commit');
@ -1052,7 +1057,9 @@ export async function prepareCommit({
return null;
}
const commitSha = (await git.revparse([branchName])).trim();
const commitSha = (
await git.revparse([branchName])
).trim() as LongCommitSha;
const result: CommitResult = {
parentCommitSha,
commitSha,
@ -1108,7 +1115,7 @@ export async function fetchBranch(
try {
const ref = `refs/heads/${branchName}:refs/remotes/origin/${branchName}`;
await gitRetry(() => git.pull(['origin', ref, '--force']));
const commit = (await git.revparse([branchName])).trim();
const commit = (await git.revparse([branchName])).trim() as LongCommitSha;
config.branchCommits[branchName] = commit;
config.branchIsModified[branchName] = false;
return commit;
@ -1297,7 +1304,9 @@ const treeShaRegex = regEx(/tree\s+(?<treeSha>[0-9a-f]{40})\s*/);
* > 100644 blob 7d2edde437ad4e7bceb70dbfe70e93350d99c98b package.json
*
*/
export async function listCommitTree(commitSha: string): Promise<TreeItem[]> {
export async function listCommitTree(
commitSha: LongCommitSha,
): Promise<TreeItem[]> {
const commitOutput = await git.catFile(['-p', commitSha]);
const { treeSha } =
treeShaRegex.exec(commitOutput)?.groups ??
@ -1309,7 +1318,7 @@ export async function listCommitTree(commitSha: string): Promise<TreeItem[]> {
const matchGroups = treeItemRegex.exec(line)?.groups;
if (matchGroups) {
const { path, mode, type, sha } = matchGroups;
result.push({ path, mode, type, sha });
result.push({ path, mode, type, sha: sha as LongCommitSha });
}
}
return result;

View file

@ -2,6 +2,7 @@ import { git, logger, mocked, partial } from '../../../test/util';
import * as _repositoryCache from '../cache/repository';
import type { BranchCache, RepoCacheData } from '../cache/repository/types';
import { setBranchNewCommit } from './set-branch-commit';
import type { LongCommitSha } from './types';
jest.mock('../cache/repository');
jest.mock('.');
@ -17,7 +18,7 @@ describe('util/git/set-branch-commit', () => {
describe('setBranchCommit', () => {
it('sets new branch in cache if it does not exist', () => {
git.getBranchCommit.mockReturnValueOnce('base_SHA');
git.getBranchCommit.mockReturnValueOnce('base_SHA' as LongCommitSha);
setBranchNewCommit('branch_name', 'base_branch', 'SHA');
expect(logger.logger.debug).toHaveBeenCalledWith(
'setBranchCommit(): Branch cache not present',
@ -51,7 +52,7 @@ describe('util/git/set-branch-commit', () => {
}),
],
};
git.getBranchCommit.mockReturnValueOnce('base_SHA');
git.getBranchCommit.mockReturnValueOnce('base_SHA' as LongCommitSha);
repositoryCache.getCache.mockReturnValue(repoCache);
setBranchNewCommit('branch_name', 'base_branch', 'SHA');
expect(repoCache.branches).toEqual([

View file

@ -9,7 +9,10 @@ export interface GitAuthor {
export type GitNoVerifyOption = 'commit' | 'push';
export type LongCommitSha = string;
/**
* We want to make sure this is a long sha of 40 characters and not just any string
*/
export type LongCommitSha = string & { __longCommitSha: never };
export interface StorageConfig {
currentBranch?: string;
@ -22,7 +25,7 @@ export interface StorageConfig {
export interface LocalConfig extends StorageConfig {
additionalBranches: string[];
currentBranch: string;
currentBranchSha: string;
currentBranchSha: LongCommitSha;
branchCommits: Record<string, LongCommitSha>;
branchIsModified: Record<string, boolean>;
commitBranches: Record<string, string[]>;
@ -89,8 +92,8 @@ export interface PushFilesConfig {
export type BranchName = string;
export interface CommitResult {
parentCommitSha: string;
commitSha: string;
parentCommitSha: LongCommitSha;
commitSha: LongCommitSha;
files: FileChange[];
}
@ -98,7 +101,7 @@ export interface TreeItem {
path: string;
mode: string;
type: string;
sha: string;
sha: LongCommitSha;
}
/**

View file

@ -10,6 +10,7 @@ import { REPOSITORY_CLOSED_ONBOARDING } from '../../../../constants/error-messag
import { logger } from '../../../../logger';
import type { Pr } from '../../../../modules/platform/types';
import * as _cache from '../../../../util/cache/repository';
import type { LongCommitSha } from '../../../../util/git/types';
import { isOnboarded } from './check';
jest.mock('../../../../util/cache/repository');
@ -34,8 +35,8 @@ describe('workers/repository/onboarding/branch/check', () => {
},
});
git.getBranchCommit
.mockReturnValueOnce('default-sha')
.mockReturnValueOnce('onboarding-sha');
.mockReturnValueOnce('default-sha' as LongCommitSha)
.mockReturnValueOnce('onboarding-sha' as LongCommitSha);
const res = await isOnboarded(config);
expect(res).toBeFalse();
expect(logger.debug).toHaveBeenCalledWith(

View file

@ -19,7 +19,7 @@ import type { Pr } from '../../../../modules/platform';
import * as memCache from '../../../../util/cache/memory';
import * as _cache from '../../../../util/cache/repository';
import type { RepoCacheData } from '../../../../util/cache/repository/types';
import type { FileAddition } from '../../../../util/git/types';
import type { FileAddition, LongCommitSha } from '../../../../util/git/types';
import { OnboardingState } from '../common';
import * as _config from './config';
import * as _onboardingCache from './onboarding-branch-cache';
@ -307,9 +307,9 @@ describe('workers/repository/onboarding/branch/index', () => {
mock<Pr>({ bodyStruct: { rebaseRequested: false } }),
); // finds open onboarding pr
git.getBranchCommit
.mockReturnValueOnce('default-sha')
.mockReturnValueOnce('default-sha')
.mockReturnValueOnce('onboarding-sha');
.mockReturnValueOnce('default-sha' as LongCommitSha)
.mockReturnValueOnce('default-sha' as LongCommitSha)
.mockReturnValueOnce('onboarding-sha' as LongCommitSha);
config.onboardingRebaseCheckbox = true;
await checkOnboardingBranch(config);
expect(scm.commitAndPush).not.toHaveBeenCalled();
@ -331,8 +331,8 @@ describe('workers/repository/onboarding/branch/index', () => {
platform.findPr.mockResolvedValue(null);
platform.getBranchPr.mockResolvedValueOnce(mock<Pr>());
git.getBranchCommit
.mockReturnValueOnce('default-sha')
.mockReturnValueOnce('new-onboarding-sha');
.mockReturnValueOnce('default-sha' as LongCommitSha)
.mockReturnValueOnce('new-onboarding-sha' as LongCommitSha);
config.baseBranch = 'master';
onboardingCache.isOnboardingBranchModified.mockResolvedValueOnce(true);
onboardingCache.hasOnboardingBranchChanged.mockReturnValueOnce(true);
@ -356,8 +356,8 @@ describe('workers/repository/onboarding/branch/index', () => {
platform.getBranchPr.mockResolvedValueOnce(mock<Pr>());
platform.getBranchPr.mockResolvedValueOnce(mock<Pr>());
git.getBranchCommit
.mockReturnValueOnce('default-sha')
.mockReturnValueOnce('onboarding-sha');
.mockReturnValueOnce('default-sha' as LongCommitSha)
.mockReturnValueOnce('onboarding-sha' as LongCommitSha);
onboardingCache.isOnboardingBranchModified.mockResolvedValueOnce(true);
onboardingCache.hasOnboardingBranchChanged.mockReturnValueOnce(true);
onboardingCache.isOnboardingBranchConflicted.mockResolvedValueOnce(true);
@ -376,8 +376,8 @@ describe('workers/repository/onboarding/branch/index', () => {
platform.findPr.mockResolvedValue(null);
platform.getBranchPr.mockResolvedValueOnce(mock<Pr>());
git.getBranchCommit
.mockReturnValueOnce('default-sha')
.mockReturnValueOnce('onboarding-sha');
.mockReturnValueOnce('default-sha' as LongCommitSha)
.mockReturnValueOnce('onboarding-sha' as LongCommitSha);
onboardingCache.isOnboardingBranchModified.mockResolvedValueOnce(false);
await checkOnboardingBranch(config);
expect(scm.mergeToLocal).toHaveBeenCalled();

View file

@ -4,6 +4,7 @@ import type {
OnboardingBranchCache,
RepoCacheData,
} from '../../../../util/cache/repository/types';
import type { LongCommitSha } from '../../../../util/git/types';
import {
deleteOnboardingCache,
getOnboardingConfigFromCache,
@ -83,7 +84,9 @@ describe('workers/repository/onboarding/branch/onboarding-branch-cache', () => {
describe('hasOnboardingBranchChanged()', () => {
it('return true if cache is absent', () => {
cache.getCache.mockReturnValueOnce({});
git.getBranchCommit.mockReturnValueOnce('onboarding-sha');
git.getBranchCommit.mockReturnValueOnce(
'onboarding-sha' as LongCommitSha,
);
expect(hasOnboardingBranchChanged('configure/renovate')).toBeTrue();
});
@ -97,7 +100,9 @@ describe('workers/repository/onboarding/branch/onboarding-branch-cache', () => {
},
} satisfies RepoCacheData;
cache.getCache.mockReturnValueOnce(dummyCache);
git.getBranchCommit.mockReturnValueOnce('new-onboarding-sha');
git.getBranchCommit.mockReturnValueOnce(
'new-onboarding-sha' as LongCommitSha,
);
expect(hasOnboardingBranchChanged('configure/renovate')).toBeTrue();
});
@ -111,7 +116,9 @@ describe('workers/repository/onboarding/branch/onboarding-branch-cache', () => {
},
} satisfies RepoCacheData;
cache.getCache.mockReturnValueOnce(dummyCache);
git.getBranchCommit.mockReturnValueOnce('onboarding-sha');
git.getBranchCommit.mockReturnValueOnce(
'onboarding-sha' as LongCommitSha,
);
expect(hasOnboardingBranchChanged('configure/renovate')).toBeFalse();
});
@ -125,7 +132,9 @@ describe('workers/repository/onboarding/branch/onboarding-branch-cache', () => {
},
} satisfies RepoCacheData;
cache.getCache.mockReturnValueOnce(dummyCache);
git.getBranchCommit.mockReturnValueOnce('onboarding-sha');
git.getBranchCommit.mockReturnValueOnce(
'onboarding-sha' as LongCommitSha,
);
expect(hasOnboardingBranchChanged('configure/renovate')).toBeFalse();
});
});
@ -133,7 +142,9 @@ describe('workers/repository/onboarding/branch/onboarding-branch-cache', () => {
describe('isOnboardingBranchModified()', () => {
it('falls back to git if cache is absent', async () => {
cache.getCache.mockReturnValueOnce({});
git.getBranchCommit.mockReturnValueOnce('onboarding-sha');
git.getBranchCommit.mockReturnValueOnce(
'onboarding-sha' as LongCommitSha,
);
scm.isBranchModified.mockResolvedValueOnce(false);
expect(
await isOnboardingBranchModified('configure/renovate'),
@ -150,7 +161,9 @@ describe('workers/repository/onboarding/branch/onboarding-branch-cache', () => {
},
} satisfies RepoCacheData;
cache.getCache.mockReturnValueOnce(dummyCache);
git.getBranchCommit.mockReturnValueOnce('new-onboarding-sha');
git.getBranchCommit.mockReturnValueOnce(
'new-onboarding-sha' as LongCommitSha,
);
scm.isBranchModified.mockResolvedValueOnce(true);
expect(await isOnboardingBranchModified('configure/renovate')).toBeTrue();
});
@ -165,7 +178,9 @@ describe('workers/repository/onboarding/branch/onboarding-branch-cache', () => {
},
} satisfies RepoCacheData;
cache.getCache.mockReturnValueOnce(dummyCache);
git.getBranchCommit.mockReturnValueOnce('onboarding-sha');
git.getBranchCommit.mockReturnValueOnce(
'onboarding-sha' as LongCommitSha,
);
expect(await isOnboardingBranchModified('configure/renovate')).toBeTrue();
});
});
@ -174,8 +189,8 @@ describe('workers/repository/onboarding/branch/onboarding-branch-cache', () => {
it('falls back to git if cache is absent', async () => {
cache.getCache.mockReturnValueOnce({});
git.getBranchCommit
.mockReturnValueOnce('onboarding-sha')
.mockReturnValueOnce('default-sha');
.mockReturnValueOnce('onboarding-sha' as LongCommitSha)
.mockReturnValueOnce('default-sha' as LongCommitSha);
scm.isBranchConflicted.mockResolvedValueOnce(false);
expect(
await isOnboardingBranchConflicted('master', 'configure/renovate'),
@ -193,8 +208,8 @@ describe('workers/repository/onboarding/branch/onboarding-branch-cache', () => {
} satisfies RepoCacheData;
cache.getCache.mockReturnValueOnce(dummyCache);
git.getBranchCommit
.mockReturnValueOnce('onboarding-sha')
.mockReturnValueOnce('new-default-sha');
.mockReturnValueOnce('onboarding-sha' as LongCommitSha)
.mockReturnValueOnce('new-default-sha' as LongCommitSha);
scm.isBranchConflicted.mockResolvedValueOnce(false);
expect(
await isOnboardingBranchConflicted('master', 'configure/renovate'),
@ -212,8 +227,8 @@ describe('workers/repository/onboarding/branch/onboarding-branch-cache', () => {
} satisfies RepoCacheData;
cache.getCache.mockReturnValueOnce(dummyCache);
git.getBranchCommit
.mockReturnValueOnce('new-onboarding-sha')
.mockReturnValueOnce('default-sha');
.mockReturnValueOnce('new-onboarding-sha' as LongCommitSha)
.mockReturnValueOnce('default-sha' as LongCommitSha);
scm.isBranchConflicted.mockResolvedValueOnce(false);
expect(
await isOnboardingBranchConflicted('master', 'configure/renovate'),
@ -231,8 +246,8 @@ describe('workers/repository/onboarding/branch/onboarding-branch-cache', () => {
} satisfies RepoCacheData;
cache.getCache.mockReturnValueOnce(dummyCache);
git.getBranchCommit
.mockReturnValueOnce('onboarding-sha')
.mockReturnValueOnce('default-sha');
.mockReturnValueOnce('onboarding-sha' as LongCommitSha)
.mockReturnValueOnce('default-sha' as LongCommitSha);
expect(
await isOnboardingBranchConflicted('master', 'configure/renovate'),
).toBeTrue();

View file

@ -3,6 +3,7 @@ import type { PackageFile } from '../../../modules/manager/types';
import * as _repositoryCache from '../../../util/cache/repository';
import type { BaseBranchCache } from '../../../util/cache/repository/types';
import { fingerprint } from '../../../util/fingerprint';
import type { LongCommitSha } from '../../../util/git/types';
import { generateFingerprintConfig } from '../extract/extract-fingerprint-config';
import * as _branchify from '../updates/branchify';
import { extract, isCacheExtractValid, lookup, update } from './extract-update';
@ -52,7 +53,7 @@ describe('workers/repository/process/extract-update', () => {
suppressNotifications: ['deprecationWarningIssues'],
};
repositoryCache.getCache.mockReturnValueOnce({ scan: {} });
scm.checkoutBranch.mockResolvedValueOnce('123test');
scm.checkoutBranch.mockResolvedValueOnce('123test' as LongCommitSha);
const packageFiles = await extract(config);
const res = await lookup(config, packageFiles);
expect(res).toEqual({
@ -83,7 +84,7 @@ describe('workers/repository/process/extract-update', () => {
addLabels: 'npm',
},
};
scm.checkoutBranch.mockResolvedValueOnce('123test');
scm.checkoutBranch.mockResolvedValueOnce('123test' as LongCommitSha);
repositoryCache.getCache.mockReturnValueOnce({ scan: {} });
const packageFiles = await extract(config);
expect(packageFiles).toBeUndefined();
@ -106,8 +107,8 @@ describe('workers/repository/process/extract-update', () => {
},
},
});
scm.getBranchCommit.mockResolvedValueOnce('123test');
scm.checkoutBranch.mockResolvedValueOnce('123test');
scm.getBranchCommit.mockResolvedValueOnce('123test' as LongCommitSha);
scm.checkoutBranch.mockResolvedValueOnce('123test' as LongCommitSha);
const res = await extract(config);
expect(res).toEqual(packageFiles);
});
@ -123,7 +124,7 @@ describe('workers/repository/process/extract-update', () => {
appendVulnerabilityPackageRules: appendVulnerabilityPackageRulesMock,
});
repositoryCache.getCache.mockReturnValueOnce({ scan: {} });
scm.checkoutBranch.mockResolvedValueOnce('123test');
scm.checkoutBranch.mockResolvedValueOnce('123test' as LongCommitSha);
const packageFiles = await extract(config);
await lookup(config, packageFiles);
@ -140,7 +141,7 @@ describe('workers/repository/process/extract-update', () => {
};
createVulnerabilitiesMock.mockRejectedValueOnce(new Error());
repositoryCache.getCache.mockReturnValueOnce({ scan: {} });
scm.checkoutBranch.mockResolvedValueOnce('123test');
scm.checkoutBranch.mockResolvedValueOnce('123test' as LongCommitSha);
const packageFiles = await extract(config);
await lookup(config, packageFiles);

View file

@ -16,6 +16,7 @@ import type {
RepoCacheData,
} from '../../../util/cache/repository/types';
import { fingerprint } from '../../../util/fingerprint';
import type { LongCommitSha } from '../../../util/git/types';
import { isLimitReached } from '../../global/limits';
import type { BranchConfig, BranchUpgradeConfig } from '../../types';
import * as _branchWorker from '../update/branch';
@ -313,8 +314,8 @@ describe('workers/repository/process/write', () => {
result: 'no-work',
});
scm.getBranchCommit
.mockResolvedValueOnce('sha')
.mockResolvedValueOnce('base_sha');
.mockResolvedValueOnce('sha' as LongCommitSha)
.mockResolvedValueOnce('base_sha' as LongCommitSha);
scm.branchExists.mockResolvedValueOnce(true);
await writeUpdates(config, branches);
expect(logger.logger.debug).not.toHaveBeenCalledWith(
@ -378,8 +379,8 @@ describe('workers/repository/process/write', () => {
it('creates minimal branch state when cache is not populated', () => {
const repoCacheObj = partial<RepoCacheData>();
repoCache.getCache.mockReturnValue(repoCacheObj);
scm.getBranchCommit.mockResolvedValueOnce('sha');
scm.getBranchCommit.mockResolvedValueOnce('base_sha');
scm.getBranchCommit.mockResolvedValueOnce('sha' as LongCommitSha);
scm.getBranchCommit.mockResolvedValueOnce('base_sha' as LongCommitSha);
return expect(
syncBranchState('branch_name', 'base_branch'),
).resolves.toEqual({
@ -407,8 +408,8 @@ describe('workers/repository/process/write', () => {
],
};
repoCache.getCache.mockReturnValue(repoCacheObj);
scm.getBranchCommit.mockResolvedValueOnce('sha');
scm.getBranchCommit.mockResolvedValueOnce('base_sha');
scm.getBranchCommit.mockResolvedValueOnce('sha' as LongCommitSha);
scm.getBranchCommit.mockResolvedValueOnce('base_sha' as LongCommitSha);
return expect(
syncBranchState('branch_name', 'new_base_branch'),
).resolves.toEqual({
@ -440,8 +441,10 @@ describe('workers/repository/process/write', () => {
],
};
repoCache.getCache.mockReturnValue(repoCacheObj);
scm.getBranchCommit.mockResolvedValueOnce('sha');
scm.getBranchCommit.mockResolvedValueOnce('new_base_sha');
scm.getBranchCommit.mockResolvedValueOnce('sha' as LongCommitSha);
scm.getBranchCommit.mockResolvedValueOnce(
'new_base_sha' as LongCommitSha,
);
return expect(
syncBranchState('branch_name', 'base_branch'),
).resolves.toEqual({
@ -476,8 +479,8 @@ describe('workers/repository/process/write', () => {
],
};
repoCache.getCache.mockReturnValue(repoCacheObj);
scm.getBranchCommit.mockResolvedValueOnce('new_sha');
scm.getBranchCommit.mockResolvedValueOnce('base_sha');
scm.getBranchCommit.mockResolvedValueOnce('new_sha' as LongCommitSha);
scm.getBranchCommit.mockResolvedValueOnce('base_sha' as LongCommitSha);
return expect(
syncBranchState('branch_name', 'base_branch'),
).resolves.toEqual({
@ -512,8 +515,8 @@ describe('workers/repository/process/write', () => {
],
};
repoCache.getCache.mockReturnValue(repoCacheObj);
scm.getBranchCommit.mockResolvedValueOnce('sha');
scm.getBranchCommit.mockResolvedValueOnce('base_sha');
scm.getBranchCommit.mockResolvedValueOnce('sha' as LongCommitSha);
scm.getBranchCommit.mockResolvedValueOnce('base_sha' as LongCommitSha);
return expect(
syncBranchState('branch_name', 'base_branch'),
).resolves.toEqual({

View file

@ -10,6 +10,7 @@ import {
import { logger } from '../../../logger';
import type { Pr } from '../../../modules/platform/types';
import * as _cache from '../../../util/cache/repository';
import type { LongCommitSha } from '../../../util/git/types';
import * as _merge from '../init/merge';
import { validateReconfigureBranch } from '.';
@ -32,7 +33,7 @@ describe('workers/repository/reconfigure/index', () => {
merge.detectConfigFile.mockResolvedValue('renovate.json');
scm.branchExists.mockResolvedValue(true);
cache.getCache.mockReturnValue({});
git.getBranchCommit.mockReturnValue('sha');
git.getBranchCommit.mockReturnValue('sha' as LongCommitSha);
fs.readLocalFile.mockResolvedValue(null);
platform.getBranchPr.mockResolvedValue(null);
platform.getBranchStatusCheck.mockResolvedValue(null);

View file

@ -1,5 +1,6 @@
import { scm } from '../../../../../test/util';
import { GlobalConfig } from '../../../../config/global';
import type { LongCommitSha } from '../../../../util/git/types';
import type { BranchConfig } from '../../../types';
import { commitFilesToBranch } from './commit';
@ -20,7 +21,7 @@ describe('workers/repository/update/branch/commit', () => {
updatedArtifacts: [],
upgrades: [],
} satisfies BranchConfig;
scm.commitAndPush.mockResolvedValueOnce('123test');
scm.commitAndPush.mockResolvedValueOnce('123test' as LongCommitSha);
GlobalConfig.reset();
});

View file

@ -4,13 +4,14 @@ import { GlobalConfig } from '../../../../config/global';
import { CONFIG_SECRETS_EXPOSED } from '../../../../constants/error-messages';
import { logger } from '../../../../logger';
import { scm } from '../../../../modules/platform/scm';
import type { LongCommitSha } from '../../../../util/git/types';
import { minimatch } from '../../../../util/minimatch';
import { sanitize } from '../../../../util/sanitize';
import type { BranchConfig } from '../../../types';
export function commitFilesToBranch(
config: BranchConfig,
): Promise<string | null> {
): Promise<LongCommitSha | null> {
let updatedFiles = config.updatedPackageFiles!.concat(
config.updatedArtifacts!,
);

View file

@ -28,7 +28,11 @@ import type {
import { hashBody } from '../../../../modules/platform/pr-body';
import * as _repoCache from '../../../../util/cache/repository';
import * as _exec from '../../../../util/exec';
import type { FileChange, StatusResult } from '../../../../util/git/types';
import type {
FileChange,
LongCommitSha,
StatusResult,
} from '../../../../util/git/types';
import * as _mergeConfidence from '../../../../util/merge-confidence';
import * as _sanitize from '../../../../util/sanitize';
import * as _limits from '../../../global/limits';
@ -117,7 +121,7 @@ describe('workers/repository/update/branch/index', () => {
major: undefined,
} satisfies BranchConfig;
schedule.isScheduledNow.mockReturnValue(true);
commit.commitFilesToBranch.mockResolvedValue('123test');
commit.commitFilesToBranch.mockResolvedValue('123test' as LongCommitSha);
platform.massageMarkdown.mockImplementation((prBody) => prBody);
prWorker.ensurePr.mockResolvedValue({
@ -495,7 +499,7 @@ describe('workers/repository/update/branch/index', () => {
it('continues branch if branch edited and but PR found', async () => {
scm.branchExists.mockResolvedValue(true);
scm.isBranchModified.mockResolvedValueOnce(true);
scm.getBranchCommit.mockResolvedValue('123test');
scm.getBranchCommit.mockResolvedValue('123test' as LongCommitSha);
platform.findPr.mockResolvedValueOnce({ sha: '123test' } as any);
const res = await branchWorker.processBranch(config);
expect(res).toEqual({
@ -661,7 +665,7 @@ describe('workers/repository/update/branch/index', () => {
updatedArtifacts: [partial<FileChange>()],
});
scm.branchExists.mockResolvedValue(false);
scm.getBranchCommit.mockResolvedValue('123test'); //TODO: not needed?
scm.getBranchCommit.mockResolvedValue('123test' as LongCommitSha); //TODO: not needed?
automerge.tryBranchAutomerge.mockResolvedValueOnce('automerged');
await branchWorker.processBranch({
...config,
@ -838,7 +842,7 @@ describe('workers/repository/update/branch/index', () => {
commitBody: '[skip-ci]',
fetchChangeLogs: 'branch',
} satisfies BranchConfig;
scm.getBranchCommit.mockResolvedValue('123test'); //TODO:not needed?
scm.getBranchCommit.mockResolvedValue('123test' as LongCommitSha); //TODO:not needed?
expect(await branchWorker.processBranch(inconfig)).toEqual({
branchExists: true,
updatesVerified: true,
@ -1083,7 +1087,7 @@ describe('workers/repository/update/branch/index', () => {
artifactErrors: [partial<ArtifactError>()],
updatedArtifacts: [partial<FileChange>()],
});
scm.getBranchCommit.mockResolvedValue('123test'); //TODO:not needed?
scm.getBranchCommit.mockResolvedValue('123test' as LongCommitSha); //TODO:not needed?
const processBranchResult = await branchWorker.processBranch(config);
expect(processBranchResult).toEqual({
branchExists: true,
@ -1141,7 +1145,7 @@ describe('workers/repository/update/branch/index', () => {
prWorker.ensurePr.mockImplementationOnce(() => {
throw new Error('some error');
});
scm.getBranchCommit.mockResolvedValue('123test'); //TODO:not needed?
scm.getBranchCommit.mockResolvedValue('123test' as LongCommitSha); //TODO:not needed?
const processBranchResult = await branchWorker.processBranch(config);
expect(processBranchResult).toEqual({
branchExists: true,
@ -1356,7 +1360,7 @@ describe('workers/repository/update/branch/index', () => {
it('skips branch update if same updates', async () => {
scm.branchExists.mockResolvedValueOnce(true);
scm.getBranchCommit.mockResolvedValue('111'); //TODO:not needed?
scm.getBranchCommit.mockResolvedValue('111' as LongCommitSha); //TODO:not needed?
platform.getBranchPr.mockResolvedValueOnce(
partial<Pr>({
sourceBranch: 'old/some-branch',
@ -2096,7 +2100,7 @@ describe('workers/repository/update/branch/index', () => {
branchPrefix: 'new/',
branchPrefixOld: 'old/',
};
scm.getBranchCommit.mockResolvedValue('123test'); //TODO:not needed?
scm.getBranchCommit.mockResolvedValue('123test' as LongCommitSha); //TODO:not needed?
expect(await branchWorker.processBranch(inconfig)).toEqual({
branchExists: true,
updatesVerified: true,
@ -2169,7 +2173,7 @@ describe('workers/repository/update/branch/index', () => {
},
}),
);
scm.getBranchCommit.mockResolvedValue('123test'); //TODO:not needed?
scm.getBranchCommit.mockResolvedValue('123test' as LongCommitSha); //TODO:not needed?
expect(
await branchWorker.processBranch({
...config,
@ -2206,7 +2210,7 @@ describe('workers/repository/update/branch/index', () => {
},
}),
);
scm.getBranchCommit.mockResolvedValue('123test'); //TODO:not needed?
scm.getBranchCommit.mockResolvedValue('123test' as LongCommitSha); //TODO:not needed?
expect(
await branchWorker.processBranch({
...config,
@ -2234,7 +2238,7 @@ describe('workers/repository/update/branch/index', () => {
});
scm.branchExists.mockResolvedValueOnce(true);
scm.isBranchModified.mockResolvedValueOnce(true);
scm.getBranchCommit.mockResolvedValueOnce('123test');
scm.getBranchCommit.mockResolvedValueOnce('123test' as LongCommitSha);
platform.findPr.mockResolvedValueOnce({ sha: '123test' } as any);
const res = await branchWorker.processBranch(config);
expect(automerge.tryBranchAutomerge).not.toHaveBeenCalled();
@ -2251,7 +2255,7 @@ describe('workers/repository/update/branch/index', () => {
it('continues to update PR, if branch got updated, even when prCreation!==immediate', async () => {
scm.branchExists.mockResolvedValueOnce(true);
scm.isBranchModified.mockResolvedValueOnce(false);
scm.getBranchCommit.mockResolvedValueOnce('123test');
scm.getBranchCommit.mockResolvedValueOnce('123test' as LongCommitSha);
npmPostExtract.getAdditionalFiles.mockResolvedValueOnce({
artifactErrors: [],
updatedArtifacts: [partial<FileChange>()],