11import { codeBlock } from 'common-tags' ;
2- import { DateTime } from 'luxon' ;
32import { REPOSITORY_ARCHIVED } from '../../../constants/error-messages' ;
43import type { BranchStatus } from '../../../types' ;
54import { repoFingerprint } from '../util' ;
@@ -39,15 +38,6 @@ vi.mock('./client');
3938const clientMock = vi . mocked ( _client ) ;
4039
4140describe ( 'modules/platform/gerrit/index' , ( ) => {
42- const t0 = DateTime . fromISO ( '2025-04-14T16:33:37.000000000' , {
43- zone : 'utc' ,
44- } ) as DateTime < true > ;
45-
46- beforeAll ( ( ) => {
47- vi . useFakeTimers ( ) ;
48- vi . setSystemTime ( t0 . toMillis ( ) ) ;
49- } ) ;
50-
5141 beforeEach ( async ( ) => {
5242 hostRules . find . mockReturnValue ( {
5343 username : 'user' ,
@@ -257,24 +247,42 @@ describe('modules/platform/gerrit/index', () => {
257247 } ) ;
258248
259249 describe ( 'createPr()' , ( ) => {
260- it ( 'createPr() - no existing found => rejects' , async ( ) => {
261- clientMock . findChanges . mockResolvedValueOnce ( [ ] ) ;
262- await expect (
263- gerrit . createPr ( {
264- sourceBranch : 'source' ,
265- targetBranch : 'target' ,
266- prTitle : 'title' ,
267- prBody : 'body' ,
268- } ) ,
269- ) . rejects . toThrow (
270- `the change should be created automatically from previous push to refs/for/source` ,
250+ it ( 'createPr() - creates change by pushing to refs/for/' , async ( ) => {
251+ git . pushCommit . mockResolvedValueOnce ( true ) ;
252+ const change = partial < GerritChange > ( {
253+ _number : 123456 ,
254+ current_revision : 'some-revision' ,
255+ revisions : {
256+ 'some-revision' : partial < GerritRevisionInfo > ( {
257+ commit_with_footers : 'Renovate-Branch: source' ,
258+ } ) ,
259+ } ,
260+ } ) ;
261+ clientMock . findChanges . mockResolvedValueOnce ( [ change ] ) ;
262+ const pr = await gerrit . createPr ( {
263+ sourceBranch : 'source' ,
264+ targetBranch : 'target' ,
265+ prTitle : 'title' ,
266+ prBody : 'body' ,
267+ } ) ;
268+ expect ( pr ) . toHaveProperty ( 'number' , 123456 ) ;
269+ expect ( git . pushCommit ) . toHaveBeenCalledExactlyOnceWith ( {
270+ sourceRef : 'source' ,
271+ targetRef : 'refs/for/target' ,
272+ files : [ ] ,
273+ pushOptions : [ 'notify=NONE' ] ,
274+ } ) ;
275+ expect ( clientMock . addMessage ) . toHaveBeenCalledExactlyOnceWith (
276+ 123456 ,
277+ 'body' ,
278+ TAG_PULL_REQUEST_BODY ,
271279 ) ;
272280 } ) ;
273281
274- it ( 'createPr() - found existing but not created in the last 5 minutes => rejects' , async ( ) => {
282+ it ( 'createPr() - with autoApprove' , async ( ) => {
283+ git . pushCommit . mockResolvedValueOnce ( true ) ;
275284 const change = partial < GerritChange > ( {
276285 _number : 123456 ,
277- created : t0 . minus ( { minutes : 6 } ) . toISO ( ) . replace ( 'T' , ' ' ) ,
278286 current_revision : 'some-revision' ,
279287 revisions : {
280288 'some-revision' : partial < GerritRevisionInfo > ( {
@@ -283,45 +291,90 @@ describe('modules/platform/gerrit/index', () => {
283291 } ,
284292 } ) ;
285293 clientMock . findChanges . mockResolvedValueOnce ( [ change ] ) ;
286- await expect (
287- gerrit . createPr ( {
288- sourceBranch : 'source' ,
289- targetBranch : 'target' ,
290- prTitle : 'title' ,
291- prBody : 'body' ,
292- } ) ,
293- ) . rejects . toThrow ( / i t w a s n o t c r e a t e d i n t h e l a s t 5 m i n u t e s / ) ;
294+ const pr = await gerrit . createPr ( {
295+ sourceBranch : 'source' ,
296+ targetBranch : 'target' ,
297+ prTitle : 'title' ,
298+ prBody : 'body' ,
299+ platformPrOptions : {
300+ autoApprove : true ,
301+ } ,
302+ } ) ;
303+ expect ( pr ) . toHaveProperty ( 'number' , 123456 ) ;
304+ expect ( git . pushCommit ) . toHaveBeenCalledExactlyOnceWith ( {
305+ sourceRef : 'source' ,
306+ targetRef : 'refs/for/target' ,
307+ files : [ ] ,
308+ pushOptions : [ 'notify=NONE' , 'label=Code-Review+2' ] ,
309+ } ) ;
310+ expect ( clientMock . addMessage ) . toHaveBeenCalledExactlyOnceWith (
311+ 123456 ,
312+ 'body' ,
313+ TAG_PULL_REQUEST_BODY ,
314+ ) ;
294315 } ) ;
295316
296- it ( 'createPr() - add body as message' , async ( ) => {
317+ it ( 'createPr() - with labels' , async ( ) => {
318+ git . pushCommit . mockResolvedValueOnce ( true ) ;
297319 const change = partial < GerritChange > ( {
298320 _number : 123456 ,
299321 current_revision : 'some-revision' ,
300- created : t0 . minus ( { seconds : 30 } ) . toISO ( ) . replace ( 'T' , ' ' ) ,
301322 revisions : {
302323 'some-revision' : partial < GerritRevisionInfo > ( {
303324 commit_with_footers : 'Renovate-Branch: source' ,
304325 } ) ,
305326 } ,
306- messages : [ ] ,
307327 } ) ;
308328 clientMock . findChanges . mockResolvedValueOnce ( [ change ] ) ;
309329 const pr = await gerrit . createPr ( {
310330 sourceBranch : 'source' ,
311331 targetBranch : 'target' ,
312332 prTitle : 'title' ,
313333 prBody : 'body' ,
314- platformPrOptions : {
315- autoApprove : false ,
316- } ,
334+ labels : [ 'label1' , 'label2' ] ,
317335 } ) ;
318336 expect ( pr ) . toHaveProperty ( 'number' , 123456 ) ;
337+ expect ( git . pushCommit ) . toHaveBeenCalledExactlyOnceWith ( {
338+ sourceRef : 'source' ,
339+ targetRef : 'refs/for/target' ,
340+ files : [ ] ,
341+ pushOptions : [ 'notify=NONE' , 'hashtag=label1' , 'hashtag=label2' ] ,
342+ } ) ;
319343 expect ( clientMock . addMessage ) . toHaveBeenCalledExactlyOnceWith (
320344 123456 ,
321345 'body' ,
322346 TAG_PULL_REQUEST_BODY ,
323347 ) ;
324348 } ) ;
349+
350+ it ( 'createPr() - no change found after push => rejects' , async ( ) => {
351+ git . pushCommit . mockResolvedValueOnce ( true ) ;
352+ clientMock . findChanges . mockResolvedValueOnce ( [ ] ) ;
353+ await expect (
354+ gerrit . createPr ( {
355+ sourceBranch : 'source' ,
356+ targetBranch : 'target' ,
357+ prTitle : 'title' ,
358+ prBody : 'body' ,
359+ } ) ,
360+ ) . rejects . toThrow (
361+ `Could not find the Gerrit change after pushing to refs/for/target` ,
362+ ) ;
363+ } ) ;
364+
365+ it ( 'createPr() - push fails => rejects' , async ( ) => {
366+ git . pushCommit . mockResolvedValueOnce ( false ) ;
367+ await expect (
368+ gerrit . createPr ( {
369+ sourceBranch : 'source' ,
370+ targetBranch : 'target' ,
371+ prTitle : 'title' ,
372+ prBody : 'body' ,
373+ } ) ,
374+ ) . rejects . toThrow (
375+ `Failed to push commit to refs/for/target to create Gerrit change` ,
376+ ) ;
377+ } ) ;
325378 } ) ;
326379
327380 describe ( 'getBranchPr()' , ( ) => {
0 commit comments