Skip to content

Commit

Permalink
Merge pull request #222 from jonesbusy/feature/static-flag
Browse files Browse the repository at this point in the history
Save precondition errors on metadata
  • Loading branch information
jonesbusy authored Aug 25, 2024
2 parents 18c44b7 + b816315 commit a22c6f1
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.openrewrite.ExecutionContext;
Expand Down Expand Up @@ -59,8 +60,8 @@ public String getDescription() {
public static class MetadataAccumulator {
private final List<ArchetypeCommonFile> commonFiles = new ArrayList<>();
private final List<String> otherFiles = new ArrayList<>();
private final List<MetadataFlag> flags = new LinkedList<>();
private final List<JDK> jdkVersions = new ArrayList<>();
private final Set<MetadataFlag> flags = new HashSet<>();
private final Set<JDK> jdkVersions = new HashSet<>();

public List<ArchetypeCommonFile> getCommonFiles() {
return commonFiles;
Expand All @@ -70,7 +71,7 @@ public List<String> getOtherFiles() {
return otherFiles;
}

public List<JDK> getJdkVersions() {
public Set<JDK> getJdkVersions() {
return jdkVersions;
}

Expand All @@ -86,7 +87,7 @@ public void addJdk(JDK jdk) {
jdkVersions.add(jdk);
}

public List<MetadataFlag> getFlags() {
public Set<MetadataFlag> getFlags() {
return flags;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import io.jenkins.tools.pluginmodernizer.core.model.CacheEntry;
import io.jenkins.tools.pluginmodernizer.core.model.JDK;
import io.jenkins.tools.pluginmodernizer.core.model.Plugin;
import io.jenkins.tools.pluginmodernizer.core.model.PreconditionError;
import java.io.Serializable;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Metadata of a plugin extracted from its POM file or code
Expand All @@ -22,7 +24,12 @@ public class PluginMetadata extends CacheEntry<PluginMetadata> implements Serial
/**
* List of flags present in the plugin
*/
private List<MetadataFlag> flags;
private Set<MetadataFlag> flags;

/**
* List of errors present in the plugin
*/
private Set<PreconditionError> errors;

/**
* List of well known files present in the plugin
Expand All @@ -37,7 +44,7 @@ public class PluginMetadata extends CacheEntry<PluginMetadata> implements Serial
/**
* JDK versions supported by the plugin
*/
private List<JDK> jdkVersions;
private Set<JDK> jdkVersions;

/**
* Jenkins version required by the plugin
Expand Down Expand Up @@ -91,18 +98,26 @@ public void setPluginName(String pluginName) {
this.pluginName = pluginName;
}

public List<MetadataFlag> getFlags() {
public Set<MetadataFlag> getFlags() {
return flags;
}

public void setFlags(List<MetadataFlag> flags) {
public void setFlags(Set<MetadataFlag> flags) {
this.flags = flags;
}

public boolean hasFlag(MetadataFlag flag) {
return flags.contains(flag);
}

public Set<PreconditionError> getErrors() {
return errors;
}

public void setErrors(Set<PreconditionError> errors) {
this.errors = errors;
}

public List<ArchetypeCommonFile> getCommonFiles() {
return commonFiles;
}
Expand All @@ -119,11 +134,11 @@ public void setOtherFiles(List<String> otherFiles) {
this.otherFiles = otherFiles;
}

public List<JDK> getJdks() {
public Set<JDK> getJdks() {
return jdkVersions;
}

public void setJdks(List<JDK> jdkVersions) {
public void setJdks(Set<JDK> jdkVersions) {
this.jdkVersions = jdkVersions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import io.jenkins.tools.pluginmodernizer.core.utils.UpdateCenterUtils;
import java.nio.file.Path;
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 @@ -53,7 +55,8 @@ public void start() {

// Debug config
LOG.debug("Plugins: {}", config.getPlugins());
LOG.debug("Recipes: {}", config.getRecipes());
LOG.debug(
"Recipes: {}", config.getRecipes().stream().map(Recipe::getName).collect(Collectors.joining(", ")));
LOG.debug("GitHub owner: {}", config.getGithubOwner());
LOG.debug("Update Center Url: {}", config.getJenkinsUpdateCenter());
LOG.debug("Cache Path: {}", config.getCachePath());
Expand Down Expand Up @@ -107,22 +110,18 @@ private void process(Plugin plugin) {
// Compile only if we are able to find metadata
// For the moment it's local cache only but later will fetch on remote storage
if (!config.isFetchMetadataOnly()) {
if (plugin.getMetadata() != null) {
if (plugin.getMetadata() != null && !plugin.hasPreconditionErrors()) {
JDK jdk = compilePlugin(plugin);
LOG.info("Plugin {} compiled successfully with JDK {}", plugin.getName(), jdk.getMajor());
} else {
LOG.info("No metadata found for plugin {}. Skipping initial compilation.", plugin.getName());
LOG.info(
"No metadata or precondition errors found for plugin {}. Skipping initial compilation.",
plugin.getName());
}
}

plugin.checkoutBranch(ghService);

// Ensure minimum baseline of JDK 8.
// For the moment some plugin cannot be refreshed due to some condition
// (Non HTTPS URL, java 7 build). For those plugin we will fail until we find a solution
LOG.info("Checking if plugin {} can be modernized", plugin.getName());
plugin.ensureMinimalBuild(mavenInvoker);

// Minimum JDK to run openrewrite
plugin.withJDK(JDK.JAVA_17);

Expand All @@ -131,8 +130,7 @@ private void process(Plugin plugin) {

plugin.collectMetadata(mavenInvoker);

CacheManager pluginCacheManager = new CacheManager(Path.of(Settings.TEST_PLUGINS_DIRECTORY)
.resolve(plugin.getLocalRepository().resolve("target")));
CacheManager pluginCacheManager = plugin.buildPluginTargetDirectoryCacheManager();
plugin.setMetadata(pluginCacheManager.move(
cacheManager,
Path.of(plugin.getName()),
Expand All @@ -146,6 +144,15 @@ private void process(Plugin plugin) {
LOG.debug("Metadata already computed for plugin {}. Using cached metadata.", plugin.getName());
}

// Abort here if we have errors
if (plugin.hasErrors() || plugin.hasPreconditionErrors()) {
plugin.addPreconditionErrors(plugin.getMetadata());
LOG.info(
"Skipping plugin {} due to metadata/precondition errors. Check logs for more details.",
plugin.getName());
return;
}

// Run OpenRewrite
plugin.runOpenRewrite(mavenInvoker);
if (plugin.hasErrors()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ public final T move(CacheManager newCacheManager, Path newPath, String newKey) {
*/
public final void save() {
LOG.debug(
"Saving object to {}", cacheManager.getLocation().resolve(path).resolve(key));
"Saving object to {}",
cacheManager.getLocation().resolve(path).resolve(key).toAbsolutePath());
cacheManager.put(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import org.apache.maven.artifact.versioning.ComparableVersion;

/**
Expand Down Expand Up @@ -213,7 +214,7 @@ public static JDK min() {
* @param jdks List of JDKS. Can be null or empty
* @return The minimum JDK. If the list is empty, return the minimum JDK available
*/
public static JDK min(List<JDK> jdks) {
public static JDK min(Set<JDK> jdks) {
if (jdks == null || jdks.isEmpty()) {
return JDK.min();
}
Expand Down
Loading

0 comments on commit a22c6f1

Please sign in to comment.