@@ -20,7 +20,7 @@ import { OctokitCommon } from './common';
2020import { ChatSessionWithPR , CopilotApi , getCopilotApi , RemoteAgentJobPayload , SessionInfo , SessionSetupStep } from './copilotApi' ;
2121import { CopilotPRWatcher , CopilotStateModel } from './copilotPrWatcher' ;
2222import { CredentialStore } from './credentials' ;
23- import { FolderRepositoryManager } from './folderRepositoryManager' ;
23+ import { FolderRepositoryManager , ReposManagerState } from './folderRepositoryManager' ;
2424import { GitHubRepository } from './githubRepository' ;
2525import { GithubItemStateEnum } from './interface' ;
2626import { PullRequestModel } from './pullRequestModel' ;
@@ -117,6 +117,26 @@ export class CopilotRemoteAgentManager extends Disposable {
117117 return await getCopilotApi ( this . credentialStore , this . telemetry ) ;
118118 }
119119
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+
120140 enabled ( ) : boolean {
121141 return vscode . workspace
122142 . getConfiguration ( CODING_AGENT ) . get ( CODING_AGENT_ENABLED , false ) ;
@@ -759,8 +779,8 @@ export class CopilotRemoteAgentManager extends Disposable {
759779 return [ ] ;
760780 }
761781
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 => {
764784 const timeline = await session . getTimelineEvents ( session ) ;
765785 const status = copilotEventToStatus ( mostRecentCopilotEvent ( timeline ) ) ;
766786 if ( status !== CopilotPRStatus . Completed && status !== CopilotPRStatus . Failed ) {
@@ -771,8 +791,8 @@ export class CopilotRemoteAgentManager extends Disposable {
771791 this . _register ( disposable ) ;
772792 }
773793 return {
774- id : `${ session . id } ` ,
775- label : session . title || `Session ${ session . id } ` ,
794+ id : `${ session . number } ` ,
795+ label : session . title || `Session ${ session . number } ` ,
776796 iconPath : this . getIconForSession ( status ) ,
777797 pullRequest : session
778798 } ;
@@ -808,21 +828,23 @@ export class CopilotRemoteAgentManager extends Disposable {
808828 return this . createEmptySession ( ) ;
809829 }
810830
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 ) ;
814834 return this . createEmptySession ( ) ;
815835 }
816836
817- const pullRequest = this . findPullRequestById ( pullRequestId ) ;
837+ await this . waitRepoManagerInitialization ( ) ;
838+
839+ const pullRequest = await this . findPullRequestById ( pullRequestNumber , true ) ;
818840 if ( ! pullRequest ) {
819- Logger . error ( `Pull request not found: ${ pullRequestId } ` , CopilotRemoteAgentManager . ID ) ;
841+ Logger . error ( `Pull request not found: ${ pullRequestNumber } ` , CopilotRemoteAgentManager . ID ) ;
820842 return this . createEmptySession ( ) ;
821843 }
822844
823845 const sessions = await capi . getAllSessions ( pullRequest . id ) ;
824846 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 ) ;
826848 return this . createEmptySession ( ) ;
827849 }
828850
@@ -1254,13 +1276,25 @@ export class CopilotRemoteAgentManager extends Disposable {
12541276 }
12551277 }
12561278
1257- private findPullRequestById ( id : number ) : PullRequestModel | undefined {
1279+ private async findPullRequestById ( number : number , fetch : boolean ) : Promise < PullRequestModel | undefined > {
12581280 for ( const folderManager of this . repositoriesManager . folderManagers ) {
12591281 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 ) ;
12611283 if ( pullRequest ) {
12621284 return pullRequest ;
12631285 }
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+ }
12641298 }
12651299 }
12661300 return undefined ;
0 commit comments