@@ -50,6 +50,29 @@ export type IntegrationResult<T> =
5050 | { error : Error ; duration ?: number ; value ?: never }
5151 | undefined ;
5252
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+
5376export abstract class IntegrationBase <
5477 ID extends IntegrationIds = IntegrationIds ,
5578 T extends ResourceDescriptor = ResourceDescriptor ,
@@ -174,7 +197,7 @@ export abstract class IntegrationBase<
174197 void authProvider . deleteSession ( this . authProviderDescriptor ) ;
175198 }
176199
177- this . resetRequestExceptionCount ( ) ;
200+ this . resetRequestExceptionCount ( 'all' ) ;
178201 this . _session = null ;
179202
180203 if ( connected ) {
@@ -207,14 +230,23 @@ export abstract class IntegrationBase<
207230 void this . ensureSession ( { createIfNeeded : false } ) ;
208231 }
209232
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+ }
210240 private static readonly requestExceptionLimit = 5 ;
211- private static readonly syncDueToRequestExceptionLimit = 1 ;
212- private syncCountDueToRequestException = 0 ;
213241 private requestExceptionCount = 0 ;
214242
215- resetRequestExceptionCount ( ) : void {
243+ resetRequestExceptionCount ( syncReqUsecase : SyncReqUsecase | 'all' ) : void {
216244 this . requestExceptionCount = 0 ;
217- this . syncCountDueToRequestException = 0 ;
245+ if ( syncReqUsecase === 'all' ) {
246+ this . _syncRequestsPerFailedUsecase . clear ( ) ;
247+ } else {
248+ this . _syncRequestsPerFailedUsecase . delete ( syncReqUsecase ) ;
249+ }
218250 }
219251
220252 /**
@@ -277,14 +309,19 @@ export abstract class IntegrationBase<
277309 }
278310 }
279311
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 {
281318 if ( ex instanceof CancellationError ) return defaultValue ;
282319
283320 Logger . error ( ex , scope ) ;
284321
285322 if ( ex instanceof AuthenticationError && this . _session ?. cloud ) {
286- if ( this . syncCountDueToRequestException < IntegrationBase . syncDueToRequestExceptionLimit ) {
287- this . syncCountDueToRequestException ++ ;
323+ if ( ! this . hasSessionSyncRequests ( ) ) {
324+ this . requestSessionSyncForUsecase ( syncReqUsecase ) ;
288325 this . _session = {
289326 ...this . _session ,
290327 expiresAt : new Date ( Date . now ( ) - 1 ) ,
@@ -436,10 +473,10 @@ export abstract class IntegrationBase<
436473 resources != null ? ( Array . isArray ( resources ) ? resources : [ resources ] ) : undefined ,
437474 cancellation ,
438475 ) ;
439- this . resetRequestExceptionCount ( ) ;
476+ this . resetRequestExceptionCount ( 'searchMyIssues' ) ;
440477 return issues ;
441478 } catch ( ex ) {
442- return this . handleProviderException < IssueShape [ ] | undefined > ( ex , scope , undefined ) ;
479+ return this . handleProviderException < IssueShape [ ] | undefined > ( 'searchMyIssues' , ex , scope , undefined ) ;
443480 }
444481 }
445482
@@ -476,10 +513,15 @@ export abstract class IntegrationBase<
476513 id ,
477514 options ?. type ,
478515 ) ;
479- this . resetRequestExceptionCount ( ) ;
516+ this . resetRequestExceptionCount ( 'getIssueOrPullRequest' ) ;
480517 return result ;
481518 } 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+ ) ;
483525 }
484526 } ) ( ) ,
485527 } ) ,
@@ -516,10 +558,10 @@ export abstract class IntegrationBase<
516558 value : ( async ( ) => {
517559 try {
518560 const result = await this . getProviderIssue ( this . _session ! , resource , id ) ;
519- this . resetRequestExceptionCount ( ) ;
561+ this . resetRequestExceptionCount ( 'getIssue' ) ;
520562 return result ;
521563 } catch ( ex ) {
522- return this . handleProviderException < Issue | undefined > ( ex , scope , undefined ) ;
564+ return this . handleProviderException < Issue | undefined > ( 'getIssue' , ex , scope , undefined ) ;
523565 }
524566 } ) ( ) ,
525567 } ) ,
@@ -553,10 +595,15 @@ export abstract class IntegrationBase<
553595 value : ( async ( ) => {
554596 try {
555597 const account = await this . getProviderCurrentAccount ?.( this . _session ! , opts ) ;
556- this . resetRequestExceptionCount ( ) ;
598+ this . resetRequestExceptionCount ( 'getCurrentAccount' ) ;
557599 return account ;
558600 } 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+ ) ;
560607 }
561608 } ) ( ) ,
562609 } ) ,
@@ -583,10 +630,15 @@ export abstract class IntegrationBase<
583630 value : ( async ( ) => {
584631 try {
585632 const result = await this . getProviderPullRequest ?.( this . _session ! , resource , id ) ;
586- this . resetRequestExceptionCount ( ) ;
633+ this . resetRequestExceptionCount ( 'getPullRequest' ) ;
587634 return result ;
588635 } 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+ ) ;
590642 }
591643 } ) ( ) ,
592644 } ) ) ;
0 commit comments