-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgithub.test.ts
More file actions
760 lines (661 loc) · 21.8 KB
/
github.test.ts
File metadata and controls
760 lines (661 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
import { describe, test, expect, vi } from 'vitest'
import * as github from '@actions/github'
import { Octokit } from '@octokit/rest'
import {
fetchWorkflowResults,
getLatestCompletedAt,
getWorkflowContext,
createOctokitClient
} from './github.js'
import {
toWorkflowJob,
toWorkflowRun,
toWorkflowStep,
WorkflowJob,
Workflow
} from './types.js'
import { ApplicationSettings, settings } from '../settings.js'
describe('fetchWorkflowResults', () => {
// Tips: If API limit exceed, authenticate by using below command
// $ export GITHUB_TOKEN=`gh auth token`
test('should fetch results using real api', async () => {
// not test retry because it needs mock of checkCompleted but it affects correct test case.
const octokit = createOctokitClient()
const workflowContext = getWorkflowContext(github.context, settings)
await expect(
fetchWorkflowResults(octokit, workflowContext, 0, 1)
).resolves.not.toThrow()
})
test('should handle fetchWorkflowResults error and retry', async () => {
// Create a mock Octokit that fails
const mockOctokit = {
rest: {
actions: {
getWorkflowRunAttempt: async () => {
throw new Error('API Error')
},
listJobsForWorkflowRun: async () => {
throw new Error('API Error')
}
}
}
} as unknown as Octokit
const workflowContext = getWorkflowContext(github.context, settings)
await expect(
fetchWorkflowResults(mockOctokit, workflowContext, 0, 3)
).rejects.toThrow('API Error')
})
test('should handle no completed jobs found error', async () => {
// Create a mock Octokit that returns workflow but no completed jobs
const mockOctokit = {
rest: {
actions: {
getWorkflowRunAttempt: async () => ({
data: {
id: 12345,
name: 'Test Workflow',
status: 'completed',
conclusion: 'success',
created_at: '2023-01-01T00:00:00Z',
run_attempt: 1,
html_url: 'https://github.com/test/repo/actions/runs/12345',
repository: {
full_name: 'test-owner/test-repo'
},
event: 'push'
}
}),
listJobsForWorkflowRun: async () => ({
data: {
jobs: [
// Return jobs that will be filtered out (not completed)
{
id: 1,
name: 'test-job',
status: 'in_progress',
conclusion: null,
created_at: '2023-01-01T00:00:00Z',
started_at: '2023-01-01T00:01:00Z',
completed_at: null,
workflow_name: 'Test Workflow',
run_id: 12345,
steps: []
}
]
}
})
}
}
} as unknown as Octokit
const workflowContext = getWorkflowContext(github.context, settings)
await expect(
fetchWorkflowResults(mockOctokit, workflowContext, 0, 1)
).rejects.toThrow('no completed jobs found for workflow run.')
})
test('should filter incomplete jobs and process completed ones for non-workflow_run events', async () => {
// Create a mock Octokit that returns workflow with mixed job statuses
const mockOctokit = {
rest: {
actions: {
getWorkflowRunAttempt: async () => ({
data: {
id: 12345,
name: 'Test Workflow',
status: 'in_progress',
conclusion: null,
created_at: '2023-01-01T00:00:00Z',
run_attempt: 1,
html_url: 'https://github.com/test/repo/actions/runs/12345',
repository: {
full_name: 'test-owner/test-repo'
},
event: 'push'
}
}),
listJobsForWorkflowRun: async () => ({
data: {
jobs: [
// Completed job - should be included
{
id: 1,
name: 'completed-job',
status: 'completed',
conclusion: 'success',
created_at: '2023-01-01T00:00:00Z',
started_at: '2023-01-01T00:01:00Z',
completed_at: '2023-01-01T00:05:00Z',
workflow_name: 'Test Workflow',
run_id: 12345,
steps: []
},
// In-progress job - should be filtered out
{
id: 2,
name: 'in-progress-job',
status: 'in_progress',
conclusion: null,
created_at: '2023-01-01T00:00:00Z',
started_at: '2023-01-01T00:01:00Z',
completed_at: null,
workflow_name: 'Test Workflow',
run_id: 12345,
steps: []
},
// Queued job - should be filtered out
{
id: 3,
name: 'queued-job',
status: 'queued',
conclusion: null,
created_at: '2023-01-01T00:00:00Z',
started_at: null,
completed_at: null,
workflow_name: 'Test Workflow',
run_id: 12345,
steps: []
}
]
}
})
}
}
} as unknown as Octokit
const workflowContext = getWorkflowContext(github.context, settings)
const result = await fetchWorkflowResults(
mockOctokit,
workflowContext,
0,
1
)
// Should only include the completed job
expect(result.workflowJobs).toHaveLength(1)
expect(result.workflowJobs[0].name).toBe('completed-job')
expect(result.workflowJobs[0].status).toBe('completed')
expect(result.workflow.name).toBe('Test Workflow')
})
})
describe('getWorkflowContext', () => {
test('should handle workflow_run event with payload', () => {
const mockContext = {
eventName: 'workflow_run',
repo: {
owner: 'test-owner',
repo: 'test-repo'
},
payload: {
workflow_run: {
id: 67890,
run_attempt: 3
}
}
}
const mockSettings = {
owner: null,
repository: null,
workflowRunId: null
} as unknown as ApplicationSettings
const result = getWorkflowContext(mockContext as never, mockSettings)
expect(result).toEqual({
owner: 'test-owner',
repo: 'test-repo',
attempt_number: 3,
runId: 67890
})
})
test('should handle workflow_run event with missing run_attempt', () => {
const mockContext = {
eventName: 'workflow_run',
repo: {
owner: 'test-owner',
repo: 'test-repo'
},
payload: {
workflow_run: {
id: 67890,
run_attempt: undefined
}
}
}
const mockSettings = {
owner: null,
repository: null,
workflowRunId: null
} as unknown as ApplicationSettings
const result = getWorkflowContext(mockContext as never, mockSettings)
expect(result.attempt_number).toBe(1)
})
test('should handle workflow_run event with settings override', () => {
const mockContext = {
eventName: 'workflow_run',
repo: {
owner: 'original-owner',
repo: 'original-repo'
},
payload: {
workflow_run: {
id: 67890,
run_attempt: 2
}
}
}
const mockSettings = {
owner: 'override-owner',
repository: 'override-repo',
workflowRunId: 99999
} as unknown as ApplicationSettings
const result = getWorkflowContext(mockContext as never, mockSettings)
expect(result).toEqual({
owner: 'override-owner',
repo: 'override-repo',
attempt_number: 2,
runId: 99999
})
})
test('should handle push event', () => {
const mockContext = {
eventName: 'push',
repo: {
owner: 'test-owner',
repo: 'test-repo'
},
runId: 12345,
runAttempt: 2
}
const mockSettings = {
owner: null,
repository: null,
workflowRunId: null
} as unknown as ApplicationSettings
const result = getWorkflowContext(mockContext as never, mockSettings)
expect(result).toEqual({
owner: 'test-owner',
repo: 'test-repo',
attempt_number: 2,
runId: 12345
})
})
test('should handle pull_request event', () => {
const mockContext = {
eventName: 'pull_request',
repo: {
owner: 'test-owner',
repo: 'test-repo'
},
runId: 54321,
runAttempt: 3
}
const mockSettings = {
owner: null,
repository: null,
workflowRunId: null
} as unknown as ApplicationSettings
const result = getWorkflowContext(mockContext as never, mockSettings)
expect(result).toEqual({
owner: 'test-owner',
repo: 'test-repo',
attempt_number: 3,
runId: 54321
})
})
test('should use default attempt_number for non-workflow_run events when runAttempt is missing', () => {
const mockContext = {
eventName: 'push',
repo: {
owner: 'test-owner',
repo: 'test-repo'
},
runId: 12345,
runAttempt: undefined
}
const mockSettings = {
owner: null,
repository: null,
workflowRunId: null
} as unknown as ApplicationSettings
const result = getWorkflowContext(mockContext as never, mockSettings)
expect(result.attempt_number).toBe(1)
})
test('should handle settings override for non-workflow_run events', () => {
const mockContext = {
eventName: 'push',
repo: {
owner: 'original-owner',
repo: 'original-repo'
},
runId: 12345,
runAttempt: 1
}
const mockSettings = {
owner: 'override-owner',
repository: 'override-repo',
workflowRunId: 99999
} as unknown as ApplicationSettings
const result = getWorkflowContext(mockContext as never, mockSettings)
expect(result).toEqual({
owner: 'override-owner',
repo: 'override-repo',
attempt_number: 1,
runId: 99999
})
})
})
describe('getLatestCompletedAt', () => {
test('should return latest completion time', () => {
const jobs = [
{ completed_at: new Date('2023-01-01T00:05:00Z') },
{ completed_at: new Date('2023-01-01T00:03:00Z') },
{ completed_at: new Date('2023-01-01T00:07:00Z') }
]
const result = getLatestCompletedAt(jobs as never)
expect(result).toEqual(new Date('2023-01-01T00:07:00Z'))
})
test('should handle single job', () => {
const jobs = [{ completed_at: new Date('2023-01-01T00:05:00Z') }]
const result = getLatestCompletedAt(jobs as never)
expect(result).toEqual(new Date('2023-01-01T00:05:00Z'))
})
test('should throw error when jobs array is empty', () => {
const emptyJobs: never[] = []
expect(() => getLatestCompletedAt(emptyJobs)).toThrow(
'no jobs found to get latest completed_at date.'
)
})
})
describe('Type converters', () => {
describe('toWorkflowJob', () => {
const mockJobResponse = {
id: 1,
name: 'test-job',
status: 'completed' as const,
conclusion: 'success',
created_at: '2023-01-01T00:00:00Z',
started_at: '2023-01-01T00:01:00Z',
completed_at: '2023-01-01T00:05:00Z',
workflow_name: 'Test Workflow',
run_id: 12345,
runner_name: 'test-runner',
runner_group_name: 'test-group',
steps: [
{
name: 'test-step',
conclusion: 'success' as const,
started_at: '2023-01-01T00:01:00Z',
completed_at: '2023-01-01T00:02:00Z'
}
]
}
test('should convert valid job response', () => {
const result = toWorkflowJob(mockJobResponse as never, 'workflow_run')
const expected: WorkflowJob = {
...mockJobResponse,
created_at: new Date(mockJobResponse.created_at),
started_at: new Date(mockJobResponse.started_at),
completed_at: new Date(mockJobResponse.completed_at),
steps: [
{
name: 'test-step',
conclusion: 'success' as const,
started_at: new Date('2023-01-01T00:01:00Z'),
completed_at: new Date('2023-01-01T00:02:00Z')
}
]
}
expect(result).toEqual(expected)
})
test('should handle job without steps', () => {
const jobWithoutSteps = { ...mockJobResponse, steps: null }
const result = toWorkflowJob(jobWithoutSteps as never, 'workflow_run')
expect(result).not.toBeNull()
if (result) {
expect(result.steps).toEqual([])
}
})
test('should return null when job is not completed', () => {
const incompleteJob = { ...mockJobResponse, status: 'in_progress' }
expect(() =>
toWorkflowJob(incompleteJob as never, 'workflow_run')
).toThrow('job.status must be completed on workflow_run event')
})
test('should throw error when conclusion is missing', () => {
const jobWithoutConclusion = { ...mockJobResponse, conclusion: null }
expect(() =>
toWorkflowJob(jobWithoutConclusion as never, 'workflow_run')
).toThrow('Job conclusion is required')
})
test('should throw error when completed_at is missing', () => {
const jobWithoutCompletedAt = { ...mockJobResponse, completed_at: null }
expect(() =>
toWorkflowJob(jobWithoutCompletedAt as never, 'workflow_run')
).toThrow('Job completed_at is required')
})
test('should throw error when workflow_name is missing', () => {
const jobWithoutWorkflowName = { ...mockJobResponse, workflow_name: null }
expect(() =>
toWorkflowJob(jobWithoutWorkflowName as never, 'workflow_run')
).toThrow('Job workflow_name is required for job: test-job (id: 1)')
})
test('should handle jobs with different statuses correctly', () => {
const inProgressJob = { ...mockJobResponse, status: 'in_progress' }
const queuedJob = { ...mockJobResponse, status: 'queued' }
const failedJob = {
...mockJobResponse,
status: 'completed',
conclusion: 'failure'
}
expect(toWorkflowJob(inProgressJob as never, 'push')).toBeNull()
expect(toWorkflowJob(queuedJob as never, 'push')).toBeNull()
expect(toWorkflowJob(failedJob as never, 'workflow_run')).not.toBeNull()
})
test('should include runner information when available', () => {
const jobWithRunner = {
...mockJobResponse,
runner_name: 'ubuntu-latest-runner',
runner_group_name: 'github-hosted'
}
const result = toWorkflowJob(jobWithRunner as never, 'workflow_run')
expect(result).not.toBeNull()
if (result) {
expect(result.runner_name).toBe('ubuntu-latest-runner')
expect(result.runner_group_name).toBe('github-hosted')
}
})
test('should handle null runner information', () => {
const jobWithNullRunner = {
...mockJobResponse,
runner_name: null,
runner_group_name: null
}
const result = toWorkflowJob(jobWithNullRunner as never, 'workflow_run')
expect(result).not.toBeNull()
if (result) {
expect(result.runner_name).toBeNull()
expect(result.runner_group_name).toBeNull()
}
})
test('should skip incomplete jobs for push events', () => {
const incompleteJob = {
...mockJobResponse,
status: 'in_progress',
conclusion: null,
completed_at: null
}
const result = toWorkflowJob(incompleteJob as never, 'push')
expect(result).toBeNull()
})
test('should skip incomplete jobs for pull_request events', () => {
const incompleteJob = {
...mockJobResponse,
status: 'queued',
conclusion: null,
completed_at: null
}
const result = toWorkflowJob(incompleteJob as never, 'pull_request')
expect(result).toBeNull()
})
test('should log warning for skipped incomplete jobs', () => {
const consoleLogSpy = vi
.spyOn(console, 'log')
.mockImplementation(() => {})
const incompleteJob = {
...mockJobResponse,
status: 'in_progress',
conclusion: null,
completed_at: null
}
const result = toWorkflowJob(incompleteJob as never, 'push')
expect(result).toBeNull()
expect(consoleLogSpy).toHaveBeenCalledExactlyOnceWith(
'Skipping incomplete job: test-job (status: in_progress)'
)
consoleLogSpy.mockRestore()
})
test('should process completed jobs for non-workflow_run events', () => {
const result = toWorkflowJob(mockJobResponse as never, 'push')
expect(result).not.toBeNull()
if (result) {
const expected: WorkflowJob = {
...mockJobResponse,
created_at: new Date(mockJobResponse.created_at),
started_at: new Date(mockJobResponse.started_at),
completed_at: new Date(mockJobResponse.completed_at),
steps: [
{
name: 'test-step',
conclusion: 'success' as const,
started_at: new Date('2023-01-01T00:01:00Z'),
completed_at: new Date('2023-01-01T00:02:00Z')
}
]
}
expect(result).toEqual(expected)
}
})
})
describe('toWorkflowRun', () => {
const mockWorkflowResponse = {
id: 12345,
name: 'Test Workflow',
status: 'completed' as const,
conclusion: 'success',
created_at: '2023-01-01T00:00:00Z',
run_attempt: 1,
html_url: 'https://github.com/test/repo/actions/runs/12345',
repository: {
full_name: 'test-owner/test-repo'
}
}
const expectedWorkflowBase: Workflow = {
id: 12345,
name: 'Test Workflow',
conclusion: 'success',
created_at: new Date('2023-01-01T00:00:00Z'),
run_attempt: 1,
html_url: 'https://github.com/test/repo/actions/runs/12345',
actor: null,
event: null,
head_branch: null,
base_branch: null,
repository: {
full_name: 'test-owner/test-repo'
}
}
test('should convert valid workflow response', () => {
const result = toWorkflowRun(mockWorkflowResponse as never)
expect(result).toEqual(expectedWorkflowBase)
})
test('should throw error when name is missing', () => {
const workflowWithoutName = { ...mockWorkflowResponse, name: null }
expect(() => toWorkflowRun(workflowWithoutName as never)).toThrow(
'Workflow run name is required'
)
})
test('should throw error when run_attempt is missing', () => {
const workflowWithoutAttempt = {
...mockWorkflowResponse,
run_attempt: null
}
expect(() => toWorkflowRun(workflowWithoutAttempt as never)).toThrow(
'Workflow run attempt is required'
)
})
test('should handle in-progress workflows for non-workflow_run events', () => {
const consoleLogSpy = vi
.spyOn(console, 'log')
.mockImplementation(() => {})
const inProgressWorkflow = {
...mockWorkflowResponse,
status: 'in_progress',
event: 'push'
}
const result = toWorkflowRun(inProgressWorkflow as never)
expect(result).toEqual(expectedWorkflowBase)
expect(consoleLogSpy).toHaveBeenCalledExactlyOnceWith(
'Processing in-progress workflow: 12345'
)
consoleLogSpy.mockRestore()
})
test('should throw error for in-progress workflow_run events', () => {
const inProgressWorkflowRun = {
...mockWorkflowResponse,
status: 'in_progress',
event: 'workflow_run'
}
expect(() => toWorkflowRun(inProgressWorkflowRun as never)).toThrow(
'workflow status must be completed on workflow_run event'
)
})
test('should handle completed workflows for non-workflow_run events', () => {
const pushWorkflow = {
...mockWorkflowResponse,
event: 'push'
}
const result = toWorkflowRun(pushWorkflow as never)
expect(result).toEqual(expectedWorkflowBase)
})
test('should throw error when conclusion is null', () => {
const workflowWithoutConclusion = {
...mockWorkflowResponse,
conclusion: null,
event: 'workflow_run'
}
expect(() => toWorkflowRun(workflowWithoutConclusion as never)).toThrow(
`workflow status must be completed on workflow_run event`
)
})
})
describe('toWorkflowStep', () => {
const mockStepResponse = {
name: 'test-step',
conclusion: 'success',
started_at: '2023-01-01T00:01:00Z',
completed_at: '2023-01-01T00:02:00Z'
}
test('should convert valid step response', () => {
const result = toWorkflowStep(mockStepResponse as never)
expect(result).toEqual({
name: 'test-step',
conclusion: 'success',
started_at: new Date('2023-01-01T00:01:00Z'),
completed_at: new Date('2023-01-01T00:02:00Z')
})
})
test('should throw error when conclusion is missing', () => {
const stepWithoutConclusion = { ...mockStepResponse, conclusion: null }
expect(() => toWorkflowStep(stepWithoutConclusion as never)).toThrow(
'Step conclusion is required'
)
})
test('should throw error when started_at is missing', () => {
const stepWithoutStartedAt = { ...mockStepResponse, started_at: null }
expect(() => toWorkflowStep(stepWithoutStartedAt as never)).toThrow(
'Step started_at is required'
)
})
test('should throw error when completed_at is missing', () => {
const stepWithoutCompletedAt = { ...mockStepResponse, completed_at: null }
expect(() => toWorkflowStep(stepWithoutCompletedAt as never)).toThrow(
'Step completed_at is required'
)
})
})
})