@@ -327,6 +327,18 @@ export abstract class IntegrationBase<
327
327
return defaultValue ;
328
328
}
329
329
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
+
330
342
@debug ( )
331
343
trackRequestException ( ) : void {
332
344
this . requestExceptionCount ++ ;
@@ -433,6 +445,8 @@ export abstract class IntegrationBase<
433
445
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
434
446
if ( ! connected ) return undefined ;
435
447
448
+ await this . refreshSessionIfExpired ( scope ) ;
449
+
436
450
try {
437
451
const issues = await this . searchProviderMyIssues (
438
452
this . _session ! ,
@@ -463,6 +477,8 @@ export abstract class IntegrationBase<
463
477
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
464
478
if ( ! connected ) return undefined ;
465
479
480
+ await this . refreshSessionIfExpired ( scope ) ;
481
+
466
482
const issueOrPR = this . container . cache . getIssueOrPullRequest (
467
483
id ,
468
484
options ?. type ,
@@ -507,6 +523,8 @@ export abstract class IntegrationBase<
507
523
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
508
524
if ( ! connected ) return undefined ;
509
525
526
+ await this . refreshSessionIfExpired ( scope ) ;
527
+
510
528
const issue = this . container . cache . getIssue (
511
529
id ,
512
530
resource ,
@@ -542,6 +560,8 @@ export abstract class IntegrationBase<
542
560
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
543
561
if ( ! connected ) return undefined ;
544
562
563
+ await this . refreshSessionIfExpired ( scope ) ;
564
+
545
565
const { expiryOverride, ...opts } = options ?? { } ;
546
566
547
567
const currentAccount = await this . container . cache . getCurrentAccount (
@@ -574,6 +594,8 @@ export abstract class IntegrationBase<
574
594
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
575
595
if ( ! connected ) return undefined ;
576
596
597
+ await this . refreshSessionIfExpired ( scope ) ;
598
+
577
599
const pr = await this . container . cache . getPullRequest ( id , resource , this , ( ) => ( {
578
600
value : ( async ( ) => {
579
601
try {
@@ -604,9 +626,12 @@ export abstract class IssueIntegration<
604
626
@gate ( )
605
627
@debug ( )
606
628
async getAccountForResource ( resource : T ) : Promise < Account | undefined > {
629
+ const scope = getLogScope ( ) ;
607
630
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
608
631
if ( ! connected ) return undefined ;
609
632
633
+ await this . refreshSessionIfExpired ( scope ) ;
634
+
610
635
try {
611
636
const account = await this . getProviderAccountForResource ( this . _session ! , resource ) ;
612
637
this . resetRequestExceptionCount ( ) ;
@@ -624,9 +649,12 @@ export abstract class IssueIntegration<
624
649
@gate ( )
625
650
@debug ( )
626
651
async getResourcesForUser ( ) : Promise < T [ ] | undefined > {
652
+ const scope = getLogScope ( ) ;
627
653
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
628
654
if ( ! connected ) return undefined ;
629
655
656
+ await this . refreshSessionIfExpired ( scope ) ;
657
+
630
658
try {
631
659
const resources = await this . getProviderResourcesForUser ( this . _session ! ) ;
632
660
this . resetRequestExceptionCount ( ) ;
@@ -640,9 +668,12 @@ export abstract class IssueIntegration<
640
668
641
669
@debug ( )
642
670
async getProjectsForResources ( resources : T [ ] ) : Promise < T [ ] | undefined > {
671
+ const scope = getLogScope ( ) ;
643
672
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
644
673
if ( ! connected ) return undefined ;
645
674
675
+ await this . refreshSessionIfExpired ( scope ) ;
676
+
646
677
try {
647
678
const projects = await this . getProviderProjectsForResources ( this . _session ! , resources ) ;
648
679
this . resetRequestExceptionCount ( ) ;
@@ -669,9 +700,12 @@ export abstract class IssueIntegration<
669
700
project : T ,
670
701
options ?: { user ?: string ; filters ?: IssueFilter [ ] } ,
671
702
) : Promise < IssueShape [ ] | undefined > {
703
+ const scope = getLogScope ( ) ;
672
704
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
673
705
if ( ! connected ) return undefined ;
674
706
707
+ await this . refreshSessionIfExpired ( scope ) ;
708
+
675
709
try {
676
710
const issues = await this . getProviderIssuesForProject ( this . _session ! , project , options ) ;
677
711
this . resetRequestExceptionCount ( ) ;
@@ -708,6 +742,8 @@ export abstract class HostingIntegration<
708
742
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
709
743
if ( ! connected ) return undefined ;
710
744
745
+ await this . refreshSessionIfExpired ( scope ) ;
746
+
711
747
try {
712
748
const author = await this . getProviderAccountForEmail ( this . _session ! , repo , email , options ) ;
713
749
this . resetRequestExceptionCount ( ) ;
@@ -740,6 +776,8 @@ export abstract class HostingIntegration<
740
776
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
741
777
if ( ! connected ) return undefined ;
742
778
779
+ await this . refreshSessionIfExpired ( scope ) ;
780
+
743
781
try {
744
782
const author = await this . getProviderAccountForCommit ( this . _session ! , repo , rev , options ) ;
745
783
this . resetRequestExceptionCount ( ) ;
@@ -768,6 +806,8 @@ export abstract class HostingIntegration<
768
806
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
769
807
if ( ! connected ) return undefined ;
770
808
809
+ await this . refreshSessionIfExpired ( scope ) ;
810
+
771
811
const defaultBranch = this . container . cache . getRepositoryDefaultBranch (
772
812
repo ,
773
813
this ,
@@ -805,6 +845,8 @@ export abstract class HostingIntegration<
805
845
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
806
846
if ( ! connected ) return undefined ;
807
847
848
+ await this . refreshSessionIfExpired ( scope ) ;
849
+
808
850
const metadata = this . container . cache . getRepositoryMetadata (
809
851
repo ,
810
852
this ,
@@ -845,6 +887,8 @@ export abstract class HostingIntegration<
845
887
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
846
888
if ( ! connected ) return false ;
847
889
890
+ await this . refreshSessionIfExpired ( scope ) ;
891
+
848
892
try {
849
893
const result = await this . mergeProviderPullRequest ( this . _session ! , pr , options ) ;
850
894
this . resetRequestExceptionCount ( ) ;
@@ -877,6 +921,8 @@ export abstract class HostingIntegration<
877
921
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
878
922
if ( ! connected ) return undefined ;
879
923
924
+ await this . refreshSessionIfExpired ( scope ) ;
925
+
880
926
const { expiryOverride, ...opts } = options ?? { } ;
881
927
882
928
const pr = this . container . cache . getPullRequestForBranch (
@@ -920,6 +966,8 @@ export abstract class HostingIntegration<
920
966
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
921
967
if ( ! connected ) return undefined ;
922
968
969
+ await this . refreshSessionIfExpired ( scope ) ;
970
+
923
971
const pr = this . container . cache . getPullRequestForSha (
924
972
rev ,
925
973
repo ,
@@ -954,10 +1002,13 @@ export abstract class HostingIntegration<
954
1002
customUrl ?: string ;
955
1003
} ,
956
1004
) : Promise < PagedResult < ProviderIssue > | undefined > {
1005
+ const scope = getLogScope ( ) ;
957
1006
const providerId = this . authProvider . id ;
958
1007
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
959
1008
if ( ! connected ) return undefined ;
960
1009
1010
+ await this . refreshSessionIfExpired ( scope ) ;
1011
+
961
1012
const api = await this . getProvidersApi ( ) ;
962
1013
if (
963
1014
providerId !== HostingIntegrationId . GitLab &&
@@ -1157,10 +1208,13 @@ export abstract class HostingIntegration<
1157
1208
customUrl ?: string ;
1158
1209
} ,
1159
1210
) : Promise < PagedResult < ProviderPullRequest > | undefined > {
1211
+ const scope = getLogScope ( ) ;
1160
1212
const providerId = this . authProvider . id ;
1161
1213
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
1162
1214
if ( ! connected ) return undefined ;
1163
1215
1216
+ await this . refreshSessionIfExpired ( scope ) ;
1217
+
1164
1218
const api = await this . getProvidersApi ( ) ;
1165
1219
if (
1166
1220
providerId !== HostingIntegrationId . GitLab &&
@@ -1319,6 +1373,8 @@ export abstract class HostingIntegration<
1319
1373
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
1320
1374
if ( ! connected ) return undefined ;
1321
1375
1376
+ await this . refreshSessionIfExpired ( scope ) ;
1377
+
1322
1378
const start = Date . now ( ) ;
1323
1379
try {
1324
1380
const pullRequests = await this . searchProviderMyPullRequests (
@@ -1361,6 +1417,8 @@ export abstract class HostingIntegration<
1361
1417
const connected = this . maybeConnected ?? ( await this . isConnected ( ) ) ;
1362
1418
if ( ! connected ) return undefined ;
1363
1419
1420
+ await this . refreshSessionIfExpired ( scope ) ;
1421
+
1364
1422
try {
1365
1423
const prs = await this . searchProviderPullRequests ?.(
1366
1424
this . _session ! ,
0 commit comments