Skip to content

Commit e365d9b

Browse files
Passing REWRITE_GRADLE_MIRROR_* vars to Gradle test builds
1 parent 555ea17 commit e365d9b

2 files changed

Lines changed: 62 additions & 4 deletions

File tree

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ jobs:
3939
servers: '[{"id": "moderne-cache", "username": "${{ secrets.ARTIFACTORY_USERNAME }}", "password": "${{ secrets.ARTIFACTORY_PASSWORD }}"}]'
4040

4141
- uses: openrewrite/gh-automation/.github/actions/build@main
42+
env:
43+
REWRITE_GRADLE_MIRROR_URL: https://artifactory.moderne.ninja/artifactory/moderne-cache-3/
44+
REWRITE_GRADLE_MIRROR_USERNAME: ${{ secrets.ARTIFACTORY_USERNAME }}
45+
REWRITE_GRADLE_MIRROR_PASSWORD: ${{ secrets.ARTIFACTORY_PASSWORD }}
4246

4347
- if: failure() && github.event_name == 'schedule' && (github.repository_owner == 'openrewrite' || github.repository_owner == 'moderneinc')
4448
uses: openrewrite/gh-automation/.github/actions/slack-failure@main

rewrite-gradle-tooling-model/model/src/main/java/org/openrewrite/gradle/toolingapi/OpenRewriteModelBuilder.java

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,23 +119,25 @@ public static OpenRewriteModel forProjectDirectory(File projectDir, @Nullable Fi
119119
}
120120
try (ProjectConnection connection = connector.connect()) {
121121
ModelBuilder<OpenRewriteModelProxy> customModelBuilder = connection.model(OpenRewriteModelProxy.class);
122+
String resolvedInitScript;
122123
if (initScript == null) {
123124
if (System.getProperty("org.openrewrite.gradle.local.use-embedded-classpath") != null) {
124125
// code path only expected to be taken from within openrewrite/rewrite
125-
String generatedInitScript = generateInitScriptFromManifest();
126-
Files.write(init, generatedInitScript.getBytes(StandardCharsets.UTF_8));
126+
resolvedInitScript = generateInitScriptFromManifest();
127127
} else {
128128
// Use default init.gradle from resources
129129
try (InputStream is = OpenRewriteModel.class.getResourceAsStream("/init.gradle")) {
130130
if (is == null) {
131131
throw new IllegalStateException("Expected to find init.gradle on the classpath");
132132
}
133-
Files.copy(is, init);
133+
byte[] bytes = readAllBytes(is);
134+
resolvedInitScript = new String(bytes, StandardCharsets.UTF_8);
134135
}
135136
}
136137
} else {
137-
Files.write(init, initScript.getBytes());
138+
resolvedInitScript = initScript;
138139
}
140+
Files.write(init, (resolvedInitScript + mirrorScriptSnippet()).getBytes(StandardCharsets.UTF_8));
139141
customModelBuilder.withArguments(arguments);
140142
return OpenRewriteModel.from(customModelBuilder.get());
141143
} finally {
@@ -153,6 +155,58 @@ public static OpenRewriteModel forProjectDirectory(File projectDir, @Nullable Fi
153155
}
154156
}
155157

158+
/**
159+
* When the env vars {@code REWRITE_GRADLE_MIRROR_URL}, {@code REWRITE_GRADLE_MIRROR_USERNAME}, and
160+
* {@code REWRITE_GRADLE_MIRROR_PASSWORD} are all set, returns a Groovy init-script fragment that
161+
* routes plugin and dependency resolution for the embedded Gradle build through that repository.
162+
* If any var is missing, returns the empty string and the embedded Gradle resolves repositories
163+
* exactly as the build files declare.
164+
*/
165+
private static String mirrorScriptSnippet() {
166+
String url = System.getenv("REWRITE_GRADLE_MIRROR_URL");
167+
String user = System.getenv("REWRITE_GRADLE_MIRROR_USERNAME");
168+
String pass = System.getenv("REWRITE_GRADLE_MIRROR_PASSWORD");
169+
if (url == null || user == null || pass == null || url.isEmpty() || user.isEmpty() || pass.isEmpty()) {
170+
return "";
171+
}
172+
return "\n\n" +
173+
"def __rewriteMirrorUrl = '" + escapeGroovy(url) + "'\n" +
174+
"def __rewriteMirrorUser = '" + escapeGroovy(user) + "'\n" +
175+
"def __rewriteMirrorPass = '" + escapeGroovy(pass) + "'\n" +
176+
"def __rewriteConfigureMirror = { container ->\n" +
177+
" container.clear()\n" +
178+
" container.maven {\n" +
179+
" url = __rewriteMirrorUrl\n" +
180+
" credentials {\n" +
181+
" username = __rewriteMirrorUser\n" +
182+
" password = __rewriteMirrorPass\n" +
183+
" }\n" +
184+
" }\n" +
185+
"}\n" +
186+
"allprojects {\n" +
187+
" buildscript.repositories { __rewriteConfigureMirror(delegate) }\n" +
188+
" repositories { __rewriteConfigureMirror(delegate) }\n" +
189+
"}\n" +
190+
"settingsEvaluated { settings ->\n" +
191+
" settings.pluginManagement.repositories { __rewriteConfigureMirror(delegate) }\n" +
192+
" try { settings.dependencyResolutionManagement.repositories { __rewriteConfigureMirror(delegate) } } catch (Throwable ignored) {}\n" +
193+
"}\n";
194+
}
195+
196+
private static String escapeGroovy(String s) {
197+
return s.replace("\\", "\\\\").replace("'", "\\'");
198+
}
199+
200+
private static byte[] readAllBytes(InputStream in) throws IOException {
201+
java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
202+
byte[] buf = new byte[8192];
203+
int n;
204+
while ((n = in.read(buf)) != -1) {
205+
out.write(buf, 0, n);
206+
}
207+
return out.toByteArray();
208+
}
209+
156210
private static boolean isGradle9OrLater(@Nullable String version) {
157211
if (version == null) {
158212
return false;

0 commit comments

Comments
 (0)