Skip to content

Commit

Permalink
Merge pull request #528 from jonesbusy/feature/remove-release-drafter
Browse files Browse the repository at this point in the history
Add recipe to remove release drafter if cd is present
  • Loading branch information
jonesbusy authored Dec 29, 2024
2 parents 758a5a5 + 6410bd9 commit 135870b
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public enum ArchetypeCommonFile {
*/
RELEASE_DRAFTER(".github/release-drafter.yml"),

/**
* Release workflows
*/
RELEASE_DRAFTER_WORKFLOW(".github/workflows/release-drafter.yml"),

/**
* Pull request template file
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.jenkins.tools.pluginmodernizer.core.recipes;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.jenkins.tools.pluginmodernizer.core.extractor.ArchetypeCommonFile;
import java.util.concurrent.atomic.AtomicBoolean;
import org.openrewrite.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Remove release drafter if CD is in place (overlap of workflow)
*/
@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "No user input")
public class RemoveReleaseDrafter extends ScanningRecipe<AtomicBoolean> {

/**
* LOGGER.
*/
private static final Logger LOG = LoggerFactory.getLogger(RemoveReleaseDrafter.class);

@Override
public String getDisplayName() {
return "Remove release drafter if CD is in place";
}

@Override
public String getDescription() {
return "Remove the release drafter file if CD is in place.";
}

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

@Override
public TreeVisitor<?, ExecutionContext> getScanner(AtomicBoolean shouldRemove) {
return new TreeVisitor<>() {

@Override
public Tree visit(Tree tree, ExecutionContext ctx) {
SourceFile sourceFile = (SourceFile) tree;
if (sourceFile.getSourcePath().equals(ArchetypeCommonFile.WORKFLOW_CD.getPath())) {
LOG.info("Project is using CD. Need to remove release drafter.");
shouldRemove.set(true);
}
return tree;
}
};
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor(AtomicBoolean shouldRemove) {
return new TreeVisitor<>() {
@Override
public Tree visit(Tree tree, ExecutionContext ctx) {
LOG.info("Checking if release drafter should be removed");
if (shouldRemove.get() && tree instanceof SourceFile sourceFile) {
if (sourceFile.getSourcePath().equals(ArchetypeCommonFile.RELEASE_DRAFTER.getPath())) {
LOG.info("Deleting release drafter file: {}", sourceFile.getSourcePath());
return null;
}
if (sourceFile.getSourcePath().equals(ArchetypeCommonFile.RELEASE_DRAFTER_WORKFLOW.getPath())) {
LOG.info("Deleting release drafter workflow file: {}", sourceFile.getSourcePath());
return null;
}
}
return tree;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import io.jenkins.tools.pluginmodernizer.core.model.Plugin
@import io.jenkins.tools.pluginmodernizer.core.model.Recipe
@param Plugin plugin
@param Recipe recipe
Remove release drafter due to enabled cd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import io.jenkins.tools.pluginmodernizer.core.model.Plugin
@import io.jenkins.tools.pluginmodernizer.core.model.Recipe
@param Plugin plugin
@param Recipe recipe
Automate dependency updates with Dependabot
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,14 @@ recipeList:
- io.jenkins.tools.pluginmodernizer.core.recipes.SetupDependabot
---
type: specs.openrewrite.org/v1beta/recipe
name: io.jenkins.tools.pluginmodernizer.RemoveReleaseDrafter
displayName: Remove Release Drafter if CD is enabled
description: Remove Release Drafter if CD is enabled. See https://www.jenkins.io/doc/developer/publishing/releasing-cd/#configure-release-drafter.
tags: ['chore']
recipeList:
- io.jenkins.tools.pluginmodernizer.core.recipes.RemoveReleaseDrafter
---
type: specs.openrewrite.org/v1beta/recipe
name: io.jenkins.tools.pluginmodernizer.FixJellyIssues
displayName: Add XML declaration to Jelly files and create index.jelly if it doesn't exist
description: Ensure the XML declaration `<?jelly escape-by-default='true'?>` is present in all `.jelly` files and create index.jelly if it doesn't exist.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,20 @@ void migrateToJenkinsBaseLinePropertyTest() {
"""));
}

@Test
void shouldRemoveReleaseDrafterIfContinuousDeliveryEnabled() {
rewriteRun(
spec -> spec.recipeFromResource(
"/META-INF/rewrite/recipes.yml", "io.jenkins.tools.pluginmodernizer.RemoveReleaseDrafter"),
// language=yaml
yaml("{}", sourceSpecs -> {
sourceSpecs.path(ArchetypeCommonFile.WORKFLOW_CD.getPath());
}),
yaml("{}", null, sourceSpecs -> {
sourceSpecs.path(ArchetypeCommonFile.RELEASE_DRAFTER.getPath());
}));
}

/**
* Note this test need to be adapted to fix the dependabot config
* (For example to reduce frequency or increase frequency for API plugins)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.jenkins.tools.pluginmodernizer.core.recipes;

import static org.openrewrite.yaml.Assertions.yaml;

import io.jenkins.tools.pluginmodernizer.core.extractor.ArchetypeCommonFile;
import org.junit.jupiter.api.Test;
import org.openrewrite.test.RewriteTest;

/**
* Test for {@link RemoveReleaseDrafter}.
*/
public class RemoveReleaseDrafterTest implements RewriteTest {

@Test
void shouldNotRemoveReleaseDrafter() {
rewriteRun(
spec -> spec.recipe(new RemoveReleaseDrafter()),
// language=yaml
yaml("{}", sourceSpecs -> {
sourceSpecs.path(ArchetypeCommonFile.RELEASE_DRAFTER.getPath());
}),
yaml("{}", sourceSpecs -> {
sourceSpecs.path(ArchetypeCommonFile.RELEASE_DRAFTER_WORKFLOW.getPath());
}));
}

@Test
void shouldRemoveReleaseDrafterIfContinuousDeliveryEnabled() {
rewriteRun(
spec -> spec.recipe(new RemoveReleaseDrafter()),
// language=yaml
yaml("{}", sourceSpecs -> {
sourceSpecs.path(ArchetypeCommonFile.WORKFLOW_CD.getPath());
}),
yaml("{}", null, sourceSpecs -> {
sourceSpecs.path(ArchetypeCommonFile.RELEASE_DRAFTER.getPath());
}),
yaml("{}", null, sourceSpecs -> {
sourceSpecs.path(ArchetypeCommonFile.RELEASE_DRAFTER_WORKFLOW.getPath());
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import org.junit.jupiter.api.Test;
import org.openrewrite.test.RewriteTest;

/**
* Test for {@link SetupDependabot}.
*/
public class SetupDependabotTest implements RewriteTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,44 @@ public void testFriendlyPrTitleMigrateToJenkinsBaseLineProperty() {
assertEquals(
"Update pom.xml to match archetype and use `jenkins.baseline` property to keep bom in sync", result);
}

@Test
public void testFriendlyPrTitleSetupDependabot() {

// Mocks
Plugin plugin = mock(Plugin.class);
PluginMetadata metadata = mock(PluginMetadata.class);
Recipe recipe = mock(Recipe.class);

doReturn(metadata).when(plugin).getMetadata();
doReturn("io.jenkins.tools.pluginmodernizer.SetupDependabot")
.when(recipe)
.getName();

// Test
String result = TemplateUtils.renderPullRequestTitle(plugin, recipe);

// Assert
assertEquals("Automate dependency updates with Dependabot", result);
}

@Test
public void testFriendlyPrTitleRemoveReleaseDrafter() {

// Mocks
Plugin plugin = mock(Plugin.class);
PluginMetadata metadata = mock(PluginMetadata.class);
Recipe recipe = mock(Recipe.class);

doReturn(metadata).when(plugin).getMetadata();
doReturn("io.jenkins.tools.pluginmodernizer.RemoveReleaseDrafter")
.when(recipe)
.getName();

// Test
String result = TemplateUtils.renderPullRequestTitle(plugin, recipe);

// Assert
assertEquals("Remove release drafter due to enabled cd", result);
}
}

0 comments on commit 135870b

Please sign in to comment.