1
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
2
+ const { getBuildArtifact } = require ( '../../.github/scripts/get-build-artifact' ) ;
3
+ import * as sinon from 'sinon' ;
4
+
5
+ describe ( 'getBuildArtifact' , ( ) => {
6
+ let mockGithub : any ;
7
+ let mockContext : any ;
8
+ let mockCore : any ;
9
+ let sandbox : sinon . SinonSandbox ;
10
+
11
+ beforeEach ( ( ) => {
12
+ sandbox = sinon . createSandbox ( ) ;
13
+
14
+ // Create mock GitHub API client
15
+ mockGithub = {
16
+ rest : {
17
+ actions : {
18
+ listWorkflowRuns : sandbox . stub ( )
19
+ }
20
+ }
21
+ } ;
22
+
23
+ mockContext = {
24
+ repo : {
25
+ owner : 'scality' ,
26
+ repo : 'zenko' ,
27
+ } ,
28
+ sha : 'abcd1234567890abcdef1234567890abcdefabcd' ,
29
+ } ;
30
+
31
+ mockCore = {
32
+ info : sandbox . stub ( ) ,
33
+ exportVariable : sandbox . stub ( ) ,
34
+ } ;
35
+ } ) ;
36
+
37
+ afterEach ( ( ) => {
38
+ sandbox . restore ( ) ;
39
+ } ) ;
40
+
41
+ it ( 'should construct correct artifacts name from successful run' , async ( ) => {
42
+ mockGithub . rest . actions . listWorkflowRuns . resolves ( {
43
+ data : {
44
+ total_count : 1 ,
45
+ workflow_runs : [
46
+ {
47
+ id : 12345 ,
48
+ name : 'build-iso-and-end2end-test' ,
49
+ conclusion : 'success' ,
50
+ run_number : 678 ,
51
+ status : 'completed' ,
52
+ head_sha : 'abcd1234567890abcdef1234567890abcdefabcd' ,
53
+ workflow_id : 1 ,
54
+ url : 'https://api.github.com/repos/scality/zenko/actions/runs/12345' ,
55
+ html_url : 'https://github.com/scality/zenko/actions/runs/12345' ,
56
+ created_at : '2023-01-01T00:00:00Z' ,
57
+ updated_at : '2023-01-01T00:00:00Z' ,
58
+ run_started_at : '2023-01-01T00:00:00Z' ,
59
+ } ,
60
+ ] ,
61
+ } ,
62
+ } ) ;
63
+
64
+ const result = await getBuildArtifact ( mockGithub , mockContext , mockCore ) ;
65
+ expect ( result ) . toBe ( 'github:scality:zenko:staging-abcd123.build-iso-and-end2end-test.678' ) ;
66
+
67
+ // Verify the API call was made with correct parameters
68
+ expect ( mockGithub . rest . actions . listWorkflowRuns . calledOnce ) . toBe ( true ) ;
69
+ expect ( mockGithub . rest . actions . listWorkflowRuns . calledWith ( {
70
+ owner : 'scality' ,
71
+ repo : 'zenko' ,
72
+ workflow_id : 'build-iso-and-end2end-test' ,
73
+ head_sha : 'abcd1234567890abcdef1234567890abcdefabcd' ,
74
+ status : 'completed' ,
75
+ conclusion : 'success'
76
+ } ) ) . toBe ( true ) ;
77
+
78
+ // Verify logging calls
79
+ expect ( mockCore . info . calledWith ( 'Looking for successful builds for commit: abcd1234567890abcdef1234567890abcdefabcd' ) ) . toBe ( true ) ;
80
+ expect ( mockCore . info . calledWith ( 'Found staging run: 12345 with conclusion: success' ) ) . toBe ( true ) ;
81
+ expect ( mockCore . info . calledWith ( 'Auto-derived artifacts name: github:scality:zenko:staging-abcd123.build-iso-and-end2end-test.678' ) ) . toBe ( true ) ;
82
+ } ) ;
83
+
84
+ it ( 'should fail when no staging workflow run is found' , async ( ) => {
85
+ mockGithub . rest . actions . listWorkflowRuns . resolves ( {
86
+ data : {
87
+ total_count : 0 ,
88
+ workflow_runs : [ ] ,
89
+ } ,
90
+ } ) ;
91
+
92
+ await expect ( getBuildArtifact ( mockGithub , mockContext , mockCore ) )
93
+ . rejects
94
+ . toThrow ( 'No successful end2end workflow run found for commit abcd1234567890abcdef1234567890abcdefabcd' ) ;
95
+
96
+ // Verify the API call was made with correct parameters
97
+ expect ( mockGithub . rest . actions . listWorkflowRuns . calledOnce ) . toBe ( true ) ;
98
+ expect ( mockGithub . rest . actions . listWorkflowRuns . calledWith ( {
99
+ owner : 'scality' ,
100
+ repo : 'zenko' ,
101
+ workflow_id : 'build-iso-and-end2end-test' ,
102
+ head_sha : 'abcd1234567890abcdef1234567890abcdefabcd' ,
103
+ status : 'completed' ,
104
+ conclusion : 'success'
105
+ } ) ) . toBe ( true ) ;
106
+ } ) ;
107
+
108
+ it ( 'should handle workflow run with multiple workflows' , async ( ) => {
109
+ mockGithub . rest . actions . listWorkflowRuns . resolves ( {
110
+ data : {
111
+ total_count : 2 ,
112
+ workflow_runs : [
113
+ {
114
+ id : 56789 ,
115
+ name : 'build-iso-and-end2end-test' ,
116
+ conclusion : 'success' ,
117
+ run_number : 999 ,
118
+ status : 'completed' ,
119
+ head_sha : 'abcd1234567890abcdef1234567890abcdefabcd' ,
120
+ url : 'https://api.github.com/repos/scality/zenko/actions/runs/56789' ,
121
+ html_url : 'https://github.com/scality/zenko/actions/runs/56789' ,
122
+ created_at : '2024-01-01T00:00:00Z' ,
123
+ updated_at : '2024-01-01T00:00:00Z' ,
124
+ run_started_at : '2024-01-01T00:00:00Z' ,
125
+ } ,
126
+ {
127
+ id : 12345 ,
128
+ name : 'build-iso-and-end2end-test' ,
129
+ conclusion : 'success' ,
130
+ run_number : 678 ,
131
+ status : 'completed' ,
132
+ head_sha : 'abcd1234567890abcdef1234567890abcdefabcd' ,
133
+ workflow_id : 1 ,
134
+ url : 'https://api.github.com/repos/scality/zenko/actions/runs/12345' ,
135
+ html_url : 'https://github.com/scality/zenko/actions/runs/12345' ,
136
+ created_at : '2023-01-01T00:00:00Z' ,
137
+ updated_at : '2023-01-01T00:00:00Z' ,
138
+ run_started_at : '2023-01-01T00:00:00Z' ,
139
+ } ,
140
+ ] ,
141
+ } ,
142
+ } ) ;
143
+
144
+ const result = await getBuildArtifact ( mockGithub , mockContext , mockCore ) ;
145
+
146
+ expect ( result ) . toBe ( 'github:scality:zenko:staging-abcd123.build-iso-and-end2end-test.999' ) ;
147
+ expect ( mockCore . info . calledWith ( 'Found staging run: 56789 with conclusion: success' ) ) . toBe ( true ) ;
148
+ } ) ;
149
+ } ) ;
0 commit comments