diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f5f05691ed8..0da460b7982 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,18 +20,40 @@ concurrency: jobs: build: - uses: openrewrite/gh-automation/.github/workflows/ci-gradle.yml@main - with: - java_version: | - 25 - 21 - secrets: - gradle_enterprise_access_key: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }} - sonatype_username: ${{ secrets.SONATYPE_USERNAME }} - sonatype_token: ${{ secrets.SONATYPE_TOKEN}} - ossrh_signing_key: ${{ secrets.OSSRH_SIGNING_KEY }} - ossrh_signing_password: ${{ secrets.OSSRH_SIGNING_PASSWORD }} - OPS_GITHUB_ACTIONS_WEBHOOK: ${{ secrets.OPS_GITHUB_ACTIONS_WEBHOOK }} - node_auth_token: ${{ secrets.NPM_TOKEN }} - pypi_token: ${{ secrets.PYPI_OPENREWRITE_PUBLISH }} - nuget_api_key: ${{ secrets.NUGET_API_KEY }} + runs-on: ubuntu-latest + if: github.event_name != 'schedule' || github.repository_owner == 'openrewrite' || github.repository_owner == 'moderneinc' + steps: + - uses: openrewrite/gh-automation/.github/actions/setup@main + with: + java_version: | + 25 + 21 + develocity_access_key: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }} + + # Route Maven resolution through Moderne's Artifactory cache to avoid + # Maven Central rate-limiting (HTTP 404 + Retry-After) under parallel + # test load. Picked up by MavenSettingsAutoLoadingExtension at test time. + - uses: s4u/maven-settings-action@v3.1.0 + with: + mirrors: '[{"id": "moderne-cache", "name": "Moderne Artifactory Cache", "mirrorOf": "*", "url": "https://artifactory.moderne.ninja/artifactory/moderne-cache-3/"}]' + + - uses: openrewrite/gh-automation/.github/actions/build@main + + - if: failure() && github.event_name == 'schedule' && (github.repository_owner == 'openrewrite' || github.repository_owner == 'moderneinc') + uses: openrewrite/gh-automation/.github/actions/slack-failure@main + with: + webhook: ${{ secrets.OPS_GITHUB_ACTIONS_WEBHOOK }} + + - if: > + github.event_name != 'pull_request' && + github.ref == 'refs/heads/main' && + (github.repository_owner == 'openrewrite' || github.repository_owner == 'moderneinc') + uses: openrewrite/gh-automation/.github/actions/publish-snapshots@main + with: + sonatype_username: ${{ secrets.SONATYPE_USERNAME }} + sonatype_token: ${{ secrets.SONATYPE_TOKEN }} + ossrh_signing_key: ${{ secrets.OSSRH_SIGNING_KEY }} + ossrh_signing_password: ${{ secrets.OSSRH_SIGNING_PASSWORD }} + node_auth_token: ${{ secrets.NPM_TOKEN }} + pypi_token: ${{ secrets.PYPI_OPENREWRITE_PUBLISH }} + nuget_api_key: ${{ secrets.NUGET_API_KEY }} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/MavenSettingsAutoLoadingExtension.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/MavenSettingsAutoLoadingExtension.java new file mode 100644 index 00000000000..6a4b588759e --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/MavenSettingsAutoLoadingExtension.java @@ -0,0 +1,55 @@ +/* + * Copyright 2026 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle; + +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.openrewrite.ExecutionContext; +import org.openrewrite.maven.MavenExecutionContextView; +import org.openrewrite.maven.MavenSettings; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.maven.tree.MavenRepository.MAVEN_LOCAL_DEFAULT; + +/** + * Loads {@code ~/.m2/settings.xml} (and {@code $M2_HOME/conf/settings.xml}) into the + * default {@link ExecutionContext} for every {@link RewriteTest} in this module, so a + * configured Maven mirror or repository is honored by recipes that resolve artifacts. + * Auto-registered via {@code META-INF/services/org.junit.jupiter.api.extension.Extension}; + * Gradle does not normally read the user's Maven settings, so this is opt-in at the + * module test classpath level. + */ +public class MavenSettingsAutoLoadingExtension implements BeforeAllCallback { + + @Override + public void beforeAll(ExtensionContext context) { + RewriteTest.defaultExecutionContextCustomizers.putIfAbsent( + MavenSettingsAutoLoadingExtension.class, + MavenSettingsAutoLoadingExtension::loadMavenSettings); + } + + private static void loadMavenSettings(ExecutionContext ctx) { + MavenExecutionContextView mctx = MavenExecutionContextView.view(ctx); + boolean nothingConfigured = mctx.getSettings() == null && + mctx.getLocalRepository().equals(MAVEN_LOCAL_DEFAULT) && + mctx.getRepositories().isEmpty() && + mctx.getActiveProfiles().isEmpty() && + mctx.getMirrors().isEmpty(); + if (nothingConfigured) { + mctx.setMavenSettings(MavenSettings.readMavenSettingsFromDisk(mctx)); + } + } +} diff --git a/rewrite-gradle/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension b/rewrite-gradle/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension new file mode 100644 index 00000000000..e2edebd662d --- /dev/null +++ b/rewrite-gradle/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension @@ -0,0 +1 @@ +org.openrewrite.gradle.MavenSettingsAutoLoadingExtension diff --git a/rewrite-gradle/src/test/resources/junit-platform.properties b/rewrite-gradle/src/test/resources/junit-platform.properties new file mode 100644 index 00000000000..82e077a0bdb --- /dev/null +++ b/rewrite-gradle/src/test/resources/junit-platform.properties @@ -0,0 +1,17 @@ +# +# Copyright 2026 the original author or authors. +#

+# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +#

+# https://www.apache.org/licenses/LICENSE-2.0 +#

+# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +junit.jupiter.extensions.autodetection.enabled=true diff --git a/rewrite-test/src/main/java/org/openrewrite/test/RewriteTest.java b/rewrite-test/src/main/java/org/openrewrite/test/RewriteTest.java index c5d8264e5eb..e68ec8542b2 100644 --- a/rewrite-test/src/main/java/org/openrewrite/test/RewriteTest.java +++ b/rewrite-test/src/main/java/org/openrewrite/test/RewriteTest.java @@ -35,6 +35,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Consumer; import java.util.function.Function; @@ -51,6 +52,18 @@ @SuppressWarnings("unused") public interface RewriteTest extends SourceSpecs { + /** + * Registry of customizers applied to the {@link ExecutionContext} produced by + * {@link #defaultExecutionContext(SourceSpec[])}. Test framework integrations + * (e.g. a JUnit Jupiter {@code BeforeAllCallback}) can register a customizer + * once at startup without {@code rewrite-test} taking a dependency on the + * framework. + *

+ * The map is keyed by the integration's class so {@code putIfAbsent(MyExt.class, MyExt::load)} + * is naturally idempotent — callers do not need their own one-shot guard. + */ + Map, Consumer> defaultExecutionContextCustomizers = new ConcurrentHashMap<>(); + static AdHocRecipe toRecipe(Supplier> visitor) { return new AdHocRecipe(null, null, null, visitor, null, null); } @@ -696,6 +709,9 @@ default ExecutionContext defaultExecutionContext(SourceSpec[] sourceSpecs) { } fail("Failed to parse sources or run recipe", t); }); + for (Consumer customizer : defaultExecutionContextCustomizers.values()) { + customizer.accept(ctx); + } return ParsingExecutionContextView.view(ctx).setCharset(StandardCharsets.UTF_8); }