@@ -327,6 +327,18 @@ export abstract class IntegrationBase<
327327 return defaultValue ;
328328 }
329329
330+ @gate ( )
331+ protected async refreshSessionIfExpired ( scope ?: LogScope ) : Promise < void > {
332+ if ( this . _session ?. expiresAt != null && this . _session . expiresAt < new Date ( ) ) {
333+ // The current session is expired, so get the latest from the cloud and refresh if needed
334+ try {
335+ await this . syncCloudConnection ( 'connected' , true ) ;
336+ } catch ( ex ) {
337+ Logger . error ( ex , scope ) ;
338+ }
339+ }
340+ }
341+
330342 @debug ( )
331343 trackRequestException ( ) : void {
332344 this . requestExceptionCount ++ ;
@@ -433,6 +445,8 @@ export abstract class IntegrationBase<
433445 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
434446 if ( ! connected ) return undefined ;
435447
448+ await this . refreshSessionIfExpired ( scope ) ;
449+
436450 try {
437451 const issues = await this . searchProviderMyIssues (
438452 this . _session ! ,
@@ -463,6 +477,8 @@ export abstract class IntegrationBase<
463477 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
464478 if ( ! connected ) return undefined ;
465479
480+ await this . refreshSessionIfExpired ( scope ) ;
481+
466482 const issueOrPR = this . container . cache . getIssueOrPullRequest (
467483 id ,
468484 options ?. type ,
@@ -507,6 +523,8 @@ export abstract class IntegrationBase<
507523 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
508524 if ( ! connected ) return undefined ;
509525
526+ await this . refreshSessionIfExpired ( scope ) ;
527+
510528 const issue = this . container . cache . getIssue (
511529 id ,
512530 resource ,
@@ -542,6 +560,8 @@ export abstract class IntegrationBase<
542560 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
543561 if ( ! connected ) return undefined ;
544562
563+ await this . refreshSessionIfExpired ( scope ) ;
564+
545565 const { expiryOverride, ...opts } = options ?? { } ;
546566
547567 const currentAccount = await this . container . cache . getCurrentAccount (
@@ -574,6 +594,8 @@ export abstract class IntegrationBase<
574594 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
575595 if ( ! connected ) return undefined ;
576596
597+ await this . refreshSessionIfExpired ( scope ) ;
598+
577599 const pr = await this . container . cache . getPullRequest ( id , resource , this , ( ) => ( {
578600 value : ( async ( ) => {
579601 try {
@@ -604,9 +626,12 @@ export abstract class IssueIntegration<
604626 @gate ( )
605627 @debug ( )
606628 async getAccountForResource ( resource : T ) : Promise < Account | undefined > {
629+ const scope = getLogScope ( ) ;
607630 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
608631 if ( ! connected ) return undefined ;
609632
633+ await this . refreshSessionIfExpired ( scope ) ;
634+
610635 try {
611636 const account = await this . getProviderAccountForResource ( this . _session ! , resource ) ;
612637 this . resetRequestExceptionCount ( ) ;
@@ -624,9 +649,12 @@ export abstract class IssueIntegration<
624649 @gate ( )
625650 @debug ( )
626651 async getResourcesForUser ( ) : Promise < T [ ] | undefined > {
652+ const scope = getLogScope ( ) ;
627653 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
628654 if ( ! connected ) return undefined ;
629655
656+ await this . refreshSessionIfExpired ( scope ) ;
657+
630658 try {
631659 const resources = await this . getProviderResourcesForUser ( this . _session ! ) ;
632660 this . resetRequestExceptionCount ( ) ;
@@ -640,9 +668,12 @@ export abstract class IssueIntegration<
640668
641669 @debug ( )
642670 async getProjectsForResources ( resources : T [ ] ) : Promise < T [ ] | undefined > {
671+ const scope = getLogScope ( ) ;
643672 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
644673 if ( ! connected ) return undefined ;
645674
675+ await this . refreshSessionIfExpired ( scope ) ;
676+
646677 try {
647678 const projects = await this . getProviderProjectsForResources ( this . _session ! , resources ) ;
648679 this . resetRequestExceptionCount ( ) ;
@@ -669,9 +700,12 @@ export abstract class IssueIntegration<
669700 project : T ,
670701 options ?: { user ?: string ; filters ?: IssueFilter [ ] } ,
671702 ) : Promise < IssueShape [ ] | undefined > {
703+ const scope = getLogScope ( ) ;
672704 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
673705 if ( ! connected ) return undefined ;
674706
707+ await this . refreshSessionIfExpired ( scope ) ;
708+
675709 try {
676710 const issues = await this . getProviderIssuesForProject ( this . _session ! , project , options ) ;
677711 this . resetRequestExceptionCount ( ) ;
@@ -708,6 +742,8 @@ export abstract class HostingIntegration<
708742 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
709743 if ( ! connected ) return undefined ;
710744
745+ await this . refreshSessionIfExpired ( scope ) ;
746+
711747 try {
712748 const author = await this . getProviderAccountForEmail ( this . _session ! , repo , email , options ) ;
713749 this . resetRequestExceptionCount ( ) ;
@@ -740,6 +776,8 @@ export abstract class HostingIntegration<
740776 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
741777 if ( ! connected ) return undefined ;
742778
779+ await this . refreshSessionIfExpired ( scope ) ;
780+
743781 try {
744782 const author = await this . getProviderAccountForCommit ( this . _session ! , repo , rev , options ) ;
745783 this . resetRequestExceptionCount ( ) ;
@@ -768,6 +806,8 @@ export abstract class HostingIntegration<
768806 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
769807 if ( ! connected ) return undefined ;
770808
809+ await this . refreshSessionIfExpired ( scope ) ;
810+
771811 const defaultBranch = this . container . cache . getRepositoryDefaultBranch (
772812 repo ,
773813 this ,
@@ -805,6 +845,8 @@ export abstract class HostingIntegration<
805845 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
806846 if ( ! connected ) return undefined ;
807847
848+ await this . refreshSessionIfExpired ( scope ) ;
849+
808850 const metadata = this . container . cache . getRepositoryMetadata (
809851 repo ,
810852 this ,
@@ -845,6 +887,8 @@ export abstract class HostingIntegration<
845887 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
846888 if ( ! connected ) return false ;
847889
890+ await this . refreshSessionIfExpired ( scope ) ;
891+
848892 try {
849893 const result = await this . mergeProviderPullRequest ( this . _session ! , pr , options ) ;
850894 this . resetRequestExceptionCount ( ) ;
@@ -877,6 +921,8 @@ export abstract class HostingIntegration<
877921 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
878922 if ( ! connected ) return undefined ;
879923
924+ await this . refreshSessionIfExpired ( scope ) ;
925+
880926 const { expiryOverride, ...opts } = options ?? { } ;
881927
882928 const pr = this . container . cache . getPullRequestForBranch (
@@ -920,6 +966,8 @@ export abstract class HostingIntegration<
920966 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
921967 if ( ! connected ) return undefined ;
922968
969+ await this . refreshSessionIfExpired ( scope ) ;
970+
923971 const pr = this . container . cache . getPullRequestForSha (
924972 rev ,
925973 repo ,
@@ -954,10 +1002,13 @@ export abstract class HostingIntegration<
9541002 customUrl ?: string ;
9551003 } ,
9561004 ) : Promise < PagedResult < ProviderIssue > | undefined > {
1005+ const scope = getLogScope ( ) ;
9571006 const providerId = this . authProvider . id ;
9581007 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
9591008 if ( ! connected ) return undefined ;
9601009
1010+ await this . refreshSessionIfExpired ( scope ) ;
1011+
9611012 const api = await this . getProvidersApi ( ) ;
9621013 if (
9631014 providerId !== HostingIntegrationId . GitLab &&
@@ -1157,10 +1208,13 @@ export abstract class HostingIntegration<
11571208 customUrl ?: string ;
11581209 } ,
11591210 ) : Promise < PagedResult < ProviderPullRequest > | undefined > {
1211+ const scope = getLogScope ( ) ;
11601212 const providerId = this . authProvider . id ;
11611213 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
11621214 if ( ! connected ) return undefined ;
11631215
1216+ await this . refreshSessionIfExpired ( scope ) ;
1217+
11641218 const api = await this . getProvidersApi ( ) ;
11651219 if (
11661220 providerId !== HostingIntegrationId . GitLab &&
@@ -1319,6 +1373,8 @@ export abstract class HostingIntegration<
13191373 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
13201374 if ( ! connected ) return undefined ;
13211375
1376+ await this . refreshSessionIfExpired ( scope ) ;
1377+
13221378 const start = Date . now ( ) ;
13231379 try {
13241380 const pullRequests = await this . searchProviderMyPullRequests (
@@ -1361,6 +1417,8 @@ export abstract class HostingIntegration<
13611417 const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
13621418 if ( ! connected ) return undefined ;
13631419
1420+ await this . refreshSessionIfExpired ( scope ) ;
1421+
13641422 try {
13651423 const prs = await this . searchProviderPullRequests ?.(
13661424 this . _session ! ,
0 commit comments