@@ -5,13 +5,29 @@ import { GitHubCommit, GitHubRepository } from "interfaces/github";
5
5
import { shouldSendAll , booleanFlag , BooleanFlags , numberFlag , NumberFlags } from "config/feature-flags" ;
6
6
import { getLogger } from "config/logger" ;
7
7
import { GitHubInstallationClient } from "../github/client/github-installation-client" ;
8
+ import { GithubClientCommitNotFoundBySHAError } from "../github/client/github-client-errors" ;
8
9
import { DatabaseStateCreator , CreatorResult } from "test/utils/database-state-creator" ;
9
10
10
11
jest . mock ( "../sqs/queues" ) ;
11
12
jest . mock ( "config/feature-flags" ) ;
12
13
const logger = getLogger ( "test" ) ;
13
14
14
15
describe ( "Enqueue push" , ( ) => {
16
+
17
+ let db : CreatorResult ;
18
+ let issueKey : string ;
19
+ let sha : string ;
20
+ beforeEach ( async ( ) => {
21
+ db = await new DatabaseStateCreator ( )
22
+ . forCloud ( )
23
+ . create ( ) ;
24
+ when ( shouldSendAll ) . calledWith ( "commits" , expect . anything ( ) , expect . anything ( ) ) . mockResolvedValue ( false ) ;
25
+ when ( numberFlag ) . calledWith ( NumberFlags . SKIP_PROCESS_QUEUE_IF_ISSUE_NOT_FOUND_TIMEOUT , expect . anything ( ) , expect . anything ( ) )
26
+ . mockResolvedValue ( 10000 ) ;
27
+ issueKey = `KEY-${ new Date ( ) . getTime ( ) } ` ;
28
+ sha = `sha-${ issueKey } ` ;
29
+ } ) ;
30
+
15
31
it ( "should push GitHubAppConfig to payload" , async ( ) => {
16
32
await enqueuePush ( {
17
33
installation : { id : 123 , node_id : 456 } ,
@@ -105,21 +121,72 @@ describe("Enqueue push", () => {
105
121
} ) , 0 , expect . anything ( ) ) ;
106
122
} ) ;
107
123
108
- describe ( "Skipping msg when issue not exist" , ( ) => {
124
+ describe ( "Maybe skipp commit when ff is on and commimt sha on not found" , ( ) => {
125
+ it ( "when ff is OFF, should throw error for the problematic commits if 422 and even on last try" , async ( ) => {
126
+
127
+ when ( booleanFlag ) . calledWith ( BooleanFlags . SKIP_COMMIT_IF_SHA_NOT_FOUND_ON_LAST_TRY , expect . anything ( ) )
128
+ . mockResolvedValue ( false ) ;
129
+
130
+ githubUserTokenNock ( db . subscription . gitHubInstallationId ) ;
131
+ githubUserTokenNock ( db . subscription . gitHubInstallationId ) ;
132
+ mockGitHubCommitRestApi ( ) ;
133
+ mockGitHubCommitRestApi422 ( "invalid-sha" ) ;
134
+
135
+ const pushPayload = getPushPayload ( ) ;
136
+ pushPayload . shas . push ( {
137
+ id : "invalid-sha" ,
138
+ issueKeys : [ issueKey ]
139
+ } ) ;
140
+
141
+ await expect ( async ( ) => {
142
+ await processPush ( getGitHubClient ( ) , getContext ( { lastAttempt : true } ) , pushPayload , logger ) ;
143
+ } ) . rejects . toThrow ( GithubClientCommitNotFoundBySHAError ) ;
144
+ } ) ;
145
+
146
+ it ( "should throw error for the problematic commits if 422 but NOT on last try" , async ( ) => {
147
+
148
+ when ( booleanFlag ) . calledWith ( BooleanFlags . SKIP_COMMIT_IF_SHA_NOT_FOUND_ON_LAST_TRY , expect . anything ( ) )
149
+ . mockResolvedValue ( true ) ;
150
+
151
+ githubUserTokenNock ( db . subscription . gitHubInstallationId ) ;
152
+ githubUserTokenNock ( db . subscription . gitHubInstallationId ) ;
153
+ mockGitHubCommitRestApi ( ) ;
154
+ mockGitHubCommitRestApi422 ( "invalid-sha" ) ;
155
+
156
+ const pushPayload = getPushPayload ( ) ;
157
+ pushPayload . shas . push ( {
158
+ id : "invalid-sha" ,
159
+ issueKeys : [ issueKey ]
160
+ } ) ;
161
+
162
+ await expect ( async ( ) => {
163
+ await processPush ( getGitHubClient ( ) , getContext ( { lastAttempt : false } ) , pushPayload , logger ) ;
164
+ } ) . rejects . toThrow ( GithubClientCommitNotFoundBySHAError ) ;
165
+ } ) ;
109
166
110
- let db : CreatorResult ;
111
- let issueKey ;
112
- let sha ;
113
- beforeEach ( async ( ) => {
114
- db = await new DatabaseStateCreator ( )
115
- . forCloud ( )
116
- . create ( ) ;
117
- when ( shouldSendAll ) . calledWith ( "commits" , expect . anything ( ) , expect . anything ( ) ) . mockResolvedValue ( false ) ;
118
- when ( numberFlag ) . calledWith ( NumberFlags . SKIP_PROCESS_QUEUE_IF_ISSUE_NOT_FOUND_TIMEOUT , expect . anything ( ) , expect . anything ( ) )
119
- . mockResolvedValue ( 10000 ) ;
120
- issueKey = `KEY-${ new Date ( ) . getTime ( ) } ` ;
121
- sha = `sha-${ issueKey } ` ;
167
+ it ( "should success skip the problematic commits if 422 on last try" , async ( ) => {
168
+
169
+ when ( booleanFlag ) . calledWith ( BooleanFlags . SKIP_COMMIT_IF_SHA_NOT_FOUND_ON_LAST_TRY , expect . anything ( ) )
170
+ . mockResolvedValue ( true ) ;
171
+
172
+ githubUserTokenNock ( db . subscription . gitHubInstallationId ) ;
173
+ githubUserTokenNock ( db . subscription . gitHubInstallationId ) ;
174
+ mockGitHubCommitRestApi ( ) ;
175
+ mockGitHubCommitRestApi422 ( "invalid-sha" ) ;
176
+
177
+ mockJiraDevInfoAcceptUpdate ( ) ;
178
+
179
+ const pushPayload = getPushPayload ( ) ;
180
+ pushPayload . shas . push ( {
181
+ id : "invalid-sha" ,
182
+ issueKeys : [ issueKey ]
183
+ } ) ;
184
+
185
+ await processPush ( getGitHubClient ( ) , getContext ( { lastAttempt : true } ) , pushPayload , logger ) ;
122
186
} ) ;
187
+ } ) ;
188
+
189
+ describe ( "Skipping msg when issue not exist" , ( ) => {
123
190
124
191
describe ( "Use redis to avoid overload jira" , ( ) => {
125
192
it ( "should reuse status from redis and only call jira once for same issue-key" , async ( ) => {
@@ -128,8 +195,8 @@ describe("Enqueue push", () => {
128
195
129
196
mockIssueNotExists ( ) ;
130
197
131
- await processPush ( getGitHubClient ( ) , getPushPayload ( ) , logger ) ;
132
- await processPush ( getGitHubClient ( ) , getPushPayload ( ) , logger ) ;
198
+ await processPush ( getGitHubClient ( ) , getContext ( ) , getPushPayload ( ) , logger ) ;
199
+ await processPush ( getGitHubClient ( ) , getContext ( ) , getPushPayload ( ) , logger ) ;
133
200
} ) ;
134
201
} ) ;
135
202
@@ -140,7 +207,7 @@ describe("Enqueue push", () => {
140
207
141
208
mockIssueNotExists ( ) ;
142
209
143
- await processPush ( getGitHubClient ( ) , getPushPayload ( ) , logger ) ;
210
+ await processPush ( getGitHubClient ( ) , getContext ( ) , getPushPayload ( ) , logger ) ;
144
211
145
212
} ) ;
146
213
@@ -156,7 +223,7 @@ describe("Enqueue push", () => {
156
223
157
224
mockJiraDevInfoAcceptUpdate ( ) ;
158
225
159
- await processPush ( getGitHubClient ( ) , getPushPayload ( ) , logger ) ;
226
+ await processPush ( getGitHubClient ( ) , getContext ( ) , getPushPayload ( ) , logger ) ;
160
227
161
228
} ) ;
162
229
@@ -170,66 +237,81 @@ describe("Enqueue push", () => {
170
237
171
238
mockJiraDevInfoAcceptUpdate ( ) ;
172
239
173
- await processPush ( getGitHubClient ( ) , getPushPayload ( ) , logger ) ;
240
+ await processPush ( getGitHubClient ( ) , getContext ( ) , getPushPayload ( ) , logger ) ;
174
241
175
242
} ) ;
243
+ } ) ;
176
244
177
- const mockJiraDevInfoAcceptUpdate = ( ) => {
178
- jiraNock . post ( "/rest/devinfo/0.10/bulk" , ( reqBody ) => {
179
- return reqBody . repositories [ 0 ] . commits . flatMap ( c => c . issueKeys ) . some ( ck => ck === issueKey ) ;
180
- } ) . reply ( 202 , "" ) ;
181
- } ;
182
-
183
- const mockGitHubCommitRestApi = ( ) => {
184
- githubNock
185
- . get ( "/repos/org1/repo1/commits/" + sha )
186
- . reply ( 200 , {
187
- files : [ ] ,
188
- sha
189
- } ) ;
190
- } ;
191
-
192
- const getPushPayload = ( ) => {
193
- return {
194
- jiraHost,
195
- installationId : db . subscription . gitHubInstallationId ,
196
- gitHubAppConfig : undefined ,
197
- webhookId : "aaa" ,
198
- repository : {
199
- owner : { login : "org1" } ,
200
- name : "repo1"
201
- } as GitHubRepository ,
202
- shas : [ {
203
- id : sha ,
204
- issueKeys : [ issueKey ]
205
- } ]
206
- } ;
245
+ const mockJiraDevInfoAcceptUpdate = ( ) => {
246
+ jiraNock . post ( "/rest/devinfo/0.10/bulk" , ( reqBody ) => {
247
+ return reqBody . repositories [ 0 ] . commits . flatMap ( c => c . issueKeys ) . some ( ck => ck === issueKey ) ;
248
+ } ) . reply ( 202 , "" ) ;
249
+ } ;
250
+
251
+ const mockGitHubCommitRestApi = ( ) => {
252
+ githubNock
253
+ . get ( "/repos/org1/repo1/commits/" + sha )
254
+ . reply ( 200 , {
255
+ files : [ ] ,
256
+ sha
257
+ } ) ;
258
+ } ;
259
+
260
+ const mockGitHubCommitRestApi422 = ( sha : string ) => {
261
+ githubNock
262
+ . get ( "/repos/org1/repo1/commits/" + sha )
263
+ . reply ( 422 , {
264
+ message : `No commit found for SHA: ${ sha } ` ,
265
+ documentation_url : "https://docs.github.com"
266
+ } ) ;
267
+ } ;
268
+
269
+ const getPushPayload = ( ) => {
270
+ return {
271
+ jiraHost,
272
+ installationId : db . subscription . gitHubInstallationId ,
273
+ gitHubAppConfig : undefined ,
274
+ webhookId : "aaa" ,
275
+ repository : {
276
+ owner : { login : "org1" } ,
277
+ name : "repo1"
278
+ } as GitHubRepository ,
279
+ shas : [ {
280
+ id : sha ,
281
+ issueKeys : [ issueKey ]
282
+ } ]
207
283
} ;
284
+ } ;
208
285
209
- const mockIssueExists = ( ) => {
210
- jiraNock . get ( `/rest/api/latest/issue/${ issueKey } ` )
211
- . query ( { fields : "summary" } ) . reply ( 200 , { } ) ;
212
- } ;
286
+ const mockIssueExists = ( ) => {
287
+ jiraNock . get ( `/rest/api/latest/issue/${ issueKey } ` )
288
+ . query ( { fields : "summary" } ) . reply ( 200 , { } ) ;
289
+ } ;
213
290
214
- const mockIssueNotExists = ( ) => {
215
- jiraNock . get ( `/rest/api/latest/issue/${ issueKey } ` )
216
- . query ( { fields : "summary" } ) . reply ( 404 , "" ) ;
217
- } ;
291
+ const mockIssueNotExists = ( ) => {
292
+ jiraNock . get ( `/rest/api/latest/issue/${ issueKey } ` )
293
+ . query ( { fields : "summary" } ) . reply ( 404 , "" ) ;
294
+ } ;
218
295
219
- const getGitHubClient = ( ) => {
220
- return new GitHubInstallationClient ( {
221
- appId : 2 ,
222
- githubBaseUrl : "https://api.github.com" ,
223
- installationId : db . subscription . gitHubInstallationId
224
- } , {
225
- apiUrl : "https://api.github.com" ,
226
- baseUrl : "https://github.com" ,
227
- graphqlUrl : "https://api.github.com/graphql" ,
228
- hostname : "https://github.com" ,
229
- apiKeyConfig : undefined ,
230
- proxyBaseUrl : undefined
231
- } , jiraHost , { trigger : "test" } , logger , undefined ) ;
296
+ const getGitHubClient = ( ) => {
297
+ return new GitHubInstallationClient ( {
298
+ appId : 2 ,
299
+ githubBaseUrl : "https://api.github.com" ,
300
+ installationId : db . subscription . gitHubInstallationId
301
+ } , {
302
+ apiUrl : "https://api.github.com" ,
303
+ baseUrl : "https://github.com" ,
304
+ graphqlUrl : "https://api.github.com/graphql" ,
305
+ hostname : "https://github.com" ,
306
+ apiKeyConfig : undefined ,
307
+ proxyBaseUrl : undefined
308
+ } , jiraHost , { trigger : "test" } , logger , undefined ) ;
309
+ } ;
310
+
311
+ const getContext = ( override ?: any ) : any => {
312
+ return {
313
+ ...override
232
314
} ;
233
- } ) ;
315
+ } ;
234
316
235
317
} ) ;
0 commit comments