diff --git a/src/main/kotlin/com/sourcegraph/cody/context/RemoteRepoUtils.kt b/src/main/kotlin/com/sourcegraph/cody/context/RemoteRepoUtils.kt index 1917638e0a..e6fce22ed0 100644 --- a/src/main/kotlin/com/sourcegraph/cody/context/RemoteRepoUtils.kt +++ b/src/main/kotlin/com/sourcegraph/cody/context/RemoteRepoUtils.kt @@ -8,14 +8,18 @@ import com.sourcegraph.vcs.CodebaseName import java.util.concurrent.CompletableFuture object RemoteRepoUtils { - fun getRepository(project: Project, codebaseName: CodebaseName): CompletableFuture { - val result = CompletableFuture() + fun getRepositories( + project: Project, + codebaseNames: List + ): CompletableFuture> { + val result = CompletableFuture>() CodyAgentService.withAgent(project) { agent -> try { - val repos = agent.server.getRepoIds(GetRepoIdsParam(listOf(codebaseName.value), 1)).get() - result.complete(repos?.repos?.firstOrNull()) + val param = GetRepoIdsParam(codebaseNames.map { it.value }, codebaseNames.size) + val repos = agent.server.getRepoIds(param).get() + result.complete(repos?.repos ?: emptyList()) } catch (e: Exception) { - result.complete(null) + result.complete(emptyList()) } } return result diff --git a/src/main/kotlin/com/sourcegraph/cody/context/ui/AddRepositoryDialog.kt b/src/main/kotlin/com/sourcegraph/cody/context/ui/AddRepositoryDialog.kt index d44117519e..8613dd829b 100644 --- a/src/main/kotlin/com/sourcegraph/cody/context/ui/AddRepositoryDialog.kt +++ b/src/main/kotlin/com/sourcegraph/cody/context/ui/AddRepositoryDialog.kt @@ -12,6 +12,7 @@ import com.sourcegraph.cody.context.RemoteRepoUtils import com.sourcegraph.common.CodyBundle import com.sourcegraph.vcs.CodebaseName import com.sourcegraph.vcs.convertGitCloneURLToCodebaseNameOrError +import org.jetbrains.annotations.NotNull import java.awt.GridBagConstraints import java.awt.GridBagLayout import java.net.URL @@ -19,7 +20,6 @@ import java.util.concurrent.TimeUnit import javax.swing.JComponent import javax.swing.JLabel import javax.swing.JPanel -import org.jetbrains.annotations.NotNull class AddRepositoryDialog( private val project: Project, @@ -61,11 +61,10 @@ class AddRepositoryDialog( val codebaseName = runCatching { convertGitCloneURLToCodebaseNameOrError(text) }.getOrNull() codebaseName ?: return@custom false - val repo = - RemoteRepoUtils.getRepository(project, codebaseName) - .completeOnTimeout(null, 2, TimeUnit.SECONDS) - .get() - repo != null + !RemoteRepoUtils.getRepositories(project, listOf(codebaseName)) + .completeOnTimeout(null, 2, TimeUnit.SECONDS) + .get() + .isNullOrEmpty() } fun validateRepoNotAddedYet() = diff --git a/src/main/kotlin/com/sourcegraph/cody/context/ui/EnhancedContextPanel.kt b/src/main/kotlin/com/sourcegraph/cody/context/ui/EnhancedContextPanel.kt index b1ceec3d3c..8f5fa3c1bc 100644 --- a/src/main/kotlin/com/sourcegraph/cody/context/ui/EnhancedContextPanel.kt +++ b/src/main/kotlin/com/sourcegraph/cody/context/ui/EnhancedContextPanel.kt @@ -92,10 +92,10 @@ class EnhancedContextPanel(private val project: Project, private val chatSession } else { CodyAgentCodebase.getInstance(project).getUrl().thenApply { repoUrl -> val codebaseName = convertGitCloneURLToCodebaseNameOrError(repoUrl) - RemoteRepoUtils.getRepository(project, codebaseName) + RemoteRepoUtils.getRepositories(project, listOf(codebaseName)) .completeOnTimeout(null, 15, TimeUnit.SECONDS) - .thenApply { repo -> - if (repo != null) { + .thenApply { repos -> + if (repos?.size == 1) { ApplicationManager.getApplication().invokeLater { addRemoteRepository(codebaseName) } @@ -126,10 +126,11 @@ class EnhancedContextPanel(private val project: Project, private val chatSession private fun isDotComAccount() = CodyAuthenticationManager.instance.getActiveAccount(project)?.isDotcomAccount() ?: false - private fun getRepoByUrlAndRun(codebaseName: CodebaseName, consumer: Consumer) { - RemoteRepoUtils.getRepository(project, codebaseName).thenApply { - it?.let { repo -> consumer.accept(repo) } - } + private fun getReposByUrlAndRun( + codebaseNames: List, + consumer: Consumer> + ) { + RemoteRepoUtils.getRepositories(project, codebaseNames).thenApply { consumer.accept(it) } } private fun enableRemote(codebaseName: CodebaseName) { @@ -137,10 +138,17 @@ class EnhancedContextPanel(private val project: Project, private val chatSession contextState.remoteRepositories.find { it.codebaseName == codebaseName.value }?.isEnabled = true } - getRepoByUrlAndRun(codebaseName) { repo -> + + val enabledCodebases = + getContextState() + ?.remoteRepositories + ?.filter { it.isEnabled } + ?.mapNotNull { it.codebaseName } + ?.map { CodebaseName(it) } ?: listOf() + + getReposByUrlAndRun(enabledCodebases) { repos -> chatSession.sendWebviewMessage( - WebviewMessage( - command = "context/choose-remote-search-repo", explicitRepos = listOf(repo))) + WebviewMessage(command = "context/choose-remote-search-repo", explicitRepos = repos)) } } @@ -150,9 +158,12 @@ class EnhancedContextPanel(private val project: Project, private val chatSession contextState.remoteRepositories.find { it.codebaseName == codebaseName.value }?.isEnabled = false } - getRepoByUrlAndRun(codebaseName) { repo -> - chatSession.sendWebviewMessage( - WebviewMessage(command = "context/remove-remote-search-repo", repoId = repo.id)) + + getReposByUrlAndRun(listOf(codebaseName)) { repos -> + repos.firstOrNull()?.let { repo -> + chatSession.sendWebviewMessage( + WebviewMessage(command = "context/remove-remote-search-repo", repoId = repo.id)) + } } }