@@ -20,7 +20,7 @@ import { OctokitCommon } from './common';
20
20
import { ChatSessionWithPR , CopilotApi , getCopilotApi , RemoteAgentJobPayload , SessionInfo , SessionSetupStep } from './copilotApi' ;
21
21
import { CopilotPRWatcher , CopilotStateModel } from './copilotPrWatcher' ;
22
22
import { CredentialStore } from './credentials' ;
23
- import { FolderRepositoryManager } from './folderRepositoryManager' ;
23
+ import { FolderRepositoryManager , ReposManagerState } from './folderRepositoryManager' ;
24
24
import { GitHubRepository } from './githubRepository' ;
25
25
import { GithubItemStateEnum } from './interface' ;
26
26
import { PullRequestModel } from './pullRequestModel' ;
@@ -117,6 +117,26 @@ export class CopilotRemoteAgentManager extends Disposable {
117
117
return await getCopilotApi ( this . credentialStore , this . telemetry ) ;
118
118
}
119
119
120
+ private _repoManagerInitializationPromise : Promise < void > | undefined ;
121
+ private async waitRepoManagerInitialization ( ) {
122
+ if ( this . repositoriesManager . state === ReposManagerState . RepositoriesLoaded || this . repositoriesManager . state === ReposManagerState . NeedsAuthentication ) {
123
+ return ;
124
+ }
125
+
126
+ if ( ! this . _repoManagerInitializationPromise ) {
127
+ this . _repoManagerInitializationPromise = new Promise ( ( resolve ) => {
128
+ const disposable = this . repositoriesManager . onDidChangeState ( ( ) => {
129
+ if ( this . repositoriesManager . state === ReposManagerState . RepositoriesLoaded || this . repositoriesManager . state === ReposManagerState . NeedsAuthentication ) {
130
+ disposable . dispose ( ) ;
131
+ resolve ( ) ;
132
+ }
133
+ } ) ;
134
+ } ) ;
135
+ }
136
+
137
+ return this . _repoManagerInitializationPromise ;
138
+ }
139
+
120
140
enabled ( ) : boolean {
121
141
return vscode . workspace
122
142
. getConfiguration ( CODING_AGENT ) . get ( CODING_AGENT_ENABLED , false ) ;
@@ -759,8 +779,8 @@ export class CopilotRemoteAgentManager extends Disposable {
759
779
return [ ] ;
760
780
}
761
781
762
- const sessions = await capi . getAllCodingAgentPRs ( this . repositoriesManager ) ;
763
- return await Promise . all ( sessions . map ( async session => {
782
+ const codingAgentPRs = await capi . getAllCodingAgentPRs ( this . repositoriesManager ) ;
783
+ return await Promise . all ( codingAgentPRs . map ( async session => {
764
784
const timeline = await session . getTimelineEvents ( session ) ;
765
785
const status = copilotEventToStatus ( mostRecentCopilotEvent ( timeline ) ) ;
766
786
if ( status !== CopilotPRStatus . Completed && status !== CopilotPRStatus . Failed ) {
@@ -771,8 +791,8 @@ export class CopilotRemoteAgentManager extends Disposable {
771
791
this . _register ( disposable ) ;
772
792
}
773
793
return {
774
- id : `${ session . id } ` ,
775
- label : session . title || `Session ${ session . id } ` ,
794
+ id : `${ session . number } ` ,
795
+ label : session . title || `Session ${ session . number } ` ,
776
796
iconPath : this . getIconForSession ( status ) ,
777
797
pullRequest : session
778
798
} ;
@@ -808,21 +828,23 @@ export class CopilotRemoteAgentManager extends Disposable {
808
828
return this . createEmptySession ( ) ;
809
829
}
810
830
811
- const pullRequestId = parseInt ( id ) ;
812
- if ( isNaN ( pullRequestId ) ) {
813
- Logger . error ( `Invalid pull request ID : ${ id } ` , CopilotRemoteAgentManager . ID ) ;
831
+ const pullRequestNumber = parseInt ( id ) ;
832
+ if ( isNaN ( pullRequestNumber ) ) {
833
+ Logger . error ( `Invalid pull request number : ${ id } ` , CopilotRemoteAgentManager . ID ) ;
814
834
return this . createEmptySession ( ) ;
815
835
}
816
836
817
- const pullRequest = this . findPullRequestById ( pullRequestId ) ;
837
+ await this . waitRepoManagerInitialization ( ) ;
838
+
839
+ const pullRequest = await this . findPullRequestById ( pullRequestNumber , true ) ;
818
840
if ( ! pullRequest ) {
819
- Logger . error ( `Pull request not found: ${ pullRequestId } ` , CopilotRemoteAgentManager . ID ) ;
841
+ Logger . error ( `Pull request not found: ${ pullRequestNumber } ` , CopilotRemoteAgentManager . ID ) ;
820
842
return this . createEmptySession ( ) ;
821
843
}
822
844
823
845
const sessions = await capi . getAllSessions ( pullRequest . id ) ;
824
846
if ( ! sessions || sessions . length === 0 ) {
825
- Logger . warn ( `No sessions found for pull request ${ pullRequestId } ` , CopilotRemoteAgentManager . ID ) ;
847
+ Logger . warn ( `No sessions found for pull request ${ pullRequestNumber } ` , CopilotRemoteAgentManager . ID ) ;
826
848
return this . createEmptySession ( ) ;
827
849
}
828
850
@@ -1254,13 +1276,25 @@ export class CopilotRemoteAgentManager extends Disposable {
1254
1276
}
1255
1277
}
1256
1278
1257
- private findPullRequestById ( id : number ) : PullRequestModel | undefined {
1279
+ private async findPullRequestById ( number : number , fetch : boolean ) : Promise < PullRequestModel | undefined > {
1258
1280
for ( const folderManager of this . repositoriesManager . folderManagers ) {
1259
1281
for ( const githubRepo of folderManager . gitHubRepositories ) {
1260
- const pullRequest = githubRepo . pullRequestModels . find ( pr => pr . id === id ) ;
1282
+ const pullRequest = githubRepo . pullRequestModels . find ( pr => pr . number === number ) ;
1261
1283
if ( pullRequest ) {
1262
1284
return pullRequest ;
1263
1285
}
1286
+
1287
+ if ( fetch ) {
1288
+ try {
1289
+ const pullRequest = await githubRepo . getPullRequest ( number , false ) ;
1290
+ if ( pullRequest ) {
1291
+ return pullRequest ;
1292
+ }
1293
+ } catch ( error ) {
1294
+ // Continue to next repository if this one doesn't have the PR
1295
+ Logger . debug ( `PR ${ number } not found in ${ githubRepo . remote . owner } /${ githubRepo . remote . repositoryName } : ${ error } ` , CopilotRemoteAgentManager . ID ) ;
1296
+ }
1297
+ }
1264
1298
}
1265
1299
}
1266
1300
return undefined ;
0 commit comments