Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(platform/azure): Recreate Auto-Approve on PR-Update (#22028)
Signed-off-by: Heiko Helmle <heiko.helmle@horiba.com>
Co-authored-by: Jamie Magee <jamie.magee@gmail.com>
Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
3 people committed May 11, 2023
1 parent eec46d1 commit 08233ff
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/modules/platform/azure/__snapshots__/index.spec.ts.snap
Expand Up @@ -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`] = `
[
[
{
Expand All @@ -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`] = `
[
[
{
Expand All @@ -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`] = `
[
[
{
Expand All @@ -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`] = `
[
[
{
Expand Down
39 changes: 38 additions & 1 deletion lib/modules/platform/azure/index.spec.ts
Expand Up @@ -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();
Expand Down Expand Up @@ -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', () => {
Expand Down
16 changes: 16 additions & 0 deletions lib/modules/platform/azure/index.ts
Expand Up @@ -506,6 +506,7 @@ export async function updatePr({
prTitle: title,
prBody: body,
state,
platformOptions,
}: UpdatePrConfig): Promise<void> {
logger.debug(`updatePr(${prNo}, ${title}, body)`);

Expand All @@ -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);
}
Expand Down

0 comments on commit 08233ff

Please sign in to comment.