diff --git a/src/main/java/org/openrewrite/github/AddDependabotCooldown.java b/src/main/java/org/openrewrite/github/AddDependabotCooldown.java index 9587b78..d58523d 100644 --- a/src/main/java/org/openrewrite/github/AddDependabotCooldown.java +++ b/src/main/java/org/openrewrite/github/AddDependabotCooldown.java @@ -84,6 +84,13 @@ public class AddDependabotCooldown extends Recipe { @Nullable List exclude; + @Option(displayName = "Exclude ecosystems", + description = "List of ecosystems to be excluded", + example = "github-actions", + required = false) + @Nullable + List excludeEcosystems; + String displayName = "Add cooldown periods to Dependabot configuration"; String description = "Adds a `cooldown` section to each update configuration in Dependabot files. " + @@ -148,6 +155,13 @@ public Yaml.Mapping visitMapping(Yaml.Mapping mapping, ExecutionContext ctx) { Yaml.Mapping m = super.visitMapping(mapping, ctx); if (Boolean.TRUE.equals(getCursor().pollMessage("ADD_COOLDOWN"))) { + final boolean ecosystemIsExcluded = excludeEcosystems != null && m.getEntries() + .stream() + .anyMatch(entry -> "package-ecosystem".equals(entry.getKey().getValue()) + && entry.getValue() instanceof Yaml.Scalar && excludeEcosystems.contains(((Yaml.Scalar)entry.getValue()).getValue())); + if (ecosystemIsExcluded) { + return m; + } // Check if cooldown already exists boolean hasCooldown = m.getEntries().stream() .anyMatch(entry -> "cooldown".equals(entry.getKey().getValue())); diff --git a/src/test/java/org/openrewrite/github/AddDependabotCooldownTest.java b/src/test/java/org/openrewrite/github/AddDependabotCooldownTest.java index 20bcd58..b4e9591 100644 --- a/src/test/java/org/openrewrite/github/AddDependabotCooldownTest.java +++ b/src/test/java/org/openrewrite/github/AddDependabotCooldownTest.java @@ -29,7 +29,7 @@ class AddDependabotCooldownTest implements RewriteTest { @Test void addCooldownWithDefaultDays() { rewriteRun( - spec -> spec.recipe(new AddDependabotCooldown(null, null, null, null, null, null)), + spec -> spec.recipe(new AddDependabotCooldown(null, null, null, null, null, null, null)), //language=yaml yaml( """ @@ -68,7 +68,7 @@ void addCooldownWithDefaultDays() { @Test void addCooldownWithCustomDays() { rewriteRun( - spec -> spec.recipe(new AddDependabotCooldown(14, null, null, null, null, null)), + spec -> spec.recipe(new AddDependabotCooldown(14, null, null, null, null, null, null)), //language=yaml yaml( """ @@ -97,7 +97,7 @@ void addCooldownWithCustomDays() { @Test void cooldownAlreadyExists() { rewriteRun( - spec -> spec.recipe(new AddDependabotCooldown(7, null, null, null, null, null)), + spec -> spec.recipe(new AddDependabotCooldown(7, null, null, null, null, null, null)), //language=yaml yaml( """ @@ -118,7 +118,7 @@ void cooldownAlreadyExists() { @Test void multipleEcosystemsWithExistingOptions() { rewriteRun( - spec -> spec.recipe(new AddDependabotCooldown(null, null, null, null, null, null)), + spec -> spec.recipe(new AddDependabotCooldown(null, null, null, null, null, null, null)), //language=yaml yaml( """ @@ -163,7 +163,7 @@ void multipleEcosystemsWithExistingOptions() { @Test void worksWithDependabotYamlExtension() { rewriteRun( - spec -> spec.recipe(new AddDependabotCooldown(null, null, null, null, null, null)), + spec -> spec.recipe(new AddDependabotCooldown(null, null, null, null, null, null, null)), //language=yaml yaml( """ @@ -192,7 +192,7 @@ void worksWithDependabotYamlExtension() { @Test void addsSemverSpecificCooldowns() { rewriteRun( - spec -> spec.recipe(new AddDependabotCooldown(7, 14, 7, 3, null, null)), + spec -> spec.recipe(new AddDependabotCooldown(7, 14, 7, 3, null, null, null)), //language=yaml yaml( """ @@ -225,7 +225,7 @@ void addsSemverSpecificCooldowns() { void addsIncludeList() { rewriteRun( spec -> spec.recipe(new AddDependabotCooldown(7, null, null, null, - List.of("lodash", "react*"), null)), + List.of("lodash", "react*"), null, null)), //language=yaml yaml( """ @@ -258,7 +258,7 @@ void addsIncludeList() { void addsExcludeList() { rewriteRun( spec -> spec.recipe(new AddDependabotCooldown(7, null, null, null, - null, List.of("critical-security-package"))), + null, List.of("critical-security-package"), null)), //language=yaml yaml( """ @@ -291,7 +291,7 @@ void addsAllCooldownOptions() { rewriteRun( spec -> spec.recipe(new AddDependabotCooldown(7, 14, 7, 3, List.of("lodash", "express"), - List.of("security-lib"))), + List.of("security-lib"), null)), //language=yaml yaml( """ @@ -328,7 +328,7 @@ void addsAllCooldownOptions() { @Test void addsToMultipleEcosystemsWithDifferentConfigurations() { rewriteRun( - spec -> spec.recipe(new AddDependabotCooldown(7, 14, null, null, null, null)), + spec -> spec.recipe(new AddDependabotCooldown(7, 14, null, null, null, null, null)), //language=yaml yaml( """ @@ -376,4 +376,118 @@ void addsToMultipleEcosystemsWithDifferentConfigurations() { ) ); } + + @Test + void addCooldownWithExcludeOneEcosystem() { + rewriteRun( + spec -> spec.recipe(new AddDependabotCooldown(9, null, null, + null, null, null, List.of("npm"))), + //language=yaml + yaml( + """ + version: 2 + updates: + - package-ecosystem: github-actions + directory: / + schedule: + interval: daily + - package-ecosystem: npm + directory: / + schedule: + interval: weekly + - package-ecosystem: pip + directory: / + schedule: + interval: daily + - package-ecosystem: gradle + directory: / + schedule: + interval: monthly + """, + """ + version: 2 + updates: + - package-ecosystem: github-actions + directory: / + schedule: + interval: daily + cooldown: + default-days: 9 + - package-ecosystem: npm + directory: / + schedule: + interval: weekly + - package-ecosystem: pip + directory: / + schedule: + interval: daily + cooldown: + default-days: 9 + - package-ecosystem: gradle + directory: / + schedule: + interval: monthly + cooldown: + default-days: 9 + """, + spec -> spec.path(".github/dependabot.yml") + ) + ); + } + + @Test + void addCooldownWithExcludeManyEcosystems() { + rewriteRun( + spec -> spec.recipe(new AddDependabotCooldown(11, null, null, + null, null, null, List.of("npm", "github-actions"))), + //language=yaml + yaml( + """ + version: 2 + updates: + - package-ecosystem: github-actions + directory: / + schedule: + interval: daily + - package-ecosystem: npm + directory: / + schedule: + interval: weekly + - package-ecosystem: pip + directory: / + schedule: + interval: daily + - package-ecosystem: gradle + directory: / + schedule: + interval: monthly + """, + """ + version: 2 + updates: + - package-ecosystem: github-actions + directory: / + schedule: + interval: daily + - package-ecosystem: npm + directory: / + schedule: + interval: weekly + - package-ecosystem: pip + directory: / + schedule: + interval: daily + cooldown: + default-days: 11 + - package-ecosystem: gradle + directory: / + schedule: + interval: monthly + cooldown: + default-days: 11 + """, + spec -> spec.path(".github/dependabot.yml") + ) + ); + } }