@@ -313,3 +313,253 @@ test('returns false if the deployment is not found', async () => {
313313
314314 expect ( octokit . graphql ) . toHaveBeenCalled ( )
315315} )
316+
317+ test ( 'returns null when no deployments are found with task parameter' , async ( ) => {
318+ octokit = createMockGraphQLOctokit ( {
319+ repository : {
320+ deployments : {
321+ nodes : [ ]
322+ }
323+ }
324+ } )
325+
326+ expect (
327+ await latestActiveDeployment ( octokit , context , environment , 'backend' )
328+ ) . toBeNull ( )
329+
330+ expect ( octokit . graphql ) . toHaveBeenCalled ( )
331+ expect ( core . debug ) . toHaveBeenCalledWith (
332+ 'no deployments found for production with task backend'
333+ )
334+ } )
335+
336+ test ( 'returns active deployment with matching task on first page' , async ( ) => {
337+ const mockDataWithTask = {
338+ repository : {
339+ deployments : {
340+ nodes : [
341+ {
342+ createdAt : '2024-09-19T20:18:18Z' ,
343+ environment : 'production' ,
344+ updatedAt : '2024-09-19T20:18:21Z' ,
345+ id : 'DE_kwDOID9x8M5sC6QZ' ,
346+ payload : '{"type":"branch-deploy"}' ,
347+ state : 'ACTIVE' ,
348+ task : 'backend' ,
349+ creator : {
350+ login : 'github-actions'
351+ } ,
352+ ref : {
353+ name : 'main'
354+ } ,
355+ commit : {
356+ oid : '315cec138fc9d7dac8a47c6bba4217d3965ede3b'
357+ }
358+ } ,
359+ {
360+ createdAt : '2024-09-19T20:18:10Z' ,
361+ environment : 'production' ,
362+ updatedAt : '2024-09-19T20:18:15Z' ,
363+ id : 'DE_kwDOID9x8M5sC6QY' ,
364+ payload : '{"type":"branch-deploy"}' ,
365+ state : 'ACTIVE' ,
366+ task : 'frontend' ,
367+ creator : {
368+ login : 'github-actions'
369+ } ,
370+ ref : {
371+ name : 'main'
372+ } ,
373+ commit : {
374+ oid : 'abc123'
375+ }
376+ }
377+ ] ,
378+ pageInfo : {
379+ endCursor : null ,
380+ hasNextPage : false
381+ }
382+ }
383+ }
384+ }
385+
386+ octokit = createMockGraphQLOctokit ( mockDataWithTask )
387+
388+ const result = await latestActiveDeployment (
389+ octokit ,
390+ context ,
391+ environment ,
392+ 'backend'
393+ )
394+
395+ expect ( result ) . toStrictEqual ( {
396+ createdAt : '2024-09-19T20:18:18Z' ,
397+ environment : 'production' ,
398+ updatedAt : '2024-09-19T20:18:21Z' ,
399+ id : 'DE_kwDOID9x8M5sC6QZ' ,
400+ payload : '{"type":"branch-deploy"}' ,
401+ state : 'ACTIVE' ,
402+ task : 'backend' ,
403+ creator : {
404+ login : 'github-actions'
405+ } ,
406+ ref : {
407+ name : 'main'
408+ } ,
409+ commit : {
410+ oid : '315cec138fc9d7dac8a47c6bba4217d3965ede3b'
411+ }
412+ } )
413+
414+ expect ( octokit . graphql ) . toHaveBeenCalledTimes ( 1 )
415+ expect ( core . debug ) . toHaveBeenCalledWith (
416+ 'found active deployment for production with task backend in page 1'
417+ )
418+ } )
419+
420+ test ( 'returns active deployment with matching task during pagination' , async ( ) => {
421+ octokit . graphql = jest
422+ . fn ( )
423+ . mockReturnValueOnce ( {
424+ repository : {
425+ deployments : {
426+ nodes : [
427+ {
428+ state : 'ACTIVE' ,
429+ task : 'frontend'
430+ } ,
431+ {
432+ state : 'INACTIVE' ,
433+ task : 'backend'
434+ }
435+ ] ,
436+ pageInfo : {
437+ endCursor : 'cursor1' ,
438+ hasNextPage : true
439+ }
440+ }
441+ }
442+ } )
443+ . mockReturnValueOnce ( {
444+ repository : {
445+ deployments : {
446+ nodes : [
447+ {
448+ state : 'INACTIVE' ,
449+ task : 'backend'
450+ } ,
451+ {
452+ state : 'ACTIVE' ,
453+ task : 'backend'
454+ }
455+ ] ,
456+ pageInfo : {
457+ endCursor : 'cursor2' ,
458+ hasNextPage : false
459+ }
460+ }
461+ }
462+ } )
463+
464+ const result = await latestActiveDeployment (
465+ octokit ,
466+ context ,
467+ environment ,
468+ 'backend'
469+ )
470+
471+ expect ( result ) . toStrictEqual ( {
472+ state : 'ACTIVE' ,
473+ task : 'backend'
474+ } )
475+
476+ expect ( octokit . graphql ) . toHaveBeenCalledTimes ( 2 )
477+ expect ( core . debug ) . toHaveBeenCalledWith (
478+ 'found active deployment for production with task backend in page 2'
479+ )
480+ } )
481+
482+ test ( 'returns null when no active deployment found after pagination with task filter' , async ( ) => {
483+ octokit . graphql = jest
484+ . fn ( )
485+ . mockReturnValueOnce ( {
486+ repository : {
487+ deployments : {
488+ nodes : [
489+ {
490+ state : 'ACTIVE' ,
491+ task : 'frontend'
492+ } ,
493+ {
494+ state : 'INACTIVE' ,
495+ task : 'backend'
496+ }
497+ ] ,
498+ pageInfo : {
499+ endCursor : 'cursor1' ,
500+ hasNextPage : true
501+ }
502+ }
503+ }
504+ } )
505+ . mockReturnValueOnce ( {
506+ repository : {
507+ deployments : {
508+ nodes : [
509+ {
510+ state : 'INACTIVE' ,
511+ task : 'backend'
512+ } ,
513+ {
514+ state : 'ACTIVE' ,
515+ task : 'frontend'
516+ }
517+ ] ,
518+ pageInfo : {
519+ endCursor : 'cursor2' ,
520+ hasNextPage : true
521+ }
522+ }
523+ }
524+ } )
525+ . mockReturnValueOnce ( {
526+ repository : {
527+ deployments : {
528+ nodes : [
529+ {
530+ state : 'PENDING' ,
531+ task : 'backend'
532+ } ,
533+ {
534+ state : 'ACTIVE' ,
535+ task : 'frontend'
536+ }
537+ ] ,
538+ pageInfo : {
539+ endCursor : null ,
540+ hasNextPage : false
541+ }
542+ }
543+ }
544+ } )
545+
546+ const result = await latestActiveDeployment (
547+ octokit ,
548+ context ,
549+ environment ,
550+ 'backend'
551+ )
552+
553+ expect ( result ) . toBeNull ( )
554+
555+ expect ( octokit . graphql ) . toHaveBeenCalledTimes ( 3 )
556+ expect ( core . debug ) . toHaveBeenCalledWith (
557+ 'no active deployment found for production with task backend in page 2'
558+ )
559+ expect ( core . debug ) . toHaveBeenCalledWith (
560+ 'no active deployment found for production with task backend in page 3'
561+ )
562+ expect ( core . debug ) . toHaveBeenCalledWith (
563+ 'no active deployment found for production with task backend after 3 pages'
564+ )
565+ } )
0 commit comments