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

refactor(gerrit): remove deprecated source branch as hashtags support #33329

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
46f8eed
fix(gerrit): not auto-approving if change already had a Code-Review +1
felipecrs Nov 29, 2024
107a298
chore(gerrit): improve auto-approve on new patchset workaround
felipecrs Nov 30, 2024
6ac7b9e
chore(gerrit): try auto-approve right before auto-merge
felipecrs Nov 30, 2024
6ffa5b6
Merge branch 'main' into fix-auto-merge-if-plus-one
felipecrs Nov 30, 2024
7877331
Merge branch 'main' of https://github.com/renovatebot/renovate into f…
felipecrs Dec 1, 2024
1d8daf5
test(gerrit): fix approvePr coverage
felipecrs Dec 1, 2024
5eb2846
chore(gerrit): still approve if change was approved another user
felipecrs Dec 1, 2024
c679c28
feat(gitlab,azure): try approving before auto-merge
felipecrs Dec 1, 2024
dbdf41a
chore: add better jsdoc for approvePr
felipecrs Dec 1, 2024
bd05816
test(gerrit): fix lint in client.spec.ts
felipecrs Dec 1, 2024
ac30f2b
test(automerge): add test for auto approve before merging
felipecrs Dec 1, 2024
539c12f
Merge branch 'main' into fix-auto-merge-if-plus-one
felipecrs Dec 2, 2024
7923d5b
Revert "feat(gitlab,azure): try approving before auto-merge"
felipecrs Dec 2, 2024
c4c81e3
chore: rename approvePr to approvePrForAutomerge
felipecrs Dec 2, 2024
4dfc817
Revert "chore(gerrit): still approve if change was approved another u…
felipecrs Dec 2, 2024
bb317ce
chore(gerrit): improve approve method to avoid some API calls
felipecrs Dec 2, 2024
b1ce02b
Merge branch 'main' of https://github.com/renovatebot/renovate into f…
felipecrs Dec 2, 2024
53743b2
chore: fix type import
felipecrs Dec 2, 2024
e0b6f8d
chore: remove spurious comment
felipecrs Dec 2, 2024
74137a8
Merge branch 'main' of https://github.com/renovatebot/renovate into f…
felipecrs Dec 29, 2024
b93ee7a
Address review comments
felipecrs Dec 29, 2024
add6ca5
Add tests for approvePrForAutomerge
felipecrs Dec 29, 2024
a6d9d91
refactor(gerrit): remove deprecated source branch as hashtags support
felipecrs Dec 29, 2024
933f13d
Merge
felipecrs Jan 17, 2025
f700afb
Merge branch 'fix-auto-merge-if-plus-one' into gerrit-remove-deprecat…
felipecrs Jan 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 50 additions & 61 deletions lib/modules/platform/gerrit/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ describe('modules/platform/gerrit/client', () => {
'footer:Renovate-Branch=dependency-xyz',
{ branchName: 'dependency-xyz' },
],
['hashtag:sourceBranch-dependency-xyz', { branchName: 'dependency-xyz' }], // for backwards compatibility
['label:Code-Review=-2', { branchName: 'dependency-xyz', label: '-2' }],
[
'branch:otherTarget',
Expand Down Expand Up @@ -399,10 +398,20 @@ describe('modules/platform/gerrit/client', () => {

describe('approveChange()', () => {
it('already approved - do nothing', async () => {
const change = partial<GerritChange>({});
const change = partial<GerritChange>({
labels: {
'Code-Review': {
all: [{ value: 2 }],
},
},
});
httpMock
.scope(gerritEndpointUrl)
.get((url) => url.includes('/a/changes/123456?o='))
.get(
(url) =>
url.includes('/a/changes/123456?o=') &&
url.includes('o=DETAILED_LABELS'),
)
.reply(200, gerritRestResponse(change), jsonResultHeader);
await expect(client.approveChange(123456)).toResolve();
});
Expand All @@ -411,17 +420,27 @@ describe('modules/platform/gerrit/client', () => {
const change = partial<GerritChange>({ labels: {} });
httpMock
.scope(gerritEndpointUrl)
.get((url) => url.includes('/a/changes/123456?o='))
.get(
(url) =>
url.includes('/a/changes/123456?o=') &&
url.includes('o=DETAILED_LABELS'),
)
.reply(200, gerritRestResponse(change), jsonResultHeader);

await expect(client.approveChange(123456)).toResolve();
});

it('not already approved - approve now', async () => {
const change = partial<GerritChange>({ labels: { 'Code-Review': {} } });
const change = partial<GerritChange>({
labels: { 'Code-Review': { all: [] } },
});
httpMock
.scope(gerritEndpointUrl)
.get((url) => url.includes('/a/changes/123456?o='))
.get(
(url) =>
url.includes('/a/changes/123456?o=') &&
url.includes('o=DETAILED_LABELS'),
)
.reply(200, gerritRestResponse(change), jsonResultHeader);
const approveMock = httpMock
.scope(gerritEndpointUrl)
Expand All @@ -433,62 +452,32 @@ describe('modules/platform/gerrit/client', () => {
await expect(client.approveChange(123456)).toResolve();
expect(approveMock.isDone()).toBeTrue();
});
});

describe('wasApprovedBy()', () => {
it('label not exists', () => {
expect(
client.wasApprovedBy(partial<GerritChange>({}), 'user'),
).toBeUndefined();
});

it('not approved by anyone', () => {
expect(
client.wasApprovedBy(
partial<GerritChange>({
labels: {
'Code-Review': {},
},
}),
'user',
),
).toBeUndefined();
});

it('approved by given user', () => {
expect(
client.wasApprovedBy(
partial<GerritChange>({
labels: {
'Code-Review': {
approved: {
_account_id: 1,
username: 'user',
},
},
},
}),
'user',
),
).toBeTrue();
});

it('approved by given other', () => {
expect(
client.wasApprovedBy(
partial<GerritChange>({
labels: {
'Code-Review': {
approved: {
_account_id: 1,
username: 'other',
},
},
},
}),
'user',
),
).toBeFalse();
it('not already approved because of +1 - approve now', async () => {
const change = partial<GerritChange>({
labels: {
'Code-Review': {
all: [{ value: 1 }],
},
},
});
httpMock
.scope(gerritEndpointUrl)
.get(
(url) =>
url.includes('/a/changes/123456?o=') &&
url.includes('o=DETAILED_LABELS'),
)
.reply(200, gerritRestResponse(change), jsonResultHeader);
const approveMock = httpMock
.scope(gerritEndpointUrl)
.post('/a/changes/123456/revisions/current/review', {
labels: { 'Code-Review': +2 },
notify: 'NONE',
})
.reply(200, gerritRestResponse(''), jsonResultHeader);
await expect(client.approveChange(123456)).toResolve();
expect(approveMock.isDone()).toBeTrue();
});
});
});
Expand Down
34 changes: 13 additions & 21 deletions lib/modules/platform/gerrit/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { REPOSITORY_ARCHIVED } from '../../../constants/error-messages';
import { logger } from '../../../logger';
import { GerritHttp } from '../../../util/http/gerrit';
import { regEx } from '../../../util/regex';
import { getQueryString } from '../../../util/url';
import type {
GerritAccountInfo,
GerritBranchInfo,
Expand Down Expand Up @@ -73,10 +74,14 @@ class GerritClient {
return changes.body;
}

async getChange(changeNumber: number): Promise<GerritChange> {
async getChange(
changeNumber: number,
extraOptions?: string[],
): Promise<GerritChange> {
const options = [...this.requestDetails, ...(extraOptions ?? [])];
const queryString = getQueryString({ o: options });
const changes = await this.gerritHttp.getJsonUnchecked<GerritChange>(
`a/changes/${changeNumber}?` +
this.requestDetails.map((det) => `o=${det}`).join('&'),
`a/changes/${changeNumber}?${queryString}`,
);
return changes.body;
}
Expand Down Expand Up @@ -197,15 +202,11 @@ class GerritClient {
}

async checkIfApproved(changeId: number): Promise<boolean> {
const change = await client.getChange(changeId);
const reviewLabels = change?.labels?.['Code-Review'];
return reviewLabels === undefined || reviewLabels.approved !== undefined;
}

wasApprovedBy(change: GerritChange, username: string): boolean | undefined {
const change = await client.getChange(changeId, ['DETAILED_LABELS']);
const reviewLabel = change?.labels?.['Code-Review'];
return (
change.labels?.['Code-Review'].approved &&
change.labels['Code-Review'].approved.username === username
reviewLabel === undefined ||
reviewLabel.all?.some((label) => label.value === 2) === true
);
}

Expand All @@ -221,16 +222,7 @@ class GerritClient {
const filterState = mapPrStateToGerritFilter(searchConfig.state);
const filters = ['owner:self', 'project:' + repository, filterState];
if (searchConfig.branchName) {
filters.push(
...[
'(',
`footer:Renovate-Branch=${searchConfig.branchName}`,
// for backwards compatibility
'OR',
`hashtag:sourceBranch-${searchConfig.branchName}`,
')',
],
);
filters.push(`footer:Renovate-Branch=${searchConfig.branchName}`);
}
if (searchConfig.targetBranch) {
filters.push(`branch:${searchConfig.targetBranch}`);
Expand Down
51 changes: 8 additions & 43 deletions lib/modules/platform/gerrit/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,28 +187,6 @@ describe('modules/platform/gerrit/index', () => {
gerrit.writeToConfig({ labels: {} });
});

it('updatePr() - auto approve enabled', async () => {
const change = partial<GerritChange>({
current_revision: 'some-revision',
revisions: {
'some-revision': partial<GerritRevisionInfo>({
commit: {
message: 'some message',
},
}),
},
});
clientMock.getChange.mockResolvedValueOnce(change);
await gerrit.updatePr({
number: 123456,
prTitle: 'subject',
platformPrOptions: {
autoApprove: true,
},
});
expect(clientMock.approveChange).toHaveBeenCalledWith(123456);
});

it('updatePr() - closed => abandon the change', async () => {
const change = partial<GerritChange>({});
clientMock.getChange.mockResolvedValueOnce(change);
Expand Down Expand Up @@ -309,7 +287,7 @@ describe('modules/platform/gerrit/index', () => {
]);
});

it('createPr() - update body WITHOUT approve', async () => {
it('createPr() - update body', async () => {
const pr = await gerrit.createPr({
sourceBranch: 'source',
targetBranch: 'target',
Expand All @@ -325,26 +303,6 @@ describe('modules/platform/gerrit/index', () => {
'body',
TAG_PULL_REQUEST_BODY,
);
expect(clientMock.approveChange).not.toHaveBeenCalled();
});

it('createPr() - update body and approve', async () => {
const pr = await gerrit.createPr({
sourceBranch: 'source',
targetBranch: 'target',
prTitle: change.subject,
prBody: 'body',
platformPrOptions: {
autoApprove: true,
},
});
expect(pr).toHaveProperty('number', 123456);
expect(clientMock.addMessageIfNotAlreadyExists).toHaveBeenCalledWith(
123456,
'body',
TAG_PULL_REQUEST_BODY,
);
expect(clientMock.approveChange).toHaveBeenCalledWith(123456);
});
});

Expand Down Expand Up @@ -750,6 +708,13 @@ describe('modules/platform/gerrit/index', () => {
//TODO: add some tests for Gerrit-specific replacements..
});

describe('approvePrForAutomerge()', () => {
it('approvePrForAutomerge() - calls approveChange', async () => {
await expect(gerrit.approvePrForAutomerge(123456)).toResolve();
expect(clientMock.approveChange).toHaveBeenCalledWith(123456);
});
});

describe('currently unused/not-implemented functions', () => {
it('deleteLabel()', async () => {
await expect(
Expand Down
17 changes: 11 additions & 6 deletions lib/modules/platform/gerrit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,6 @@ export async function updatePr(prConfig: UpdatePrConfig): Promise<void> {
TAG_PULL_REQUEST_BODY,
);
}
if (prConfig.platformPrOptions?.autoApprove) {
await client.approveChange(prConfig.number);
}
if (prConfig.state && prConfig.state === 'closed') {
await client.abandonChange(prConfig.number);
}
Expand Down Expand Up @@ -195,9 +192,6 @@ export async function createPr(prConfig: CreatePRConfig): Promise<Pr | null> {
prConfig.prBody,
TAG_PULL_REQUEST_BODY,
);
if (prConfig.platformPrOptions?.autoApprove) {
await client.approveChange(pr._number);
}
return getPr(pr._number);
}

Expand Down Expand Up @@ -442,3 +436,14 @@ export function findIssue(title: string): Promise<Issue | null> {
export function getIssueList(): Promise<Issue[]> {
return Promise.resolve([]);
}

/**
* The Code-Review +2 vote of when the change was created or updated in Gerrit
* may have been downgraded by a CI check utilizing the same account as
* Renovate (e.g. SonarQube which posts Code-Review +1). This function will
* vote with +2 again on the change, if needed, before Renovate attempt to
* automerge it.
*/
export async function approvePrForAutomerge(number: number): Promise<void> {
await client.approveChange(number);
}
6 changes: 0 additions & 6 deletions lib/modules/platform/gerrit/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ Support for Gerrit is currently _experimental_, meaning that it _might_ still ha

Renovate stores its metadata in the _commit message footer_.

Previously Renovate stored metadata in Gerrit's _hashtags_.
To keep backwards compatibility, Renovate still reads metadata from hashtags.
But Renovate _always_ puts its metadata in the _commit message footer_!
When the Renovate maintainers mark Gerrit support as stable, the maintainers will remove the "read metadata from hashtags" feature.
This means changes without metadata in the commit message footer will be "forgotten" by Renovate.

## Authentication

<figure markdown>
Expand Down
10 changes: 3 additions & 7 deletions lib/modules/platform/gerrit/scm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ describe('modules/platform/gerrit/scm', () => {
},
});
clientMock.findChanges.mockResolvedValueOnce([existingChange]);
clientMock.wasApprovedBy.mockReturnValueOnce(true);
git.prepareCommit.mockResolvedValueOnce({
commitSha: 'commitSha' as LongCommitSha,
parentCommitSha: 'parentSha' as LongCommitSha,
Expand All @@ -385,6 +384,7 @@ describe('modules/platform/gerrit/scm', () => {
message: 'commit msg',
files: [],
prTitle: 'pr title',
autoApprove: true,
}),
).toBe('commitSha');
expect(git.prepareCommit).toHaveBeenCalledWith({
Expand All @@ -396,19 +396,15 @@ describe('modules/platform/gerrit/scm', () => {
'Renovate-Branch: renovate/dependency-1.x\nChange-Id: ...',
],
prTitle: 'pr title',
autoApprove: true,
force: true,
});
expect(git.fetchRevSpec).toHaveBeenCalledWith('refs/changes/1/2');
expect(git.pushCommit).toHaveBeenCalledWith({
files: [],
sourceRef: 'renovate/dependency-1.x',
targetRef: 'refs/for/main%notify=NONE',
targetRef: 'refs/for/main%notify=NONE,l=Code-Review+2',
});
expect(clientMock.wasApprovedBy).toHaveBeenCalledWith(
existingChange,
'user',
);
expect(clientMock.approveChange).toHaveBeenCalledWith(123456);
});
});
});
Loading
Loading