From da1051c78493f1aec00563c91d74bd03c52f7637 Mon Sep 17 00:00:00 2001 From: Sergei Vorobyov Date: Fri, 26 Feb 2021 18:51:47 +0300 Subject: [PATCH] IDEA-262969 added merging of untrusted project dialogs into one for several linked projects GitOrigin-RevId: 44cd546b7ef69a54314191a5d3a4db0cc6caa07f --- .../action/AttachExternalProjectAction.java | 2 +- .../messages/ExternalSystemBundle.properties | 8 ++ .../project/ExternalResolverIsSafe.java | 15 +-- .../RefreshAllExternalProjectsAction.java | 3 +- .../action/RefreshExternalProjectAction.java | 2 +- .../autoimport/AutoImportProjectTracker.kt | 6 +- .../autolink/UnlinkedProjectUtils.kt | 2 +- .../importing/AbstractOpenProjectProvider.kt | 22 ++++- .../UntrustedProjectNotificationProvider.kt | 23 +++-- .../util/ExternalSystemUtil.java | 93 +++++++++---------- .../com/intellij/ide/impl/TrustedProjects.kt | 23 +++-- .../action/ImportProjectFromScriptAction.kt | 2 +- .../jetbrains/idea/maven/utils/MavenUtil.java | 2 +- 13 files changed, 119 insertions(+), 84 deletions(-) diff --git a/java/idea-ui/src/com/intellij/openapi/externalSystem/action/AttachExternalProjectAction.java b/java/idea-ui/src/com/intellij/openapi/externalSystem/action/AttachExternalProjectAction.java index d4a379da53f1..f5ed68e866b0 100644 --- a/java/idea-ui/src/com/intellij/openapi/externalSystem/action/AttachExternalProjectAction.java +++ b/java/idea-ui/src/com/intellij/openapi/externalSystem/action/AttachExternalProjectAction.java @@ -83,7 +83,7 @@ public class AttachExternalProjectAction extends DumbAwareAction { projectImportProvider ); if (wizard != null && (wizard.getStepCount() <= 0 || wizard.showAndGet())) { - ExternalSystemUtil.confirmLoadingUntrustedProjectIfNeeded(project, externalSystemId); + ExternalSystemUtil.confirmFullLoadingUntrustedProjectIfNeeded(project, externalSystemId); ImportModuleAction.createFromWizard(project, wizard); } } diff --git a/platform/external-system-api/resources/messages/ExternalSystemBundle.properties b/platform/external-system-api/resources/messages/ExternalSystemBundle.properties index 54030f849820..1166b2f994b2 100644 --- a/platform/external-system-api/resources/messages/ExternalSystemBundle.properties +++ b/platform/external-system-api/resources/messages/ExternalSystemBundle.properties @@ -15,6 +15,14 @@ unlinked.project.notification.help.text=\ If you are unsure or this is not a {0} project, press 'Skip'.
\ You''ll be able to import the {0} project later from context menu of {0} build script. +untrusted.project.notification.trust.button=Load {0} {1,choice,1#Project|1 extension.systemId.equals(systemId.getId())) - .findFirst() - .map(extension -> extension.executesTrustedCodeOnly) - .orElse(false); + public static boolean executesTrustedCodeOnly(ProjectSystemId... systemIds) { + Map executesTrustedCodeOnlyIndex = + ContainerUtil.map2Map(EP_NAME.getExtensionList(), it -> new Pair<>(it.systemId, it.executesTrustedCodeOnly)); + return ContainerUtil.and(systemIds, it -> executesTrustedCodeOnlyIndex.getOrDefault(it.getId(), false)); } } diff --git a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/action/RefreshAllExternalProjectsAction.java b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/action/RefreshAllExternalProjectsAction.java index 72ccdeb60ee8..f7b0a4169967 100644 --- a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/action/RefreshAllExternalProjectsAction.java +++ b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/action/RefreshAllExternalProjectsAction.java @@ -73,10 +73,11 @@ public class RefreshAllExternalProjectsAction extends DumbAwareAction { // We save all documents because there is a possible case that there is an external system config file changed inside the ide. FileDocumentManager.getInstance().saveAllDocuments(); + boolean isPreviewMode = ExternalSystemUtil.confirmFullLoadingUntrustedProjectIfNeeded(project, systemIds.toArray(new ProjectSystemId[0])); for (ProjectSystemId externalSystemId : systemIds) { ExternalSystemActionsCollector.trigger(project, externalSystemId, this, e); ImportSpecBuilder importSpec = new ImportSpecBuilder(project, externalSystemId); - if (!ExternalSystemUtil.confirmLoadingUntrustedProjectIfNeeded(project, externalSystemId)) { + if (!isPreviewMode) { importSpec.usePreviewMode(); } ExternalSystemUtil.refreshProjects(importSpec.forceWhenUptodate(true)); diff --git a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/action/RefreshExternalProjectAction.java b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/action/RefreshExternalProjectAction.java index 41f0da60d870..f88214052c39 100644 --- a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/action/RefreshExternalProjectAction.java +++ b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/action/RefreshExternalProjectAction.java @@ -82,7 +82,7 @@ public class RefreshExternalProjectAction extends ExternalSystemNodeAction ThreeState.UNSURE.equals(getTrustedState(project)), systemId); + } + + public static boolean confirmFullLoadingUntrustedProjectIfNeeded( + @NotNull Project project, + ProjectSystemId... systemIds + ) { + return confirmFullLoadingUntrustedProjectIfNeeded(project, () -> true, systemIds); } public static boolean confirmLoadingUntrustedProjectIfNeeded( @NotNull Project project, - @NotNull ProjectSystemId systemId + ProjectSystemId... systemIds ) { - return confirmLoadingUntrustedProjectIfNeeded(project, systemId, __ -> true); + return confirmLoadingUntrustedProjectIfNeeded(project, () -> true, systemIds); + } + + public static boolean confirmFullLoadingUntrustedProjectIfNeeded( + @NotNull Project project, + @NotNull Supplier confirmation, + ProjectSystemId... systemIds + ) { + String systemsPresentation = StringUtil.join(systemIds, it -> it.getReadableName(), ", "); + return isTrusted(project) || project.isDefault() || executesTrustedCodeOnly(systemIds) || + confirmation.get() && confirmImportingUntrustedProject(project, () -> + MessageDialogBuilder.yesNo( + ExternalSystemBundle.message("untrusted.project.notification.title", systemsPresentation, systemIds.length), + ExternalSystemBundle.message("untrusted.project.notification.text", systemsPresentation, systemIds.length) + ) + .yesText(ExternalSystemBundle.message("untrusted.project.notification.trust.button", systemsPresentation, systemIds.length)) + .noText(ExternalSystemBundle.message("untrusted.project.notification.open.preview.action")) + .asWarning() + .ask(project) + ); } public static boolean confirmLoadingUntrustedProjectIfNeeded( @NotNull Project project, - @NotNull ProjectSystemId systemId, - @NotNull @Button String cancelButtonText + @NotNull Supplier confirmation, + ProjectSystemId... systemIds ) { - return confirmLoadingUntrustedProjectIfNeeded(project, systemId, cancelButtonText, __ -> true); - } - - public static boolean confirmLoadingUntrustedProjectIfNeeded( - @NotNull Project project, - @NotNull ProjectSystemId systemId, - @NotNull Predicate confirmation - ) { - String cancelButtonText = ExternalSystemBundle.message("unlinked.project.notification.open.preview.action"); - return confirmLoadingUntrustedProjectIfNeeded(project, systemId, cancelButtonText, confirmation); - } - - public static boolean confirmLoadingUntrustedProjectIfNeeded( - @NotNull Project project, - @NotNull ProjectSystemId systemId, - @NotNull @Button String cancelButtonText, - @NotNull Predicate confirmation - ) { - if (project.isDefault()) { - return true; - } - if (ExternalResolverIsSafe.executesTrustedCodeOnly(systemId)) { - return true; - } - ThreeState state = getTrustedState(project); - if (state.equals(ThreeState.YES)) { - return true; - } - if (!confirmation.test(state)) { - return false; - } - String systemName = systemId.getReadableName(); - return confirmImportingUntrustedProject( - project, systemName, - ExternalSystemBundle.message("unlinked.project.notification.load.action", systemName), - cancelButtonText - ); + String systemsPresentation = StringUtil.join(systemIds, it -> it.getReadableName(), ", "); + return isTrusted(project) || project.isDefault() || executesTrustedCodeOnly(systemIds) || + confirmation.get() && confirmImportingUntrustedProject(project, () -> + MessageDialogBuilder.yesNo( + ExternalSystemBundle.message("untrusted.project.notification.title", systemsPresentation, systemIds.length), + ExternalSystemBundle.message("untrusted.project.notification.text", systemsPresentation, systemIds.length) + ) + .yesText(ExternalSystemBundle.message("untrusted.project.notification.trust.button", systemsPresentation, systemIds.length)) + .noText(CommonBundle.getCancelButtonText()) + .asWarning() + .ask(project) + ); } public static boolean isNewProject(Project project) { diff --git a/platform/platform-impl/src/com/intellij/ide/impl/TrustedProjects.kt b/platform/platform-impl/src/com/intellij/ide/impl/TrustedProjects.kt index e91c73d995fb..a17df4eed186 100644 --- a/platform/platform-impl/src/com/intellij/ide/impl/TrustedProjects.kt +++ b/platform/platform-impl/src/com/intellij/ide/impl/TrustedProjects.kt @@ -54,19 +54,24 @@ fun confirmImportingUntrustedProject(project: Project, @Nls buildSystemName: String, @Nls yesButtonText: String, @Nls noButtonText: String): Boolean { + return confirmImportingUntrustedProject(project) { + MessageDialogBuilder.yesNo(title = IdeBundle.message("untrusted.project.import.warning.title", buildSystemName), + message = IdeBundle.message("untrusted.project.import.warning.text", buildSystemName)) + .yesText(yesButtonText) + .noText(noButtonText) + .asWarning() + .ask(project) + } +} + +fun confirmImportingUntrustedProject(project: Project, confirm: () -> Boolean): Boolean { val trustedCheckResult = isProjectImplicitlyTrusted(project) if (trustedCheckResult is Trusted) { project.setTrusted(true) return true } - val answer = MessageDialogBuilder.yesNo(title = IdeBundle.message("untrusted.project.import.warning.title", buildSystemName), - message = IdeBundle.message("untrusted.project.import.warning.text", buildSystemName)) - .yesText(yesButtonText) - .noText(noButtonText) - .asWarning() - .ask(project) - + val answer = confirm() project.setTrusted(answer) return answer } @@ -167,11 +172,11 @@ class TrustedProjectSettings : SimplePersistentStateComponent(CommonDataKeys.VIRTUAL_FILE) ?: return val project = e.getData(CommonDataKeys.PROJECT) ?: return val externalProjectPath = getDefaultPath(virtualFile) - ExternalSystemUtil.confirmLoadingUntrustedProjectIfNeeded(project, GradleConstants.SYSTEM_ID) + ExternalSystemUtil.confirmFullLoadingUntrustedProjectIfNeeded(project, GradleConstants.SYSTEM_ID) linkAndRefreshGradleProject(externalProjectPath, project) } diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenUtil.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenUtil.java index 6b3a3432af06..1a2454ad4335 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenUtil.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenUtil.java @@ -1046,7 +1046,7 @@ public class MavenUtil { } public static boolean isProjectTrustedEnoughToImport(Project project, boolean askConfirmation) { - return ExternalSystemUtil.confirmLoadingUntrustedProjectIfNeeded(project, SYSTEM_ID, __ -> askConfirmation); + return ExternalSystemUtil.confirmFullLoadingUntrustedProjectIfNeeded(project, () -> askConfirmation, SYSTEM_ID); } public static void restartMavenConnectors(Project project) {