Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(platform/azure): Recreate Auto-Approve on PR-Update #22028

Merged
merged 11 commits into from May 11, 2023
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,
getPullRequest: jest.fn(() => ({
horihel marked this conversation as resolved.
Show resolved Hide resolved
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 @@
prTitle: title,
prBody: body,
state,
platformOptions,
}: UpdatePrConfig): Promise<void> {
logger.debug(`updatePr(${prNo}, ${title}, body)`);

Expand All @@ -527,6 +528,21 @@
} else if (state === 'closed') {
objToUpdate.status = PullRequestStatus.Abandoned;
}
if (platformOptions?.autoApprove) {
const pr = await azureApiGit.getPullRequestById(prNo, config.project);

Check failure on line 532 in lib/modules/platform/azure/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

modules/platform/azure/index › updatePr(prNo

TypeError: azureApiGit.getPullRequestById is not a function at Object.getPullRequestById [as updatePr] (lib/modules/platform/azure/index.ts:532:34) at Object.<anonymous> (lib/modules/platform/azure/index.spec.ts:912:18)
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