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

Check Jenkinsfile presence #43

Merged
merged 2 commits into from
Jun 24, 2024
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,15 +1,21 @@
package io.jenkins.tools.pluginmodernizer.core.extractor;

import static java.util.Objects.requireNonNull;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

import com.google.gson.Gson;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.ScanningRecipe;
import org.openrewrite.SourceFile;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.marker.Markers;
import org.openrewrite.maven.MavenIsoVisitor;
import org.openrewrite.maven.tree.MavenResolutionResult;
Expand All @@ -19,24 +25,48 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PomParser extends Recipe {
public class MetadataCollector extends ScanningRecipe<MetadataCollector.Metadata> {

private static final Logger LOG = LoggerFactory.getLogger(PomParser.class);
private static final Logger LOG = LoggerFactory.getLogger(MetadataCollector.class);

PluginMetadata pluginMetadata = PluginMetadata.getInstance();

@Override
public String getDisplayName() {
return "Pom Parser";
return "Plugin metadata extractor";
}

@Override
public String getDescription() {
return "Extracts Metadata from pom file.";
return "Extracts metadata from plugin.";
}

public static class Metadata {
boolean hasJenkinsfile = false;
}

@Override
public Metadata getInitialValue(ExecutionContext ctx) {
return new Metadata();
}

@Override
public TreeVisitor<?, ExecutionContext> getScanner(Metadata acc) {
return new TreeVisitor<Tree, ExecutionContext>() {
@SuppressFBWarnings(value = "NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE", justification = "false postive")
@Override
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
SourceFile sourceFile = (SourceFile) requireNonNull(tree);
if (sourceFile.getSourcePath().endsWith("Jenkinsfile")) {
acc.hasJenkinsfile = true;
}
return tree;
}
};
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
public TreeVisitor<?, ExecutionContext> getVisitor(Metadata acc) {
return new MavenIsoVisitor<>() {
@Override
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
Expand All @@ -61,6 +91,7 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
pluginMetadata.setHasDevelopersTag(tagExtractor.hasDevelopersTag());
pluginMetadata.setLicensed(!pom.getLicenses().isEmpty());
pluginMetadata.setUsesHttps(tagExtractor.usesHttps());
pluginMetadata.setHasJenkinsfile(acc.hasJenkinsfile);

try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("target/pluginMetadata.json"), StandardCharsets.UTF_8)) {
Gson gson = new Gson();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class PluginMetadata {
private boolean hasDevelopersTag;
private boolean hasJavaLevel;
private boolean usesHttps;
private boolean hasJenkinsfile;
private List<Dependency> dependencies;
private String jenkinsVersion;
private Parent pluginParent;
Expand Down Expand Up @@ -72,6 +73,14 @@ public void setUsesHttps(boolean usesHttps) {
this.usesHttps = usesHttps;
}

public boolean hasJenkinsfile() {
return hasJenkinsfile;
}

public void setHasJenkinsfile(boolean hasJenkinsfile) {
this.hasJenkinsfile = hasJenkinsfile;
}

public List<Dependency> getDependencies() {
return dependencies;
}
Expand Down
4 changes: 2 additions & 2 deletions plugin-modernizer-core/src/main/resources/recipe_data.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
recipes:
PomParser:
fqcn: "io.jenkins.tools.pluginmodernizer.core.extractor.PomParser"
FetchMetadata:
fqcn: "io.jenkins.tools.pluginmodernizer.core.extractor.MetadataCollector"
artifactCoordinates: "io.jenkins.plugin-modernizer:plugin-modernizer-core:999999-SNAPSHOT"
AddPluginsBom:
fqcn: "org.openrewrite.jenkins.AddPluginsBom"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@



public class PomParserTest implements RewriteTest {
public class MetadataCollectorTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new PomParser());
spec.recipe(new MetadataCollector());
}

@Test
Expand Down
Loading