Skip to content

Commit

Permalink
Merge pull request #573 from jonesbusy/bugfix/local-run-mode
Browse files Browse the repository at this point in the history
Fix --plugin-path when running with run command and support default value
  • Loading branch information
jonesbusy authored Jan 5, 2025
2 parents 9716249 + b097991 commit 1c46fc7
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class BuildMetadataCommand implements ICommand {
/**
* Plugins options
*/
@CommandLine.ArgGroup(exclusive = true, multiplicity = "1")
@CommandLine.ArgGroup
private PluginOptions pluginOptions;

/**
Expand All @@ -56,6 +56,9 @@ public class BuildMetadataCommand implements ICommand {
public Config setup(Config.Builder builder) {
options.config(builder);
envOptions.config(builder);
if (pluginOptions == null) {
pluginOptions = new PluginOptions();
}
pluginOptions.config(builder);
return builder.withSshPrivateKey(sshPrivateKey)
.withRecipe(Settings.FETCH_METADATA_RECIPE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class DryRunCommand implements ICommand {
/**
* Plugins options
*/
@CommandLine.ArgGroup(exclusive = true, multiplicity = "1")
@CommandLine.ArgGroup
private PluginOptions pluginOptions;

/**
Expand Down Expand Up @@ -61,6 +61,9 @@ public class DryRunCommand implements ICommand {
@Override
public Config setup(Config.Builder builder) {
options.config(builder);
if (pluginOptions == null) {
pluginOptions = new PluginOptions();
}
pluginOptions.config(builder);
githubOptions.config(builder);
envOptions.config(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class RunCommand implements ICommand {
/**
* Plugins options
*/
@CommandLine.ArgGroup(exclusive = true, multiplicity = "1")
@CommandLine.ArgGroup
private PluginOptions pluginOptions;

/**
Expand Down Expand Up @@ -73,10 +73,12 @@ public class RunCommand implements ICommand {
public Config setup(Config.Builder builder) {
options.config(builder);
envOptions.config(builder);
if (pluginOptions == null) {
pluginOptions = new PluginOptions();
}
pluginOptions.config(builder);
githubOptions.config(builder);
return builder.withDryRun(false)
.withRecipe(recipe)
return builder.withRecipe(recipe)
.withDraft(draft)
.withRemoveForks(removeForks)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import io.jenkins.tools.pluginmodernizer.cli.converter.PluginFileConverter;
import io.jenkins.tools.pluginmodernizer.cli.converter.PluginPathConverter;
import io.jenkins.tools.pluginmodernizer.core.config.Config;
import io.jenkins.tools.pluginmodernizer.core.model.ModernizerException;
import io.jenkins.tools.pluginmodernizer.core.model.Plugin;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -70,9 +70,19 @@ private List<Plugin> getEffectivePlugins() {
if (pluginsFromFile == null) {
pluginsFromFile = List.of();
}
return Stream.concat(
List<Plugin> effectivePlugins = Stream.concat(
pluginPath != null ? Stream.of(pluginPath) : Stream.empty(),
Stream.concat(plugins.stream(), pluginsFromFile.stream()))
.collect(Collectors.toList());
.toList();

// Use current folder as plugin if no plugin is provided
if (effectivePlugins.isEmpty()) {
try {
effectivePlugins.add(new PluginPathConverter().convert("."));
} catch (Exception e) {
throw new ModernizerException("Current directory doesn't seem to contains a plugin", e);
}
}
return effectivePlugins;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,50 @@ public void testRecipeOnLocalPlugin(WireMockRuntimeInfo wmRuntimeInfo) throws Ex
}
}

@Test
public void testRecipeOnLocalPluginWithRunMode(WireMockRuntimeInfo wmRuntimeInfo) throws Exception {

Path logFile = setupLogs("testRecipeOnLocalPluginWithRunMode");

// Copy empty plugin to cache and use as local plugin
final String plugin = "empty";
final Path pluginPath = Path.of("src/test/resources").resolve(plugin);
Path targetPath = cachePath
.resolve("jenkins-plugin-modernizer-cli")
.resolve(plugin)
.resolve("sources");
FileUtils.copyDirectory(pluginPath.toFile(), targetPath.toFile());

final String recipe = "AddCodeOwner";

try (GitHubServerContainer gitRemote = new GitHubServerContainer(wmRuntimeInfo, keysPath, plugin, "main")) {

gitRemote.start();

// Junit attachment with logs file for the plugin build
System.out.printf(
"[[ATTACHMENT|%s]]%n", Plugin.build(plugin).getLogFile().toAbsolutePath());
System.out.printf("[[ATTACHMENT|%s]]%n", logFile.toAbsolutePath());

Invoker invoker = buildInvoker();
InvocationRequest request = buildRequest(
"run --recipe %s %s".formatted(recipe, getRunArgs(wmRuntimeInfo, Plugin.build(plugin, targetPath))),
logFile);
InvocationResult result = invoker.execute(request);

// Assert output
assertAll(
() -> assertEquals(0, result.getExitCode()),
() -> assertTrue(Files.readAllLines(logFile).stream()
.anyMatch(line -> line.matches("(.*)Dry run mode. Changes were commited on (.*)"))));

// Check that new file was created
assertTrue(
Files.exists(targetPath.resolve(ArchetypeCommonFile.CODEOWNERS.getPath())),
"Code owner file was not created");
}
}

/**
* Build the invoker
* @return the invoker
Expand Down

0 comments on commit 1c46fc7

Please sign in to comment.