@@ -50,6 +50,29 @@ export type IntegrationResult<T> =
50
50
| { error : Error ; duration ?: number ; value ?: never }
51
51
| undefined ;
52
52
53
+ type SyncReqUsecase = Exclude <
54
+ | 'getAccountForCommit'
55
+ | 'getAccountForEmail'
56
+ | 'getAccountForResource'
57
+ | 'getCurrentAccount'
58
+ | 'getDefaultBranch'
59
+ | 'getIssue'
60
+ | 'getIssueOrPullRequest'
61
+ | 'getIssuesForProject'
62
+ | 'getProjectsForResources'
63
+ | 'getPullRequest'
64
+ | 'getPullRequestForBranch'
65
+ | 'getPullRequestForCommit'
66
+ | 'getRepositoryMetadata'
67
+ | 'getResourcesForUser'
68
+ | 'mergePullRequest'
69
+ | 'searchMyIssues'
70
+ | 'searchMyPullRequests'
71
+ | 'searchPullRequests' ,
72
+ // excluding to show explicitly that we don't want to add 'all' key occasionally
73
+ 'all'
74
+ > ;
75
+
53
76
export abstract class IntegrationBase <
54
77
ID extends IntegrationIds = IntegrationIds ,
55
78
T extends ResourceDescriptor = ResourceDescriptor ,
@@ -174,7 +197,7 @@ export abstract class IntegrationBase<
174
197
void authProvider . deleteSession ( this . authProviderDescriptor ) ;
175
198
}
176
199
177
- this . resetRequestExceptionCount ( ) ;
200
+ this . resetRequestExceptionCount ( 'all' ) ;
178
201
this . _session = null ;
179
202
180
203
if ( connected ) {
@@ -207,14 +230,23 @@ export abstract class IntegrationBase<
207
230
void this . ensureSession ( { createIfNeeded : false } ) ;
208
231
}
209
232
233
+ private _syncRequestsPerFailedUsecase = new Set < SyncReqUsecase > ( ) ;
234
+ hasSessionSyncRequests ( ) : boolean {
235
+ return this . _syncRequestsPerFailedUsecase . size > 0 ;
236
+ }
237
+ requestSessionSyncForUsecase ( syncReqUsecase : SyncReqUsecase ) : void {
238
+ this . _syncRequestsPerFailedUsecase . add ( syncReqUsecase ) ;
239
+ }
210
240
private static readonly requestExceptionLimit = 5 ;
211
- private static readonly syncDueToRequestExceptionLimit = 1 ;
212
- private syncCountDueToRequestException = 0 ;
213
241
private requestExceptionCount = 0 ;
214
242
215
- resetRequestExceptionCount ( ) : void {
243
+ resetRequestExceptionCount ( syncReqUsecase : SyncReqUsecase | 'all' ) : void {
216
244
this . requestExceptionCount = 0 ;
217
- this . syncCountDueToRequestException = 0 ;
245
+ if ( syncReqUsecase === 'all' ) {
246
+ this . _syncRequestsPerFailedUsecase . clear ( ) ;
247
+ } else {
248
+ this . _syncRequestsPerFailedUsecase . delete ( syncReqUsecase ) ;
249
+ }
218
250
}
219
251
220
252
/**
@@ -277,14 +309,19 @@ export abstract class IntegrationBase<
277
309
}
278
310
}
279
311
280
- protected handleProviderException < T > ( ex : Error , scope : LogScope | undefined , defaultValue : T ) : T {
312
+ protected handleProviderException < T > (
313
+ syncReqUsecase : SyncReqUsecase ,
314
+ ex : Error ,
315
+ scope : LogScope | undefined ,
316
+ defaultValue : T ,
317
+ ) : T {
281
318
if ( ex instanceof CancellationError ) return defaultValue ;
282
319
283
320
Logger . error ( ex , scope ) ;
284
321
285
322
if ( ex instanceof AuthenticationError && this . _session ?. cloud ) {
286
- if ( this . syncCountDueToRequestException < IntegrationBase . syncDueToRequestExceptionLimit ) {
287
- this . syncCountDueToRequestException ++ ;
323
+ if ( ! this . hasSessionSyncRequests ( ) ) {
324
+ this . requestSessionSyncForUsecase ( syncReqUsecase ) ;
288
325
this . _session = {
289
326
...this . _session ,
290
327
expiresAt : new Date ( Date . now ( ) - 1 ) ,
@@ -436,10 +473,10 @@ export abstract class IntegrationBase<
436
473
resources != null ? ( Array . isArray ( resources ) ? resources : [ resources ] ) : undefined ,
437
474
cancellation ,
438
475
) ;
439
- this . resetRequestExceptionCount ( ) ;
476
+ this . resetRequestExceptionCount ( 'searchMyIssues' ) ;
440
477
return issues ;
441
478
} catch ( ex ) {
442
- return this . handleProviderException < IssueShape [ ] | undefined > ( ex , scope , undefined ) ;
479
+ return this . handleProviderException < IssueShape [ ] | undefined > ( 'searchMyIssues' , ex , scope , undefined ) ;
443
480
}
444
481
}
445
482
@@ -476,10 +513,15 @@ export abstract class IntegrationBase<
476
513
id ,
477
514
options ?. type ,
478
515
) ;
479
- this . resetRequestExceptionCount ( ) ;
516
+ this . resetRequestExceptionCount ( 'getIssueOrPullRequest' ) ;
480
517
return result ;
481
518
} catch ( ex ) {
482
- return this . handleProviderException < IssueOrPullRequest | undefined > ( ex , scope , undefined ) ;
519
+ return this . handleProviderException < IssueOrPullRequest | undefined > (
520
+ 'getIssueOrPullRequest' ,
521
+ ex ,
522
+ scope ,
523
+ undefined ,
524
+ ) ;
483
525
}
484
526
} ) ( ) ,
485
527
} ) ,
@@ -516,10 +558,10 @@ export abstract class IntegrationBase<
516
558
value : ( async ( ) => {
517
559
try {
518
560
const result = await this . getProviderIssue ( this . _session ! , resource , id ) ;
519
- this . resetRequestExceptionCount ( ) ;
561
+ this . resetRequestExceptionCount ( 'getIssue' ) ;
520
562
return result ;
521
563
} catch ( ex ) {
522
- return this . handleProviderException < Issue | undefined > ( ex , scope , undefined ) ;
564
+ return this . handleProviderException < Issue | undefined > ( 'getIssue' , ex , scope , undefined ) ;
523
565
}
524
566
} ) ( ) ,
525
567
} ) ,
@@ -553,10 +595,15 @@ export abstract class IntegrationBase<
553
595
value : ( async ( ) => {
554
596
try {
555
597
const account = await this . getProviderCurrentAccount ?.( this . _session ! , opts ) ;
556
- this . resetRequestExceptionCount ( ) ;
598
+ this . resetRequestExceptionCount ( 'getCurrentAccount' ) ;
557
599
return account ;
558
600
} catch ( ex ) {
559
- return this . handleProviderException < Account | undefined > ( ex , scope , undefined ) ;
601
+ return this . handleProviderException < Account | undefined > (
602
+ 'getCurrentAccount' ,
603
+ ex ,
604
+ scope ,
605
+ undefined ,
606
+ ) ;
560
607
}
561
608
} ) ( ) ,
562
609
} ) ,
@@ -583,10 +630,15 @@ export abstract class IntegrationBase<
583
630
value : ( async ( ) => {
584
631
try {
585
632
const result = await this . getProviderPullRequest ?.( this . _session ! , resource , id ) ;
586
- this . resetRequestExceptionCount ( ) ;
633
+ this . resetRequestExceptionCount ( 'getPullRequest' ) ;
587
634
return result ;
588
635
} catch ( ex ) {
589
- return this . handleProviderException < PullRequest | undefined > ( ex , scope , undefined ) ;
636
+ return this . handleProviderException < PullRequest | undefined > (
637
+ 'getPullRequest' ,
638
+ ex ,
639
+ scope ,
640
+ undefined ,
641
+ ) ;
590
642
}
591
643
} ) ( ) ,
592
644
} ) ) ;
0 commit comments