Skip to content

Commit 47c3ebe

Browse files
sergeichmeta-codesync[bot]
authored andcommitted
Add Sonatype Central repository for users of nightly builds only (#55305)
Summary: Even though Sonatype Central repository isn't used by an app, still the React Native Gradle plugin adds the Sonatype Central to repositories. In some cases it leads to a considerable amount of failed requests on Gradle downloading dependencies. <img width="1076" height="109" alt="858 failed requests out of 858 requests" src="https://github.com/user-attachments/assets/40d6aeb6-dcc4-4ebb-9f0b-742d26ba6e65" /> I'm introducing a change where Sonatype Central repository is included for users of nightly builds only. ## Changelog: [ANDROID] [CHANGED] - Add Sonatype Central repository for users of nightly builds only Pull Request resolved: #55305 Test Plan: All of the tests in gradle plugin are green ``` $ ./gradlew test BUILD SUCCESSFUL in 10s 20 actionable tasks: 20 executed ``` Reviewed By: cipolleschi Differential Revision: D91887976 Pulled By: cortinico fbshipit-source-id: 1e61d3b8d97a444e17a02bfe9ed0334e9b0ab0bc
1 parent 51c9658 commit 47c3ebe

3 files changed

Lines changed: 114 additions & 43 deletions

File tree

packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactPlugin.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ class ReactPlugin : Plugin<Project> {
7474
val hermesVersionPropertiesFile =
7575
File(reactNativeDir, "sdks/hermes-engine/version.properties")
7676
val versionAndGroupStrings =
77-
readVersionAndGroupStrings(propertiesFile, hermesVersionPropertiesFile)
77+
readVersionAndGroupStrings(project, propertiesFile, hermesVersionPropertiesFile)
7878
val hermesV1Enabled = rootExtension.hermesV1Enabled.get()
7979
configureDependencies(project, versionAndGroupStrings, hermesV1Enabled)
80-
configureRepositories(project)
80+
configureRepositories(project, versionAndGroupStrings.isNightly)
8181
}
8282

8383
configureReactNativeNdk(project, extension)

packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/DependencyUtils.kt

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@ internal object DependencyUtils {
3535
val hermesV1VersionString: String,
3636
val reactGroupString: String = DEFAULT_INTERNAL_REACT_PUBLISHING_GROUP,
3737
val hermesGroupString: String = DEFAULT_INTERNAL_HERMES_PUBLISHING_GROUP,
38-
)
38+
private val isHermesNightly: Boolean = false,
39+
) {
40+
val isNightly: Boolean
41+
get() = versionString.isNightly() || isHermesNightly
42+
}
3943

4044
/**
4145
* This method takes care of configuring the repositories{} block for both the app and all the 3rd
4246
* party libraries which are auto-linked.
4347
*/
44-
fun configureRepositories(project: Project) {
48+
fun configureRepositories(project: Project, isNightly: Boolean) {
4549
val exclusiveEnterpriseRepository = project.rootProject.exclusiveEnterpriseRepository()
4650
if (exclusiveEnterpriseRepository != null) {
4751
project.logger.lifecycle(
@@ -67,9 +71,11 @@ internal object DependencyUtils {
6771
return@allprojects
6872
}
6973

70-
// We add the snapshot for users on nightlies.
71-
mavenRepoFromUrl("https://central.sonatype.com/repository/maven-snapshots/") { repo ->
72-
repo.content { it.excludeGroup("org.webkit") }
74+
if (isNightly) {
75+
// We add the snapshot for users on nightlies.
76+
mavenRepoFromUrl("https://central.sonatype.com/repository/maven-snapshots/") { repo ->
77+
repo.content { it.excludeGroup("org.webkit") }
78+
}
7379
}
7480
repositories.mavenCentral { repo ->
7581
// We don't want to fetch JSC from Maven Central as there are older versions there.
@@ -204,13 +210,17 @@ internal object DependencyUtils {
204210
return dependencySubstitution
205211
}
206212

207-
fun readVersionAndGroupStrings(propertiesFile: File, hermesVersionFile: File): Coordinates {
213+
fun readVersionAndGroupStrings(
214+
project: Project,
215+
propertiesFile: File,
216+
hermesVersionFile: File,
217+
): Coordinates {
208218
val reactAndroidProperties = Properties()
209219
propertiesFile.inputStream().use { reactAndroidProperties.load(it) }
210220
val versionStringFromFile = (reactAndroidProperties[INTERNAL_VERSION_NAME] as? String).orEmpty()
211221
// If on a nightly, we need to fetch the -SNAPSHOT artifact from Sonatype.
212222
val versionString =
213-
if (versionStringFromFile.startsWith("0.0.0") || "-nightly-" in versionStringFromFile) {
223+
if (versionStringFromFile.isNightly()) {
214224
"$versionStringFromFile-SNAPSHOT"
215225
} else {
216226
versionStringFromFile
@@ -237,13 +247,15 @@ internal object DependencyUtils {
237247

238248
val hermesV1Version =
239249
(hermesVersionProperties[INTERNAL_HERMES_V1_VERSION_NAME] as? String).orEmpty()
250+
val isHermesNightly = (project.findProperty(INTERNAL_USE_HERMES_NIGHTLY) as? String).toBoolean()
240251

241252
return Coordinates(
242253
versionString,
243254
hermesVersion,
244255
hermesV1Version,
245256
reactGroupString,
246257
hermesGroupString,
258+
isHermesNightly,
247259
)
248260
}
249261

@@ -274,6 +286,8 @@ internal object DependencyUtils {
274286
else -> INCLUDE_JITPACK_REPOSITORY_DEFAULT
275287
}
276288

289+
internal fun String.isNightly(): Boolean = this.startsWith("0.0.0") || "-nightly-" in this
290+
277291
internal fun Project.exclusiveEnterpriseRepository() =
278292
when {
279293
hasProperty(SCOPED_EXCLUSIVE_ENTEPRISE_REPOSITORY) ->

packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/DependencyUtilsTest.kt

Lines changed: 91 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.facebook.react.utils.DependencyUtils.configureDependencies
1212
import com.facebook.react.utils.DependencyUtils.configureRepositories
1313
import com.facebook.react.utils.DependencyUtils.exclusiveEnterpriseRepository
1414
import com.facebook.react.utils.DependencyUtils.getDependencySubstitutions
15+
import com.facebook.react.utils.DependencyUtils.isNightly
1516
import com.facebook.react.utils.DependencyUtils.mavenRepoFromURI
1617
import com.facebook.react.utils.DependencyUtils.mavenRepoFromUrl
1718
import com.facebook.react.utils.DependencyUtils.readVersionAndGroupStrings
@@ -35,7 +36,7 @@ class DependencyUtilsTest {
3536
val project = createProject()
3637
project.extensions.extraProperties.set("react.internal.mavenLocalRepo", localMaven.absolutePath)
3738

38-
configureRepositories(project)
39+
configureRepositories(project, false)
3940

4041
assertThat(
4142
project.repositories.firstOrNull {
@@ -45,27 +46,12 @@ class DependencyUtilsTest {
4546
.isNotNull()
4647
}
4748

48-
@Test
49-
fun configureRepositories_containsSnapshotRepo() {
50-
val repositoryURI = URI.create("https://central.sonatype.com/repository/maven-snapshots/")
51-
val project = createProject()
52-
53-
configureRepositories(project)
54-
55-
assertThat(
56-
project.repositories.firstOrNull {
57-
it is MavenArtifactRepository && it.url == repositoryURI
58-
}
59-
)
60-
.isNotNull()
61-
}
62-
6349
@Test
6450
fun configureRepositories_containsMavenCentral() {
6551
val repositoryURI = URI.create("https://repo.maven.apache.org/maven2/")
6652
val project = createProject()
6753

68-
configureRepositories(project)
54+
configureRepositories(project, false)
6955

7056
assertThat(
7157
project.repositories.firstOrNull {
@@ -80,7 +66,7 @@ class DependencyUtilsTest {
8066
val repositoryURI = URI.create("https://dl.google.com/dl/android/maven2/")
8167
val project = createProject()
8268

83-
configureRepositories(project)
69+
configureRepositories(project, false)
8470

8571
assertThat(
8672
project.repositories.firstOrNull {
@@ -95,7 +81,7 @@ class DependencyUtilsTest {
9581
val repositoryURI = URI.create("https://www.jitpack.io")
9682
val project = createProject()
9783

98-
configureRepositories(project)
84+
configureRepositories(project, false)
9985

10086
assertThat(
10187
project.repositories.firstOrNull {
@@ -115,7 +101,7 @@ class DependencyUtilsTest {
115101
repositoryURI.toString(),
116102
)
117103

118-
configureRepositories(project)
104+
configureRepositories(project, false)
119105

120106
assertThat(project.repositories).hasSize(1)
121107
assertThat(
@@ -132,7 +118,7 @@ class DependencyUtilsTest {
132118
var project = createProject()
133119
project.extensions.extraProperties.set("includeJitpackRepository", "false")
134120

135-
configureRepositories(project)
121+
configureRepositories(project, false)
136122

137123
assertThat(
138124
project.repositories.firstOrNull {
@@ -145,7 +131,7 @@ class DependencyUtilsTest {
145131
project = createProject()
146132
project.extensions.extraProperties.set("react.includeJitpackRepository", "false")
147133

148-
configureRepositories(project)
134+
configureRepositories(project, false)
149135

150136
assertThat(
151137
project.repositories.firstOrNull {
@@ -161,7 +147,7 @@ class DependencyUtilsTest {
161147
var project = createProject()
162148
project.extensions.extraProperties.set("includeJitpackRepository", "true")
163149

164-
configureRepositories(project)
150+
configureRepositories(project, false)
165151

166152
assertThat(
167153
project.repositories.firstOrNull {
@@ -174,7 +160,37 @@ class DependencyUtilsTest {
174160
project = createProject()
175161
project.extensions.extraProperties.set("react.includeJitpackRepository", "true")
176162

177-
configureRepositories(project)
163+
configureRepositories(project, false)
164+
165+
assertThat(
166+
project.repositories.firstOrNull {
167+
it is MavenArtifactRepository && it.url == repositoryURI
168+
}
169+
)
170+
.isNotNull()
171+
}
172+
173+
@Test
174+
fun configureRepositories_notNightly_doesNotContainSonatype() {
175+
val repositoryURI = URI.create("https://central.sonatype.com/repository/maven-snapshots/")
176+
var project = createProject()
177+
178+
configureRepositories(project, false)
179+
180+
assertThat(
181+
project.repositories.firstOrNull {
182+
it is MavenArtifactRepository && it.url == repositoryURI
183+
}
184+
)
185+
.isNull()
186+
}
187+
188+
@Test
189+
fun configureRepositories_nightly_containSonatype() {
190+
val repositoryURI = URI.create("https://central.sonatype.com/repository/maven-snapshots/")
191+
var project = createProject()
192+
193+
configureRepositories(project, true)
178194

179195
assertThat(
180196
project.repositories.firstOrNull {
@@ -192,7 +208,7 @@ class DependencyUtilsTest {
192208
val project = createProject()
193209
project.extensions.extraProperties.set("react.internal.mavenLocalRepo", localMaven.absolutePath)
194210

195-
configureRepositories(project)
211+
configureRepositories(project, false)
196212

197213
val indexOfLocalRepo =
198214
project.repositories.indexOfFirst {
@@ -211,7 +227,7 @@ class DependencyUtilsTest {
211227
val mavenCentralURI = URI.create("https://repo.maven.apache.org/maven2/")
212228
val project = createProject()
213229

214-
configureRepositories(project)
230+
configureRepositories(project, false)
215231

216232
val indexOfSnapshotRepo =
217233
project.repositories.indexOfFirst {
@@ -231,7 +247,7 @@ class DependencyUtilsTest {
231247
val appProject = ProjectBuilder.builder().withName("app").withParent(rootProject).build()
232248
val libProject = ProjectBuilder.builder().withName("lib").withParent(rootProject).build()
233249

234-
configureRepositories(appProject)
250+
configureRepositories(appProject, false)
235251

236252
assertThat(
237253
appProject.repositories.firstOrNull {
@@ -259,7 +275,7 @@ class DependencyUtilsTest {
259275
repo.content { content -> content.excludeGroup("com.facebook.react") }
260276
}
261277

262-
configureRepositories(appProject)
278+
configureRepositories(appProject, false)
263279

264280
// We need to make sure we have Maven Central defined twice, one by the library,
265281
// and another is the override by RNGP.
@@ -598,7 +614,8 @@ class DependencyUtilsTest {
598614
)
599615
}
600616

601-
val strings = readVersionAndGroupStrings(propertiesFile, hermesVersionFile)
617+
val project = createProject()
618+
val strings = readVersionAndGroupStrings(project, propertiesFile, hermesVersionFile)
602619
val versionString = strings.versionString
603620
val hermesVersionString = strings.hermesVersionString
604621
val hermesV1VersionString = strings.hermesV1VersionString
@@ -635,7 +652,8 @@ class DependencyUtilsTest {
635652
)
636653
}
637654

638-
val strings = readVersionAndGroupStrings(propertiesFile, hermesVersionFile)
655+
val project = createProject()
656+
val strings = readVersionAndGroupStrings(project, propertiesFile, hermesVersionFile)
639657
val versionString = strings.versionString
640658
val hermesVersionString = strings.hermesVersionString
641659
val hermesV1VersionString = strings.hermesV1VersionString
@@ -667,7 +685,8 @@ class DependencyUtilsTest {
667685
)
668686
}
669687

670-
val strings = readVersionAndGroupStrings(propertiesFile, hermesVersionFile)
688+
val project = createProject()
689+
val strings = readVersionAndGroupStrings(project, propertiesFile, hermesVersionFile)
671690
val versionString = strings.versionString
672691
val hermesVersionString = strings.hermesVersionString
673692
val hermesV1VersionString = strings.hermesV1VersionString
@@ -701,7 +720,8 @@ class DependencyUtilsTest {
701720
)
702721
}
703722

704-
val strings = readVersionAndGroupStrings(propertiesFile, hermesVersionFile)
723+
val project = createProject()
724+
val strings = readVersionAndGroupStrings(project, propertiesFile, hermesVersionFile)
705725
val versionString = strings.versionString
706726
val hermesVersionString = strings.hermesVersionString
707727
val hermesV1VersionString = strings.hermesV1VersionString
@@ -736,7 +756,8 @@ class DependencyUtilsTest {
736756
)
737757
}
738758

739-
val strings = readVersionAndGroupStrings(propertiesFile, hermesVersionFile)
759+
val project = createProject()
760+
val strings = readVersionAndGroupStrings(project, propertiesFile, hermesVersionFile)
740761
val reactGroupString = strings.reactGroupString
741762
val hermesGroupString = strings.hermesGroupString
742763

@@ -768,7 +789,8 @@ class DependencyUtilsTest {
768789
)
769790
}
770791

771-
val strings = readVersionAndGroupStrings(propertiesFile, hermesVersionFile)
792+
val project = createProject()
793+
val strings = readVersionAndGroupStrings(project, propertiesFile, hermesVersionFile)
772794
val reactGroupString = strings.reactGroupString
773795
val hermesGroupString = strings.hermesGroupString
774796

@@ -840,4 +862,39 @@ class DependencyUtilsTest {
840862
val project = createProject(tempFolder.root)
841863
assertThat(project.exclusiveEnterpriseRepository()).isNull()
842864
}
865+
866+
@Test
867+
fun isNightly_returnsTrue_forValidNightlyVersions() {
868+
val trueCases =
869+
listOf(
870+
"0.85.0-nightly-20260128-36f07a1b2",
871+
"0.82.0-nightly-date-commit",
872+
"0.0.0-20230505-2109-9b69263a1",
873+
"0.0.0-date-commit",
874+
"0.0.0-nightly-",
875+
)
876+
877+
trueCases.forEach { version ->
878+
assert(version.isNightly()) { "Expected '$version' to be detected as nightly" }
879+
}
880+
}
881+
882+
@Test
883+
fun isNightly_returnsFalse_forNonNightlyVersions() {
884+
val falseCases =
885+
listOf(
886+
"0.83.0", // Standard version
887+
"0.0.1",
888+
"nightly", // Missing hyphens
889+
"0.83.0-nightly", // Missing trailing hyphen
890+
"any-nightly", // Missing trailing hyphen
891+
"nightly-build", // Missing leading hyphen
892+
"", // Empty string
893+
" ", // Blank string
894+
)
895+
896+
falseCases.forEach { version ->
897+
assert(!version.isNightly()) { "Expected '$version' to NOT be detected as nightly" }
898+
}
899+
}
843900
}

0 commit comments

Comments
 (0)