Add ChangeRepository recipe for Gradle build scripts#6785
Add ChangeRepository recipe for Gradle build scripts#6785Jenson3210 wants to merge 3 commits intomainfrom
Conversation
Adds a new recipe to replace or remove repository declarations in build.gradle and build.gradle.kts files, enabling organizations to standardize repository usage. Supports all combinations: named-to-named (jcenter to mavenCentral), named-to-custom-maven, custom-maven-to-named, and URL changes. Automatically deduplicates when the target repository already exists. All four parameters (oldType, oldUrl, newType, newUrl) are optional, allowing matching by type, URL, or both, and either replacing or removing the matched repository.
When newType is null but newUrl is set, keep the matched repository's type and only change the URL. Previously this would incorrectly enter remove mode. Also refactored to use effectiveNewType throughout the visitor to eliminate null checks on newType.
| } | ||
|
|
||
| @Test | ||
| void removeNamedRepository() { |
There was a problem hiding this comment.
This feels a little weird for a "Change" recipe to be removing entries. We do also already have RemoveRepository, so maybe remove isn't necessary for this recipe?
I think changing to something that is already present is fine to trigger a removal for though, so as to not lead to a duplicate definition.
| @Override | ||
| public String getDisplayName() { | ||
| return "Change repository"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Replace or remove a repository in Gradle build scripts to standardize repository usage across an organization. " + | ||
| "When no new type or URL is specified, the matching repository will be removed."; | ||
| } |
There was a problem hiding this comment.
nit: For these we should utilize the = syntax that was adopted in other recipes.
|
|
||
| @Value | ||
| @EqualsAndHashCode(callSuper = false) | ||
| public class ChangeRepository extends Recipe { |
There was a problem hiding this comment.
callout: We should add a validation to check that old and new repositories are not the same.
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| MethodMatcher repoMatcher = new MethodMatcher("org.gradle.api.artifacts.dsl.RepositoryHandler " + (oldType != null ? oldType : "*") + "(..)", true); | ||
|
|
||
| return Preconditions.check(new IsBuildGradle<>(), new JavaIsoVisitor<ExecutionContext>() { |
There was a problem hiding this comment.
optional: We have a couple of recipes now that interact with repositories. Should we consider wrapping the discovery portion of this up into a Trait instead?
|
Thanks for the early review @shanman190! Was a draft as I did not look at CC's output before heading into the weekend. Will take this into account when asking him to "do more"! 🙏 |
|
No worries at all; I figured that I'd just write up some comments to help out. 🙂 |
Summary
ChangeRepositoryrecipe to replace or remove repository declarations inbuild.gradleandbuild.gradle.ktsfilesjcentertomavenCentral, consolidate internal Nexus URLs)Solution
All four parameters are optional, supporting flexible matching and replacement:
oldTypejcenter,maven, etc.). Omit to match any type.oldUrlnewTypenewUrl) to remove the match.newUrlSupported transformations:
jcenter()→mavenCentral()jcenter()→maven { url = "https://..." }maven { url = "..." }→mavenCentral()Example usage for org-wide uniformity
Test plan