[gitlab] remember repo and account selection in MR toolwindow

GitOrigin-RevId: ca5f6f3eb0a87450dd8787f3f89f2e6c453aba69
This commit is contained in:
Ivan Semenov
2023-06-19 13:23:22 +02:00
committed by intellij-monorepo-bot
parent 769c5d84d4
commit f87f1c86e7
3 changed files with 51 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.plugins.gitlab.mergerequest
import com.intellij.openapi.components.*
import com.intellij.openapi.project.Project
import git4idea.remote.hosting.knownRepositories
import kotlinx.serialization.Serializable
import org.jetbrains.plugins.gitlab.GitLabProjectsManager
import org.jetbrains.plugins.gitlab.authentication.accounts.GitLabAccount
import org.jetbrains.plugins.gitlab.authentication.accounts.GitLabAccountManager
import org.jetbrains.plugins.gitlab.util.GitLabProjectMapping
@Service(Service.Level.PROJECT)
@State(name = "GitLabMergeRequestsSettings", storages = [Storage(StoragePathMacros.WORKSPACE_FILE)], reportStatistic = false)
class GitLabMergeRequestsPreferences(private val project: Project)
: SerializablePersistentStateComponent<GitLabMergeRequestsPreferences.SettingsState>(SettingsState()) {
@Serializable
data class SettingsState(val selectedUrlAndAccountId: Pair<String, String>? = null)
var selectedRepoAndAccount: Pair<GitLabProjectMapping, GitLabAccount>?
get() {
val (url, accountId) = state.selectedUrlAndAccountId ?: return null
val repo = project.service<GitLabProjectsManager>().knownRepositories.find {
it.remote.url == url
} ?: return null
val account = service<GitLabAccountManager>().accountsState.value.find {
it.id == accountId
} ?: return null
return repo to account
}
set(value) {
updateState {
val saved = value?.let { (repo, account) -> repo.remote.url to account.id }
SettingsState(saved)
}
}
}

View File

@@ -19,6 +19,7 @@ import org.jetbrains.plugins.gitlab.api.GitLabProjectConnectionManager
import org.jetbrains.plugins.gitlab.authentication.accounts.GitLabAccount
import org.jetbrains.plugins.gitlab.authentication.accounts.GitLabAccountManager
import org.jetbrains.plugins.gitlab.createSingleProjectAndAccountState
import org.jetbrains.plugins.gitlab.mergerequest.GitLabMergeRequestsPreferences
import org.jetbrains.plugins.gitlab.mergerequest.ui.GitLabProjectUIContext.Companion.GitLabProjectUIContext
import org.jetbrains.plugins.gitlab.util.GitLabProjectMapping
@@ -49,6 +50,7 @@ internal class GitLabProjectUIContextHolder(
fun switchProject() {
cs.launch {
project.service<GitLabMergeRequestsPreferences>().selectedRepoAndAccount = null
connectionManager.closeConnection()
}
}

View File

@@ -23,6 +23,7 @@ import org.jetbrains.plugins.gitlab.authentication.GitLabLoginUtil
import org.jetbrains.plugins.gitlab.authentication.accounts.GitLabAccountManager
import org.jetbrains.plugins.gitlab.authentication.accounts.GitLabAccountViewModelImpl
import org.jetbrains.plugins.gitlab.authentication.ui.GitLabAccountsDetailsProvider
import org.jetbrains.plugins.gitlab.mergerequest.GitLabMergeRequestsPreferences
import org.jetbrains.plugins.gitlab.mergerequest.action.GitLabMergeRequestsActionKeys
import org.jetbrains.plugins.gitlab.mergerequest.data.GitLabMergeRequestId
import org.jetbrains.plugins.gitlab.mergerequest.diff.ChangesSelection
@@ -140,16 +141,26 @@ internal class GitLabReviewTabComponentFactory(
}
private fun createSelectorsComponent(cs: CoroutineScope): JComponent {
val preferences = project.service<GitLabMergeRequestsPreferences>()
// TODO: move vm creation to another place
val selectorVm = GitLabRepositoryAndAccountSelectorViewModel(
cs, toolwindowViewModel.projectsManager, toolwindowViewModel.accountManager,
onSelected = { mapping, account ->
withContext(cs.coroutineContext) {
toolwindowViewModel.connectionManager.openConnection(mapping, account)
preferences.selectedRepoAndAccount = mapping to account
}
}
)
preferences.selectedRepoAndAccount?.let { (repo, account) ->
with(selectorVm) {
repoSelectionState.value = repo
accountSelectionState.value = account
submitSelection()
}
}
val accountsDetailsProvider = GitLabAccountsDetailsProvider(cs) {
// TODO: separate loader
service<GitLabAccountManager>().findCredentials(it)?.let(service<GitLabApiManager>()::getClient)