From 08233ff7054a87608f704edeaaa1f93b0093c73f Mon Sep 17 00:00:00 2001 From: horihel Date: Thu, 11 May 2023 07:15:33 +0200 Subject: [PATCH] feat(platform/azure): Recreate Auto-Approve on PR-Update (#22028) Signed-off-by: Heiko Helmle Co-authored-by: Jamie Magee Co-authored-by: Rhys Arkins --- .../azure/__snapshots__/index.spec.ts.snap | 10 +++-- lib/modules/platform/azure/index.spec.ts | 39 ++++++++++++++++++- lib/modules/platform/azure/index.ts | 16 ++++++++ 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/lib/modules/platform/azure/__snapshots__/index.spec.ts.snap b/lib/modules/platform/azure/__snapshots__/index.spec.ts.snap index 21858fbbb4b3cc..3be9ce9411245c 100644 --- a/lib/modules/platform/azure/__snapshots__/index.spec.ts.snap +++ b/lib/modules/platform/azure/__snapshots__/index.spec.ts.snap @@ -274,7 +274,7 @@ exports[`modules/platform/azure/index initRepo should initialise the config for } `; -exports[`modules/platform/azure/index updatePr(prNo, title, body) should close the PR 1`] = ` +exports[`modules/platform/azure/index updatePr(prNo, title, body, platformOptions) should close the PR 1`] = ` [ [ { @@ -288,7 +288,9 @@ exports[`modules/platform/azure/index updatePr(prNo, title, body) should close t ] `; -exports[`modules/platform/azure/index updatePr(prNo, title, body) should reopen the PR 1`] = ` +exports[`modules/platform/azure/index updatePr(prNo, title, body, platformOptions) should re-approve the PR 1`] = `undefined`; + +exports[`modules/platform/azure/index updatePr(prNo, title, body, platformOptions) should reopen the PR 1`] = ` [ [ { @@ -308,7 +310,7 @@ exports[`modules/platform/azure/index updatePr(prNo, title, body) should reopen ] `; -exports[`modules/platform/azure/index updatePr(prNo, title, body) should update the PR 1`] = ` +exports[`modules/platform/azure/index updatePr(prNo, title, body, platformOptions) should update the PR 1`] = ` [ [ { @@ -321,7 +323,7 @@ exports[`modules/platform/azure/index updatePr(prNo, title, body) should update ] `; -exports[`modules/platform/azure/index updatePr(prNo, title, body) should update the PR without description 1`] = ` +exports[`modules/platform/azure/index updatePr(prNo, title, body, platformOptions) should update the PR without description 1`] = ` [ [ { diff --git a/lib/modules/platform/azure/index.spec.ts b/lib/modules/platform/azure/index.spec.ts index da8a998a7b702e..29493cd2421217 100644 --- a/lib/modules/platform/azure/index.spec.ts +++ b/lib/modules/platform/azure/index.spec.ts @@ -812,7 +812,7 @@ describe('modules/platform/azure/index', () => { }); }); - describe('updatePr(prNo, title, body)', () => { + describe('updatePr(prNo, title, body, platformOptions)', () => { it('should update the PR', async () => { await initRepo({ repository: 'some/repo' }); const updatePullRequest = jest.fn(); @@ -881,6 +881,43 @@ describe('modules/platform/azure/index', () => { }); expect(updatePullRequest.mock.calls).toMatchSnapshot(); }); + + it('should re-approve the PR', async () => { + await initRepo({ repository: 'some/repo' }); + const prResult = { + pullRequestId: 456, + createdBy: { + id: 123, + url: 'user-url', + }, + }; + const prUpdateResult = { + reviewerUrl: prResult.createdBy.url, + vote: AzurePrVote.Approved, + isFlagged: false, + isRequired: false, + }; + const updateFn = jest.fn(() => prUpdateResult); + azureApi.gitApi.mockImplementationOnce( + () => + ({ + updatePullRequest: jest.fn(() => prResult), + createPullRequestReviewer: updateFn, + getPullRequestById: jest.fn(() => ({ + pullRequestId: prResult.pullRequestId, + createdBy: prResult.createdBy, + })), + } as any) + ); + const pr = await azure.updatePr({ + number: prResult.pullRequestId, + prTitle: 'The Title', + prBody: 'Hello world', + platformOptions: { autoApprove: true }, + }); + expect(updateFn).toHaveBeenCalled(); + expect(pr).toMatchSnapshot(); + }); }); describe('ensureComment', () => { diff --git a/lib/modules/platform/azure/index.ts b/lib/modules/platform/azure/index.ts index b1eb1430369d14..f5b39106638bb2 100644 --- a/lib/modules/platform/azure/index.ts +++ b/lib/modules/platform/azure/index.ts @@ -506,6 +506,7 @@ export async function updatePr({ prTitle: title, prBody: body, state, + platformOptions, }: UpdatePrConfig): Promise { logger.debug(`updatePr(${prNo}, ${title}, body)`); @@ -527,6 +528,21 @@ export async function updatePr({ } else if (state === 'closed') { objToUpdate.status = PullRequestStatus.Abandoned; } + if (platformOptions?.autoApprove) { + const pr = await azureApiGit.getPullRequestById(prNo, config.project); + await azureApiGit.createPullRequestReviewer( + { + reviewerUrl: pr.createdBy!.url, + vote: AzurePrVote.Approved, + isFlagged: false, + isRequired: false, + }, + config.repoId, + // TODO #7154 + pr.pullRequestId!, + pr.createdBy!.id! + ); + } await azureApiGit.updatePullRequest(objToUpdate, config.repoId, prNo); }