Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New option to parse plugins.txt file #86

Merged
merged 6 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> plugins;

@Option(names = {"-r", "--recipes"}, required = true, description = "List of Recipes to be applied.", split = ",", parameterConsumer = CommaSeparatedParameterConsumer.class)
private List<String> 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.")
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -23,6 +25,9 @@ public class MainTest {
private ByteArrayOutputStream outputStream;
private Main main;

@TempDir
Path tempDir;

@BeforeEach
public void setUp() {
main = new Main();
Expand Down Expand Up @@ -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() {
sridamul marked this conversation as resolved.
Show resolved Hide resolved
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<String> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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<String> loadPluginsFromFile(Path pluginFile) {
try (Stream<String> 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());
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> plugins = PluginListParser.loadPluginsFromFile(pluginFile);
assertNotNull(plugins);
assertTrue(plugins.isEmpty());
}

@Test
public void testLoadPluginsFromFileFileNotFound() {
Path pluginFile = tempDir.resolve("nonexistent.txt");

List<String> plugins = PluginListParser.loadPluginsFromFile(pluginFile);

assertNull(plugins);
}

@Test
public void testLoadPluginsFromResourceFile() {
Path resourceFilePath = Path.of("src", "test", "resources", "plugins.txt");

List<String> plugins = PluginListParser.loadPluginsFromFile(resourceFilePath);

assertNotNull(plugins);
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"));
}
}
4 changes: 4 additions & 0 deletions plugin-modernizer-core/src/test/resources/plugins.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
jobcacher
sridamul marked this conversation as resolved.
Show resolved Hide resolved
login-theme
next-executions:1.0.0
cloudbees-bitbucket-branch-source:2.4.4
Loading