Skip to content

Commit 268e0e2

Browse files
committed
fix auth in maven
1 parent 1618b69 commit 268e0e2

File tree

4 files changed

+124
-54
lines changed

4 files changed

+124
-54
lines changed

build.gradle.kts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
*/
44

55
import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion
6-
import util.getSpacePassword
6+
import util.configureApiValidation
7+
import util.configureNpm
8+
import util.configureProjectReport
79
import util.libs
810

911
plugins {
@@ -30,7 +32,7 @@ println("kotlinx.rpc project version: $version, Kotlin version: $kotlinVersionFu
3032
// If the prefix of the kPRC version is not Kotlin gradle plugin version – you have a problem :)
3133
// Probably some dependency brings kotlin with the later version.
3234
// To mitigate so, refer to `versions-root/kotlin-version-lookup.json`
33-
// and its usage in `gradle-conventions-settings/src/main/kotlin/settings-conventions.settings.gradle.kts`
35+
// and its usage in `gradle-conventions-settings/src/main/kotlin/conventions-version-resolution.settings.gradle.kts`
3436
val kotlinGPVersion = getKotlinPluginVersion()
3537
if (kotlinVersionFull != kotlinGPVersion) {
3638
error("KGP version mismatch. Project version: $kotlinVersionFull, KGP version: $kotlinGPVersion")

gradle-conventions-settings/src/main/kotlin/conventions-repositories.settings.gradle.kts

Lines changed: 93 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,26 @@ pluginManagement {
1515
propertyName.replace(".", "_").uppercase()
1616
)?.ifEmpty { null }
1717

18-
fun getSpaceUsername(): String? {
19-
val username = "kotlinx.rpc.team.space.username"
20-
return settings.providers.gradleProperty(username).orNull
21-
?: getEnv(username)
22-
?: logAbsentProperty(username)
18+
fun getLocalProperties(): java.util.Properties {
19+
return java.util.Properties().apply {
20+
val propertiesDir = File(
21+
rootDir.path
22+
.removeSuffix("/gradle-conventions")
23+
.removeSuffix("/gradle-conventions-settings")
24+
.removeSuffix("/compiler-plugin")
25+
.removeSuffix("/gradle-plugin")
26+
)
27+
val localFile = File(propertiesDir, "local.properties")
28+
if (localFile.exists()) {
29+
localFile.inputStream().use { load(it) }
30+
}
31+
}
2332
}
2433

2534
fun getSpacePassword(): String? {
2635
val password = "kotlinx.rpc.team.space.password"
27-
return settings.providers.gradleProperty(password).orNull
36+
return getLocalProperties()[password] as String?
37+
?: settings.providers.gradleProperty(password).orNull
2838
?: getEnv(password)
2939
?: logAbsentProperty(password)
3040
}
@@ -39,13 +49,16 @@ pluginManagement {
3949
name = repoName.split("-").joinToString("") { it.replaceFirstChar { c -> c.titlecase() } }
4050
url = uri("https://packages.jetbrains.team/maven/p/krpc/$repoName")
4151

42-
val spaceUsername = getSpaceUsername()
4352
val spacePassword = getSpacePassword()
4453

45-
if (spaceUsername != null && spacePassword != null) {
46-
credentials {
47-
username = spaceUsername
48-
password = spacePassword
54+
if (spacePassword != null) {
55+
credentials(HttpHeaderCredentials::class.java) {
56+
name = "Authorization"
57+
value = "Bearer $spacePassword"
58+
}
59+
60+
authentication {
61+
create<HttpHeaderAuthentication>("http_auth_header")
4962
}
5063
} else {
5164
logger.info("Skipping adding credentials for Space repository '$repoName'")
@@ -57,8 +70,16 @@ pluginManagement {
5770
fun RepositoryHandler.buildDepsEap() = jbTeamPackages(repoName = "build-deps-eap")
5871

5972
repositories {
60-
buildDeps()
61-
buildDepsEap()
73+
val useProxyProperty = getLocalProperties()["kotlinx.rpc.useProxyRepositories"] as String?
74+
val useProxy = useProxyProperty == null || useProxyProperty == "true"
75+
76+
if (useProxy) {
77+
buildDeps()
78+
buildDepsEap()
79+
} else {
80+
mavenCentral()
81+
gradlePluginPortal()
82+
}
6283
}
6384
}
6485

@@ -73,16 +94,31 @@ gradle.rootProject {
7394
propertyName.replace(".", "_").uppercase()
7495
)?.ifEmpty { null }
7596

76-
fun getSpaceUsername(): String? {
77-
val username = "kotlinx.rpc.team.space.username"
78-
return settings.providers.gradleProperty(username).orNull
79-
?: getEnv(username)
80-
?: logAbsentProperty(username)
97+
fun getLocalProperties(): java.util.Properties {
98+
return java.util.Properties().apply {
99+
val propertiesDir = File(
100+
rootDir.path
101+
.removeSuffix("/gradle-conventions")
102+
.removeSuffix("/gradle-conventions-settings")
103+
.removeSuffix("/compiler-plugin")
104+
.removeSuffix("/gradle-plugin")
105+
)
106+
val localFile = File(propertiesDir, "local.properties")
107+
if (localFile.exists()) {
108+
localFile.inputStream().use { load(it) }
109+
}
110+
}
111+
}
112+
113+
fun java.util.Properties.isUsingProxyRepositories(): Boolean {
114+
val useProxyProperty = this["kotlinx.rpc.useProxyRepositories"] as String?
115+
return useProxyProperty == null || useProxyProperty == "true"
81116
}
82117

83118
fun getSpacePassword(): String? {
84119
val password = "kotlinx.rpc.team.space.password"
85-
return settings.providers.gradleProperty(password).orNull
120+
return getLocalProperties()[password] as String?
121+
?: settings.providers.gradleProperty(password).orNull
86122
?: getEnv(password)
87123
?: logAbsentProperty(password)
88124
}
@@ -95,16 +131,18 @@ gradle.rootProject {
95131
fun RepositoryHandler.jbTeamPackages(repoName: String) {
96132
maven {
97133
name = repoName.split("-").joinToString("") { it.replaceFirstChar { c -> c.titlecase() } }
98-
99134
url = uri("https://packages.jetbrains.team/maven/p/krpc/$repoName")
100135

101-
val spaceUsername = getSpaceUsername()
102136
val spacePassword = getSpacePassword()
103137

104-
if (spaceUsername != null && spacePassword != null) {
105-
credentials {
106-
username = spaceUsername
107-
password = spacePassword
138+
if (spacePassword != null) {
139+
credentials(HttpHeaderCredentials::class.java) {
140+
name = "Authorization"
141+
value = "Bearer $spacePassword"
142+
}
143+
144+
authentication {
145+
create<HttpHeaderAuthentication>("http_auth_header")
108146
}
109147
} else {
110148
logger.info("Skipping adding credentials for Space repository '$repoName'")
@@ -116,15 +154,41 @@ gradle.rootProject {
116154
fun RepositoryHandler.buildDepsEap() = jbTeamPackages(repoName = "build-deps-eap")
117155

118156
allprojects {
157+
val localProps = getLocalProperties()
158+
159+
this.extra["spacePassword"] = getSpacePassword()
160+
this.extra["localProperties"] = localProps
161+
this.extra["useProxyRepositories"] = localProps.isUsingProxyRepositories()
162+
163+
val useProxy = localProps.isUsingProxyRepositories()
164+
119165
buildscript {
120166
repositories {
121-
buildDeps()
122-
buildDepsEap()
167+
if (useProxy) {
168+
buildDeps()
169+
buildDepsEap()
170+
} else {
171+
mavenCentral()
172+
gradlePluginPortal()
173+
}
123174
}
124175
}
125176
repositories {
126-
buildDeps()
127-
buildDepsEap()
177+
if (useProxy) {
178+
buildDeps()
179+
buildDepsEap()
180+
} else {
181+
mavenCentral()
182+
gradlePluginPortal()
183+
184+
maven("https://www.jetbrains.com/intellij-repository/releases")
185+
186+
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/kotlin-ide-plugin-dependencies")
187+
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap")
188+
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev")
189+
190+
maven("https://maven.pkg.jetbrains.space/public/p/ktor/eap")
191+
}
128192
}
129193
}
130194
}

gradle-conventions-settings/src/main/kotlin/util/npm.kt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@ package util
77
import org.gradle.api.GradleException
88
import org.gradle.api.Project
99
import org.gradle.kotlin.dsl.*
10+
import org.jetbrains.kotlin.gradle.targets.js.yarn.YarnLockMismatchReport
1011
import java.io.File
1112

1213
fun Project.configureNpm() {
1314
val executeNpmLogin by tasks.registering {
15+
if (!useProxyRepositories) {
16+
return@registering
17+
}
18+
1419
val registryUrl = "https://packages.jetbrains.team/npm/p/krpc/build-deps/"
1520

1621
// To prevent leaking of credentials in VCS on dev machine use the build directory config file
1722
val buildYarnConfigFile = File(project.rootDir, "build/js/.yarnrc")
1823
val buildNpmConfigFile = File(project.rootDir, "build/js/.npmrc")
1924

20-
val spacePassword: String? = getSpacePassword()
25+
val spacePassword: String? = spacePassword
2126

2227
doLast {
2328
val outputYarnText = """
@@ -50,10 +55,14 @@ fun Project.configureNpm() {
5055
outputs.file(buildNpmConfigFile).withPropertyName("buildOutputNpmFile")
5156
}
5257

58+
val useProxy = useProxyRepositories
59+
5360
plugins.withType(org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin::class.java).configureEach {
5461
rootProject.extensions.configure<org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension> {
5562
download = true
56-
downloadBaseUrl = "https://packages.jetbrains.team/files/p/krpc/build-deps/"
63+
if (useProxy) {
64+
downloadBaseUrl = "https://packages.jetbrains.team/files/p/krpc/build-deps/"
65+
}
5766
}
5867

5968
tasks.named("kotlinNpmInstall").configure {
@@ -66,7 +75,15 @@ fun Project.configureNpm() {
6675
rootProject.extensions.configure<org.jetbrains.kotlin.gradle.targets.js.yarn.YarnRootExtension> {
6776
ignoreScripts = false
6877
download = true
69-
downloadBaseUrl = "https://packages.jetbrains.team/files/p/krpc/build-deps/"
78+
79+
yarnLockMismatchReport = when (useProxy) {
80+
true -> YarnLockMismatchReport.FAIL
81+
false -> YarnLockMismatchReport.WARNING
82+
}
83+
84+
if (useProxy) {
85+
downloadBaseUrl = "https://packages.jetbrains.team/files/p/krpc/build-deps/"
86+
}
7087
}
7188
}
7289
}

gradle-conventions-settings/src/main/kotlin/util/properties.kt

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,16 @@
77
package util
88

99
import org.gradle.api.Project
10+
import org.gradle.kotlin.dsl.extra
1011

11-
fun Project.logAbsentProperty(name: String): Nothing? {
12-
logger.info("Property '$name' is not present.")
13-
14-
return null
12+
val Project.spacePassword get(): String? {
13+
return (this.extra["spacePassword"] as String?)
1514
}
1615

17-
fun getEnv(propertyName: String): String? = System.getenv(
18-
propertyName.replace(".", "_").uppercase()
19-
)?.ifEmpty { null }
20-
21-
private const val SPACE_USERNAME = "kotlinx.rpc.team.space.username"
22-
23-
fun Project.getSpaceUsername(): String? {
24-
return providers.gradleProperty(SPACE_USERNAME).orNull
25-
?: getEnv(SPACE_USERNAME)
26-
?: logAbsentProperty(SPACE_USERNAME)
16+
val Project.localProperties get(): java.util.Properties {
17+
return (this.extra["localProperties"] as java.util.Properties)
2718
}
2819

29-
private const val SPACE_PASSWORD = "kotlinx.rpc.team.space.password"
30-
31-
fun Project.getSpacePassword(): String? {
32-
return providers.gradleProperty(SPACE_PASSWORD).orNull
33-
?: getEnv(SPACE_PASSWORD)
34-
?: logAbsentProperty(SPACE_USERNAME)
20+
val Project.useProxyRepositories get(): Boolean {
21+
return (this.extra["useProxyRepositories"] as Boolean)
3522
}

0 commit comments

Comments
 (0)