diff --git a/lib/modules/platform/gerrit/client.spec.ts b/lib/modules/platform/gerrit/client.spec.ts index dbae6713445b6e..59a5dcd788ca21 100644 --- a/lib/modules/platform/gerrit/client.spec.ts +++ b/lib/modules/platform/gerrit/client.spec.ts @@ -160,7 +160,7 @@ describe('modules/platform/gerrit/client', () => { httpMock .scope(gerritEndpointUrl) .get( - '/a/changes/123456?o=SUBMITTABLE&o=CHECK&o=MESSAGES&o=DETAILED_ACCOUNTS&o=LABELS&o=CURRENT_ACTIONS&o=CURRENT_REVISION', + '/a/changes/123456?o=SUBMITTABLE&o=CHECK&o=MESSAGES&o=DETAILED_ACCOUNTS&o=LABELS&o=CURRENT_ACTIONS&o=CURRENT_REVISION&o=CURRENT_COMMIT', ) .reply(200, gerritRestResponse(change), jsonResultHeader); await expect(client.getChange(123456)).resolves.toEqual(change); @@ -215,17 +215,24 @@ describe('modules/platform/gerrit/client', () => { }); }); - describe('updateCommitMessage', () => { - it('updateCommitMessage - success', async () => { + describe('updateChangeSubject', () => { + it('updateChangeSubject - success', async () => { const change = partial({}); + const newSubject = 'new subject'; + const previousSubject = 'some subject'; + const previousBody = `some body\n\nChange-Id: some-change-id\n`; httpMock .scope(gerritEndpointUrl) .put('/a/changes/123456/message', { - message: `new message\n\nChange-Id: changeID\n`, + message: `${newSubject}\n\n${previousBody}`, }) .reply(200, gerritRestResponse(change), jsonResultHeader); await expect( - client.updateCommitMessage(123456, 'changeID', 'new message'), + client.updateChangeSubject( + 123456, + `${previousSubject}\n\n${previousBody}`, + 'new subject', + ), ).toResolve(); }); }); diff --git a/lib/modules/platform/gerrit/client.ts b/lib/modules/platform/gerrit/client.ts index ff11a0a69f8bcc..3182db64db37b2 100644 --- a/lib/modules/platform/gerrit/client.ts +++ b/lib/modules/platform/gerrit/client.ts @@ -24,6 +24,7 @@ class GerritClient { 'LABELS', 'CURRENT_ACTIONS', //to check if current_revision can be "rebased" 'CURRENT_REVISION', //get RevisionInfo::ref to fetch + 'CURRENT_COMMIT', // to get the commit message ] as const; private gerritHttp = new GerritHttp(); @@ -103,15 +104,17 @@ class GerritClient { }); } - async updateCommitMessage( + async updateChangeSubject( number: number, - gerritChangeID: string, - prTitle: string, + currentMessage: string, + newSubject: string, ): Promise { - await this.setCommitMessage( - number, - `${prTitle}\n\nChange-Id: ${gerritChangeID}\n`, + // Replace first line of the commit message with the new subject + const newMessage = currentMessage.replace( + new RegExp(`^.*$`, 'm'), + newSubject, ); + await this.setCommitMessage(number, newMessage); } async getMessages(changeNumber: number): Promise { diff --git a/lib/modules/platform/gerrit/index.spec.ts b/lib/modules/platform/gerrit/index.spec.ts index 7b7c7027e7c79e..17ee4188cde69d 100644 --- a/lib/modules/platform/gerrit/index.spec.ts +++ b/lib/modules/platform/gerrit/index.spec.ts @@ -11,6 +11,7 @@ import type { GerritLabelInfo, GerritLabelTypeInfo, GerritProjectInfo, + GerritRevisionInfo, } from './types'; import { TAG_PULL_REQUEST_BODY, mapGerritChangeToPr } from './utils'; import { writeToConfig } from '.'; @@ -187,21 +188,39 @@ describe('modules/platform/gerrit/index', () => { }); it('updatePr() - new prTitle => copy to commit msg', async () => { + const oldSubject = 'old title'; + const oldMessage = `${oldSubject}\n\nsome body\n\nChange-Id: ...`; const change = partial({ - change_id: '...', - subject: 'old title', + subject: oldSubject, + current_revision: 'some-revision', + revisions: { + 'some-revision': partial({ + commit: { + message: oldMessage, + }, + }), + }, }); clientMock.getChange.mockResolvedValueOnce(change); await gerrit.updatePr({ number: 123456, prTitle: 'new title' }); - expect(clientMock.updateCommitMessage).toHaveBeenCalledWith( + expect(clientMock.updateChangeSubject).toHaveBeenCalledWith( 123456, - '...', + oldMessage, 'new title', ); }); it('updatePr() - auto approve enabled', async () => { - const change = partial({}); + const change = partial({ + current_revision: 'some-revision', + revisions: { + 'some-revision': partial({ + commit: { + message: 'some message', + }, + }), + }, + }); clientMock.getChange.mockResolvedValueOnce(change); await gerrit.updatePr({ number: 123456, @@ -225,7 +244,16 @@ describe('modules/platform/gerrit/index', () => { }); it('updatePr() - existing prBody found in change.messages => nothing todo...', async () => { - const change = partial({}); + const change = partial({ + current_revision: 'some-revision', + revisions: { + 'some-revision': partial({ + commit: { + message: 'some message', + }, + }), + }, + }); clientMock.getChange.mockResolvedValueOnce(change); clientMock.getMessages.mockResolvedValueOnce([ partial({ @@ -279,9 +307,18 @@ describe('modules/platform/gerrit/index', () => { gerrit.writeToConfig({ labels: {} }); }); + const message = 'some subject\n\nsome body\n\nChange-Id: some-change-id'; + const change = partial({ _number: 123456, - change_id: '...', + current_revision: 'some-revision', + revisions: { + 'some-revision': partial({ + commit: { + message, + }, + }), + }, }); beforeEach(() => { @@ -312,9 +349,9 @@ describe('modules/platform/gerrit/index', () => { TAG_PULL_REQUEST_BODY, ); expect(clientMock.approveChange).not.toHaveBeenCalled(); - expect(clientMock.updateCommitMessage).toHaveBeenCalledWith( + expect(clientMock.updateChangeSubject).toHaveBeenCalledWith( 123456, - '...', + message, 'title', ); }); diff --git a/lib/modules/platform/gerrit/index.ts b/lib/modules/platform/gerrit/index.ts index 5e02b30abc677b..ddd058f21e11d2 100644 --- a/lib/modules/platform/gerrit/index.ts +++ b/lib/modules/platform/gerrit/index.ts @@ -155,9 +155,9 @@ export async function updatePr(prConfig: UpdatePrConfig): Promise { logger.debug(`updatePr(${prConfig.number}, ${prConfig.prTitle})`); const change = await client.getChange(prConfig.number); if (change.subject !== prConfig.prTitle) { - await client.updateCommitMessage( + await client.updateChangeSubject( prConfig.number, - change.change_id, + change.revisions[change.current_revision].commit.message, prConfig.prTitle, ); } @@ -200,9 +200,9 @@ export async function createPr(prConfig: CreatePRConfig): Promise { } //Workaround for "Known Problems.1" if (pr.subject !== prConfig.prTitle) { - await client.updateCommitMessage( + await client.updateChangeSubject( pr._number, - pr.change_id, + pr.revisions[pr.current_revision].commit.message, prConfig.prTitle, ); } diff --git a/lib/modules/platform/gerrit/types.ts b/lib/modules/platform/gerrit/types.ts index 7ec71999f4f1e2..3aae7d47e6831e 100644 --- a/lib/modules/platform/gerrit/types.ts +++ b/lib/modules/platform/gerrit/types.ts @@ -43,14 +43,18 @@ export interface GerritChange { labels?: Record; reviewers?: Record; messages?: GerritChangeMessageInfo[]; - current_revision?: string; + current_revision: string; /** * All patch sets of this change as a map that maps the commit ID of the patch set to a RevisionInfo entity. */ - revisions?: Record; + revisions: Record; problems: unknown[]; } +export interface GerritCommitInfo { + message: string; +} + export interface GerritRevisionInfo { uploader: GerritAccountInfo; /** @@ -58,6 +62,7 @@ export interface GerritRevisionInfo { */ ref: string; actions?: Record; + commit: GerritCommitInfo; } export interface GerritChangeMessageInfo {