Skip to content

Commit

Permalink
fix(gitlab): Fix the platform test performance (#34410)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov authored Feb 23, 2025
1 parent f5d01de commit b78eb9b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 53 deletions.
94 changes: 41 additions & 53 deletions lib/modules/platform/gitlab/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// TODO fix mocks
import type * as _timers from 'timers/promises';
import _timers from 'timers/promises';
import { mockDeep } from 'jest-mock-extended';
import type { Platform, RepoParams } from '..';
import type { RepoParams } from '..';
import * as httpMock from '../../../../test/http-mock';
import { mocked } from '../../../../test/util';
import { git, hostRules, logger, mocked } from '../../../../test/util';
import {
CONFIG_GIT_URL_UNAVAILABLE,
REPOSITORY_ARCHIVED,
Expand All @@ -12,36 +12,23 @@ import {
REPOSITORY_EMPTY,
REPOSITORY_MIRRORED,
} from '../../../constants/error-messages';
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';
import { getPrBodyStruct } from '../pr-body';
import * as prBodyModule from '../utils/pr-body';
import * as gitlab from '.';

jest.mock('../../../util/host-rules', () => mockDeep());
jest.mock('../../../util/git');
jest.mock('timers/promises');

const timers = mocked(_timers);

const gitlabApiHost = 'https://gitlab.com';

describe('modules/platform/gitlab/index', () => {
let gitlab: Platform;
let hostRules: jest.Mocked<typeof _hostRules>;
let git: jest.Mocked<typeof _git>;
let logger: jest.Mocked<typeof _logger>;
let timers: jest.Mocked<typeof _timers>;

beforeEach(async () => {
// reset module
jest.resetModules();

gitlab = await import('.');
logger = mocked(await import('../../../logger')).logger;
timers = jest.requireMock('timers/promises');
hostRules = jest.requireMock('../../../util/host-rules');
git = jest.requireMock('../../../util/git');
beforeEach(() => {
git.branchExists.mockReturnValue(true);
git.isBranchBehindBase.mockResolvedValue(true);
git.getBranchCommit.mockReturnValue(
Expand All @@ -55,6 +42,9 @@ describe('modules/platform/gitlab/index', () => {
delete process.env.RENOVATE_X_GITLAB_BRANCH_STATUS_DELAY;
delete process.env.RENOVATE_X_GITLAB_AUTO_MERGEABLE_CHECK_ATTEMPS;
delete process.env.RENOVATE_X_GITLAB_MERGE_REQUEST_DELAY;
delete process.env.RENOVATE_X_PLATFORM_VERSION;

gitlab.resetPlatform();
});

async function initFakePlatform(version: string) {
Expand All @@ -77,11 +67,11 @@ describe('modules/platform/gitlab/index', () => {
}

describe('initPlatform()', () => {
it(`should throw if no token`, async () => {
it('should throw if no token', async () => {
await expect(gitlab.initPlatform({} as any)).rejects.toThrow();
});

it(`should throw if auth fails`, async () => {
it('should throw if auth fails', async () => {
// user
httpMock.scope(gitlabApiHost).get('/api/v4/user').reply(403);
const res = gitlab.initPlatform({
Expand All @@ -91,7 +81,7 @@ describe('modules/platform/gitlab/index', () => {
await expect(res).rejects.toThrow('Init: Authentication failure');
});

it(`should default to gitlab.com`, async () => {
it('should default to gitlab.com', async () => {
httpMock.scope(gitlabApiHost).get('/api/v4/user').reply(200, {
email: 'a@b.com',
name: 'Renovate Bot',
Expand All @@ -107,7 +97,7 @@ describe('modules/platform/gitlab/index', () => {
).toMatchSnapshot();
});

it(`should accept custom endpoint`, async () => {
it('should accept custom endpoint', async () => {
const endpoint = 'https://gitlab.renovatebot.com';
httpMock
.scope(endpoint)
Expand All @@ -128,7 +118,7 @@ describe('modules/platform/gitlab/index', () => {
).toMatchSnapshot();
});

it(`should reuse existing gitAuthor`, async () => {
it('should reuse existing gitAuthor', async () => {
httpMock.scope(gitlabApiHost).get('/api/v4/version').reply(200, {
version: '13.3.6-ee',
});
Expand Down Expand Up @@ -502,7 +492,7 @@ describe('modules/platform/gitlab/index', () => {
merge_method: 'merge',
},
);
expect(await gitlab.getBranchForceRebase!('master')).toBeFalse();
expect(await gitlab.getBranchForceRebase()).toBeFalse();
});

it('should return true', async () => {
Expand All @@ -516,7 +506,7 @@ describe('modules/platform/gitlab/index', () => {
merge_method: 'ff',
},
);
expect(await gitlab.getBranchForceRebase!('master')).toBeTrue();
expect(await gitlab.getBranchForceRebase()).toBeTrue();
});
});

Expand Down Expand Up @@ -1068,7 +1058,7 @@ describe('modules/platform/gitlab/index', () => {
state: 'green',
url: 'some-url',
});
expect(logger.warn).toHaveBeenCalledWith(
expect(logger.logger.warn).toHaveBeenCalledWith(
'Failed to get the branch commit SHA',
);
});
Expand Down Expand Up @@ -1099,7 +1089,7 @@ describe('modules/platform/gitlab/index', () => {
state: 'green',
url: 'some-url',
});
expect(logger.warn).toHaveBeenCalledWith(
expect(logger.logger.warn).toHaveBeenCalledWith(
'Failed to retrieve commit pipeline',
);
});
Expand Down Expand Up @@ -1227,13 +1217,13 @@ describe('modules/platform/gitlab/index', () => {

expect(timers.setTimeout.mock.calls).toHaveLength(retry + 1);
expect(timers.setTimeout.mock.calls[0][0]).toBe(delay);
expect(logger.debug).toHaveBeenCalledWith(
expect(logger.logger.debug).toHaveBeenCalledWith(
`Pipeline not yet created. Retrying 1`,
);
expect(logger.debug).toHaveBeenCalledWith(
expect(logger.logger.debug).toHaveBeenCalledWith(
`Pipeline not yet created. Retrying 2`,
);
expect(logger.debug).toHaveBeenCalledWith(
expect(logger.logger.debug).toHaveBeenCalledWith(
`Pipeline not yet created after 3 attempts`,
);
});
Expand Down Expand Up @@ -1571,13 +1561,13 @@ describe('modules/platform/gitlab/index', () => {
await expect(
gitlab.addAssignees(42, ['someuser', 'someotheruser']),
).toResolve();
expect(logger.warn).toHaveBeenCalledWith(
expect(logger.logger.warn).toHaveBeenCalledWith(
{
assignee: 'someuser',
},
'Failed to add assignee - could not get ID',
);
expect(logger.debug).toHaveBeenCalledWith(
expect(logger.logger.debug).toHaveBeenCalledWith(
{
assignee: 'someuser',
err: new Error(
Expand All @@ -1594,7 +1584,7 @@ describe('modules/platform/gitlab/index', () => {
it('should not be supported in too low version', async () => {
await initFakePlatform('13.8.0');
await gitlab.addReviewers(42, ['someuser', 'foo', 'someotheruser']);
expect(logger.warn).toHaveBeenCalledWith(
expect(logger.logger.warn).toHaveBeenCalledWith(
{ version: '13.8.0' },
'Adding reviewers is only available in GitLab 13.9 and onwards',
);
Expand Down Expand Up @@ -2206,13 +2196,13 @@ describe('modules/platform/gitlab/index', () => {
sourceBranch: 'some-branch',
title: 'some title',
});
expect(logger.debug).toHaveBeenCalledWith(
expect(logger.logger.debug).toHaveBeenCalledWith(
'PR not yet in mergeable state. Retrying 1',
);
expect(logger.debug).toHaveBeenCalledWith(
expect(logger.logger.debug).toHaveBeenCalledWith(
'PR not yet in mergeable state. Retrying 2',
);
expect(logger.debug).toHaveBeenCalledWith(
expect(logger.logger.debug).toHaveBeenCalledWith(
'PR not yet in mergeable state. Retrying 3',
);
expect(timers.setTimeout.mock.calls).toMatchObject([
Expand Down Expand Up @@ -2262,13 +2252,13 @@ describe('modules/platform/gitlab/index', () => {
sourceBranch: 'some-branch',
title: 'some title',
});
expect(logger.debug).toHaveBeenCalledWith(
expect(logger.logger.debug).toHaveBeenCalledWith(
'PR not yet in mergeable state. Retrying 1',
);
expect(logger.debug).toHaveBeenCalledWith(
expect(logger.logger.debug).toHaveBeenCalledWith(
'PR not yet in mergeable state. Retrying 2',
);
expect(logger.debug).toHaveBeenCalledWith(
expect(logger.logger.debug).toHaveBeenCalledWith(
'PR not yet in mergeable state. Retrying 3',
);
expect(timers.setTimeout.mock.calls).toMatchObject([[100], [400], [900]]);
Expand Down Expand Up @@ -2317,20 +2307,20 @@ describe('modules/platform/gitlab/index', () => {
sourceBranch: 'some-branch',
title: 'some title',
});
expect(logger.debug).toHaveBeenCalledWith(
expect(logger.logger.debug).toHaveBeenCalledWith(
'PR not yet in mergeable state. Retrying 1',
);
expect(logger.debug).toHaveBeenCalledWith(
expect(logger.logger.debug).toHaveBeenCalledWith(
'PR not yet in mergeable state. Retrying 2',
);
expect(logger.debug).toHaveBeenCalledWith(
expect(logger.logger.debug).toHaveBeenCalledWith(
'PR not yet in mergeable state. Retrying 3',
);
expect(logger.debug).toHaveBeenCalledWith(
expect(logger.logger.debug).toHaveBeenCalledWith(
expect.any(Object),
'Automerge on PR creation failed. Retrying 1',
);
expect(logger.debug).toHaveBeenCalledWith(
expect(logger.logger.debug).toHaveBeenCalledWith(
expect.any(Object),
'Automerge on PR creation failed. Retrying 2',
);
Expand Down Expand Up @@ -3381,7 +3371,7 @@ describe('modules/platform/gitlab/index', () => {

await expect(gitlab.reattemptPlatformAutomerge?.(pr)).toResolve();

expect(logger.debug).toHaveBeenLastCalledWith(
expect(logger.logger.debug).toHaveBeenLastCalledWith(
'PR platform automerge re-attempted...prNo: 12345',
);
});
Expand Down Expand Up @@ -3444,8 +3434,7 @@ These updates have all been created already. Click a checkbox below to force a r
});

it('truncates description if too low API version', async () => {
jest.doMock('../utils/pr-body');
const { smartTruncate } = await import('../utils/pr-body');
const smartTruncate = jest.spyOn(prBodyModule, 'smartTruncate');

await initFakePlatform('13.3.0');
gitlab.massageMarkdown(prBody);
Expand All @@ -3454,8 +3443,7 @@ These updates have all been created already. Click a checkbox below to force a r
});

it('truncates description for API version gt 13.4', async () => {
jest.doMock('../utils/pr-body');
const { smartTruncate } = await import('../utils/pr-body');
const smartTruncate = jest.spyOn(prBodyModule, 'smartTruncate');

await initFakePlatform('13.4.1');
gitlab.massageMarkdown(prBody);
Expand Down Expand Up @@ -3667,7 +3655,7 @@ These updates have all been created already. Click a checkbox below to force a r
'@group',
]);
expect(expandedGroupMembers).toEqual(['group']);
expect(logger.debug).toHaveBeenCalledWith(
expect(logger.logger.debug).toHaveBeenCalledWith(
expect.any(Object),
'Unable to fetch group',
);
Expand Down
9 changes: 9 additions & 0 deletions lib/modules/platform/gitlab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ let config: {
squash: boolean;
} = {} as any;

export function resetPlatform(): void {
config = {} as any;
draftPrefix = DRAFT_PREFIX;
defaults.hostType = 'gitlab';
defaults.endpoint = 'https://gitlab.com/api/v4/';
defaults.version = '0.0.0';
setBaseUrl(defaults.endpoint);
}

const defaults = {
hostType: 'gitlab',
endpoint: 'https://gitlab.com/api/v4/',
Expand Down

0 comments on commit b78eb9b

Please sign in to comment.