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

fix(gerrit): not auto-merging if Code-Review changed from +2 to +1 #32818

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

felipecrs
Copy link
Contributor

@felipecrs felipecrs commented Nov 29, 2024

Changes

The checkIfApproved function was not working as expected when someone already had published a Code-Review +1 vote. It was wrongly assuming that the change was approved in such case, while the change should only be approved if the change had a Code-Review +2 vote.

Note that when o=DETAILED_LABELS is set, the change response from Gerrit API no longer contains the content from o=LABELS, meaning it should only ever be used in this case (when looking for the approvals), otherwise it would break existing code.

This fix also ensures that the change will be auto-approved again during the auto-merge process, it case the Code-Review +2 was changed to +1 by some CI check using the same account as Renovate, like SonarQube (which posts Code-Review +1).

Finally, this fix will improve the approval logic for when the change is initially created or updated. It will avoid two API calls in each case.

Context

I self-host Renovate and I noticed that auto-merge was not working as expected. I realized it was caused by this issue, because for a given project we have here, the same user/robot also votes the change with a Code-Review +1 vote when SonarQube analysis is successful (which is a very known pattern).

Documentation (please check one with an [x])

  • I have updated the documentation, or
  • No documentation update is required

How I've tested my work (please select one)

I have verified these changes via:

  • Code inspection only, or
  • Newly added/modified unit tests, or
  • No unit tests but ran on a real repository, or
  • Both unit tests + ran on a real repository

@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from 5b42180 to 4028843 Compare November 29, 2024 23:14
@felipecrs felipecrs marked this pull request as draft November 29, 2024 23:33
@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from 4028843 to 06ecde1 Compare November 29, 2024 23:53
@felipecrs felipecrs marked this pull request as ready for review November 29, 2024 23:53
@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from 06ecde1 to 46f8eed Compare November 29, 2024 23:58
@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from 82fe8e1 to 107a298 Compare November 30, 2024 01:52
@felipecrs felipecrs marked this pull request as draft November 30, 2024 06:18
@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from dcc53d7 to 6ac7b9e Compare November 30, 2024 07:12
@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from 4817e82 to 6ffa5b6 Compare November 30, 2024 07:18
@felipecrs felipecrs marked this pull request as ready for review November 30, 2024 07:24
rarkins
rarkins previously approved these changes Nov 30, 2024
Copy link
Member

@viceice viceice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conflicted

@felipecrs
Copy link
Contributor Author

I just realized this function is also implemented by the GitLab provider, and there it doesn't check for whether it's approved already or not, meaning it will probably issue an error, which is not desired.

I'll think about it.

@felipecrs felipecrs marked this pull request as draft December 1, 2024 15:29
@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from 56bd600 to 761106b Compare December 1, 2024 16:38
@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from 761106b to 45763e9 Compare December 1, 2024 16:40
@felipecrs
Copy link
Contributor Author

@viceice, @rarkins, can you please help me understand this failed check?

❌ Error: Snapshots have been added in this PR. Use toMatch instead.

@felipecrs felipecrs marked this pull request as ready for review December 1, 2024 16:43
@felipecrs
Copy link
Contributor Author

felipecrs commented Dec 1, 2024

I've refactored the other two platforms (Azure and GitLab) that also use autoApprove to account for these improvements as well:

  1. As last resort, the PR will be attempted to be auto-approved before auto-merge, in case the approval got dismissed after it was initially published for whatever reason.
  2. It will no longer attempt to auto-approve if the PR is already approved

Note I do not use Azure or GitLab. I coded these functions based on their documentation. But I believe there is no big risk involved, given it will still attempt to auto-approve in case the is approved condition doesn't match.

@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from 45763e9 to 5a0955c Compare December 1, 2024 16:49
@felipecrs felipecrs marked this pull request as draft December 1, 2024 16:51
@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from 5a0955c to ab9907a Compare December 1, 2024 17:06
@felipecrs felipecrs marked this pull request as ready for review December 1, 2024 17:06
@felipecrs felipecrs requested review from rarkins and viceice December 1, 2024 17:07
@felipecrs felipecrs changed the title fix(gerrit): not auto-approving if change already had a Code-Review +1 feat(autoApprove): approve change before auto-merging if not already approved Dec 1, 2024
@felipecrs felipecrs changed the title feat(autoApprove): approve change before auto-merging if not already approved feat(autoApprove): auto-approve PR before auto-merge attempt if needed Dec 1, 2024
@felipecrs
Copy link
Contributor Author

I've retitled the PR to reflect the broader impact of it.

@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from ab9907a to c679c28 Compare December 1, 2024 17:17
Comment on lines 74 to 77
if (config.autoApprove && platform.approvePr) {
logger.debug('Auto-approving PR if needed before automerge');
await platform.approvePr(pr.number);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this cause each PR to be approved on each run? i.e. one extra API call per PR per run?

Copy link
Contributor Author

@felipecrs felipecrs Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this cause each PR to be approved on each run?

Only if the PR is not yet approved. I implemented all approvePr functions to account for this case.

i.e. one extra API call per PR per run?

Per auto-merge attempt, there will be one extra API call to verify whether the PR is not yet approved, and one more API call to approve it in case it's not yet approved.

Just remember:

  1. this is only applicable for azure, gitlab, and gerrit
  2. and for people who have autoApprove: true
  3. and all other conditions in automerge.ts already passed (except branchStatus, which depends on the approval to be green)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if you believe it's not a good idea, I can find another way.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly it could be optimized so that it doesn't perform this "is the PR already approved?" check right after we create the PR? i.e. because we know it's impossible that we approved it already.

lib/modules/platform/gitlab/index.ts Outdated Show resolved Hide resolved
Comment on lines 74 to 77
if (config.autoApprove && platform.approvePr) {
logger.debug('Auto-approving PR if needed before automerge');
await platform.approvePr(pr.number);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly it could be optimized so that it doesn't perform this "is the PR already approved?" check right after we create the PR? i.e. because we know it's impossible that we approved it already.

@felipecrs
Copy link
Contributor Author

@rarkins, it's presumptuous from my part that this would be of any help to the existing platforms. I will revert the changes to Azure and GitLab, and focus on Gerrit, which is what I use and know this would make sense for.

@felipecrs felipecrs marked this pull request as draft December 2, 2024 17:02
@felipecrs felipecrs changed the title feat(autoApprove): auto-approve PR before auto-merge attempt if needed fix(gerrit): not auto-merging if Code-Review vote was downgraded Dec 2, 2024
@felipecrs felipecrs changed the title fix(gerrit): not auto-merging if Code-Review vote was downgraded fix(gerrit): not auto-merging if Code-Review changed from +2 to +1 Dec 2, 2024
@felipecrs felipecrs requested a review from rarkins December 2, 2024 18:32
@felipecrs felipecrs marked this pull request as ready for review December 2, 2024 18:32
const changes = await this.gerritHttp.getJson<GerritChange>(
`a/changes/${changeNumber}?` +
this.requestDetails.map((det) => `o=${det}`).join('&'),
`a/changes/${changeNumber}?` + options.map((det) => `o=${det}`).join('&'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use getQueryString for proper quoting

export function getQueryString(params: Record<string, any>): string {

change.labels?.['Code-Review'].approved &&
change.labels['Code-Review'].approved.username === username
reviewLabel === undefined ||
Boolean(reviewLabel.all?.some((label) => label.value === 2))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Boolean(reviewLabel.all?.some((label) => label.value === 2))
reviewLabel.all?.some((label) => label.value === 2) === true

lib/modules/platform/gerrit/index.spec.ts Show resolved Hide resolved
config.autoApprove = true;
config.automerge = true;
config.pruneBranchAfterAutomerge = true;
platform.approvePrForAutomerge.mockResolvedValueOnce();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
platform.approvePrForAutomerge.mockResolvedValueOnce();

not needed, default for a mock

lib/modules/platform/gerrit/scm.ts Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants