[github] Declarative application/project message bus listeners

GitOrigin-RevId: 5c94532c59bd79be74235c24c52617c67fde2a3b
This commit is contained in:
Ivan Semenov
2020-04-21 14:54:24 +00:00
committed by intellij-monorepo-bot
parent 3be4b17109
commit 198d08461b
6 changed files with 61 additions and 55 deletions
@@ -56,6 +56,23 @@
</extensions>
<applicationListeners>
<listener class="org.jetbrains.plugins.github.authentication.accounts.GithubAccountManager$PasswordStorageClearedListener"
topic="com.intellij.credentialStore.PasswordSafeSettingsListener"
activeInHeadlessMode="false" activeInTestMode="false"/>
<listener class="org.jetbrains.plugins.github.api.GithubApiRequestExecutorManager"
topic="org.jetbrains.plugins.github.authentication.accounts.AccountTokenChangedListener"
activeInHeadlessMode="false" activeInTestMode="false"/>
<listener class="org.jetbrains.plugins.github.authentication.accounts.GithubAccountInformationProvider$AccountTokenListener"
topic="org.jetbrains.plugins.github.authentication.accounts.AccountTokenChangedListener"
activeInHeadlessMode="false" activeInTestMode="false"/>
<listener class="org.jetbrains.plugins.github.extensions.GithubAccountGitAuthenticationFailureManager$AccountTokenListener"
topic="org.jetbrains.plugins.github.authentication.accounts.AccountTokenChangedListener"
activeInHeadlessMode="false" activeInTestMode="false"/>
<listener class="org.jetbrains.plugins.github.pullrequest.GHPRToolWindowTabsManager$AccountsListener"
topic="org.jetbrains.plugins.github.authentication.accounts.AccountRemovedListener"
activeInHeadlessMode="false" activeInTestMode="false"/>
@@ -65,6 +82,11 @@
</applicationListeners>
<projectListeners>
<listener class="org.jetbrains.plugins.github.authentication.accounts.GithubProjectDefaultAccountHolder$RemovalListener"
topic="org.jetbrains.plugins.github.authentication.accounts.AccountRemovedListener"
activeInHeadlessMode="false" activeInTestMode="false"/>
<listener class="org.jetbrains.plugins.github.pullrequest.GHPRToolWindowTabsManager$RemoteUrlsListener"
topic="com.intellij.dvcs.repo.VcsRepositoryMappingListener"
activeInHeadlessMode="false" activeInTestMode="false"/>
@@ -1,7 +1,6 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.github.api
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import org.jetbrains.annotations.CalledInAwt
@@ -18,12 +17,6 @@ import java.awt.Component
class GithubApiRequestExecutorManager : AccountTokenChangedListener {
private val executors = mutableMapOf<GithubAccount, GithubApiRequestExecutor.WithTokenAuth>()
init {
ApplicationManager.getApplication().messageBus
.connect()
.subscribe(GithubAccountManager.ACCOUNT_TOKEN_CHANGED_TOPIC, this)
}
companion object {
@JvmStatic
fun getInstance(): GithubApiRequestExecutorManager = service()
@@ -1,8 +1,7 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.github.authentication.accounts
import com.google.common.cache.CacheBuilder
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.service
import com.intellij.openapi.progress.ProgressIndicator
import org.jetbrains.annotations.CalledInBackground
@@ -22,16 +21,6 @@ class GithubAccountInformationProvider {
.expireAfterAccess(30, TimeUnit.MINUTES)
.build<GithubAccount, GithubAuthenticatedUser>()
init {
ApplicationManager.getApplication().messageBus
.connect()
.subscribe(GithubAccountManager.ACCOUNT_TOKEN_CHANGED_TOPIC, object : AccountTokenChangedListener {
override fun tokenChanged(account: GithubAccount) {
informationCache.invalidate(account)
}
})
}
@CalledInBackground
@Throws(IOException::class)
fun getInformation(executor: GithubApiRequestExecutor, indicator: ProgressIndicator, account: GithubAccount): GithubAuthenticatedUser {
@@ -44,4 +33,10 @@ class GithubAccountInformationProvider {
return service()
}
}
class AccountTokenListener : AccountTokenChangedListener {
override fun tokenChanged(account: GithubAccount) {
getInstance().informationCache.invalidate(account)
}
}
}
@@ -1,12 +1,16 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.github.authentication.accounts
import com.intellij.credentialStore.*
import com.intellij.credentialStore.CredentialAttributes
import com.intellij.credentialStore.Credentials
import com.intellij.credentialStore.PasswordSafeSettingsListener
import com.intellij.credentialStore.generateServiceName
import com.intellij.ide.passwordSafe.PasswordSafe
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.logger
import com.intellij.util.messages.Topic
import org.jetbrains.plugins.github.api.GithubServerPath
@@ -26,18 +30,6 @@ internal class GithubAccountManager : PersistentStateComponent<Array<GithubAccou
LOG.debug("Account list changed to: $newValue")
}
init {
ApplicationManager.getApplication().messageBus.connect()
.subscribe(PasswordSafeSettings.TOPIC, object : PasswordSafeSettingsListener {
override fun credentialStoreCleared() {
val publisher = ApplicationManager.getApplication()
.messageBus
.syncPublisher(ACCOUNT_TOKEN_CHANGED_TOPIC)
accounts.forEach(publisher::tokenChanged)
}
})
}
private fun accountRemoved(account: GithubAccount) {
updateAccountToken(account, null)
ApplicationManager.getApplication()
@@ -69,13 +61,24 @@ internal class GithubAccountManager : PersistentStateComponent<Array<GithubAccou
companion object {
private val LOG = logger<GithubAccountManager>()
@JvmStatic
val ACCOUNT_REMOVED_TOPIC = Topic("GITHUB_ACCOUNT_REMOVED", AccountRemovedListener::class.java)
@JvmStatic
val ACCOUNT_TOKEN_CHANGED_TOPIC = Topic("GITHUB_ACCOUNT_TOKEN_CHANGED", AccountTokenChangedListener::class.java)
fun createAccount(name: String, server: GithubServerPath) = GithubAccount(name, server)
}
class PasswordStorageClearedListener : PasswordSafeSettingsListener {
override fun credentialStoreCleared() {
val publisher = ApplicationManager.getApplication()
.messageBus
.syncPublisher(ACCOUNT_TOKEN_CHANGED_TOPIC)
service<GithubAccountManager>().accounts.forEach(publisher::tokenChanged)
}
}
}
private fun createCredentialAttributes(accountId: String) = CredentialAttributes(createServiceName(accountId))
@@ -1,7 +1,6 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.github.authentication.accounts
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.components.*
import com.intellij.openapi.project.Project
@@ -16,14 +15,6 @@ import org.jetbrains.plugins.github.util.GithubNotifications
internal class GithubProjectDefaultAccountHolder(private val project: Project) : PersistentStateComponent<AccountState> {
var account: GithubAccount? = null
init {
ApplicationManager.getApplication().messageBus.connect(project).subscribe(GithubAccountManager.ACCOUNT_REMOVED_TOPIC, object : AccountRemovedListener {
override fun accountRemoved(removedAccount: GithubAccount) {
if (account == removedAccount) account = null
}
})
}
override fun getState(): AccountState {
return AccountState().apply { defaultAccountId = account?.id }
}
@@ -40,6 +31,13 @@ internal class GithubProjectDefaultAccountHolder(private val project: Project) :
}
return account
}
class RemovalListener(private val project: Project) : AccountRemovedListener {
override fun accountRemoved(removedAccount: GithubAccount) {
val holder = project.service<GithubProjectDefaultAccountHolder>()
if (holder.account == removedAccount) holder.account = null
}
}
}
internal class AccountState {
@@ -1,28 +1,23 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.github.extensions
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.service
import org.jetbrains.plugins.github.authentication.accounts.AccountTokenChangedListener
import org.jetbrains.plugins.github.authentication.accounts.GithubAccount
import org.jetbrains.plugins.github.authentication.accounts.GithubAccountManager
import java.util.concurrent.ConcurrentHashMap
class GithubAccountGitAuthenticationFailureManager {
private val storeMap = ConcurrentHashMap<GithubAccount, Set<String>>()
init {
ApplicationManager.getApplication().messageBus
.connect()
.subscribe(GithubAccountManager.ACCOUNT_TOKEN_CHANGED_TOPIC, object : AccountTokenChangedListener {
override fun tokenChanged(account: GithubAccount) {
storeMap.remove(account)
}
})
}
fun ignoreAccount(url: String, account: GithubAccount) {
storeMap.compute(account) { _, current -> current?.plus(url) ?: setOf(url) }
}
fun isAccountIgnored(url: String, account: GithubAccount): Boolean = storeMap[account]?.contains(url) ?: false
class AccountTokenListener : AccountTokenChangedListener {
override fun tokenChanged(account: GithubAccount) {
service<GithubAccountGitAuthenticationFailureManager>().storeMap.remove(account)
}
}
}