Add ChangeRepository recipe for Gradle build scripts#6785
Add ChangeRepository recipe for Gradle build scripts#6785Jenson3210 wants to merge 5 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.
rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeRepositoryTest.java
Outdated
Show resolved
Hide resolved
rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeRepository.java
Outdated
Show resolved
Hide resolved
rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeRepository.java
Show resolved
Hide resolved
| 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?
There was a problem hiding this comment.
I think that is definitely a possibility here.
Just wondering if we should do this in this PR or in a next one.
We have a customer usecase atm for this functionality where a trait might take a larger refactor/bigger PR/ longer throughput.
So thinking we could potentially split the Trait in a different PR and already release the recipe functionality. Internal kitchen of it can still change.
|
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. 🙂 |
- Use field-level = syntax for displayName/description per convention - Remove explicit removal mode (RemoveRepository already covers that) - Add validate() to ensure old != new and at least one of newType/newUrl - Update description and @option docs to not reference removal - Remove related removal tests; keep dedup removal tests
Verify that credentials, authentication blocks, and other repository properties (name, allowInsecureProtocol) are preserved when changing URLs within the same repository type.
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