Skip to content

Commit

Permalink
fix(platform/gerrit): Check for comment size limit (#26454)
Browse files Browse the repository at this point in the history
  • Loading branch information
valodzka committed Dec 30, 2023
1 parent b2422d8 commit d8ad99f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
12 changes: 12 additions & 0 deletions lib/modules/platform/gerrit/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,18 @@ describe('modules/platform/gerrit/client', () => {
.reply(200, gerritRestResponse([]), jsonResultHeader);
await expect(client.addMessage(123456, 'message')).toResolve();
});

it('add too big message', async () => {
const okMessage = 'a'.repeat(0x4000);
const tooBigMessage = okMessage + 'b';
httpMock
.scope(gerritEndpointUrl)
.post('/a/changes/123456/revisions/current/review', {
message: okMessage,
})
.reply(200, gerritRestResponse([]), jsonResultHeader);
await expect(client.addMessage(123456, tooBigMessage)).toResolve();
});
});

describe('checkForExistingMessage()', () => {
Expand Down
10 changes: 8 additions & 2 deletions lib/modules/platform/gerrit/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,10 @@ class GerritClient {

async addMessage(
changeNumber: number,
message: string,
fullMessage: string,
tag?: string,
): Promise<void> {
const message = this.normalizeMessage(fullMessage);
await this.gerritHttp.postJson(
`a/changes/${changeNumber}/revisions/current/review`,
{ body: { message, tag } },
Expand All @@ -148,7 +149,7 @@ class GerritClient {
message: string,
tag?: string,
): Promise<void> {
const newMsg = message.trim(); //the last \n was removed from gerrit after the comment was added...
const newMsg = this.normalizeMessage(message);
if (!(await this.checkForExistingMessage(changeNumber, newMsg, tag))) {
await this.addMessage(changeNumber, newMsg, tag);
}
Expand Down Expand Up @@ -213,6 +214,11 @@ class GerritClient {
);
}

normalizeMessage(message: string): string {
//the last \n was removed from gerrit after the comment was added...
return message.substring(0, 0x4000).trim();
}

private static buildSearchFilters(
repository: string,
searchConfig: GerritFindPRConfig,
Expand Down

0 comments on commit d8ad99f

Please sign in to comment.