From 03eb2223bebfc6c5bdf8166b0aadeda25cc77db3 Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Thu, 18 Aug 2022 20:43:17 +0200 Subject: [PATCH 1/3] fix(platform/azure): don't filter zero as repo id --- lib/modules/platform/azure/index.spec.ts | 149 +++++++++++++++-------- lib/modules/platform/azure/index.ts | 2 +- 2 files changed, 97 insertions(+), 54 deletions(-) diff --git a/lib/modules/platform/azure/index.spec.ts b/lib/modules/platform/azure/index.spec.ts index b02867336a2336..9667e332176e2b 100644 --- a/lib/modules/platform/azure/index.spec.ts +++ b/lib/modules/platform/azure/index.spec.ts @@ -135,35 +135,34 @@ describe('modules/platform/azure/index', () => { }); function initRepo(args?: Partial | string) { - azureApi.gitApi.mockImplementationOnce( - () => - ({ - getRepositories: jest.fn(() => [ - { - name: 'repo', - id: '1', - privateRepo: true, - isFork: false, - defaultBranch: 'defBr', - project: { - name: 'some', - }, + azureApi.gitApi.mockResolvedValueOnce( + partial({ + getRepositories: jest.fn().mockResolvedValue([ + { + name: 'repo', + id: '1', + privateRepo: true, + isFork: false, + defaultBranch: 'defBr', + project: { + name: 'some', }, - { - name: 'repo2', - project: { - name: 'prj2', - }, + }, + { + name: 'repo2', + project: { + name: 'prj2', }, - { - name: 'repo3', - project: { - name: 'some', - }, - isDisabled: true, + }, + { + name: 'repo3', + project: { + name: 'some', }, - ]), - } as any) + isDisabled: true, + }, + ]), + }) ); if (is.string(args)) { @@ -317,6 +316,19 @@ describe('modules/platform/azure/index', () => { }); expect(res).toMatchSnapshot(); }); + + it('catches errors', async () => { + azureApi.gitApi.mockResolvedValueOnce( + partial({ + getPullRequests: jest.fn().mockRejectedValueOnce(new Error()), + }) + ); + const res = await azure.findPr({ + branchName: 'branch-a', + prTitle: 'branch a pr', + }); + expect(res).toBeNull(); + }); }); describe('getPrList()', () => { @@ -334,14 +346,10 @@ describe('modules/platform/azure/index', () => { describe('getBranchPr(branchName)', () => { it('should return null if no PR exists', async () => { await initRepo({ repository: 'some/repo' }); - azureApi.gitApi.mockImplementationOnce( - () => - ({ - findPr: jest.fn(() => false), - getPr: jest.fn(() => { - 'myPRName'; - }), - } as any) + azureApi.gitApi.mockResolvedValue( + partial({ + getPullRequests: jest.fn().mockResolvedValueOnce([]), + }) ); const pr = await azure.getBranchPr('somebranch'); expect(pr).toBeNull(); @@ -349,26 +357,40 @@ describe('modules/platform/azure/index', () => { it('should return the pr', async () => { await initRepo({ repository: 'some/repo' }); - azureApi.gitApi.mockImplementation( - () => - ({ - getPullRequests: jest - .fn() - .mockReturnValue([]) - .mockReturnValueOnce([ - { - pullRequestId: 1, - sourceRefName: 'refs/heads/branch-a', - title: 'branch a pr', - status: 2, - }, - ]), - getPullRequestCommits: jest.fn().mockReturnValue([]), - } as any) + azureApi.gitApi.mockResolvedValue( + partial({ + getPullRequests: jest + .fn() + .mockResolvedValueOnce([ + { + pullRequestId: 1, + sourceRefName: 'refs/heads/branch-a', + title: 'branch a pr', + status: 1, + }, + ]) + .mockResolvedValueOnce([]), + getPullRequestLabels: jest.fn().mockResolvedValue([]), + }) ); - const pr = await azure.getBranchPr('somebranch'); - // TODO: should this return a PR instead? - expect(pr).toBeNull(); + const pr = await azure.getBranchPr('branch-a'); + expect(pr).toEqual({ + bodyStruct: { + hash: 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', + }, + createdAt: undefined, + displayNumber: 'Pull Request #1', + hasReviewers: false, + labels: [], + number: 1, + pullRequestId: 1, + sourceBranch: 'branch-a', + sourceRefName: 'refs/heads/branch-a', + state: 'open', + status: 1, + targetBranch: undefined, + title: 'branch a pr', + }); }); }); @@ -499,6 +521,27 @@ describe('modules/platform/azure/index', () => { expect(res).toBe(BranchStatus.yellow); }); + it('should return yellow if status is unknown', async () => { + await initRepo({ repository: 'some/repo' }); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + getBranch: jest.fn(() => ({ commit: { commitId: 'abcd1234' } })), + getStatuses: jest.fn(() => [ + { + state: -1, + context: { genre: 'a-genre', name: 'a-name' }, + }, + ]), + } as any) + ); + const res = await azure.getBranchStatusCheck( + 'somebranch', + 'a-genre/a-name' + ); + expect(res).toBe(BranchStatus.yellow); + }); + it('should return null if status not found', async () => { await initRepo({ repository: 'some/repo' }); azureApi.gitApi.mockImplementationOnce( diff --git a/lib/modules/platform/azure/index.ts b/lib/modules/platform/azure/index.ts index 3259fc07486a01..c3aa51947693d8 100644 --- a/lib/modules/platform/azure/index.ts +++ b/lib/modules/platform/azure/index.ts @@ -134,7 +134,7 @@ export async function getRawFile( repoId = config.repoId; } - if (!repoId) { + if (!is.string(repoId)) { return null; } From c5a120fc755c8e421e396807f15c199310a09984 Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Thu, 18 Aug 2022 21:24:18 +0200 Subject: [PATCH 2/3] chore: revert --- lib/modules/platform/azure/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/modules/platform/azure/index.ts b/lib/modules/platform/azure/index.ts index c3aa51947693d8..3259fc07486a01 100644 --- a/lib/modules/platform/azure/index.ts +++ b/lib/modules/platform/azure/index.ts @@ -134,7 +134,7 @@ export async function getRawFile( repoId = config.repoId; } - if (!is.string(repoId)) { + if (!repoId) { return null; } From bad6ec9f763aedf3a82827475598e419ee2c8c9f Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Thu, 18 Aug 2022 21:29:14 +0200 Subject: [PATCH 3/3] test: move cleanify --- lib/modules/platform/azure/index.spec.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/modules/platform/azure/index.spec.ts b/lib/modules/platform/azure/index.spec.ts index 9667e332176e2b..c234df87956723 100644 --- a/lib/modules/platform/azure/index.spec.ts +++ b/lib/modules/platform/azure/index.spec.ts @@ -523,17 +523,18 @@ describe('modules/platform/azure/index', () => { it('should return yellow if status is unknown', async () => { await initRepo({ repository: 'some/repo' }); - azureApi.gitApi.mockImplementationOnce( - () => - ({ - getBranch: jest.fn(() => ({ commit: { commitId: 'abcd1234' } })), - getStatuses: jest.fn(() => [ - { - state: -1, - context: { genre: 'a-genre', name: 'a-name' }, - }, - ]), - } as any) + azureApi.gitApi.mockResolvedValueOnce( + partial({ + getBranch: jest + .fn() + .mockResolvedValue({ commit: { commitId: 'abcd1234' } }), + getStatuses: jest.fn().mockResolvedValue([ + { + state: -1, + context: { genre: 'a-genre', name: 'a-name' }, + }, + ]), + }) ); const res = await azure.getBranchStatusCheck( 'somebranch',