Skip to content

Commit

Permalink
chore(gerrit): improve auto-approve on new patchset workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecrs committed Nov 30, 2024
1 parent 46f8eed commit 82fe8e1
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 87 deletions.
68 changes: 0 additions & 68 deletions lib/modules/platform/gerrit/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,74 +473,6 @@ describe('modules/platform/gerrit/client', () => {
expect(approveMock.isDone()).toBeTrue();
});
});

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

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

it('not approved but with +1', () => {
expect(
client.wasApprovedBy(
partial<GerritChange>({
labels: {
'Code-Review': {
all: [{ value: 1, username: 'user' }],
},
},
}),
'user',
),
).toBeFalse();
});

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

it('approved by given other', () => {
expect(
client.wasApprovedBy(
partial<GerritChange>({
labels: {
'Code-Review': {
all: [{ value: 2, username: 'other' }],
},
},
}),
'user',
),
).toBeFalse();
});
});
});

function gerritRestResponse(body: any): any {
Expand Down
9 changes: 0 additions & 9 deletions lib/modules/platform/gerrit/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,6 @@ class GerritClient {
);
}

wasApprovedBy(change: GerritChange, username: string): boolean {
const reviewLabel = change?.labels?.['Code-Review'];
return Boolean(
reviewLabel?.all?.some(
(label) => label.value === 2 && label.username === username,
),
);
}

normalizeMessage(message: string): string {
//the last \n was removed from gerrit after the comment was added...
return message.substring(0, 0x4000).trim();
Expand Down
7 changes: 2 additions & 5 deletions lib/modules/platform/gerrit/scm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,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 @@ -386,6 +385,7 @@ describe('modules/platform/gerrit/scm', () => {
message: 'commit msg',
files: [],
prTitle: 'pr title',
autoApprove: true,
}),
).toBe('commitSha');
expect(git.prepareCommit).toHaveBeenCalledWith({
Expand All @@ -397,6 +397,7 @@ 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');
Expand All @@ -405,10 +406,6 @@ describe('modules/platform/gerrit/scm', () => {
sourceRef: 'renovate/dependency-1.x',
targetRef: 'refs/for/main',
});
expect(clientMock.wasApprovedBy).toHaveBeenCalledWith(
existingChange,
'user',
);
expect(clientMock.approveChange).toHaveBeenCalledWith(123456);
});
});
Expand Down
7 changes: 2 additions & 5 deletions lib/modules/platform/gerrit/scm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,8 @@ export class GerritScm extends DefaultGitScm {
files: commit.files,
});
if (pushResult) {
//existingChange was the old change before commit/push. we need to approve again, if it was previously approved from renovate
if (
existingChange &&
client.wasApprovedBy(existingChange, username)
) {
// New patchsets dismisses the approval, so we need to approve it again
if (existingChange && commit.autoApprove) {
await client.approveChange(existingChange._number);
}
return commitSha;
Expand Down
2 changes: 2 additions & 0 deletions lib/util/git/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export interface CommitFilesConfig {
platformCommit?: PlatformCommitOptions;
/** Only needed by Gerrit platform */
prTitle?: string;
/** Only needed by Gerrit platform */
autoApprove?: boolean;
}

export interface PushFilesConfig {
Expand Down
2 changes: 2 additions & 0 deletions lib/workers/repository/update/branch/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,7 @@ export function commitFilesToBranch(
platformCommit: config.platformCommit,
// Only needed by Gerrit platform
prTitle: config.prTitle,
// Only needed by Gerrit platform
autoApprove: config.autoApprove,
});
}

0 comments on commit 82fe8e1

Please sign in to comment.