From 2cf3a3af588113cf1f9b613a3078db6937ceac46 Mon Sep 17 00:00:00 2001 From: sridamul Date: Mon, 15 Jul 2024 18:01:42 +0530 Subject: [PATCH 1/6] Add new option to parse plugins.txt file --- .../tools/pluginmodernizer/cli/Main.java | 12 +++- .../core/config/Settings.java | 2 +- .../core/utils/PluginListParser.java | 26 +++++++ .../core/utils/PluginListParserTest.java | 67 +++++++++++++++++++ .../src/test/resources/plugins.txt | 3 + 5 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParser.java create mode 100644 plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParserTest.java create mode 100644 plugin-modernizer-core/src/test/resources/plugins.txt diff --git a/plugin-modernizer-cli/src/main/java/io/jenkins/tools/pluginmodernizer/cli/Main.java b/plugin-modernizer-cli/src/main/java/io/jenkins/tools/pluginmodernizer/cli/Main.java index 362cab7b..4c231335 100644 --- a/plugin-modernizer-cli/src/main/java/io/jenkins/tools/pluginmodernizer/cli/Main.java +++ b/plugin-modernizer-cli/src/main/java/io/jenkins/tools/pluginmodernizer/cli/Main.java @@ -10,6 +10,7 @@ import io.jenkins.tools.pluginmodernizer.core.config.Settings; import io.jenkins.tools.pluginmodernizer.core.impl.PluginModernizer; import io.jenkins.tools.pluginmodernizer.core.model.RecipeDescriptor; +import io.jenkins.tools.pluginmodernizer.core.utils.PluginListParser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.bridge.SLF4JBridgeHandler; @@ -31,13 +32,16 @@ public static void main(final String[] args) { new CommandLine(new Main()).setOptionsCaseInsensitive(true).execute(args); } - @Option(names = {"-p", "--plugins"}, required = true, description = "List of Plugins to Modernize.", split = ",", parameterConsumer = CommaSeparatedParameterConsumer.class) + @Option(names = {"-p", "--plugins"}, description = "List of Plugins to Modernize.", split = ",", parameterConsumer = CommaSeparatedParameterConsumer.class) private List plugins; @Option(names = {"-r", "--recipes"}, required = true, description = "List of Recipes to be applied.", split = ",", parameterConsumer = CommaSeparatedParameterConsumer.class) private List recipes; - @Option(names = {"-g", "--github-owner"}, description = "GitHub owner for forked repositories (only username supported for now)") + @Option(names = {"-f", "--plugin-file"}, description = "Path to the file that contains a list of plugins") + private Path pluginFile; + + @Option(names = {"-g", "--github-owner"}, description = "GitHub owner for forked repositories") private String githubOwner = Settings.GITHUB_OWNER; @Option(names = {"-n", "--dry-run"}, description = "Perform a dry run without making any changes.") @@ -100,6 +104,10 @@ public void listAvailableRecipes() { @Override public void run() { + if (pluginFile != null && (plugins == null || plugins.isEmpty())) { + plugins = PluginListParser.loadPluginsFromFile(pluginFile); + } + if (listRecipes) { listAvailableRecipes(); return; diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/Settings.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/Settings.java index 8c4cdd71..1a63d485 100644 --- a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/Settings.java +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/Settings.java @@ -28,7 +28,7 @@ public class Settings { public static final String TEST_PLUGINS_DIRECTORY; - public static final String ORGANIZATION = "jenkinsci"; + public static final String ORGANIZATION = "sridamul"; public static final String RECIPE_DATA_YAML_PATH = "recipe_data.yaml"; diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParser.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParser.java new file mode 100644 index 00000000..1391bd83 --- /dev/null +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParser.java @@ -0,0 +1,26 @@ +package io.jenkins.tools.pluginmodernizer.core.utils; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PluginListParser { + + private static final Logger LOG = LoggerFactory.getLogger(PluginListParser.class); + + public static List loadPluginsFromFile(Path pluginFile) { + try (Stream lines = Files.lines(pluginFile)) { + return lines.filter(line -> !line.trim().isEmpty()) + .collect(Collectors.toList()); + } catch (IOException e) { + LOG.error("Error reading plugins from file: {}", e.getMessage()); + return null; + } + } +} diff --git a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParserTest.java b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParserTest.java new file mode 100644 index 00000000..dd85911e --- /dev/null +++ b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParserTest.java @@ -0,0 +1,67 @@ +package io.jenkins.tools.pluginmodernizer.core.utils; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +public class PluginListParserTest { + + @TempDir + Path tempDir; + + @Test + public void testLoadPluginsFromFileWithEmptyLines() throws IOException { + Path pluginFile = tempDir.resolve("plugins.txt"); + Files.write(pluginFile, List.of("plugin1", "", "plugin2", " ", "plugin3")); + + List plugins = PluginListParser.loadPluginsFromFile(pluginFile); + + assertNotNull(plugins); + assertEquals(3, plugins.size()); + assertTrue(plugins.contains("plugin1")); + assertTrue(plugins.contains("plugin2")); + assertTrue(plugins.contains("plugin3")); + } + + @Test + public void testLoadPluginsFromFileEmptyFile() throws IOException { + Path pluginFile = tempDir.resolve("plugins.txt"); + Files.createFile(pluginFile); + + List plugins = PluginListParser.loadPluginsFromFile(pluginFile); + assertNotNull(plugins); + assertTrue(plugins.isEmpty()); + } + + @Test + public void testLoadPluginsFromFileFileNotFound() { + Path pluginFile = tempDir.resolve("nonexistent.txt"); + + List plugins = PluginListParser.loadPluginsFromFile(pluginFile); + + assertNull(plugins); + } + + @Test + public void testLoadPluginsFromResourceFile() { + // Load plugins.txt from resources + Path resourceFilePath = Path.of("src", "test", "resources", "plugins.txt"); + + List plugins = PluginListParser.loadPluginsFromFile(resourceFilePath); + + assertNotNull(plugins); + assertEquals(3, plugins.size()); + assertTrue(plugins.contains("jobcacher-plugin")); + assertTrue(plugins.contains("login-theme-plugin")); + assertTrue(plugins.contains("next-executions-plugin")); + } +} diff --git a/plugin-modernizer-core/src/test/resources/plugins.txt b/plugin-modernizer-core/src/test/resources/plugins.txt new file mode 100644 index 00000000..93f4227d --- /dev/null +++ b/plugin-modernizer-core/src/test/resources/plugins.txt @@ -0,0 +1,3 @@ +jobcacher-plugin +login-theme-plugin +next-executions-plugin From ed7bbace8bdb85fb2c964f7dc6c8f4e6176cc9d3 Mon Sep 17 00:00:00 2001 From: sridamul Date: Mon, 15 Jul 2024 18:27:01 +0530 Subject: [PATCH 2/6] Update tests --- .../tools/pluginmodernizer/cli/MainTest.java | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/plugin-modernizer-cli/src/test/java/io/jenkins/tools/pluginmodernizer/cli/MainTest.java b/plugin-modernizer-cli/src/test/java/io/jenkins/tools/pluginmodernizer/cli/MainTest.java index 374a8321..a84bf622 100644 --- a/plugin-modernizer-cli/src/test/java/io/jenkins/tools/pluginmodernizer/cli/MainTest.java +++ b/plugin-modernizer-cli/src/test/java/io/jenkins/tools/pluginmodernizer/cli/MainTest.java @@ -9,12 +9,14 @@ import java.io.IOException; import java.io.PrintStream; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import io.jenkins.tools.pluginmodernizer.core.config.Config; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import picocli.CommandLine; public class MainTest { @@ -23,6 +25,9 @@ public class MainTest { private ByteArrayOutputStream outputStream; private Main main; + @TempDir + Path tempDir; + @BeforeEach public void setUp() { main = new Main(); @@ -56,17 +61,24 @@ public void testGetRecipes() { } @Test - public void testMissingPluginsArgument() { - String[] args = {"-r", "recipe1,recipe2"}; + public void testMissingRecipesArgument() { + String[] args = {"-p", "plugin1,plugin2"}; int exitCode = commandLine.execute(args); assertEquals(CommandLine.ExitCode.USAGE, exitCode); } @Test - public void testMissingRecipesArgument() { - String[] args = {"-p", "plugin1,plugin2"}; + public void testPluginFile() throws IOException { + Path pluginFile = tempDir.resolve("plugins.txt"); + Files.write(pluginFile, List.of("plugin1", "", "plugin2", " ", "plugin3")); + String[] args = {"-f", pluginFile.toString(), "-r", "recipe1,recipe2"}; int exitCode = commandLine.execute(args); - assertEquals(CommandLine.ExitCode.USAGE, exitCode); + List plugins = main.setup().getPlugins(); + assertNotNull(plugins); + assertEquals(3, plugins.size()); + assertEquals("plugin1", plugins.get(0)); + assertEquals("plugin2", plugins.get(1)); + assertEquals("plugin3", plugins.get(2)); } @Test From 3f704f7fdb555597ddac83ff49435a62785a6f6a Mon Sep 17 00:00:00 2001 From: sridamul Date: Mon, 15 Jul 2024 18:29:47 +0530 Subject: [PATCH 3/6] Change org back to jenkinsci --- .../io/jenkins/tools/pluginmodernizer/core/config/Settings.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/Settings.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/Settings.java index 1a63d485..8c4cdd71 100644 --- a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/Settings.java +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/config/Settings.java @@ -28,7 +28,7 @@ public class Settings { public static final String TEST_PLUGINS_DIRECTORY; - public static final String ORGANIZATION = "sridamul"; + public static final String ORGANIZATION = "jenkinsci"; public static final String RECIPE_DATA_YAML_PATH = "recipe_data.yaml"; From dd0ed28b0ce4d2c1bcfe0d024e5ade33130f5666 Mon Sep 17 00:00:00 2001 From: sridamul Date: Mon, 15 Jul 2024 19:18:45 +0530 Subject: [PATCH 4/6] Use plugin names instead of repo names --- .../pluginmodernizer/core/utils/PluginListParserTest.java | 7 +++---- plugin-modernizer-core/src/test/resources/plugins.txt | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParserTest.java b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParserTest.java index dd85911e..b8a7a1c7 100644 --- a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParserTest.java +++ b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParserTest.java @@ -53,15 +53,14 @@ public void testLoadPluginsFromFileFileNotFound() { @Test public void testLoadPluginsFromResourceFile() { - // Load plugins.txt from resources Path resourceFilePath = Path.of("src", "test", "resources", "plugins.txt"); List plugins = PluginListParser.loadPluginsFromFile(resourceFilePath); assertNotNull(plugins); assertEquals(3, plugins.size()); - assertTrue(plugins.contains("jobcacher-plugin")); - assertTrue(plugins.contains("login-theme-plugin")); - assertTrue(plugins.contains("next-executions-plugin")); + assertTrue(plugins.contains("jobcacher")); + assertTrue(plugins.contains("login-theme")); + assertTrue(plugins.contains("next-executions")); } } diff --git a/plugin-modernizer-core/src/test/resources/plugins.txt b/plugin-modernizer-core/src/test/resources/plugins.txt index 93f4227d..b6dd0ce8 100644 --- a/plugin-modernizer-core/src/test/resources/plugins.txt +++ b/plugin-modernizer-core/src/test/resources/plugins.txt @@ -1,3 +1,3 @@ -jobcacher-plugin -login-theme-plugin -next-executions-plugin +jobcacher +login-theme +next-executions From 7f8a320718ab6af4d49a9b6bbc72e9aab68b01b2 Mon Sep 17 00:00:00 2001 From: sridamul Date: Tue, 16 Jul 2024 13:02:09 +0530 Subject: [PATCH 5/6] Split plugin name that contains version info --- .../tools/pluginmodernizer/core/utils/PluginListParser.java | 1 + .../pluginmodernizer/core/utils/PluginListParserTest.java | 3 ++- plugin-modernizer-core/src/test/resources/plugins.txt | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParser.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParser.java index 1391bd83..dd13c5cc 100644 --- a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParser.java +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParser.java @@ -17,6 +17,7 @@ public class PluginListParser { public static List loadPluginsFromFile(Path pluginFile) { try (Stream lines = Files.lines(pluginFile)) { return lines.filter(line -> !line.trim().isEmpty()) + .map(line -> line.split(":")[0]) .collect(Collectors.toList()); } catch (IOException e) { LOG.error("Error reading plugins from file: {}", e.getMessage()); diff --git a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParserTest.java b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParserTest.java index b8a7a1c7..b904de0e 100644 --- a/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParserTest.java +++ b/plugin-modernizer-core/src/test/java/io/jenkins/tools/pluginmodernizer/core/utils/PluginListParserTest.java @@ -58,9 +58,10 @@ public void testLoadPluginsFromResourceFile() { List plugins = PluginListParser.loadPluginsFromFile(resourceFilePath); assertNotNull(plugins); - assertEquals(3, plugins.size()); + assertEquals(4, plugins.size()); assertTrue(plugins.contains("jobcacher")); assertTrue(plugins.contains("login-theme")); assertTrue(plugins.contains("next-executions")); + assertTrue(plugins.contains("cloudbees-bitbucket-branch-source")); } } diff --git a/plugin-modernizer-core/src/test/resources/plugins.txt b/plugin-modernizer-core/src/test/resources/plugins.txt index b6dd0ce8..38ef6141 100644 --- a/plugin-modernizer-core/src/test/resources/plugins.txt +++ b/plugin-modernizer-core/src/test/resources/plugins.txt @@ -1,3 +1,4 @@ jobcacher login-theme -next-executions +next-executions:1.0.0 +cloudbees-bitbucket-branch-source:2.4.4 From e1382efe15b3b67ba854f6d9ef4caaa42bf0b4be Mon Sep 17 00:00:00 2001 From: sridamul Date: Tue, 16 Jul 2024 15:30:25 +0530 Subject: [PATCH 6/6] Load plugins from both plugins.txt and -p option --- .../tools/pluginmodernizer/cli/Main.java | 21 ++++++++-- .../tools/pluginmodernizer/cli/MainTest.java | 38 +++++++++++++++++-- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/plugin-modernizer-cli/src/main/java/io/jenkins/tools/pluginmodernizer/cli/Main.java b/plugin-modernizer-cli/src/main/java/io/jenkins/tools/pluginmodernizer/cli/Main.java index 4c231335..f4e3570f 100644 --- a/plugin-modernizer-cli/src/main/java/io/jenkins/tools/pluginmodernizer/cli/Main.java +++ b/plugin-modernizer-cli/src/main/java/io/jenkins/tools/pluginmodernizer/cli/Main.java @@ -2,6 +2,8 @@ import java.io.InputStream; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import com.fasterxml.jackson.core.type.TypeReference; @@ -102,11 +104,24 @@ public void listAvailableRecipes() { } } + private List loadPlugins() { + List loadedPlugins = new ArrayList<>(); + + if (pluginFile != null) { + List pluginsFromFile = PluginListParser.loadPluginsFromFile(pluginFile); + loadedPlugins.addAll(pluginsFromFile); + } + + if (plugins != null) { + loadedPlugins.addAll(plugins); + } + + return new ArrayList<>(new HashSet<>(loadedPlugins)); + } + @Override public void run() { - if (pluginFile != null && (plugins == null || plugins.isEmpty())) { - plugins = PluginListParser.loadPluginsFromFile(pluginFile); - } + plugins = loadPlugins(); if (listRecipes) { listAvailableRecipes(); diff --git a/plugin-modernizer-cli/src/test/java/io/jenkins/tools/pluginmodernizer/cli/MainTest.java b/plugin-modernizer-cli/src/test/java/io/jenkins/tools/pluginmodernizer/cli/MainTest.java index a84bf622..7aa43e64 100644 --- a/plugin-modernizer-cli/src/test/java/io/jenkins/tools/pluginmodernizer/cli/MainTest.java +++ b/plugin-modernizer-cli/src/test/java/io/jenkins/tools/pluginmodernizer/cli/MainTest.java @@ -72,13 +72,43 @@ public void testPluginFile() throws IOException { Path pluginFile = tempDir.resolve("plugins.txt"); Files.write(pluginFile, List.of("plugin1", "", "plugin2", " ", "plugin3")); String[] args = {"-f", pluginFile.toString(), "-r", "recipe1,recipe2"}; - int exitCode = commandLine.execute(args); + commandLine.execute(args); List plugins = main.setup().getPlugins(); assertNotNull(plugins); assertEquals(3, plugins.size()); - assertEquals("plugin1", plugins.get(0)); - assertEquals("plugin2", plugins.get(1)); - assertEquals("plugin3", plugins.get(2)); + assertTrue(plugins.contains("plugin1")); + assertTrue(plugins.contains("plugin2")); + assertTrue(plugins.contains("plugin3")); + } + + @Test + public void testPluginFileAlongWithPluginOptionWithoutCommonPlugins() throws IOException { + Path pluginFile = tempDir.resolve("plugins.txt"); + Files.write(pluginFile, List.of("plugin1", "", "plugin2", " ", "plugin3")); + String[] args = {"-f", pluginFile.toString(), "-r", "recipe1,recipe2", "-p", "plugin4,plugin5"}; + commandLine.execute(args); + List plugins = main.setup().getPlugins(); + assertNotNull(plugins); + assertEquals(5, plugins.size()); + assertTrue(plugins.contains("plugin1")); + assertTrue(plugins.contains("plugin2")); + assertTrue(plugins.contains("plugin3")); + assertTrue(plugins.contains("plugin4")); + assertTrue(plugins.contains("plugin5")); + } + + @Test + public void testPluginFileAlongWithPluginOptionWithCommonPlugins() throws IOException { + Path pluginFile = tempDir.resolve("plugins.txt"); + Files.write(pluginFile, List.of("plugin1", "", "plugin2", " ", "plugin3")); + String[] args = {"-f", pluginFile.toString(), "-r", "recipe1,recipe2", "-p", "plugin2,plugin3"}; + commandLine.execute(args); + List plugins = main.setup().getPlugins(); + assertNotNull(plugins); + assertEquals(3, plugins.size()); + assertTrue(plugins.contains("plugin1")); + assertTrue(plugins.contains("plugin2")); + assertTrue(plugins.contains("plugin3")); } @Test