-
Notifications
You must be signed in to change notification settings - Fork 31
fix(uninstall): Only clear credentials if the Cody app was uninstalled #2485
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
Open
jamesmcnamara
wants to merge
5
commits into
main
Choose a base branch
from
jsm/cody-1043-uninstall-redux
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
da5e95a
only clear credentials if the cody app was uninstalled
jamesmcnamara 7984cb2
added unit test
jamesmcnamara 1c05f87
added unit test
jamesmcnamara 8ee6c71
Merge branch 'main' of https://github.com/sourcegraph/jetbrains into …
jamesmcnamara 994b4de
unreverted changes
jamesmcnamara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/kotlin/com/sourcegraph/cody/initialization/UninstallListener.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package com.sourcegraph.cody.initialization | ||
|
|
||
| import com.intellij.ide.plugins.IdeaPluginDescriptor | ||
| import com.intellij.ide.plugins.PluginInstaller | ||
| import com.intellij.ide.plugins.PluginStateListener | ||
| import com.intellij.openapi.project.Project | ||
| import com.intellij.openapi.startup.StartupActivity | ||
| import com.sourcegraph.cody.agent.CodyAgentService | ||
| import com.sourcegraph.cody.agent.protocol.BillingCategory | ||
| import com.sourcegraph.cody.agent.protocol.BillingMetadata | ||
| import com.sourcegraph.cody.agent.protocol.BillingProduct | ||
| import com.sourcegraph.cody.agent.protocol.TelemetryEventParameters | ||
| import com.sourcegraph.cody.config.CodyAuthenticationManager | ||
| import com.sourcegraph.cody.telemetry.TelemetryV2 | ||
| import com.sourcegraph.config.ConfigUtil | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| class UninstallListener : StartupActivity { | ||
| override fun runActivity(project: Project) { | ||
| PluginInstaller.addStateListener( | ||
| object : PluginStateListener { | ||
| override fun uninstall(descriptor: IdeaPluginDescriptor) { | ||
| // Only run for this plugin | ||
| if (descriptor.pluginId != ConfigUtil.getPluginId()) { | ||
| return | ||
| } | ||
| val authManager = CodyAuthenticationManager.getInstance() | ||
| authManager.setActiveAccount(null) | ||
| authManager.removeAll() | ||
| TelemetryV2.sendTelemetryEvent( | ||
| project, | ||
| "cody.extension", | ||
| "uninstalled", | ||
| TelemetryEventParameters( | ||
| billingMetadata = | ||
| BillingMetadata(BillingProduct.CODY, BillingCategory.BILLABLE))) | ||
| CodyAgentService.withAgent(project) { | ||
| it.server.extension_reset(null).get(20, TimeUnit.SECONDS) | ||
| } | ||
| } | ||
|
|
||
| override fun install(descriptor: IdeaPluginDescriptor) {} | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/test/kotlin/com/sourcegraph/cody/initialization/UninstallListenerTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package com.sourcegraph.cody.initialization | ||
|
|
||
| import com.intellij.ide.plugins.* | ||
| import com.intellij.openapi.extensions.PluginId | ||
| import com.intellij.testFramework.fixtures.BasePlatformTestCase | ||
| import com.sourcegraph.cody.config.CodyAuthenticationManager | ||
| import com.sourcegraph.cody.telemetry.TelemetryV2 | ||
| import com.sourcegraph.config.ConfigUtil | ||
| import io.mockk.* | ||
|
|
||
| @Suppress("UnstableApiUsage") | ||
| class UninstallListenerTest : BasePlatformTestCase() { | ||
|
|
||
| private val uninstallListener = UninstallListener() | ||
|
|
||
| // Mock dependencies | ||
| // relaxed = true to allow unit returning methods to auto-mock | ||
| private val authManager = mockk<CodyAuthenticationManager>(relaxed = true) | ||
|
|
||
| override fun setUp() { | ||
| super.setUp() | ||
|
|
||
| // setup mock objects | ||
| mockkObject(CodyAuthenticationManager) | ||
| every { CodyAuthenticationManager.getInstance() } returns authManager | ||
| mockkObject(TelemetryV2) | ||
| every { TelemetryV2.sendTelemetryEvent(any(), any(), any()) } returns Unit | ||
| } | ||
|
|
||
| private fun getPlugin() = | ||
| PluginManagerCore.findPlugin(ConfigUtil.getPluginId()) ?: throw Exception("Plugin not found") | ||
|
|
||
| fun `test plugin uninstall cleans up resources`() { | ||
| // Execute uninstall | ||
| uninstallListener.runActivity(project) | ||
| val plugin = getPlugin() | ||
| PluginInstaller.prepareToUninstall(plugin) | ||
| verify { | ||
| authManager.setActiveAccount(null) | ||
| authManager.removeAll() | ||
| TelemetryV2.sendTelemetryEvent(any(), "cody.extension", "uninstalled", any()) | ||
| } | ||
| } | ||
|
|
||
| fun `test plugin uninstall does nothing for unrelated plugins`() { | ||
| // Execute uninstall | ||
| val plugin = getPlugin() | ||
| uninstallListener.runActivity(project) | ||
|
|
||
| // Now mock out config util so that it returns a different plugin id | ||
| // so that the UninstallListener thinks it's a different plugin | ||
| mockkStatic(ConfigUtil::getPluginId) | ||
| every { ConfigUtil.getPluginId() } returns PluginId.getId("com.sourcegraph.cody.test") | ||
|
|
||
| PluginInstaller.prepareToUninstall(plugin) | ||
| // Remove the static method mock so that it doesn't interfere with other tests | ||
| unmockkStatic(ConfigUtil::getPluginId) | ||
|
|
||
| // Verify that the uninstall listener didn't do anything | ||
| verify(exactly = 0) { | ||
| authManager.setActiveAccount(null) | ||
| authManager.removeAll() | ||
| TelemetryV2.sendTelemetryEvent(any(), "cody.extension", "uninstalled", any()) | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed it looks like it is currently broken: