Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Sourcegraph actions (search selection, open selection, etc) #325

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/main/java/com/sourcegraph/vcs/ConvertUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.sourcegraph.vcs

import java.net.URL

fun convertGitCloneURLToCodebaseNameOrError(cloneURL: String): String {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copied from the agent and translated (TS -> Kotlin)


// Handle common Git SSH URL format
val matchResult = Regex("""^[\w-]+@([^:]+):([\w-]+)/([\w-]+)(\.git)?$""").find(cloneURL)
if (matchResult != null) {
val (host, owner, repo) = matchResult.destructured
return "$host/$owner/$repo"
}

val uri = URL(cloneURL)

// Handle Azure DevOps URLs
if (uri.host?.contains("dev.azure") == true && uri.path.isNotEmpty()) {
return "${uri.host}${uri.path.replace("/_git", "")}"
}

// Handle GitHub URLs
if (uri.protocol?.startsWith("github") == true || uri.toString().startsWith("github")) {
return "github.com/${uri.path.replace(".git", "")}"
}

// Handle GitLab URLs
if (uri.protocol?.startsWith("gitlab") == true || uri.toString().startsWith("gitlab")) {
return "gitlab.com/${uri.path.replace(".git", "")}"
}

// Handle HTTPS URLs
if (uri.protocol?.startsWith("http") == true && uri.host != null && uri.path.isNotEmpty()) {
return "${uri.host}${uri.path.replace(".git", "")}"
}

// Generic URL
if (uri.host != null && uri.path.isNotEmpty()) {
return "${uri.host}${uri.path.replace(".git", "")}"
}

return ""
}
27 changes: 19 additions & 8 deletions src/main/java/com/sourcegraph/vcs/RepoUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.sourcegraph.vcs;

import static com.sourcegraph.vcs.ConvertUtilKt.convertGitCloneURLToCodebaseNameOrError;

import com.intellij.dvcs.repo.Repository;
import com.intellij.dvcs.repo.VcsRepositoryManager;
import com.intellij.openapi.diagnostic.Logger;
Expand All @@ -11,12 +13,14 @@
import com.sourcegraph.cody.agent.protocol.CloneURL;
import com.sourcegraph.cody.config.CodyProjectSettings;
import com.sourcegraph.common.ErrorNotification;
import com.sourcegraph.config.ConfigUtil;
import git4idea.GitVcs;
import git4idea.repo.GitRepository;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.perforce.perforce.PerforceAuthenticationException;
Expand Down Expand Up @@ -147,15 +151,22 @@ private static String doReplacements(

if (vcsType == VCSType.GIT && repository != null) {
String cloneURL = GitUtil.getRemoteRepoUrl((GitRepository) repository, project);
String codebaseName =
CodyAgentService.getAgent(project)
.thenCompose(
agent ->
agent.getServer().convertGitCloneURLToCodebaseName(new CloneURL(cloneURL)))
.join();
String codebaseName = null;

if (ConfigUtil.isCodyEnabled()) {
codebaseName =
CodyAgentService.getAgent(project)
.thenCompose(
agent ->
agent.getServer().convertGitCloneURLToCodebaseName(new CloneURL(cloneURL)))
.completeOnTimeout(/* value= */ null, /* timeout= */ 4, TimeUnit.SECONDS)
.get();
}
Comment on lines +156 to +164
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to use agent only with Cody enabled. Also let's complete on timeout.


if (codebaseName == null) {
throw new Exception(
"Failed to convert git clone URL to codebase name for cloneURL: " + cloneURL);
logger.warn(
"Failed to convert git clone URL to codebase name for cloneURL via agent: " + cloneURL);
codebaseName = convertGitCloneURLToCodebaseNameOrError(cloneURL);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fallback...

}
return codebaseName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class CodyToolWindowContent(private val project: Project) : UpdatableChat {
SUBSCRIPTION_TAB_INDEX)
}
subscriptionPanel.update(data.isCurrentUserPro)
} else {
} else if (SUBSCRIPTION_TAB_INDEX < tabbedPane.tabCount) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrel to the sourcegraph actions issue - minor fix (IndexOutOfBoundsException appeared once from this place)

tabbedPane.remove(SUBSCRIPTION_TAB_INDEX)
}
}
Expand Down