Skip to content
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
14 changes: 14 additions & 0 deletions src/main/java/org/openrewrite/github/AddDependabotCooldown.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ public class AddDependabotCooldown extends Recipe {
@Nullable
List<String> exclude;

@Option(displayName = "Exclude ecosystems",
description = "List of ecosystems to be excluded",
example = "github-actions",
required = false)
@Nullable
List<String> excludeEcosystems;

String displayName = "Add cooldown periods to Dependabot configuration";

String description = "Adds a `cooldown` section to each update configuration in Dependabot files. " +
Expand Down Expand Up @@ -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()));
Expand Down
134 changes: 124 additions & 10 deletions src/test/java/org/openrewrite/github/AddDependabotCooldownTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
Expand Down Expand Up @@ -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(
"""
Expand Down Expand Up @@ -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(
"""
Expand All @@ -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(
"""
Expand Down Expand Up @@ -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(
"""
Expand Down Expand Up @@ -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(
"""
Expand Down Expand Up @@ -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(
"""
Expand Down Expand Up @@ -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(
"""
Expand Down Expand Up @@ -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(
"""
Expand Down Expand Up @@ -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(
"""
Expand Down Expand Up @@ -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")
)
);
}
}
Loading