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

Add declarative recipes using supported yaml OpenRewrite format #131

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -1,18 +1,19 @@
package io.jenkins.tools.pluginmodernizer.cli;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import io.jenkins.tools.pluginmodernizer.core.config.Config;
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 java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
import org.openrewrite.Recipe;
import org.openrewrite.config.YamlResourceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;
Expand Down Expand Up @@ -131,21 +132,22 @@ public void listAvailableRecipes() {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(Settings.RECIPE_DATA_YAML_PATH);
if (inputStream == null) {
LOG.error("Failed to load the available recipes.");
return;
throw new IllegalArgumentException("Failed to load the available recipes.");
}
try {
List<RecipeDescriptor> recipeDescriptors =
new YAMLMapper().readValue(inputStream, new TypeReference<List<RecipeDescriptor>>() {});

if (recipeDescriptors == null || recipeDescriptors.isEmpty()) {
LOG.error("No recipes found in the YAML file.");
return;
}

YamlResourceLoader yamlResourceLoader =
new YamlResourceLoader(inputStream, URI.create(Settings.RECIPE_DATA_YAML_PATH), new Properties());
List<Recipe> recipeDescriptors =
yamlResourceLoader.listRecipes().stream().toList();
LOG.info("Available recipes:");
recipeDescriptors.forEach(recipe -> LOG.info("{} - {}", recipe.name(), recipe.description()));
// Strip the FQDN prefix from the recipe name
recipeDescriptors.forEach(recipe -> LOG.info(
"{} - {}",
recipe.getName().replaceAll(Settings.RECIPE_FQDN_PREFIX + ".", ""),
recipe.getDescription()));
} catch (Exception e) {
LOG.error("Error loading recipes from YAML: {}", e.getMessage());
throw new IllegalArgumentException("Error loading recipes from YAML: " + e.getMessage());
}
}

Expand Down
13 changes: 12 additions & 1 deletion plugin-modernizer-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-xml</artifactId>
</dependency>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-jenkins</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand All @@ -71,11 +75,18 @@
<filtering>true</filtering>
<directory>src/main/resources</directory>
<includes>
<include>recipe_data.yaml</include>
<include>versions.properties</include>
<include>update_center.properties</include>
</includes>
</resource>
<resource>
<targetPath>META-INF/rewrite</targetPath>
<filtering>false</filtering>
<directory>src/main/resources/META-INF/rewrite</directory>
<includes>
<include>recipes.yml</include>
</includes>
</resource>
</resources>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public class Settings {

public static final String ORGANIZATION = "jenkinsci";

public static final String RECIPE_DATA_YAML_PATH = "recipe_data.yaml";
public static final String RECIPE_DATA_YAML_PATH = "META-INF/rewrite/recipes.yml";

public static final String RECIPE_FQDN_PREFIX = "io.jenkins.tools.pluginmodernizer";

public static final ComparableVersion MAVEN_MINIMAL_VERSION = new ComparableVersion("3.9.7");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package io.jenkins.tools.pluginmodernizer.core.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import edu.umd.cs.findbugs.annotations.Nullable;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.jenkins.tools.pluginmodernizer.core.config.Config;
import io.jenkins.tools.pluginmodernizer.core.config.Settings;
import io.jenkins.tools.pluginmodernizer.core.model.RecipeDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import org.apache.maven.artifact.versioning.ComparableVersion;
Expand All @@ -23,6 +22,8 @@
import org.apache.maven.shared.invoker.InvocationResult;
import org.apache.maven.shared.invoker.Invoker;
import org.apache.maven.shared.invoker.MavenInvocationException;
import org.openrewrite.Recipe;
import org.openrewrite.config.YamlResourceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MarkerFactory;
Expand Down Expand Up @@ -82,37 +83,28 @@ private List<String> createGoalsList() throws IOException {
goals.add("-Drewrite.exportDatatables=" + config.isExportDatatables());

try (InputStream inputStream = getClass().getResourceAsStream("/" + Settings.RECIPE_DATA_YAML_PATH)) {
List<RecipeDescriptor> recipeDescriptors =
new YAMLMapper().readValue(inputStream, new TypeReference<List<RecipeDescriptor>>() {});
YamlResourceLoader yamlResourceLoader =
new YamlResourceLoader(inputStream, URI.create(Settings.RECIPE_DATA_YAML_PATH), new Properties());
List<Recipe> recipeDescriptors =
yamlResourceLoader.listRecipes().stream().toList();

List<String> recipes = config.getRecipes();
List<String> activeRecipes = getActiveRecipes(recipes, recipeDescriptors);
List<String> recipeArtifactCoordinates = getRecipeArtifactCoordinates(recipes, recipeDescriptors);

LOG.debug("Active recipes: {}", activeRecipes);
if (activeRecipes.isEmpty()) {
return null;
}
goals.add("-Drewrite.activeRecipes=" + String.join(",", activeRecipes));
if (!recipeArtifactCoordinates.isEmpty()) {
goals.add("-Drewrite.recipeArtifactCoordinates=" + String.join(",", recipeArtifactCoordinates));
}
goals.add("-Drewrite.recipeArtifactCoordinates=io.jenkins.plugin-modernizer:plugin-modernizer-core:"
+ config.getVersion());
}
return goals;
}

private List<String> getActiveRecipes(List<String> recipes, List<RecipeDescriptor> recipeDescriptors) {
return recipes.stream()
.flatMap(recipe -> recipeDescriptors.stream()
.filter(descriptor -> descriptor.name().equals(recipe))
.map(RecipeDescriptor::fqcn))
.collect(Collectors.toList());
}

private List<String> getRecipeArtifactCoordinates(List<String> recipes, List<RecipeDescriptor> recipeDescriptors) {
private List<String> getActiveRecipes(List<String> recipes, List<Recipe> recipeDescriptors) {
return recipes.stream()
.flatMap(recipe -> recipeDescriptors.stream()
.filter(descriptor -> descriptor.name().equals(recipe))
.map(RecipeDescriptor::artifactCoordinates))
.flatMap(recipe ->
recipeDescriptors.stream().map(Recipe::getName).filter(name -> name.endsWith(recipe)))
.collect(Collectors.toList());
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
type: specs.openrewrite.org/v1beta/recipe
name: io.jenkins.tools.pluginmodernizer.FetchMetadata
description: Extracts metadata from a Jenkins plugin
recipeList:
- io.jenkins.tools.pluginmodernizer.core.extractor.MetadataCollector
---
type: specs.openrewrite.org/v1beta/recipe
name: io.jenkins.tools.pluginmodernizer.AddPluginsBom
description: Adds a BOM to a Jenkins plugin
recipeList:
- org.openrewrite.jenkins.AddPluginsBom
---
type: specs.openrewrite.org/v1beta/recipe
name: io.jenkins.tools.pluginmodernizer.AddCodeOwner
description: Adds a CODEOWNERS file to a Jenkins plugin
recipeList:
- org.openrewrite.jenkins.github.AddTeamToCodeowners
12 changes: 0 additions & 12 deletions plugin-modernizer-core/src/main/resources/recipe_data.yaml

This file was deleted.

Loading