Skip to content

Commit b56d0d8

Browse files
refactor: address review feedback
1 parent b685c00 commit b56d0d8

3 files changed

Lines changed: 45 additions & 144 deletions

File tree

lib/modules/platform/azure/index.spec.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
GitVersionType,
1212
PullRequestStatus,
1313
} from 'azure-devops-node-api/interfaces/GitInterfaces.js';
14+
import type { IWorkItemTrackingApi } from 'azure-devops-node-api/WorkItemTrackingApi.js';
1415
import type { Mocked, MockedObject } from 'vitest';
1516
import { vi } from 'vitest';
1617
import { mockDeep } from 'vitest-mock-extended';
@@ -2108,6 +2109,7 @@ describe('modules/platform/azure/index', () => {
21082109
});
21092110

21102111
it('findIssue returns null if not found', async () => {
2112+
await initRepo({ repository: 'some/repo' });
21112113
azureApi.workItemTrackingApi.mockImplementationOnce(
21122114
() =>
21132115
({
@@ -2121,33 +2123,32 @@ describe('modules/platform/azure/index', () => {
21212123
it('findIssue return first issue if multiple found', async () => {
21222124
await initRepo({ repository: 'some/repo' });
21232125

2124-
azureApi.workItemTrackingApi.mockImplementationOnce(
2125-
() =>
2126-
({
2127-
queryByWiql: vi.fn().mockResolvedValue({
2128-
workItems: [{ id: 123 }, { id: 456 }],
2129-
}),
2130-
getWorkItems: vi.fn().mockResolvedValue([
2131-
{
2132-
id: 123,
2133-
fields: {
2134-
'System.Title': 'The title',
2135-
'System.WorkItemType': 'Issue',
2136-
'System.State': 'Active',
2137-
'System.Description': 'fake',
2138-
},
2126+
azureApi.workItemTrackingApi.mockResolvedValueOnce(
2127+
partial<IWorkItemTrackingApi>({
2128+
queryByWiql: vi.fn().mockResolvedValue({
2129+
workItems: [{ id: 123 }, { id: 456 }],
2130+
}),
2131+
getWorkItems: vi.fn().mockResolvedValue([
2132+
{
2133+
id: 123,
2134+
fields: {
2135+
'System.Title': 'The title',
2136+
'System.WorkItemType': 'Issue',
2137+
'System.State': 'Active',
2138+
'System.Description': 'fake',
21392139
},
2140-
{
2141-
id: 456,
2142-
fields: {
2143-
'System.Title': 'The title',
2144-
'System.WorkItemType': 'Issue',
2145-
'System.State': 'New',
2146-
'System.Description': 'another fake description',
2147-
},
2140+
},
2141+
{
2142+
id: 456,
2143+
fields: {
2144+
'System.Title': 'The title',
2145+
'System.WorkItemType': 'Issue',
2146+
'System.State': 'New',
2147+
'System.Description': 'another fake description',
21482148
},
2149-
]),
2150-
}) as any,
2149+
},
2150+
]),
2151+
}),
21512152
);
21522153
const res = await azure.findIssue('The title');
21532154
expect(res).toMatchObject({
@@ -2580,6 +2581,7 @@ describe('modules/platform/azure/index', () => {
25802581
});
25812582

25822583
it('getIssueList handles empty wiql result', async () => {
2584+
await initRepo({ repository: 'some/repo' });
25832585
azureApi.workItemTrackingApi.mockImplementation(
25842586
() =>
25852587
({

lib/modules/platform/azure/index.ts

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ interface User {
7070
}
7171

7272
let config: Config = {} as any;
73+
let issueService: IssueService;
7374

7475
const defaults: {
7576
endpoint?: string;
@@ -80,25 +81,6 @@ const defaults: {
8081

8182
export const id = 'azure';
8283

83-
function createIssueOperations(config: Config): {
84-
findIssue: (title: string) => Promise<Issue | null>;
85-
ensureIssueClosing: (title: string) => Promise<void>;
86-
ensureIssue: (
87-
issueConfig: EnsureIssueConfig,
88-
) => Promise<EnsureIssueResult | null>;
89-
getIssueList: (titleFilter?: string) => Promise<Issue[]>;
90-
} {
91-
const service = new IssueService(config);
92-
93-
return {
94-
findIssue: (title: string) => service.findIssue(title),
95-
ensureIssueClosing: (title: string) => service.ensureIssueClosing(title),
96-
ensureIssue: (issueConfig: EnsureIssueConfig) =>
97-
service.ensureIssue(issueConfig),
98-
getIssueList: (titleFilter?: string) => service.getIssueList(titleFilter),
99-
};
100-
}
101-
10284
export function initPlatform({
10385
endpoint,
10486
token,
@@ -241,6 +223,7 @@ export async function initRepo({
241223
config.repoId = repo.id!;
242224

243225
config.project = repo.project!.name!;
226+
issueService = new IssueService(config);
244227
config.owner = '?owner?';
245228
logger.debug(`${repository} owner = ${config.owner}`);
246229
const defaultBranch = repo.defaultBranch.replace('refs/heads/', '');
@@ -891,21 +874,21 @@ export function maxBodyLength(): number {
891874
}
892875

893876
export async function findIssue(title: string): Promise<Issue | null> {
894-
return await createIssueOperations(config).findIssue(title);
877+
return await issueService.findIssue(title);
895878
}
896879

897880
export async function ensureIssue(
898881
issueConfig: EnsureIssueConfig,
899882
): Promise<EnsureIssueResult | null> {
900-
return await createIssueOperations(config).ensureIssue(issueConfig);
883+
return await issueService.ensureIssue(issueConfig);
901884
}
902885

903886
export async function ensureIssueClosing(title: string): Promise<void> {
904-
return await createIssueOperations(config).ensureIssueClosing(title);
887+
return await issueService.ensureIssueClosing(title);
905888
}
906889

907890
export async function getIssueList(titleFilter?: string): Promise<Issue[]> {
908-
return await createIssueOperations(config).getIssueList(titleFilter);
891+
return await issueService.getIssueList(titleFilter);
909892
}
910893

911894
async function getUserIds(users: string[]): Promise<User[]> {

lib/modules/platform/azure/issue.spec.ts

Lines changed: 12 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,29 @@
1-
import type { Mocked, MockedObject } from 'vitest';
21
import { vi } from 'vitest';
32
import { mockDeep } from 'vitest-mock-extended';
4-
import type { logger as _logger } from '../../../logger/index.ts';
5-
import type * as _hostRules from '../../../util/host-rules.ts';
6-
import type { Platform } from '../types.ts';
3+
import { logger } from '../../../logger/index.ts';
4+
import * as hostRules from '../../../util/host-rules.ts';
5+
import * as azureApi from './azure-got-wrapper.ts';
76
import { IssueService } from './issue.ts';
87
import type { Config } from './types.ts';
98

10-
vi.mock('./azure-got-wrapper', () => mockDeep());
11-
vi.mock('./azure-helper', () => mockDeep());
12-
vi.mock('../../../util/host-rules', () => mockDeep());
13-
vi.mock('../../../util/sanitize', () =>
9+
vi.mock('./azure-got-wrapper.ts', () => mockDeep());
10+
vi.mock('./azure-helper.ts', () => mockDeep());
11+
vi.mock('../../../util/host-rules.ts', () => mockDeep());
12+
vi.mock('../../../logger/index.ts', () => mockDeep());
13+
vi.mock('../../../util/sanitize.ts', () =>
1414
mockDeep({ sanitize: (s: string) => s }),
1515
);
16-
vi.mock('./util', () => ({
16+
vi.mock('./util.ts', () => ({
1717
getWorkItemTitle: vi.fn((title: string) => `[Renovate] ${title}`),
1818
}));
1919

2020
describe('modules/platform/azure/issue', () => {
21-
let hostRules: Mocked<typeof _hostRules>;
22-
let azure: Platform;
23-
let azureApi: Mocked<typeof import('./azure-got-wrapper.ts')>;
24-
let logger: MockedObject<typeof _logger>;
2521
let config: Config;
2622
let issueService: IssueService;
2723

28-
beforeEach(async () => {
29-
// reset module
30-
vi.resetModules();
31-
hostRules = await vi.importMock('../../../util/host-rules.ts');
32-
azure = await vi.importActual('./index.ts');
33-
azureApi = await vi.importMock('./azure-got-wrapper.ts');
34-
logger = (
35-
await vi.importMock<typeof import('../../../logger/index.ts')>(
36-
'../../../logger/index.ts',
37-
)
38-
).logger;
39-
hostRules.find.mockReturnValue({
40-
token: 'token',
41-
});
42-
43-
await azure.initPlatform({
44-
endpoint: 'https://dev.azure.com/renovate12345',
45-
token: 'token',
46-
});
24+
beforeEach(() => {
25+
vi.clearAllMocks();
26+
vi.mocked(hostRules.find).mockReturnValue({ token: 'token' });
4727

4828
config = {
4929
repository: 'test/repo',
@@ -683,68 +663,4 @@ describe('modules/platform/azure/issue', () => {
683663
expect(result).toBe('created');
684664
});
685665
});
686-
687-
describe('integration with main azure Platform', () => {
688-
it('getIssueList should work interface', async () => {
689-
azureApi.workItemTrackingApi.mockImplementation(
690-
() =>
691-
({
692-
queryByWiql: vi.fn().mockResolvedValue({
693-
workItems: [],
694-
}),
695-
}) as any,
696-
);
697-
698-
const result = await azure.getIssueList();
699-
expect(result).toEqual([]);
700-
});
701-
702-
it('findIssue should work', async () => {
703-
azureApi.workItemTrackingApi.mockImplementation(
704-
() =>
705-
({
706-
queryByWiql: vi.fn().mockResolvedValue({
707-
workItems: [],
708-
}),
709-
}) as any,
710-
);
711-
712-
const result = await azure.findIssue('Test Issue');
713-
expect(result).toBeNull();
714-
});
715-
716-
it('ensureIssue should work', async () => {
717-
azureApi.workItemTrackingApi.mockImplementation(
718-
() =>
719-
({
720-
queryByWiql: vi.fn().mockResolvedValue({
721-
workItems: [],
722-
}),
723-
createWorkItem: vi.fn().mockResolvedValue({ id: 123 }),
724-
}) as any,
725-
);
726-
727-
const result = await azure.ensureIssue({
728-
title: 'Test Issue',
729-
body: 'Test body content',
730-
});
731-
732-
expect(result).toEqual('created');
733-
});
734-
735-
it('ensureIssueClosing should work', async () => {
736-
azureApi.workItemTrackingApi.mockImplementation(
737-
() =>
738-
({
739-
queryByWiql: vi.fn().mockResolvedValue({
740-
workItems: [],
741-
}),
742-
}) as any,
743-
);
744-
745-
await expect(
746-
azure.ensureIssueClosing('Test Issue'),
747-
).resolves.not.toThrow();
748-
});
749-
});
750666
});

0 commit comments

Comments
 (0)