Skip to content

Commit 284ad3e

Browse files
authored
Merge pull request #410 from xakraz/feat/deployment-task
feat: add support for deployment "task" and multi-deployments for a given environment
2 parents 15a9c15 + eab3a02 commit 284ad3e

21 files changed

+3730
-312
lines changed

__tests__/functions/deployment.test.js

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as core from '@actions/core'
12
import {vi, expect, test, beforeEach} from 'vitest'
23
import {
34
createDeploymentStatus,
@@ -313,3 +314,253 @@ test('returns false if the deployment is not found', async () => {
313314

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

0 commit comments

Comments
 (0)