Skip to content

Commit

Permalink
Fix enhanced remote context when multiple repos are added (#1080)
Browse files Browse the repository at this point in the history
Fixes #1081

## Changes

Previously when adding multiple repositories last one added was
effectively overriding al the previous ones.
That was leading to lacking context and surprising behaviour depending
on the order of the repos added.

## Test plan

Manual tests according to QA testing guide but with multiple
repositories added, and enabling/disabling single repositories.
  • Loading branch information
pkukielka authored Mar 13, 2024
1 parent fe4c100 commit 1225f2e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
14 changes: 9 additions & 5 deletions src/main/kotlin/com/sourcegraph/cody/context/RemoteRepoUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import com.sourcegraph.vcs.CodebaseName
import java.util.concurrent.CompletableFuture

object RemoteRepoUtils {
fun getRepository(project: Project, codebaseName: CodebaseName): CompletableFuture<Repo?> {
val result = CompletableFuture<Repo?>()
fun getRepositories(
project: Project,
codebaseNames: List<CodebaseName>
): CompletableFuture<List<Repo>> {
val result = CompletableFuture<List<Repo>>()
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ 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
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,
Expand Down Expand Up @@ -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() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -126,21 +126,29 @@ 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<Repo>) {
RemoteRepoUtils.getRepository(project, codebaseName).thenApply {
it?.let { repo -> consumer.accept(repo) }
}
private fun getReposByUrlAndRun(
codebaseNames: List<CodebaseName>,
consumer: Consumer<List<Repo>>
) {
RemoteRepoUtils.getRepositories(project, codebaseNames).thenApply { consumer.accept(it) }
}

private fun enableRemote(codebaseName: CodebaseName) {
updateContextState { contextState ->
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))
}
}

Expand All @@ -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))
}
}
}

Expand Down

0 comments on commit 1225f2e

Please sign in to comment.