From 1dc2f6f4ea89b5badd841cb9b384f03b931fc4a0 Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Wed, 19 Jul 2023 10:34:11 +0800 Subject: [PATCH] refactor: plugin path in annotations when plugin installation (#4179) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind improvement /area core /area plugin /milestone 2.7.x #### What this PR does / why we need it: 修复生产模式下插件安装时的位置信息为绝对路径会影响迁移的问题 how to test it? 1. 生产模式下安装插件看 annotation 中 `plugin.halo.run/plugin-path` 的值是否为相对于 pluginsRoot 的相对路径 2. 在生产模式下在 main 分支启动后安装的插件切换到此 PR 后 `plugin.halo.run/plugin-path` 是否变为相对路径 #### Which issue(s) this PR fixes: Fixes #4178 #### Does this PR introduce a user-facing change? ```release-note 修复生产模式下插件安装时的位置信息为绝对路径会影响迁移的问题 ``` --- .../reconciler/PluginReconciler.java | 20 +++++++++---------- .../reconciler/PluginReconcilerTest.java | 5 +++-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/application/src/main/java/run/halo/app/core/extension/reconciler/PluginReconciler.java b/application/src/main/java/run/halo/app/core/extension/reconciler/PluginReconciler.java index e4cb26bfa..1ba7f1356 100644 --- a/application/src/main/java/run/halo/app/core/extension/reconciler/PluginReconciler.java +++ b/application/src/main/java/run/halo/app/core/extension/reconciler/PluginReconciler.java @@ -118,15 +118,13 @@ public class PluginReconciler implements Reconciler { client.fetch(Plugin.class, name).ifPresent(plugin -> { Map annotations = nullSafeAnnotations(plugin); String oldPluginPath = annotations.get(PLUGIN_PATH); - String pluginPath = oldPluginPath; - if (StringUtils.isBlank(pluginPath)) { - URI loadLocation = plugin.statusNonNull().getLoadLocation(); - pluginPath = Optional.ofNullable(loadLocation) - .map(URI::getPath) - .orElseGet(() -> PluginUtils.generateFileName(plugin)); - } - annotations.put(PLUGIN_PATH, pluginPath); - if (!StringUtils.equals(pluginPath, oldPluginPath)) { + String pluginPath = StringUtils.isBlank(oldPluginPath) + ? Optional.ofNullable(plugin.statusNonNull().getLoadLocation()) + .map(URI::getPath) + .orElseGet(() -> PluginUtils.generateFileName(plugin)) : oldPluginPath; + String pluginPathAnno = resolvePluginPathForAnno(pluginPath); + annotations.put(PLUGIN_PATH, pluginPathAnno); + if (!StringUtils.equals(pluginPathAnno, oldPluginPath)) { client.update(plugin); } }); @@ -555,7 +553,7 @@ public class PluginReconciler implements Reconciler { Map newAnnotations = nullSafeAnnotations(persisted); newAnnotations.putAll(nullSafeAnnotations(pluginInPath)); - newAnnotations.put(PLUGIN_PATH, resolvePluginPathAnnotation(newPluginPath)); + newAnnotations.put(PLUGIN_PATH, resolvePluginPathForAnno(newPluginPath)); newAnnotations.remove(RELOAD_ANNO); nullSafeLabels(persisted).putAll(nullSafeLabels(pluginInPath)); persisted.statusNonNull().setLoadLocation(toUri(newPluginPath)); @@ -567,7 +565,7 @@ public class PluginReconciler implements Reconciler { }); } - String resolvePluginPathAnnotation(String pluginPathString) { + String resolvePluginPathForAnno(String pluginPathString) { Path pluginsRoot = toPath(haloPluginManager.getPluginsRoot().toString()); Path pluginPath = toPath(pluginPathString); if (pluginPath.startsWith(pluginsRoot)) { diff --git a/application/src/test/java/run/halo/app/core/extension/reconciler/PluginReconcilerTest.java b/application/src/test/java/run/halo/app/core/extension/reconciler/PluginReconcilerTest.java index a540d1324..9d6d72094 100644 --- a/application/src/test/java/run/halo/app/core/extension/reconciler/PluginReconcilerTest.java +++ b/application/src/test/java/run/halo/app/core/extension/reconciler/PluginReconcilerTest.java @@ -79,6 +79,7 @@ class PluginReconcilerTest { @BeforeEach void setUp() { pluginReconciler = new PluginReconciler(extensionClient, haloPluginManager, eventPublisher); + lenient().when(haloPluginManager.getPluginsRoot()).thenReturn(Paths.get("plugins")); lenient().when(haloPluginManager.validatePluginVersion(any())).thenReturn(true); lenient().when(haloPluginManager.getSystemVersion()).thenReturn("0.0.0"); lenient().when(haloPluginManager.getPlugin(any())).thenReturn(pluginWrapper); @@ -378,10 +379,10 @@ class PluginReconcilerTest { @Test void resolvePluginPathAnnotation() { when(haloPluginManager.getPluginsRoot()).thenReturn(Paths.get("/tmp/plugins")); - String path = pluginReconciler.resolvePluginPathAnnotation("/tmp/plugins/sitemap-1.0.jar"); + String path = pluginReconciler.resolvePluginPathForAnno("/tmp/plugins/sitemap-1.0.jar"); assertThat(path).isEqualTo("sitemap-1.0.jar"); - path = pluginReconciler.resolvePluginPathAnnotation("/abc/plugins/sitemap-1.0.jar"); + path = pluginReconciler.resolvePluginPathForAnno("/abc/plugins/sitemap-1.0.jar"); assertThat(path).isEqualTo("/abc/plugins/sitemap-1.0.jar"); }