Skip to content

Commit

Permalink
Change CodyAuthenticationManager from project to application level se…
Browse files Browse the repository at this point in the history
…rvice (#2190)

## Changes

After discussion with @danielmarquespt we decided account settings
should be global, shared across all IntelliJ projects.

To reflect that fact I'm changing `CodyAuthenticationManager` to be APP
level service instead of project level.
Notifications about account state change are pushed to all open
projects.
All projects use shared storage, so changes done when project is closed
are also properly loaded upon opening that project.

All relevant changes are in the `CodyAuthenticationManager`, all the
rest is removal of unneeded `project` parameter.

## Test plan

**Scenario 1**
1. Sign-in with any account
2. Restart the IntelliJ
3. Make sure you are still logged in

**Scenario 2**
1. Sign-in with any account, add a few more
2. Restart the IntelliJ
3. Make sure you are still logged in, with the same account

**Scenario 3**
1. Sign-in with any account, add a few more
2. Switch account
3. Restart the IntelliJ
4. Make sure you are still logged in, with the same account you switched
to

**Scenario 4**
1. Sign-in with any account, add a few more
2. Sign-out
3. Restart the IntelliJ
4. Make sure you are not logged in

**Scenario 5**
1. Sign-in with any account, add a few more
2. Switch account
3. Open a second IntelliJ project
4. Make sure in the second IntelliJ window you are logged in, with the
same account you switched to

**Scenario 6**
1. Open two IntelliJ projects side by side
2. Open `Account` panel in both of them
3. Make sure that switching account in one of them causes switch to the
same account on the second

**Scenario 7**
1. Open two IntelliJ projects side by side
2. Open `Account` panel in both of them and make sure they have active
the same account
3. Close one of the instances
4. Switch account in the remaining instance
5. Close the second instance and open back the first one
6. Make sure account is properly switched
  • Loading branch information
pkukielka authored Sep 2, 2024
1 parent af90926 commit e8cbbc8
Show file tree
Hide file tree
Showing 35 changed files with 145 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class CodyAuthNotificationActivity implements Activity {

@Override
public void runActivity(@NotNull Project project) {
CodyAccount activeAccount = CodyAuthenticationManager.getInstance(project).getAccount();
CodyAccount activeAccount = CodyAuthenticationManager.getInstance().getAccount();
CodyAccountManager service =
ApplicationManager.getApplication().getService(CodyAccountManager.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public JBCefJSQuery.Response handle(@NotNull JsonObject request) {
try {
switch (action) {
case "getConfig":
return createSuccessResponse(ConfigUtil.getConfigAsJson(project));
return createSuccessResponse(ConfigUtil.getConfigAsJson());
case "getTheme":
JsonObject currentThemeAsJson = ThemeUtil.getCurrentThemeAsJson();
return createSuccessResponse(currentThemeAsJson);
Expand Down
22 changes: 7 additions & 15 deletions src/main/java/com/sourcegraph/telemetry/GraphQlLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public static CompletableFuture<Boolean> logInstallEvent(@NotNull Project projec
CodyApplicationSettings codyApplicationSettings = CodyApplicationSettings.getInstance();
if (codyApplicationSettings.getAnonymousUserId() != null && !project.isDisposed()) {
var event =
createEvent(
project, ConfigUtil.getServerPath(project), "CodyInstalled", new JsonObject());
createEvent(project, ConfigUtil.getServerPath(), "CodyInstalled", new JsonObject());
return logEvent(project, event);
}
return CompletableFuture.completedFuture(false);
Expand All @@ -27,8 +26,7 @@ public static void logUninstallEvent(@NotNull Project project) {
CodyApplicationSettings codyApplicationSettings = CodyApplicationSettings.getInstance();
if (codyApplicationSettings.getAnonymousUserId() != null) {
Event event =
createEvent(
project, ConfigUtil.getServerPath(project), "CodyUninstalled", new JsonObject());
createEvent(project, ConfigUtil.getServerPath(), "CodyUninstalled", new JsonObject());
logEvent(project, event);
}
}
Expand All @@ -37,8 +35,7 @@ public static void logCodyEvent(
@NotNull Project project, @NotNull String componentName, @NotNull String action) {
var eventName = "CodyJetBrainsPlugin:" + componentName + ":" + action;
logEvent(
project,
createEvent(project, ConfigUtil.getServerPath(project), eventName, new JsonObject()));
project, createEvent(project, ConfigUtil.getServerPath(), eventName, new JsonObject()));
}

public static void logCodeGenerationEvent(
Expand All @@ -54,9 +51,7 @@ public static void logCodeGenerationEvent(
eventParameters.addProperty("source", "chat");

var eventName = "CodyJetBrainsPlugin:" + componentName + ":" + action;
logEvent(
project,
createEvent(project, ConfigUtil.getServerPath(project), eventName, eventParameters));
logEvent(project, createEvent(project, ConfigUtil.getServerPath(), eventName, eventParameters));
}

@NotNull
Expand All @@ -65,8 +60,7 @@ private static Event createEvent(
@NotNull SourcegraphServerPath sourcegraphServerPath,
@NotNull String eventName,
@NotNull JsonObject eventParameters) {
var updatedEventParameters =
addGlobalEventParameters(project, eventParameters, sourcegraphServerPath);
var updatedEventParameters = addGlobalEventParameters(eventParameters, sourcegraphServerPath);
CodyApplicationSettings codyApplicationSettings = CodyApplicationSettings.getInstance();
String anonymousUserId = codyApplicationSettings.getAnonymousUserId();
return new Event(
Expand All @@ -75,13 +69,11 @@ private static Event createEvent(

@NotNull
private static JsonObject addGlobalEventParameters(
@NotNull Project project,
@NotNull JsonObject eventParameters,
@NotNull SourcegraphServerPath sourcegraphServerPath) {
@NotNull JsonObject eventParameters, @NotNull SourcegraphServerPath sourcegraphServerPath) {
// project specific properties
var updatedEventParameters = eventParameters.deepCopy();
var activeAccountTier =
CodyAuthenticationManager.getInstance(project).getActiveAccountTier().getNow(null);
CodyAuthenticationManager.getInstance().getActiveAccountTier().getNow(null);
if (activeAccountTier != null) {
updatedEventParameters.addProperty("tier", activeAccountTier.getValue());
}
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/sourcegraph/website/FileActionBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public void actionPerformed(@NotNull AnActionEvent event) {
handleFileUri(
project,
URLBuilder.buildSourcegraphBlobUrl(
project,
sourcegraphFile.getRepoUrl(),
sourcegraphFile.getCommit(),
sourcegraphFile.getRelativePath(),
Expand All @@ -70,7 +69,6 @@ public void actionPerformed(@NotNull AnActionEvent event) {
// need, so we'll go to the final URL directly.
url =
URLBuilder.buildSourcegraphBlobUrl(
project,
repoInfo.getCodeHostUrl() + "/" + repoInfo.getRepoName(),
null,
repoInfo.relativePath,
Expand Down Expand Up @@ -106,7 +104,6 @@ public void actionPerformedFromPreviewContent(
handleFileUri(
project,
URLBuilder.buildSourcegraphBlobUrl(
project,
previewContent.getRepoUrl(),
previewContent.getCommit(),
previewContent.getPath(),
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/sourcegraph/website/OpenRevisionAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ public void actionPerformed(@NotNull AnActionEvent event) {
try {
url =
URLBuilder.buildCommitUrl(
ConfigUtil.getServerPath(project).getUrl(),
ConfigUtil.getServerPath().getUrl(),
context.getRevisionNumber(),
remoteUrl,
productName,
productVersion);
} catch (IllegalArgumentException e) {
logger.warn(
"Unable to build commit view URI for url "
+ ConfigUtil.getServerPath(project).getUrl()
+ ConfigUtil.getServerPath().getUrl()
+ ", revision "
+ context.getRevisionNumber()
+ ", product "
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/sourcegraph/website/SearchActionBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void actionPerformedMode(@NotNull AnActionEvent event, @NotNull Scope sco
String url;
SourcegraphVirtualFile sourcegraphFile = (SourcegraphVirtualFile) currentFile;
String repoUrl = (scope == Scope.REPOSITORY) ? sourcegraphFile.getRepoUrl() : null;
url = URLBuilder.buildEditorSearchUrl(project, selectedText, repoUrl, null);
url = URLBuilder.buildEditorSearchUrl(selectedText, repoUrl, null);
BrowserOpener.INSTANCE.openInBrowser(project, url);
} else {
// This cannot run on EDT (Event Dispatch Thread) because it may block for a long time.
Expand All @@ -62,9 +62,7 @@ public void actionPerformedMode(@NotNull AnActionEvent event, @NotNull Scope sco
url =
URLBuilder.buildDirectSearchUrl(project, selectedText, codeHostUrl, repoName);
} else {
url =
URLBuilder.buildEditorSearchUrl(
project, selectedText, remoteUrl, remoteBranchName);
url = URLBuilder.buildEditorSearchUrl(selectedText, remoteUrl, remoteBranchName);
}
BrowserOpener.INSTANCE.openInBrowser(project, url);
});
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/com/sourcegraph/website/URLBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static String buildEditorFileUrl(
@NotNull String relativePath,
@Nullable LogicalPosition start,
@Nullable LogicalPosition end) {
return ConfigUtil.getServerPath(project).getUrl()
return ConfigUtil.getServerPath().getUrl()
+ "-/editor"
+ "?remote_url="
+ URLEncoder.encode(remoteUrl, StandardCharsets.UTF_8)
Expand All @@ -45,12 +45,9 @@ public static String buildEditorFileUrl(

@NotNull
public static String buildEditorSearchUrl(
@NotNull Project project,
@NotNull String search,
@Nullable String remoteUrl,
@Nullable String remoteBranchName) {
@NotNull String search, @Nullable String remoteUrl, @Nullable String remoteBranchName) {
String url =
ConfigUtil.getServerPath(project).getUrl()
ConfigUtil.getServerPath().getUrl()
+ "-/editor"
+ "?"
+ buildVersionParams()
Expand All @@ -77,7 +74,7 @@ public static String buildDirectSearchUrl(
(codeHost != null && repoName != null)
? "repo:^" + RegexEscaper.INSTANCE.escapeRegexChars(codeHost + "/" + repoName) + "$"
: null;
return ConfigUtil.getServerPath(project).getUrl()
return ConfigUtil.getServerPath().getUrl()
+ "/search"
+ "?patternType=literal"
+ "&q="
Expand Down Expand Up @@ -124,13 +121,12 @@ public static String buildCommitUrl(
@NotNull
// repoUrl should be like "github.com/sourcegraph/sourcegraph"
public static String buildSourcegraphBlobUrl(
@NotNull Project project,
@NotNull String repoUrl,
@Nullable String commit,
@NotNull String path,
@Nullable LogicalPosition start,
@Nullable LogicalPosition end) {
return ConfigUtil.getServerPath(project).getUrl()
return ConfigUtil.getServerPath().getUrl()
+ repoUrl
+ (commit != null ? "@" + commit : "")
+ "/-/blob/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CodyToolWindowContent(private val project: Project) {

@RequiresEdt
fun refreshPanelsVisibility() {
val codyAuthenticationManager = CodyAuthenticationManager.getInstance(project)
val codyAuthenticationManager = CodyAuthenticationManager.getInstance()
if (codyAuthenticationManager.hasNoActiveAccount() ||
codyAuthenticationManager.showInvalidAccessTokenError()) {
allContentLayout.show(allContentPanel, SIGN_IN_PANEL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SignInWithEnterpriseInstanceAction(
override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return
val accountsHost = CodyPersistentAccountsHost(project)
val authManager = CodyAuthenticationManager.getInstance(project)
val authManager = CodyAuthenticationManager.getInstance()
val serverUrl = authManager.account?.server?.url ?: defaultServer
val dialog =
SourcegraphInstanceLoginDialog(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ class CodyAutocompleteManager {

val textDocument: TextDocument = IntelliJTextDocument(editor, project)

if (isTriggeredExplicitly &&
CodyAuthenticationManager.getInstance(project).hasNoActiveAccount()) {
if (isTriggeredExplicitly && CodyAuthenticationManager.getInstance().hasNoActiveAccount()) {
HintManager.getInstance().showErrorHint(editor, "Cody: Sign in to use autocomplete")
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class NewChatAction : DumbAwareEDTAction() {
}

override fun update(event: AnActionEvent) {
val project = event.project ?: return
val hasActiveAccount = CodyAuthenticationManager.getInstance(project).hasActiveAccount()
val hasActiveAccount = CodyAuthenticationManager.getInstance().hasActiveAccount()
event.presentation.isEnabled = hasActiveAccount
if (!event.presentation.isEnabled) {
event.presentation.description =
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class LlmDropdown(
} ?: models.firstOrNull()

val isEnterpriseAccount =
CodyAuthenticationManager.getInstance(project).account?.isEnterpriseAccount() ?: false
CodyAuthenticationManager.getInstance().account?.isEnterpriseAccount() ?: false

// If the dropdown is already disabled, don't change it. It can happen
// in the case of the legacy commands (updateAfterFirstMessage happens before this call).
Expand Down Expand Up @@ -119,7 +119,7 @@ class LlmDropdown(
}

fun isCurrentUserFree(): Boolean =
CodyAuthenticationManager.getInstance(project)
CodyAuthenticationManager.getInstance()
.getActiveAccountTier()
.getNow(AccountTier.DOTCOM_FREE) == AccountTier.DOTCOM_FREE
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ class CodyAccountListModel(private val project: Project) :
override fun editAccount(parentComponent: JComponent, account: CodyAccount) {
val token = newCredentials[account] ?: getOldToken(account)
val authData =
CodyAuthenticationManager.getInstance(project)
CodyAuthenticationManager.getInstance()
.login(
project,
parentComponent,
CodyLoginRequest(
title = "Edit Sourcegraph Account",
Expand All @@ -53,7 +54,7 @@ class CodyAccountListModel(private val project: Project) :
}

private fun getOldToken(account: CodyAccount) =
CodyAuthenticationManager.getInstance(project).getTokenForAccount(account)
CodyAuthenticationManager.getInstance().getTokenForAccount(account)

override fun addAccount(
server: SourcegraphServerPath,
Expand Down
Loading

0 comments on commit e8cbbc8

Please sign in to comment.