Skip to content

Commit

Permalink
Merge pull request #419 from jonesbusy/feature/unique-recipe
Browse files Browse the repository at this point in the history
Run only one recipe per execution
  • Loading branch information
jonesbusy authored Dec 8, 2024
2 parents 02298b9 + 7f804b5 commit efbd722
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 119 deletions.
2 changes: 1 addition & 1 deletion .gitpod/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ echo -e "If you want to use another GitHub handle, please set the ${color_green}
echo -e "\nWe propose you to work with two Jenkins plugin repositories: ${color_cyan}badge-plugin${color_reset} and ${color_cyan}build-timestamp-plugin${color_reset}."
echo -e "You could, though, use other plugin repositories as well (like ${color_cyan}plot-plugin${color_reset}) by entering their name in the following ${color_cyan}java${color_reset} command."
echo -e "\nYou can now proceed with the modernizer tool thanks to the following commands:"
echo -e "${color_cyan}java -jar plugin-modernizer-cli/target/jenkins-plugin-modernizer-999999-SNAPSHOT.jar --dry-run --plugins badge,build-timestamp --recipes AddPluginsBom,AddCodeOwner --export-datatables${color_reset}"
echo -e "${color_cyan}java -jar plugin-modernizer-cli/target/jenkins-plugin-modernizer-999999-SNAPSHOT.jar --dry-run --plugins badge,build-timestamp --recipe AddCodeOwner --export-datatables${color_reset}"

echo -e "\nBy the way, you can copy/paste from/to the terminal to execute the commands. Enjoy! 🚀"
echo -e "See ${color_blue}https://www.gitpod.io/docs/configure/user-settings/browser-settings#browser-settings${color_reset} to know more.\n"
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ To activate app authentication, just set the following CLI argument
## CLI Options
- `--plugins` or `-p`: (optional) Name(s) of plugin directory cloned inside the `test-plugins` directory.

- `--recipes` or `-r`: (required) Name(s) of recipes to apply to the plugins.
- `--recipe` or `-r`: (required) Name of recipe to apply to the plugins.

- `--plugin-file` or `-f`: (optional) Path to the text file that contains a list of plugins. (see example [plugin file](docs/example-plugins.txt))

Expand Down Expand Up @@ -143,7 +143,7 @@ Plugins can be passed to the CLI tool in two ways:
Pass the plugin names directly using the `-p` or `--plugins option`. The expected input format for plugins is `artifact ID`.

```shell
java -jar plugin-modernizer-cli/target/jenkins-plugin-modernizer-999999-SNAPSHOT.jar --plugins git,git-client,jobcacher --recipes AddPluginsBom,AddCodeOwner
java -jar plugin-modernizer-cli/target/jenkins-plugin-modernizer-999999-SNAPSHOT.jar --plugins git,git-client,jobcacher --recipe AddPluginsBom
```
Here, `git`, `git-client`, and `jobcacher` are plugin artifact IDs (also known as plugin names), while `AddPluginsBom` and `AddCodeOwners` are recipe names. For more details about available recipes, refer to the [recipe_data.yaml](plugin-modernizer-core/src/main/resources/recipe_data.yaml) file.

Expand All @@ -153,7 +153,7 @@ Pass the path to a file that contains plugin names. The expected input format fo
See example [plugin file](docs/example-plugins.txt)

```shell
java -jar plugin-modernizer-cli/target/jenkins-plugin-modernizer-999999-SNAPSHOT.jar --plugin-file path/to/plugin-file --recipes AddPluginsBom,AddCodeOwner
java -jar plugin-modernizer-cli/target/jenkins-plugin-modernizer-999999-SNAPSHOT.jar --plugin-file path/to/plugin-file --recipe AddPluginsBom
```

## Configuring Environmental Variables
Expand All @@ -172,14 +172,14 @@ java -jar plugin-modernizer-cli/target/jenkins-plugin-modernizer-999999-SNAPSHOT
### without dry-run

```shell
java -jar plugin-modernizer-cli/target/jenkins-plugin-modernizer-999999-SNAPSHOT.jar --plugins git,git-client,jobcacher --recipes AddPluginsBom,AddCodeOwner
java -jar plugin-modernizer-cli/target/jenkins-plugin-modernizer-999999-SNAPSHOT.jar --plugins git,git-client,jobcacher --recipe AddPluginsBom
```
The above command creates pull requests in the respective remote repositories after applying the changes.

### with dry-run

```shell
java -jar plugin-modernizer-cli/target/jenkins-plugin-modernizer-999999-SNAPSHOT.jar --plugins git,git-client,jobcacher --recipes AddPluginsBom,AddCodeOwner --dry-run
java -jar plugin-modernizer-cli/target/jenkins-plugin-modernizer-999999-SNAPSHOT.jar --plugins git,git-client,jobcacher --recipe AddPluginsBom --dry-run
```

The above command generates patch files instead of applying changes directly. These patch files are saved in `/target/rewrite/rewrite.patch` inside each plugin directory. No pull requests will be created.
Expand All @@ -190,7 +190,7 @@ The above command generates patch files instead of applying changes directly. Th
### with export-datatables

```shell
java -jar plugin-modernizer-cli/target/jenkins-plugin-modernizer-999999-SNAPSHOT.jar --plugins git,git-client,jobcacher --recipes AddPluginsBom,AddCodeOwner --export-datatables
java -jar plugin-modernizer-cli/target/jenkins-plugin-modernizer-999999-SNAPSHOT.jar --plugins git,git-client,jobcacher --recipe AddPluginsBom --export-datatables
```

The above command creates a report of the changes made through OpenRewrite in csv format. The report will be generated in `target/rewrite/datatables` inside the plugin directory.
Expand Down Expand Up @@ -222,7 +222,7 @@ docker run \
-e GH_OWNER=${GH_OWNER} \
-v $(pwd)/plugins.txt:/plugins.txt \
ghcr.io/jenkins-infra/plugin-modernizer-tool:main \
--plugin-file /plugins.txt --recipes AddPluginsBom,AddCodeOwner
--plugin-file /plugins.txt --recipe AddCodeOwner
```

### Explanation
Expand All @@ -232,7 +232,7 @@ docker run \
- `-v $(pwd)/plugins.txt:/plugins.txt`: Mounts the plugins.txt file from the current directory to the Docker container.
- `ghcr.io/jenkins-infra/plugin-modernizer-tool:main`: Specifies the Docker image to use.
- `--plugin-file /plugins.txt`: Specifies the path to the plugin file inside the Docker container.
- `--recipes AddPluginsBom,AddCodeOwner`: Specifies the recipes to apply.
- `--recipe AddPluginsBom,`: Specifies the recipe to apply.

This command will run the Plugin Modernizer Tool inside the Docker container using the specified environment variables and plugin file.

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
env_file:
- .env
command:
- --recipes ${RECIPES}
- --recipe ${RECIPE}
- --plugins ${PLUGINS}
- --debug
- --dry-run
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,11 @@ static class PluginOptions {
private PluginOptions pluginOptions;

@Option(
names = {"-r", "--recipes"},
names = {"-r", "--recipe"},
required = true,
description = "List of Recipes to be applied.",
split = ",",
description = "Recipe to be applied.",
converter = RecipeConverter.class)
private List<Recipe> recipes;
private Recipe recipe;

@Option(
names = {"-g", "--github-owner"},
Expand Down Expand Up @@ -197,7 +196,7 @@ public Config setup() {
.withGitHubAppSourceInstallationId(githubAppSourceInstallationId)
.withGitHubAppTargetInstallationId(githubAppTargetInstallationId)
.withPlugins(pluginOptions != null ? pluginOptions.plugins : new ArrayList<>())
.withRecipes(recipes)
.withRecipe(recipe)
.withDryRun(dryRun)
.withSkipPush(skipPush)
.withSkipBuild(skipBuild)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,19 @@ public void testGetRecipes() {
String[] args = {"-p", "plugin1,plugin2", "-r", "FetchMetadata"};
commandLine.execute(args);

List<Recipe> recipes = main.setup().getRecipes();
assertNotNull(recipes);
assertEquals(1, recipes.size());
assertEquals(
"io.jenkins.tools.pluginmodernizer.FetchMetadata",
recipes.get(0).getName());
Recipe recipe = main.setup().getRecipe();
assertNotNull(recipe);
assertEquals("io.jenkins.tools.pluginmodernizer.FetchMetadata", recipe.getName());
}

@Test
public void testGetRecipesWithFQDN() {
String[] args = {"-p", "plugin1,plugin2", "-r", "io.jenkins.tools.pluginmodernizer.FetchMetadata"};
commandLine.execute(args);

List<Recipe> recipes = main.setup().getRecipes();
assertNotNull(recipes);
assertEquals(1, recipes.size());
assertEquals(
"io.jenkins.tools.pluginmodernizer.FetchMetadata",
recipes.get(0).getName());
Recipe recipe = main.setup().getRecipe();
assertNotNull(recipe);
assertEquals("io.jenkins.tools.pluginmodernizer.FetchMetadata", recipe.getName());
}

@Test
Expand Down Expand Up @@ -163,32 +157,14 @@ public void testSkipPushOptions() throws IOException {

@Test
public void testSkipPullRequestOptions() throws IOException {
String[] args = {
"-p",
"plugin1,plugin2",
"-r",
"FetchMetadata",
"--skip-push",
"--recipes",
"FetchMetadata",
"--skip-pull-request"
};
String[] args = {"-p", "plugin1,plugin2", "--skip-push", "--recipe", "FetchMetadata", "--skip-pull-request"};
commandLine.execute(args);
assertTrue(main.setup().isSkipPullRequest());
}

@Test
public void testCleanLocalData() throws IOException {
String[] args = {
"-p",
"plugin1,plugin2",
"-r",
"FetchMetadata",
"--skip-push",
"--recipes",
"FetchMetadata",
"--clean-local-data"
};
String[] args = {"-p", "plugin1,plugin2", "--skip-push", "--recipe", "FetchMetadata", "--clean-local-data"};
commandLine.execute(args);
assertTrue(main.setup().isRemoveLocalData());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Config {

private final String version;
private final List<Plugin> plugins;
private final List<Recipe> recipes;
private final Recipe recipe;
private final URL jenkinsUpdateCenter;
private final URL jenkinsPluginVersions;
private final URL pluginHealthScore;
Expand All @@ -41,7 +41,7 @@ private Config(
Long githubAppSourceInstallationId,
Long githubAppTargetInstallationId,
List<Plugin> plugins,
List<Recipe> recipes,
Recipe recipe,
URL jenkinsUpdateCenter,
URL jenkinsPluginVersions,
URL pluginHealthScore,
Expand All @@ -62,7 +62,7 @@ private Config(
this.githubAppSourceInstallationId = githubAppSourceInstallationId;
this.githubAppTargetInstallationId = githubAppTargetInstallationId;
this.plugins = plugins;
this.recipes = recipes;
this.recipe = recipe;
this.jenkinsUpdateCenter = jenkinsUpdateCenter;
this.jenkinsPluginVersions = jenkinsPluginVersions;
this.pluginHealthScore = pluginHealthScore;
Expand Down Expand Up @@ -103,16 +103,16 @@ public List<Plugin> getPlugins() {
return plugins;
}

public List<Recipe> getRecipes() {
return recipes;
public Recipe getRecipe() {
return recipe;
}

/**
* Return if the current configuration is only fetching metadata which will skip compile and verify steps
* @return True if only fetching metadata
*/
public boolean isFetchMetadataOnly() {
return recipes.size() == 1 && recipes.get(0).getName().equals(Settings.FETCH_METADATA_RECIPE.getName());
return recipe.getName().equals(Settings.FETCH_METADATA_RECIPE.getName());
}

public URL getJenkinsUpdateCenter() {
Expand Down Expand Up @@ -186,7 +186,7 @@ public static class Builder {
private Long githubAppSourceInstallationId;
private Long githubAppTargetInstallationId;
private List<Plugin> plugins;
private List<Recipe> recipes;
private Recipe recipe;
private URL jenkinsUpdateCenter = Settings.DEFAULT_UPDATE_CENTER_URL;
private URL jenkinsPluginVersions = Settings.DEFAULT_PLUGIN_VERSIONS;
private URL pluginStatsInstallations = Settings.DEFAULT_PLUGINS_STATS_INSTALLATIONS_URL;
Expand Down Expand Up @@ -232,8 +232,8 @@ public Builder withPlugins(List<Plugin> plugins) {
return this;
}

public Builder withRecipes(List<Recipe> recipes) {
this.recipes = recipes;
public Builder withRecipe(Recipe recipe) {
this.recipe = recipe;
return this;
}

Expand Down Expand Up @@ -327,7 +327,7 @@ public Config build() {
githubAppSourceInstallationId,
githubAppTargetInstallationId,
plugins,
recipes,
recipe,
jenkinsUpdateCenter,
jenkinsPluginVersions,
pluginHealthScore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ private void fetchRepository(Plugin plugin) throws GitAPIException {
.setMode(ResetCommand.ResetType.HARD)
.setRef("origin/" + defaultBranch)
.call();
git.clean().setCleanDirectories(true).setDryRun(false).call();
Ref ref = git.checkout()
.setCreateBranch(false)
.setName(defaultBranch)
Expand Down Expand Up @@ -571,7 +572,7 @@ public void commitChanges(Plugin plugin) {
}
try (Git git = Git.open(plugin.getLocalRepository().toFile())) {
git.getRepository().scanForRepoChanges();
String commitMessage = TemplateUtils.renderCommitMessage(plugin, config.getRecipes());
String commitMessage = TemplateUtils.renderCommitMessage(plugin, config.getRecipe());
LOG.debug("Commit message: {}", commitMessage);
Status status = git.status().call();
if (status.hasUncommittedChanges()) {
Expand Down Expand Up @@ -702,8 +703,8 @@ public void openPullRequest(Plugin plugin) {
refreshToken(config.getGithubAppTargetInstallationId());

// Renders parts and log then even if dry-run
String prTitle = TemplateUtils.renderPullRequestTitle(plugin, config.getRecipes());
String prBody = TemplateUtils.renderPullRequestBody(plugin, config.getRecipes());
String prTitle = TemplateUtils.renderPullRequestTitle(plugin, config.getRecipe());
String prBody = TemplateUtils.renderPullRequestBody(plugin, config.getRecipe());
LOG.debug("Pull request title: {}", prTitle);
LOG.debug("Pull request body: {}", prBody);
LOG.debug("Draft mode: {}", config.isDraft());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.InvocationRequest;
Expand Down Expand Up @@ -99,15 +98,12 @@ public void collectMetadata(Plugin plugin) {
* @param plugin The plugin to run the rewrite on
*/
public void invokeRewrite(Plugin plugin) {
plugin.addTags(config.getRecipes().stream()
.flatMap(recipe -> recipe.getTags().stream())
.collect(Collectors.toSet()));
plugin.addTags(config.getRecipe().getTags());
LOG.info(
"Running recipes {} for plugin {}... Please be patient",
String.join(
",", config.getRecipes().stream().map(Recipe::getName).toList()),
config.getRecipe().getName(),
plugin);
invokeGoals(plugin, getRewriteArgs());
invokeGoals(plugin, getSingleRecipeArgs(config.getRecipe()));
LOG.info("Done");
}

Expand All @@ -125,24 +121,6 @@ private String[] getSingleRecipeArgs(Recipe recipe) {
return goals.toArray(String[]::new);
}

/**
* Get the rewrite arguments to be executed for each plugin
* @return The list of arguments to be passed to the rewrite plugin
*/
private String[] getRewriteArgs() {
List<String> goals = new ArrayList<>();
goals.add("org.openrewrite.maven:rewrite-maven-plugin:" + Settings.MAVEN_REWRITE_PLUGIN_VERSION + ":run");
goals.add("-Drewrite.exportDatatables=" + config.isExportDatatables());

String activeRecipesStr =
config.getRecipes().stream().map(Recipe::getName).collect(Collectors.joining(", "));
LOG.debug("Active recipes: {}", activeRecipesStr);
goals.add("-Drewrite.activeRecipes=" + String.join(",", activeRecipesStr));
goals.add("-Drewrite.recipeArtifactCoordinates=io.jenkins.plugin-modernizer:plugin-modernizer-core:"
+ config.getVersion());
return goals.toArray(String[]::new);
}

/**
* Invoke a list of maven goal on the plugin
* @param plugin The plugin to run the goals on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import io.jenkins.tools.pluginmodernizer.core.utils.PluginService;
import jakarta.inject.Inject;
import java.util.List;
import java.util.stream.Collectors;
import org.openrewrite.Recipe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -54,8 +52,7 @@ public void start() {

// Debug config
LOG.debug("Plugins: {}", config.getPlugins());
LOG.debug(
"Recipes: {}", config.getRecipes().stream().map(Recipe::getName).collect(Collectors.joining(", ")));
LOG.debug("Recipe: {}", config.getRecipe().getName());
LOG.debug("GitHub owner: {}", config.getGithubOwner());
LOG.debug("Update Center Url: {}", config.getJenkinsUpdateCenter());
LOG.debug("Plugin versions Url: {}", config.getJenkinsPluginVersions());
Expand Down Expand Up @@ -340,6 +337,13 @@ private void printResults(List<Plugin> plugins) {
else {
LOG.info("Pull request was open on "
+ plugin.getRemoteRepository(this.ghService).getHtmlUrl());

// Display changes depending on the recipe
if (config.getRecipe()
.getName()
.equals("io.jenkins.tools.pluginmodernizer.UpgradeBomVersion")) {
LOG.info("New BOM version: {}", plugin.getMetadata().getBomVersion());
}
}
}
}
Expand Down
Loading

0 comments on commit efbd722

Please sign in to comment.