Skip to content

Commit

Permalink
refactor: strongly type commit SHAs (#25207)
Browse files Browse the repository at this point in the history
Co-authored-by: Rhys Arkins <[email protected]>
  • Loading branch information
Tom-V and rarkins authored Nov 22, 2023
1 parent 4f8e802 commit c82b9af
Show file tree
Hide file tree
Showing 27 changed files with 197 additions and 124 deletions.
3 changes: 2 additions & 1 deletion lib/modules/platform/bitbucket-server/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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'
Expand Down
8 changes: 4 additions & 4 deletions lib/modules/platform/default-scm.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand Down Expand Up @@ -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);
});
Expand Down
4 changes: 3 additions & 1 deletion lib/modules/platform/gitea/gitea-helper.spec.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 7 additions & 5 deletions lib/modules/platform/gitea/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 }),
},
}),
Expand All @@ -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 }),
},
}),
Expand All @@ -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 }),
},
}),
Expand Down Expand Up @@ -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' },
Expand Down
4 changes: 3 additions & 1 deletion lib/modules/platform/gitea/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { LongCommitSha } from '../../../util/git/types';

export interface PrReviewersParams {
reviewers?: string[];
team_reviewers?: string[];
Expand Down Expand Up @@ -28,7 +30,7 @@ export interface PR {
};
head?: {
label: string;
sha: string;
sha: LongCommitSha;
repo?: Repo;
};
assignee?: {
Expand Down
29 changes: 22 additions & 7 deletions lib/modules/platform/github/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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 () => {
Expand Down
7 changes: 5 additions & 2 deletions lib/modules/platform/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/platform/github/scm.spec.ts
Original file line number Diff line number Diff line change
@@ -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 '.';

Expand All @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/platform/github/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { LongCommitSha } from '../../../util/git/types';
import type { Pr, PrBodyStruct } from '../types';

// https://developer.github.com/v3/repos/statuses
Expand Down Expand Up @@ -33,7 +34,7 @@ export interface GhRestRepo {
export interface GhRestPr {
head: {
ref: string;
sha: string;
sha: LongCommitSha;
repo: {
full_name: string;
pushed_at?: string;
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/platform/gitlab/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/platform/gitlab/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { LongCommitSha } from '../../../util/git/types';
import type { Pr } from '../types';

export interface GitlabIssue {
Expand Down Expand Up @@ -31,7 +32,7 @@ export interface GitLabMergeRequest {
assignees?: GitLabUser[];
reviewers?: GitLabUser[];
labels: string[];
sha: string;
sha: LongCommitSha;
head_pipeline?: {
status: string;
};
Expand Down
9 changes: 6 additions & 3 deletions lib/modules/platform/local/scm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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> {
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/platform/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export interface Pr {
labels?: string[];
number: number;
reviewers?: string[];
sha?: string;
sha?: LongCommitSha;
sourceRepo?: string;
state: string;
targetBranch?: string;
Expand Down
Loading

0 comments on commit c82b9af

Please sign in to comment.